Skip to content

Commit 8021caf

Browse files
committed
implements Collection property mapping
1 parent 3ea0cd3 commit 8021caf

22 files changed

Lines changed: 296 additions & 109 deletions

File tree

hyperquery/src/main/java/org/slowcoders/hyperquery/core/QEntity.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.slowcoders.hyperquery.core;
22

3-
import org.slowcoders.hyperquery.impl.*;
4-
53
import java.lang.annotation.Retention;
64
import java.lang.annotation.RetentionPolicy;
75

8-
public class QEntity extends QView {
6+
public interface QEntity<T extends QEntity<T>> extends QRecord<T> {
97

108
@Retention(RetentionPolicy.RUNTIME)
119
@interface TColumn {

hyperquery/src/main/java/org/slowcoders/hyperquery/core/QFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.lang.annotation.Retention;
88
import java.lang.annotation.RetentionPolicy;
99

10-
public class QFilter<T extends QEntity> {
10+
public class QFilter<T extends QRecord<?>> {
1111

1212
@Retention(RetentionPolicy.RUNTIME)
1313
public @interface Predicate {

hyperquery/src/main/java/org/slowcoders/hyperquery/core/QInlineView.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class QInlineView extends HModel {
77

88
private final String viewDefinition;
9-
private static final HSchema schema = HSchema.registerSchema(QView.class);
9+
private static final HSchema schema = HSchema.registerSchema(HiddenView.class);
1010

1111
public QInlineView(String query) {
1212
viewDefinition = query;
@@ -26,4 +26,7 @@ protected String getQuery() {
2626
protected String getTableName() {
2727
return "";
2828
}
29+
30+
@QFrom("")
31+
private static class HiddenView implements QEntity<HiddenView> {}
2932
}

hyperquery/src/main/java/org/slowcoders/hyperquery/core/QJoin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ public enum JoinType {
1616
private final String joinCriteria;
1717
private HModel model;
1818
private final boolean toUnique;
19-
private Class<? extends QView> viewType;
19+
private Class<? extends QRecord> viewType;
2020

2121
// Cross Join 은 지원하지 읺는다.
2222
protected QJoin(QInlineView inlineView, String joinOn, boolean toUnique) {
23-
this.viewType = QView.class;
23+
this.viewType = QRecord.class;
2424
this.model = inlineView;
2525
this.joinCriteria = joinOn;
2626
this.toUnique = toUnique;
2727
}
2828

29-
protected QJoin(Class<? extends QView> viewType, String joinOn, boolean toUnique) {
29+
protected QJoin(Class<? extends QRecord> viewType, String joinOn, boolean toUnique) {
3030
this.viewType = viewType;
3131
this.joinCriteria = joinOn;
3232
this.toUnique = toUnique;

hyperquery/src/main/java/org/slowcoders/hyperquery/core/QRecord.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
11
package org.slowcoders.hyperquery.core;
22

3-
import org.slowcoders.hyperquery.impl.*;
3+
import org.slowcoders.hyperquery.impl.HSchema;
4+
import org.slowcoders.hyperquery.impl.QAttribute;
5+
import org.slowcoders.hyperquery.impl.QLambda;
6+
import org.slowcoders.hyperquery.impl.QRepository;
47

58
import java.lang.annotation.Retention;
69
import java.lang.annotation.RetentionPolicy;
710

8-
public interface QRecord<T extends QView> {
9-
@Retention(RetentionPolicy.RUNTIME)
10-
@interface Property {
11-
String value();
12-
}
11+
public interface QRecord<T extends QRecord<T>> {
12+
13+
// @Retention(RetentionPolicy.RUNTIME)
14+
// @interface Property {
15+
// String value();
16+
// }
1317

14-
class Join extends QJoin {
15-
private Join(QView view, String joinOn, boolean single) {
18+
public static class Join extends QJoin {
19+
private Join(QInlineView view, String joinOn, boolean single) {
1620
super(view, joinOn, single);
1721
}
22+
private Join(Class<? extends QRecord> recordType, String joinOn, boolean single) {
23+
super(recordType, joinOn, single);
24+
}
1825

19-
public static Join toOne(Class<? extends QView> targetEntity, String joinOn) {
20-
return new Join(HSchema.registerSchema(targetEntity), joinOn, true);
26+
public static Join toOne(Class<? extends QRecord> recordType, String joinOn) {
27+
return new Join(recordType, joinOn, true);
2128
}
2229

23-
public static Join toMany(Class<? extends QView> targetEntity, String joinOn) {
24-
return new Join(HSchema.registerSchema(targetEntity), joinOn, false);
30+
public static Join toMany(Class<? extends QRecord> recordType, String joinOn) {
31+
return new Join(recordType, joinOn, false);
2532
}
2633

27-
public static Join toOne(QView view, String joinOn) {
34+
public static Join toOne(QInlineView view, String joinOn) {
2835
return new Join(view, joinOn, true);
2936
}
3037

31-
public static Join toMany(QView view, String joinOn) {
38+
public static Join toMany(QInlineView view, String joinOn) {
3239
return new Join(view, joinOn, false);
3340
}
3441
}
3542

36-
class Property extends QAttribute {
43+
public static class Property extends QAttribute {
3744
private Property(String statement) {
3845
super(statement);
3946
}
@@ -43,7 +50,7 @@ public static Property formula(String statement) {
4350
}
4451
}
4552

46-
class Lambda extends QLambda {
53+
public static class Lambda extends QLambda {
4754
private Lambda(int argCount, String statement) {
4855
super(argCount, statement);
4956
}

hyperquery/src/main/java/org/slowcoders/hyperquery/core/QView.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

hyperquery/src/main/java/org/slowcoders/hyperquery/impl/HSchema.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
package org.slowcoders.hyperquery.impl;
22

3-
import org.slowcoders.hyperquery.core.QEntity;
43
import org.slowcoders.hyperquery.core.QFrom;
54
import org.slowcoders.hyperquery.core.QJoin;
6-
import org.slowcoders.hyperquery.core.QView;
5+
import org.slowcoders.hyperquery.core.QRecord;
76

87
import java.lang.reflect.Field;
98
import java.lang.reflect.Modifier;
109
import java.util.HashMap;
1110
import java.util.Map;
1211

1312
public class HSchema extends HModel {
14-
private final Class<? extends QView> entityType;
13+
private final Class<? extends QRecord> entityType;
1514
private final String tableName;
1615
private Map<String, QJoin> joins;
1716
private Map<String, QLambda> lambdas;
1817
private Map<String, String> properties;
1918

2019
private static final HashMap<Class<?>, HSchema> relations = new HashMap<>();
2120

22-
public HSchema(Class<? extends QView> entityType) {
21+
public HSchema(Class<? extends QRecord> entityType) {
2322
this.entityType = entityType;
2423
this.tableName = (getTableName(entityType));
2524
}
2625

27-
static String getTableName(Class<? extends QView> entityType) {
26+
static String getTableName(Class<? extends QRecord> entityType) {
2827
QFrom from = entityType.getAnnotation(QFrom.class);
2928
return from.value();
3029
}
31-
public final Class<? extends QView> getEntityType() { return entityType; }
30+
public final Class<? extends QRecord> getEntityType() { return entityType; }
3231

3332
public String translateProperty(String property) {
3433
int p = property.lastIndexOf('.');
@@ -62,9 +61,9 @@ public QLambda getLambda(String[] joinPath, String property) {
6261
return model.getLambda(property);
6362
}
6463

65-
public static HSchema getSchema(Class<? extends QView> clazz) { return relations.get(clazz); }
64+
public static HSchema getSchema(Class<? extends QRecord> clazz) { return relations.get(clazz); }
6665

67-
public static HSchema registerSchema(Class<? extends QView> clazz) {
66+
public static HSchema registerSchema(Class<? extends QRecord> clazz) {
6867
HSchema relation = relations.get(clazz);
6968
if (relation == null) {
7069
synchronized (relations) {
@@ -124,6 +123,14 @@ protected String getTableName() {
124123

125124
public QJoin getJoin(String join) {
126125
this.initialize();
126+
int next = join.indexOf('@', 1);
127+
String nextJoin = null;
128+
if (next > 0) {
129+
nextJoin = join.substring(next);
130+
join = join.substring(0, next);
131+
QJoin subJoin = getJoin(join);
132+
return subJoin.getTargetRelation().getJoin(nextJoin);
133+
}
127134
return joins.get(join);
128135
}
129136
}

hyperquery/src/main/java/org/slowcoders/hyperquery/impl/QEntityStore.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ enum EntityCardinality {
2121
public QEntityStore(Class<? extends QEntity> entityType, String prefix) {
2222
this.entityType = entityType;
2323

24-
try {
25-
for (Field f : entityType.getDeclaredFields()) {
26-
QEntity.PKColumn pk = f.getAnnotation(QEntity.PKColumn.class);
27-
QEntity.TColumn column = f.getAnnotation(QEntity.TColumn.class);
28-
29-
f.setAccessible(true);
30-
31-
if (pk != null) {
32-
pkMappings.add(new ColumnMapping(pk.value(), f));
33-
columnMappings.add(new ColumnMapping(pk.value(), f));
34-
}
35-
else if (column != null) {
36-
columnMappings.add(new ColumnMapping(column.value(), f));
24+
try {
25+
for (Field f : entityType.getDeclaredFields()) {
26+
QEntity.PKColumn pk = f.getAnnotation(QEntity.PKColumn.class);
27+
QEntity.TColumn column = f.getAnnotation(QEntity.TColumn.class);
28+
29+
f.setAccessible(true);
30+
31+
if (pk != null) {
32+
pkMappings.add(new ColumnMapping(pk.value(), f));
33+
columnMappings.add(new ColumnMapping(pk.value(), f));
34+
}
35+
else if (column != null) {
36+
columnMappings.add(new ColumnMapping(column.value(), f));
37+
}
3738
}
39+
} catch (Exception e) {
40+
throw new RuntimeException(e);
3841
}
39-
} catch (Exception e) {
40-
throw new RuntimeException(e);
41-
}
4242
}
4343

4444
private static final String updateTemplate = """

hyperquery/src/main/java/org/slowcoders/hyperquery/impl/QRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.slowcoders.hyperquery.core.QFilter;
88
import org.slowcoders.hyperquery.core.QRecord;
99

10-
public interface QRepository<ENTITY extends QEntity> {
10+
public interface QRepository {
1111

1212
@Select("${__sql__}")
1313
Object __select__(Object params);
@@ -26,7 +26,7 @@ ON CONFLICT (${__primary_keys__})
2626
RETURNING *
2727
)
2828
""")
29-
Object __insert_or_update__(ENTITY entity);
29+
Object __insert_or_update__(QEntity entity);
3030

3131
@Update("""
3232
WITH _DATA AS (
@@ -42,5 +42,5 @@ WITH _DATA AS (
4242
RETURNING *
4343
)
4444
""")
45-
<T extends ENTITY> QRecord<T> __update__(QRecord<T> record, QFilter<T> filter);
45+
QRecord<?> __update__(QRecord<?> record, QFilter<?> filter);
4646
}

0 commit comments

Comments
 (0)