Skip to content

Commit 7004210

Browse files
committed
Support setting index fields and JSON type fields.
Optimize the code.
1 parent f902e2d commit 7004210

File tree

9 files changed

+177
-247
lines changed

9 files changed

+177
-247
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ repositories {
2222
}
2323
2424
dependencies {
25-
implementation 'com.github.artbits:sqlite-java:1.0.2'
25+
implementation 'com.github.artbits:sqlite-java:1.0.3'
2626
}
2727
```
2828
Maven:
@@ -35,7 +35,7 @@ Maven:
3535
<dependency>
3636
<groupId>com.github.artbits</groupId>
3737
<artifactId>sqlite-java</artifactId>
38-
<version>1.0.2</version>
38+
<version>1.0.3</version>
3939
</dependency>
4040
```
4141

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ repositories {
1212

1313
dependencies {
1414
implementation 'org.xerial:sqlite-jdbc:3.43.0.0'
15+
implementation 'com.google.code.gson:gson:2.10.1'
1516
testImplementation platform('org.junit:junit-bom:5.9.1')
1617
testImplementation 'org.junit.jupiter:junit-jupiter'
1718
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.artbits.jsqlite;
2+
3+
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Target(ElementType.FIELD)
10+
@Retention(RetentionPolicy.RUNTIME)
11+
public @interface Column {
12+
boolean index() default false;
13+
boolean ignore() default false;
14+
boolean json() default false;
15+
}

src/main/java/com/github/artbits/jsqlite/Core.java

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,23 @@
1616

1717
package com.github.artbits.jsqlite;
1818

19+
import com.google.gson.Gson;
20+
1921
import java.nio.file.Files;
2022
import java.nio.file.Path;
2123
import java.nio.file.Paths;
2224
import java.sql.*;
2325
import java.util.*;
26+
import java.util.concurrent.locks.ReentrantLock;
2427
import java.util.function.Consumer;
2528

2629
final 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

src/main/java/com/github/artbits/jsqlite/DataSupport.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Optional;
2020
import java.util.function.Consumer;
2121

22+
import static com.github.artbits.jsqlite.Core.gson;
23+
2224
public class DataSupport<T> {
2325

2426
Long id;
@@ -53,7 +55,7 @@ public final long updatedAt() {
5355

5456

5557
public final String toJson() {
56-
return new JsonObject(this).toString();
58+
return gson.toJson(this);
5759
}
5860

5961

0 commit comments

Comments
 (0)