1616
1717package com .github .artbits .jsqlite ;
1818
19+ import com .google .gson .Gson ;
20+
1921import java .nio .file .Files ;
2022import java .nio .file .Path ;
2123import java .nio .file .Paths ;
2224import java .sql .*;
2325import java .util .*;
26+ import java .util .concurrent .locks .ReentrantLock ;
2427import java .util .function .Consumer ;
2528
2629final class Core implements DB {
2730
28- private static Connection connection ;
31+ final static Gson gson = new Gson ();
32+
33+ private final ReentrantLock lock = new ReentrantLock ();
34+
35+ private final Connection connection ;
2936
3037
3138 Core (String path ) {
@@ -57,6 +64,7 @@ public void close() {
5764 @ Override
5865 public void tables (Class <?>... classes ) {
5966 HashMap <String , HashMap <String , String >> tablesMap = new HashMap <>();
67+ HashMap <String , Boolean > indexMap = new HashMap <>();
6068 String s = SQLTemplate .query ("sqlite_master" , new Options ().where ("type = ?" , "table" ));
6169 try (Statement statement = connection .createStatement (); ResultSet result = statement .executeQuery (s )) {
6270 DatabaseMetaData metaData = connection .getMetaData ();
@@ -71,14 +79,24 @@ public void tables(Class<?>... classes) {
7179 }
7280 }
7381 tablesMap .put (tableName , tableColumnTypeMap );
82+ try (ResultSet set = metaData .getIndexInfo (null , null , tableName , false , false )){
83+ while (set .next ()) {
84+ String index = set .getString ("INDEX_NAME" );
85+ String column = set .getString ("COLUMN_NAME" );
86+ if (index != null ) {
87+ indexMap .put (column , true );
88+ }
89+ }
90+ }
7491 }
7592 for (Class <?> tClass : classes ) {
7693 String tableName = tClass .getSimpleName ().toLowerCase ();
7794 HashMap <String , String > tableColumnTypeMap = tablesMap .getOrDefault (tableName , null );
95+ Reflect <?> reflect = new Reflect <>(tClass );
7896 if (tableColumnTypeMap == null ) {
7997 statement .executeUpdate (SQLTemplate .create (tClass ));
8098 } else {
81- new Reflect <>( tClass ) .getDBColumnsWithType ((column , type ) -> {
99+ reflect .getDBColumnsWithType ((column , type ) -> {
82100 if (tableColumnTypeMap .getOrDefault (column , null ) == null ) {
83101 try {
84102 statement .executeUpdate (SQLTemplate .addTableColumn (tableName , column , type ));
@@ -88,6 +106,24 @@ public void tables(Class<?>... classes) {
88106 }
89107 });
90108 }
109+ reflect .getIndexList (column -> {
110+ try {
111+ if (indexMap .getOrDefault (column , false )) {
112+ indexMap .remove (column , true );
113+ } else {
114+ statement .executeUpdate (SQLTemplate .createIndex (tClass , column ));
115+ }
116+ } catch (SQLException e ) {
117+ throw new RuntimeException (e );
118+ }
119+ });
120+ indexMap .keySet ().forEach (column -> {
121+ try {
122+ statement .executeUpdate (SQLTemplate .dropIndex (tClass , column ));
123+ } catch (SQLException e ) {
124+ throw new RuntimeException (e );
125+ }
126+ });
91127 }
92128 } catch (Exception e ) {
93129 throw new RuntimeException (e );
@@ -120,32 +156,34 @@ public String version() {
120156
121157 @ Override
122158 public <T extends DataSupport <T >> void insert (T t ) {
123- synchronized (this ) {
124- try (Statement statement = connection .createStatement ()) {
125- t .createdAt = System .currentTimeMillis ();
126- t .updatedAt = t .createdAt ;
127- statement .executeUpdate (SQLTemplate .insert (t ));
128- try (ResultSet result = statement .executeQuery ("select last_insert_rowid()" )) {
129- if (result .next ()) {
130- t .id = result .getLong (1 );
131- }
159+ try (Statement statement = connection .createStatement ()) {
160+ lock .lock ();
161+ t .createdAt = System .currentTimeMillis ();
162+ t .updatedAt = t .createdAt ;
163+ statement .executeUpdate (SQLTemplate .insert (t ));
164+ try (ResultSet result = statement .executeQuery ("select last_insert_rowid()" )) {
165+ if (result .next ()) {
166+ t .id = result .getLong (1 );
132167 }
133- } catch (Exception e ) {
134- throw new RuntimeException (e );
135168 }
169+ } catch (Exception e ) {
170+ throw new RuntimeException (e );
171+ } finally {
172+ lock .unlock ();
136173 }
137174 }
138175
139176
140177 @ Override
141178 public <T extends DataSupport <T >> void update (T t , String predicate , Object ... args ) {
142- synchronized (this ) {
143- try (Statement statement = connection .createStatement ()) {
144- t .updatedAt = System .currentTimeMillis ();
145- statement .executeUpdate (SQLTemplate .update (t , new Options ().where (predicate , args )));
146- } catch (Exception e ) {
147- throw new RuntimeException (e );
148- }
179+ try (Statement statement = connection .createStatement ()) {
180+ lock .lock ();
181+ t .updatedAt = System .currentTimeMillis ();
182+ statement .executeUpdate (SQLTemplate .update (t , new Options ().where (predicate , args )));
183+ } catch (Exception e ) {
184+ throw new RuntimeException (e );
185+ } finally {
186+ lock .unlock ();
149187 }
150188 }
151189
@@ -158,13 +196,14 @@ public <T extends DataSupport<T>> void update(T t) {
158196
159197 @ Override
160198 public <T extends DataSupport <T >> void delete (Class <T > tClass , String predicate , Object ... args ) {
161- synchronized (this ) {
162- String sql = SQLTemplate .delete (tClass , new Options ().where (predicate , args ));
163- try (Statement statement = connection .createStatement ()) {
164- statement .executeUpdate (sql );
165- } catch (Exception e ) {
166- throw new RuntimeException (e );
167- }
199+ String sql = SQLTemplate .delete (tClass , new Options ().where (predicate , args ));
200+ try (Statement statement = connection .createStatement ()) {
201+ lock .lock ();
202+ statement .executeUpdate (sql );
203+ } catch (Exception e ) {
204+ throw new RuntimeException (e );
205+ } finally {
206+ lock .unlock ();
168207 }
169208 }
170209
0 commit comments