44import me .zort .sqllib .api .SQLDatabaseConnection ;
55import me .zort .sqllib .api .repository .CachingSQLTableRepository ;
66import me .zort .sqllib .api .repository .SQLTableRepository ;
7+ import me .zort .sqllib .internal .annotation .JsonField ;
8+ import me .zort .sqllib .internal .annotation .NullableField ;
9+ import me .zort .sqllib .internal .annotation .PrimaryKey ;
710import me .zort .sqllib .util .Arrays ;
811import me .zort .sqllib .util .Validator ;
912import org .jetbrains .annotations .ApiStatus ;
@@ -69,31 +72,50 @@ private void buildDefsFromType() {
6972 if (!(connection instanceof SQLDatabaseConnectionImpl )) {
7073 throw new IllegalStateException ("We can build defs only from SQLDatabaseConnectionImpl child-classes." );
7174 }
75+ debug ("Building defs from type class: " + info .getTypeClass ().getName ());
7276
7377 SQLDatabaseOptions options = ((SQLDatabaseConnectionImpl ) connection ).getOptions ();
7478 String [] defs = new String [0 ];
7579
76- for (Field field : info .getTypeClass ().getFields ()) {
77- if (Modifier .isTransient (field .getModifiers ()))
80+ for (Field field : info .getTypeClass ().getDeclaredFields ()) {
81+ debug ("Building def for field: " + field .getName () + " (" + field .getType ().getName () + ")" );
82+ if (Modifier .isTransient (field .getModifiers ())) {
83+ debug (String .format ("Field %s is transient, skipping." , field .getName ()));
7884 continue ;
85+ }
7986
8087 String colName = options .getNamingStrategy ().fieldNameToColumn (field .getName ());
8188 String colType = recognizeFieldTypeToDbType (field );
89+
90+ if (colType != null && !colType .contains ("NOT NULL" ) && field .isAnnotationPresent (NullableField .class )) {
91+ if (!field .getAnnotation (NullableField .class ).nullable ()) {
92+ colType += " NOT NULL" ;
93+ }
94+ }
95+
8296 defs = Arrays .add (defs , colName + " " + colType );
97+
98+ debug ("Added def: " + colName + " " + colType );
8399 }
84100
85101 info .setDefs (defs );
102+ debug ("Built defs: " + java .util .Arrays .toString (defs ));
86103 }
87104
88- private static String recognizeFieldTypeToDbType (Field field ) {
105+ private void debug (String message ) {
106+ if (connection instanceof SQLDatabaseConnectionImpl && ((SQLDatabaseConnectionImpl ) connection ).getOptions ().isDebug ())
107+ ((SQLDatabaseConnectionImpl ) connection ).debug (message );
108+ }
109+
110+ private String recognizeFieldTypeToDbType (Field field ) {
89111 Class <?>[] supportedTypes = new Class <?>[] {
90112 Integer .class ,
91113 Long .class ,
92114 Float .class ,
93115 Double .class ,
94116 String .class
95117 };
96- Class <?> fieldType = field .getType ();
118+ Class <?> fieldType = Primitives . wrap ( field .getType () );
97119
98120 boolean isSupported = false ;
99121 for (Class <?> aClass : supportedTypes ) {
@@ -103,14 +125,18 @@ private static String recognizeFieldTypeToDbType(Field field) {
103125 }
104126 }
105127
128+ if (field .isAnnotationPresent (JsonField .class ) && !isSupported ) {
129+ return "TEXT" ;
130+ }
131+
106132 if (!isSupported )
107133 throw new RuntimeException (String .format ("We don't support %s types in SQLTableRepositoryBuilder yet." , fieldType .getSimpleName ()));
108134
109135 String dbType = null ;
110136 if (fieldType .equals (String .class )) {
111137 dbType = "VARCHAR(255)" ;
112138 } else if (Primitives .wrap (fieldType ).equals (Integer .class )) {
113- dbType = "INT " ;
139+ dbType = "INTEGER " ;
114140 } else if (Primitives .wrap (fieldType ).equals (Long .class )) {
115141 dbType = "BIGINT" ;
116142 } else if (Primitives .wrap (fieldType ).equals (Double .class )) {
@@ -120,10 +146,13 @@ private static String recognizeFieldTypeToDbType(Field field) {
120146 }
121147
122148 if (Validator .validateAutoIncrement (field ))
123- dbType += " PRIMARY KEY AUTO_INCREMENT" ;
149+ dbType += " PRIMARY KEY " + (isSQLite () ? "AUTOINCREMENT" : "AUTO_INCREMENT" );
150+
151+ return dbType ;
152+ }
124153
125- // Practically, it should not come here.
126- return null ;
154+ private boolean isSQLite () {
155+ return connection instanceof SQLiteDatabaseConnectionImpl ;
127156 }
128157
129158 public interface RepoFactory <T , ID , R extends SQLTableRepository <T , ID >> {
0 commit comments