Skip to content

Commit e0a398b

Browse files
committed
Update SQL.java
1 parent 1a228fd commit e0a398b

1 file changed

Lines changed: 8 additions & 15 deletions

File tree

  • src/main/java/io/github/intisy/utils/custom

src/main/java/io/github/intisy/utils/custom/SQL.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import java.util.ArrayList;
88
import java.util.Collections;
99
import java.util.List;
10-
import java.util.Map;
11-
import java.util.concurrent.ConcurrentHashMap;
1210

1311
@SuppressWarnings("unused")
1412
public class SQL implements AutoCloseable {
@@ -18,13 +16,10 @@ public class SQL implements AutoCloseable {
1816
private SimpleLogger logger;
1917
private final DatabaseType databaseType;
2018
private Connection connection;
21-
22-
private static final Map<String, String> CREATE_TABLE_TEMPLATES = new ConcurrentHashMap<>();
19+
private static final List<Double<String, String>> CREATE_TABLE_TEMPLATES = new ArrayList<>();
2320
static {
24-
CREATE_TABLE_TEMPLATES.put("INTEGER PRIMARY KEY",
25-
"INTEGER PRIMARY KEY AUTOINCREMENT"); // SQLite
26-
CREATE_TABLE_TEMPLATES.put("INT PRIMARY KEY",
27-
"INT AUTO_INCREMENT PRIMARY KEY"); // MySQL
21+
CREATE_TABLE_TEMPLATES.add(new Double<>("INTEGER", "INT"));
22+
CREATE_TABLE_TEMPLATES.add(new Double<>("AUTOINCREMENT", "AUTO_INCREMENT"));
2823
}
2924

3025
public enum DatabaseType {
@@ -139,13 +134,11 @@ private String buildCreateTableStatement(String name, String... columns) {
139134

140135
for (int i = 0; i < columns.length; i++) {
141136
String column = columns[i];
142-
// Apply database-specific modifications
143-
for (Map.Entry<String, String> template : CREATE_TABLE_TEMPLATES.entrySet()) {
144-
if (column.toUpperCase().contains(template.getKey())) {
145-
column = column.replaceAll("(?i)" + template.getKey(),
146-
databaseType == DatabaseType.SQLITE ?
147-
CREATE_TABLE_TEMPLATES.get(template.getKey()) :
148-
template.getValue());
137+
for (Double<String, String> template : CREATE_TABLE_TEMPLATES) {
138+
if (databaseType == DatabaseType.MYSQL) {
139+
column = column.replace(template.getKey(), template.getValue());
140+
} else if (databaseType == DatabaseType.SQLITE) {
141+
column = column.replace(template.getValue(), template.getKey());
149142
}
150143
}
151144
sql.append(column);

0 commit comments

Comments
 (0)