Skip to content

Commit ebf3d92

Browse files
authored
Merge pull request #48 from ZorTik/development
Development
2 parents dbeb610 + c820dac commit ebf3d92

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

asql-core/src/main/java/me/zort/sqllib/model/builder/EntitySchemaBuilder.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import java.lang.reflect.Field;
1818
import java.lang.reflect.Modifier;
19+
import java.util.ArrayList;
20+
import java.util.List;
1921
import java.util.Objects;
2022

2123
/**
@@ -51,7 +53,10 @@ public TableSchema buildTableSchema() {
5153

5254
debug("Building defs from type class: " + typeClass.getName());
5355

56+
boolean multiplePK = getPKCount() > 1;
57+
5458
ColumnDefinition[] defs = new ColumnDefinition[0];
59+
List<String> pks = new ArrayList<>();
5560
for (Field field : typeClass.getDeclaredFields()) {
5661
debug("Building def for field: " + field.getName() + " (" + field.getType().getName() + ")");
5762
if (Modifier.isTransient(field.getModifiers())) {
@@ -60,7 +65,7 @@ public TableSchema buildTableSchema() {
6065
}
6166

6267
String colName = namingStrategy.fieldNameToColumn(field.getName());
63-
String colType = recognizeFieldTypeToDbType(field);
68+
String colType = recognizeFieldTypeToDbType(field, multiplePK);
6469

6570
if (colType != null && !colType.contains("NOT NULL") && field.isAnnotationPresent(NullableField.class)) {
6671
if (!field.getAnnotation(NullableField.class).nullable()) {
@@ -75,17 +80,35 @@ public TableSchema buildTableSchema() {
7580
colType += " DEFAULT " + defaultValue;
7681
}
7782

83+
if (field.isAnnotationPresent(PrimaryKey.class)) {
84+
pks.add(colName);
85+
}
86+
7887
defs = Arrays.add(defs, new ColumnDefinition(colName, colType));
7988

8089
debug("Added def: " + colName + " " + colType);
8190
}
8291

92+
if (multiplePK) {
93+
defs = Arrays.add(defs, new ColumnDefinition("PRIMARY KEY", String.format("(%s)", String.join(", ", pks))));
94+
}
95+
8396
debug("Built defs: " + java.util.Arrays.toString(defs));
8497

8598
return new TableSchema(tableName, defs);
8699
}
87100

88-
private String recognizeFieldTypeToDbType(Field field) {
101+
private int getPKCount() {
102+
int count = 0;
103+
for (Field field : typeClass.getDeclaredFields()) {
104+
if (field.isAnnotationPresent(PrimaryKey.class)) {
105+
count++;
106+
}
107+
}
108+
return count;
109+
}
110+
111+
private String recognizeFieldTypeToDbType(Field field, boolean multiplePK) {
89112
Class<?> fieldType = Primitives.wrap(field.getType());
90113
boolean isSupported = isSupported(fieldType);
91114

@@ -111,8 +134,9 @@ private String recognizeFieldTypeToDbType(Field field) {
111134
dbType = "FLOAT";
112135
}
113136

114-
if (field.isAnnotationPresent(PrimaryKey.class))
137+
if (field.isAnnotationPresent(PrimaryKey.class) && !multiplePK) {
115138
dbType += " PRIMARY KEY";
139+
}
116140

117141
if (Validator.validateAutoIncrement(field))
118142
dbType += " " + (isSQLite() ? "AUTOINCREMENT" : "AUTO_INCREMENT");

src/test/java/me/zort/sqllib/test/TestCase1.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,12 @@ public void test8_RawQuery() {
223223
}
224224

225225
@Test
226-
public void test9_Close() {
226+
public void test9_MultiplePk() {
227+
assertTrue(connection.buildEntitySchema("multiple_pk", MultiplePKModel.class));
228+
}
229+
230+
@AfterAll
231+
public void close() {
227232
System.out.println("Closing connection...");
228233
connection.disconnect();
229234
System.out.println("Connection closed");
@@ -264,4 +269,13 @@ public int hashCode() {
264269
}
265270
}
266271

272+
@AllArgsConstructor
273+
private static class MultiplePKModel {
274+
@PrimaryKey
275+
private final String nickname;
276+
@PrimaryKey
277+
private final String email;
278+
private final int points;
279+
}
280+
267281
}

0 commit comments

Comments
 (0)