Skip to content

Commit 8bbbc8b

Browse files
committed
Fix bugs in indexes.
1 parent 7004210 commit 8bbbc8b

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

README.md

+2-2
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.3'
25+
implementation 'com.github.artbits:sqlite-java:1.0.4'
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.3</version>
38+
<version>1.0.4</version>
3939
</dependency>
4040
```
4141

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

+12-14
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void close() {
6464
@Override
6565
public void tables(Class<?>... classes) {
6666
HashMap<String, HashMap<String, String>> tablesMap = new HashMap<>();
67-
HashMap<String, Boolean> indexMap = new HashMap<>();
67+
HashMap<String, String> indexMap = new HashMap<>();
6868
String s = SQLTemplate.query("sqlite_master", new Options().where("type = ?", "table"));
6969
try (Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(s)) {
7070
DatabaseMetaData metaData = connection.getMetaData();
@@ -83,9 +83,7 @@ public void tables(Class<?>... classes) {
8383
while (set.next()) {
8484
String index = set.getString("INDEX_NAME");
8585
String column = set.getString("COLUMN_NAME");
86-
if (index != null) {
87-
indexMap.put(column, true);
88-
}
86+
Optional.ofNullable(index).ifPresent(i -> indexMap.put(index, column));
8987
}
9088
}
9189
}
@@ -106,25 +104,25 @@ public void tables(Class<?>... classes) {
106104
}
107105
});
108106
}
109-
reflect.getIndexList(column -> {
107+
reflect.getIndexList((index, column) -> {
110108
try {
111-
if (indexMap.getOrDefault(column, false)) {
112-
indexMap.remove(column, true);
109+
if (indexMap.get(index) != null) {
110+
indexMap.remove(index, column);
113111
} else {
114112
statement.executeUpdate(SQLTemplate.createIndex(tClass, column));
115113
}
116114
} catch (SQLException e) {
117115
throw new RuntimeException(e);
118116
}
119117
});
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-
});
127118
}
119+
indexMap.forEach((index, column) -> {
120+
try {
121+
statement.executeUpdate(SQLTemplate.dropIndex(index));
122+
} catch (SQLException e) {
123+
throw new RuntimeException(e);
124+
}
125+
});
128126
} catch (Exception e) {
129127
throw new RuntimeException(e);
130128
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
final class Reflect<T> {
2828

2929
private final Map<String, Field> fieldMap = new LinkedHashMap<>();
30+
private Class<?> tClass;
3031
private T t;
3132

3233
Reflect(Class<?> tClass) {
34+
this.tClass = tClass;
3335
newInstance(tClass);
3436
}
3537

@@ -140,10 +142,13 @@ void getDBColumnsWithType(BiConsumer<String, String> consumer) {
140142
}
141143

142144

143-
void getIndexList(Consumer<String> consumer) {
145+
void getIndexList(BiConsumer<String, String> consumer) {
146+
String table = tClass.getSimpleName().toLowerCase();
144147
fieldMap.values().forEach(field -> {
145148
if (isIndex(field)) {
146-
consumer.accept(field.getName());
149+
String column = field.getName();
150+
String index = String.format("idx_%s_%s", table, column);
151+
consumer.accept(index, column);
147152
}
148153
});
149154
}

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ static <T> String createIndex(Class<T> tClass, String column) {
131131
}
132132

133133

134-
static <T> String dropIndex(Class<T> tClass, String column) {
135-
String table = tClass.getSimpleName().toLowerCase();
136-
String index = $("idx_%s_%s", table, column);
134+
static <T> String dropIndex(String index) {
137135
return $("drop index %s", index);
138136
}
139137

0 commit comments

Comments
 (0)