11package me .zort .sqllib ;
22
33import com .google .gson .internal .Primitives ;
4- import me .zort .sqllib .api .repository .CachingSQLTableRepository ;
5- import me .zort .sqllib .api .repository .SQLTableRepository ;
4+ import lombok .RequiredArgsConstructor ;
65import me .zort .sqllib .internal .annotation .JsonField ;
76import me .zort .sqllib .internal .annotation .NullableField ;
87import me .zort .sqllib .internal .annotation .PrimaryKey ;
98import me .zort .sqllib .util .Arrays ;
109import me .zort .sqllib .util .Validator ;
11- import org .jetbrains .annotations .ApiStatus ;
1210
1311import java .lang .reflect .Field ;
1412import java .lang .reflect .Modifier ;
1513import java .util .Objects ;
1614
17- @ ApiStatus .Experimental // Still in development. So it can change in the future.
18- public class SQLTableRepositoryBuilder <T , ID > {
19-
20- private final SQLTableRepository .RepositoryInfo <T , ID > info ;
21- private SQLDatabaseConnection connection ;
22-
23- public SQLTableRepositoryBuilder () {
24- this .info = new SQLTableRepository .RepositoryInfo <>();
25- this .connection = null ;
26- }
27-
28- public SQLTableRepositoryBuilder <T , ID > withConnection (SQLDatabaseConnection connection ) {
29- this .connection = connection ;
30- return this ;
31- }
32-
33- public SQLTableRepositoryBuilder <T , ID > withTypeClass (Class <T > typeClass ) {
34- info .setTypeClass (typeClass );
35- return this ;
36- }
37-
38- public SQLTableRepositoryBuilder <T , ID > withIdClass (Class <ID > idClass ) {
39- info .setIdClass (idClass );
40- return this ;
41- }
42-
43- public SQLTableRepositoryBuilder <T , ID > withTableName (String tableName ) {
44- info .setTableName (tableName );
45- return this ;
46- }
47-
48- public SQLTableRepositoryBuilder <T , ID > withDefs (String ... defs ) {
49- info .setDefs (defs );
50- return this ;
51- }
52-
53- public SQLTableRepository <T , ID > build () {
54- return build (info -> new SQLTableRepository <>(connection , info ));
15+ @ RequiredArgsConstructor
16+ final class ObjectTableConverter {
17+
18+ private final SQLDatabaseConnection connection ;
19+ private final String tableName ;
20+ private final Class <?> typeClass ;
21+ private static final Class <?>[] supportedTypes = new Class <?>[] {
22+ Integer .class ,
23+ Long .class ,
24+ Float .class ,
25+ Double .class ,
26+ String .class
27+ };
28+
29+ public String buildTableQuery () {
30+ return String .format ("CREATE TABLE IF NOT EXISTS %s(%s);" , tableName , String .join (", " , buildDefsFromType ()));
5531 }
5632
57- public CachingSQLTableRepository <T , ID > buildCaching () {
58- return build (info -> new CachingSQLTableRepository <>(connection , info ));
59- }
60-
61- public <R extends SQLTableRepository <T , ID >> R build (RepoFactory <T , ID , R > factory ) {
62- if (info .getDefs () == null )
63- buildDefsFromType ();
64-
65- return factory .create (info );
66- }
67-
68- private void buildDefsFromType () {
69- Objects .requireNonNull (info .getTypeClass (), "Type class must be set before building repository!" );
33+ public String [] buildDefsFromType () {
34+ Objects .requireNonNull (typeClass , "Type class must be set before building repository!" );
7035
7136 if (!(connection instanceof SQLDatabaseConnectionImpl )) {
7237 throw new IllegalStateException ("We can build defs only from SQLDatabaseConnectionImpl child-classes." );
7338 }
74- debug ("Building defs from type class: " + info . getTypeClass () .getName ());
39+ debug ("Building defs from type class: " + typeClass .getName ());
7540
7641 SQLDatabaseOptions options = ((SQLDatabaseConnectionImpl ) connection ).getOptions ();
7742 String [] defs = new String [0 ];
7843
79- for (Field field : info . getTypeClass () .getDeclaredFields ()) {
44+ for (Field field : typeClass .getDeclaredFields ()) {
8045 debug ("Building def for field: " + field .getName () + " (" + field .getType ().getName () + ")" );
8146 if (Modifier .isTransient (field .getModifiers ())) {
8247 debug (String .format ("Field %s is transient, skipping." , field .getName ()));
@@ -97,32 +62,14 @@ private void buildDefsFromType() {
9762 debug ("Added def: " + colName + " " + colType );
9863 }
9964
100- info .setDefs (defs );
10165 debug ("Built defs: " + java .util .Arrays .toString (defs ));
102- }
10366
104- private void debug (String message ) {
105- if (connection instanceof SQLDatabaseConnectionImpl && ((SQLDatabaseConnectionImpl ) connection ).getOptions ().isDebug ())
106- ((SQLDatabaseConnectionImpl ) connection ).debug (message );
67+ return defs ;
10768 }
10869
10970 private String recognizeFieldTypeToDbType (Field field ) {
110- Class <?>[] supportedTypes = new Class <?>[] {
111- Integer .class ,
112- Long .class ,
113- Float .class ,
114- Double .class ,
115- String .class
116- };
11771 Class <?> fieldType = Primitives .wrap (field .getType ());
118-
119- boolean isSupported = false ;
120- for (Class <?> aClass : supportedTypes ) {
121- if (aClass .equals (fieldType )) {
122- isSupported = true ;
123- break ;
124- }
125- }
72+ boolean isSupported = isSupported (fieldType );
12673
12774 if (field .isAnnotationPresent (JsonField .class ) && !isSupported ) {
12875 return "TEXT" ;
@@ -153,12 +100,22 @@ private String recognizeFieldTypeToDbType(Field field) {
153100 return dbType ;
154101 }
155102
103+ private void debug (String message ) {
104+ if (connection instanceof SQLDatabaseConnectionImpl && ((SQLDatabaseConnectionImpl ) connection ).getOptions ().isDebug ())
105+ ((SQLDatabaseConnectionImpl ) connection ).debug (message );
106+ }
107+
156108 private boolean isSQLite () {
157109 return connection instanceof SQLiteDatabaseConnectionImpl ;
158110 }
159111
160- public interface RepoFactory <T , ID , R extends SQLTableRepository <T , ID >> {
161- R create (SQLTableRepository .RepositoryInfo <T , ID > info );
112+ public static boolean isSupported (Class <?> type ) {
113+ for (Class <?> aClass : supportedTypes ) {
114+ if (aClass .equals (type )) {
115+ return true ;
116+ }
117+ }
118+ return false ;
162119 }
163120
164121}
0 commit comments