Skip to content

Commit 1c1c604

Browse files
committed
Update QueryBuilder.
1 parent 15ac841 commit 1c1c604

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

kilo-client/src/main/java/org/httprpc/kilo/sql/QueryBuilder.java

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,16 @@ public static QueryBuilder select(Class<?>... types) {
269269
for (var entry : BeanAdapter.getProperties(type).entrySet()) {
270270
var accessor = entry.getValue().getAccessor();
271271

272-
if (!getTableName(accessor.getDeclaringClass()).equals(tableName)) {
273-
continue;
274-
}
275-
276272
var column = accessor.getAnnotation(Column.class);
277273

278274
if (column == null) {
279275
continue;
280276
}
281277

278+
if (!getTableName(accessor.getDeclaringClass()).equals(tableName)) {
279+
continue;
280+
}
281+
282282
if (j > 0 && accessor.getAnnotation(ForeignKey.class) != null) {
283283
continue;
284284
}
@@ -385,13 +385,31 @@ public static QueryBuilder selectDistinctIndex(Class<?> type) {
385385
}
386386

387387
private static String getTableName(Class<?> type) {
388-
var table = type.getAnnotation(Table.class);
388+
while (type != null) {
389+
var table = type.getAnnotation(Table.class);
390+
391+
if (table != null) {
392+
return table.value();
393+
}
389394

390-
if (table == null) {
391-
throw new UnsupportedOperationException("Table name is not defined.");
395+
type = getSupertype(type);
392396
}
393397

394-
return table.value();
398+
throw new UnsupportedOperationException("Table name is not defined.");
399+
}
400+
401+
private static Class<?> getSupertype(Class<?> type) {
402+
if (type.isInterface()) {
403+
var interfaces = type.getInterfaces();
404+
405+
if (interfaces.length > 0) {
406+
return interfaces[0];
407+
} else {
408+
return null;
409+
}
410+
} else {
411+
return type.getSuperclass();
412+
}
395413
}
396414

397415
private static Function<Object, Object> getReadTransform(Method accessor) {
@@ -507,17 +525,7 @@ private static String getForeignKeyColumnName(Class<?> from, Class<?> to) {
507525
return column.value();
508526
}
509527

510-
if (type.isInterface()) {
511-
var interfaces = type.getInterfaces();
512-
513-
if (interfaces.length > 0) {
514-
type = interfaces[0];
515-
} else {
516-
type = null;
517-
}
518-
} else {
519-
type = type.getSuperclass();
520-
}
528+
type = getSupertype(type);
521529
}
522530
}
523531
}
@@ -578,16 +586,16 @@ public static QueryBuilder insert(Class<?> type) {
578586
for (var entry : BeanAdapter.getProperties(type).entrySet()) {
579587
var accessor = entry.getValue().getAccessor();
580588

581-
if (!getTableName(accessor.getDeclaringClass()).equals(tableName)) {
582-
continue;
583-
}
584-
585589
var column = accessor.getAnnotation(Column.class);
586590

587591
if (column == null) {
588592
continue;
589593
}
590594

595+
if (!getTableName(accessor.getDeclaringClass()).equals(tableName)) {
596+
continue;
597+
}
598+
591599
var primaryKey = accessor.getAnnotation(PrimaryKey.class);
592600

593601
if (primaryKey != null && primaryKey.generated()) {
@@ -674,16 +682,16 @@ public static QueryBuilder update(Class<?> type) {
674682
for (var entry : BeanAdapter.getProperties(type).entrySet()) {
675683
var accessor = entry.getValue().getAccessor();
676684

677-
if (!getTableName(accessor.getDeclaringClass()).equals(tableName)) {
678-
continue;
679-
}
680-
681685
var column = accessor.getAnnotation(Column.class);
682686

683687
if (column == null) {
684688
continue;
685689
}
686690

691+
if (!getTableName(accessor.getDeclaringClass()).equals(tableName)) {
692+
continue;
693+
}
694+
687695
if (accessor.getAnnotation(PrimaryKey.class) != null || accessor.getAnnotation(Final.class) != null) {
688696
continue;
689697
}

kilo-client/src/test/java/org/httprpc/kilo/sql/QueryBuilderTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ public interface K {
173173
String getW();
174174
}
175175

176-
@Table("K")
177176
public interface L extends K {
178177
@Column("z")
179178
String getZ();

kilo-test/src/main/java/org/httprpc/kilo/test/FilmDetail.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
import org.httprpc.kilo.Description;
1818
import org.httprpc.kilo.sql.Column;
19-
import org.httprpc.kilo.sql.Table;
2019

2120
import java.util.List;
2221

23-
@Table("film")
2422
@Description("Represents detailed information about a film.")
2523
public interface FilmDetail extends Film {
2624
@Column("description")

kilo-test/src/main/java/org/httprpc/kilo/test/ItemDetail.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
import org.httprpc.kilo.Description;
1818
import org.httprpc.kilo.sql.Column;
1919
import org.httprpc.kilo.sql.Final;
20-
import org.httprpc.kilo.sql.Table;
2120

2221
import java.util.Date;
2322

24-
@Table("item")
2523
@Description("Represents detailed information about an item in the catalog.")
2624
public interface ItemDetail extends Item {
2725
@Column("size")

0 commit comments

Comments
 (0)