Skip to content

Commit d9556b6

Browse files
committed
automatic column mapping
1 parent fff085c commit d9556b6

14 files changed

Lines changed: 31 additions & 85 deletions

File tree

hyperql-spring-lib/src/main/java/org/slowcoders/hyperql/jdbc/storage/JdbcSchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private void dumpColumnDefinition(QColumn col, SourceWriter sb, boolean isJPA) {
215215
sb.writeln("@org.hibernate.annotations.Type(io.hypersistence.utils.hibernate.type.json.JsonType.class)");
216216
}
217217
} else {
218-
sb.write("@QColumn(").writeQuoted(col.getPhysicalName()).write(")\n");
218+
// sb.write("@QColumn(").writeQuoted(col.getPhysicalName()).write(")\n");
219219
}
220220

221221
String fieldName = isJPA ? getJavaFieldName(col)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@Retention(RetentionPolicy.RUNTIME)
88
public @interface QColumn {
9-
String value();
10-
String inputTransform() default "?";
11-
String outputTransform() default "?";
9+
String name();
10+
String writeTransform() default "?";
11+
String readTransform() default "?";
1212
}

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

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ else if (QAttribute.class.isAssignableFrom(propertyType)) {
108108
((AliasNode)attr).setName(f.getName());
109109
attributes.put(f.getName(), attr);
110110
}
111+
else {
112+
113+
}
111114
}
112115
} catch (IllegalAccessException e) {
113116
throw new RuntimeException(e);
@@ -195,61 +198,27 @@ public QJoin getJoin(String join, JdbcConnector dbConn) {
195198
public String getColumnExpr(Field f) {
196199
String columnExpr = Helper.getColumnName(f);
197200
if (columnExpr != null) return columnExpr;
201+
String name = f.getName();
198202
if (this.attributes != null) {
199-
QAttribute attr = this.attributes.get(f.getName());
203+
QAttribute attr = this.attributes.get(name);
200204
if (attr != null) return attr.getEncodedExpr();
201205
}
202-
return null;
206+
for (JdbcColumn col : jdbcColumns) {
207+
if (name.equals(col.getFieldName())) {
208+
return col.getPhysicalName();
209+
}
210+
}
211+
QJoin join = this.joins.get("@" + name);
212+
if (join != null) {
213+
return "@" + name;
214+
}
215+
throw new RuntimeException("Cannot find column expression for " + name);
203216
}
204217

205-
// public static class TablePath {
206-
// private final String catalog;
207-
// private final String schema;
208-
// private final String simpleName;
209-
//
210-
// TablePath(String catalog, String schema, String simpleName) {
211-
// this.catalog = (catalog);
212-
// this.schema = (schema);
213-
// this.simpleName = (simpleName);
214-
// }
215-
//
216-
//
217-
// public String getSimpleName() {
218-
// return simpleName;
219-
// }
220-
//
221-
// public String getCatalog() {
222-
// return catalog;
223-
// }
224-
//
225-
// public String getSchema() {
226-
// return schema;
227-
// }
228-
// }
229-
// private ArrayList<String> getPrimaryKeys(Connection conn, TablePath tablePath) throws SQLException {
230-
// DatabaseMetaData md = conn.getMetaData();
231-
// ResultSet rs = md.getPrimaryKeys(tablePath.getCatalog(), tablePath.getSchema(), tablePath.getSimpleName());
232-
// ArrayList<String> keys = new ArrayList<>();
233-
// int next_key_seq = 1;
234-
// while (rs.next()) {
235-
// String key = rs.getString("column_name");
236-
// if (false) {
237-
// // postgresql 에서만 동작.
238-
// int seq = rs.getInt("key_seq");
239-
// if (seq != next_key_seq) {
240-
// throw new RuntimeException("something wrong");
241-
// }
242-
// next_key_seq++;
243-
// }
244-
// keys.add(key);
245-
// }
246-
// return keys;
247-
// }
248-
249218
static class Helper implements QEntity<Helper> {
250219
static String getColumnName(Field f) {
251220
QColumn anno = f.getAnnotation(QColumn.class);
252-
if (anno != null) return anno.value();
221+
if (anno != null) return anno.name();
253222

254223
PKColumn pk = f.getAnnotation(PKColumn.class);
255224
if (pk != null) return pk.value();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public String buildUpdate(QUniqueRecord<?> entity) {
326326
sbQuery.incTab();
327327
for (ColumnMapping mapping : columnMappings) {
328328
if (mapping.columnName.equals("id")) continue;
329-
String value = mapping.columnConfig.inputTransform().replaceAll("\\?", "_DATA." + mapping.columnName);
329+
String value = mapping.columnConfig.writeTransform().replaceAll("\\?", "_DATA." + mapping.columnName);
330330
value = value.replaceAll("@", "t_0");
331331
sbQuery.write(mapping.columnName).write(" = ").write(value).write(",\n");
332332
}

hyperquery/src/main/java/org/slowcoders/hyperquery/impl/jdbc/JdbcColumn.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonAutoDetect;
44
import com.fasterxml.jackson.databind.JsonNode;
5+
import org.slowcoders.hyperquery.util.CaseConverter;
56
import org.slowcoders.hyperquery.util.ClassUtils;
67

78
import java.lang.reflect.Field;
@@ -37,7 +38,7 @@ public class JdbcColumn { // extends QColumn {
3738
public JdbcColumn(ResultSetMetaData md, int col, /*ColumnBinder fkBinder,*/ String comment, List<String> primaryKeys) throws SQLException {
3839
this.physicalName = md.getColumnName(col);
3940
this.javaType = resolveJavaType(md, col);
40-
// this.schema = schema;
41+
this.fieldName = CaseConverter.toCamelCase(physicalName, false);
4142

4243
this.isAutoIncrement = md.isAutoIncrement(col);
4344
this.isReadOnly = md.isReadOnly(col) | this.isAutoIncrement;
@@ -161,6 +162,10 @@ public final String getPhysicalName() {
161162
return physicalName;
162163
}
163164

165+
public final String getFieldName() {
166+
return this.fieldName;
167+
}
168+
164169
// public void setJoinedPrimaryColumn_unsafe(String jsonKey, String pk_table, String pk_column) {
165170
// JdbcSchema schema1 = this.schema;
166171
// if (this.fkBinder != null) {

sample-app/src/main/java/org/slowcoders/hyperql/sample/hq/bookstore/AuthorDto.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,16 @@
1111
@Getter
1212
public class AuthorDto implements QRecord<Author> {
1313

14-
@QColumn("id")
1514
private Long id;
1615

17-
@QColumn("name")
1816
private String name;
1917

20-
@QColumn("@books")
2118
private List<Book> books;
2219

23-
@QColumn("bookCount")
2420
private int bookCount;
2521

26-
@QColumn("salesAmount")
2722
private int salesAmount;
2823

29-
@QColumn("bookPriceAvr")
3024
private int bookPriceAvr;
3125

3226
// @QColumn("attr2")

sample-app/src/main/java/org/slowcoders/hyperql/sample/hq/bookstore/BookDto.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import lombok.Getter;
44
import lombok.Setter;
55
import lombok.ToString;
6-
import org.slowcoders.hyperql.sample.hq.bookstore.api.HqBookController;
76
import org.slowcoders.hyperql.sample.hq.bookstore.model.Book;
87
import org.slowcoders.hyperquery.core.QColumn;
98
import org.slowcoders.hyperquery.core.QFilter;
109
import org.slowcoders.hyperquery.core.QRecord;
1110

1211
import java.time.LocalDate;
13-
import java.util.List;
1412

1513
@Getter
1614
@ToString
@@ -20,7 +18,7 @@ public class BookDto implements QRecord<Book> {
2018

2119
private String title;
2220

23-
@QColumn("@author.name")
21+
@QColumn(name = "@author.name")
2422
private String author;
2523

2624
// @QColumn("@weeklySales")

sample-app/src/main/java/org/slowcoders/hyperql/sample/hq/bookstore/api/SalesPricePercent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import lombok.Setter;
55
import org.slowcoders.hyperql.sample.hq.bookstore.model.Book;
66
import org.slowcoders.hyperquery.core.QColumn;
7-
import org.slowcoders.hyperquery.core.QRecord;
87
import org.slowcoders.hyperquery.core.QUniqueRecord;
98

109
@Getter @Setter
1110
public class SalesPricePercent implements QUniqueRecord<Book> {
12-
@QColumn("id")
11+
1312
private int id;
14-
@QColumn(value = "price", inputTransform = "@.price * ?")
13+
14+
@QColumn(name = "price", writeTransform = "@.price * ?")
1515
private double percent;
1616
}

sample-app/src/main/java/org/slowcoders/hyperql/sample/hq/bookstore/model/Author.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111
public class Author implements QEntity<Author> {
1212
@Getter
1313
@Setter
14-
@QColumn("id")
1514
private Long id;
1615

1716
@Getter
1817
@Setter
19-
@QColumn("name")
2018
private String name;
2119

2220
@Getter
2321
@Setter
24-
@QColumn("profile")
2522
private com.fasterxml.jackson.databind.JsonNode profile;
2623

2724
public static QJoin book_ = QJoin.of(Book.class, "#.author_id = @.id");

sample-app/src/main/java/org/slowcoders/hyperql/sample/hq/bookstore/model/Book.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,22 @@
1111
public class Book implements QEntity<Book> {
1212
@Getter
1313
@Setter
14-
@QColumn("id")
1514
private Long id;
1615

1716
@Getter
1817
@Setter
19-
@QColumn("price")
2018
private Float price;
2119

2220
@Getter
2321
@Setter
24-
@QColumn("title")
2522
private String title;
2623

2724
@Getter
2825
@Setter
29-
@QColumn("author_id")
3026
private Long authorId;
3127

3228
@Getter
3329
@Setter
34-
@QColumn("publisher_id")
3530
private Long publisherId;
3631

3732
public static QJoin bookOrder_ = QJoin.of(BookOrder.class, "#.book_id = @.id");

0 commit comments

Comments
 (0)