[DEV] add support of SQLITE
All checks were successful
WEB karideo and rabbit/archidata/pipeline/head This commit looks good

This commit is contained in:
Edouard DUPIN 2023-02-01 20:21:11 +01:00
parent b85d5ec423
commit fab2b3b017
6 changed files with 167 additions and 71 deletions

View File

@ -190,4 +190,4 @@
</plugins> </plugins>
</reporting> </reporting>
</project> </project>

View File

@ -7,7 +7,8 @@ public class GlobalConfiguration {
public static DBConfig dbConfig = null; public static DBConfig dbConfig = null;
static { static {
dbConfig = new DBConfig(ConfigBaseVariable.getDBHost(), dbConfig = new DBConfig(ConfigBaseVariable.getDBType(),
ConfigBaseVariable.getDBHost(),
Integer.parseInt(ConfigBaseVariable.getDBPort()), Integer.parseInt(ConfigBaseVariable.getDBPort()),
ConfigBaseVariable.getDBLogin(), ConfigBaseVariable.getDBLogin(),
ConfigBaseVariable.getDBPassword(), ConfigBaseVariable.getDBPassword(),

View File

@ -1,5 +1,6 @@
package org.kar.archidata; package org.kar.archidata;
import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
@ -22,6 +23,7 @@ import org.kar.archidata.annotation.SQLTableLinkGeneric.ModelLink;
import org.kar.archidata.annotation.SQLTableName; import org.kar.archidata.annotation.SQLTableName;
import org.kar.archidata.annotation.SQLUpdateTime; import org.kar.archidata.annotation.SQLUpdateTime;
import org.kar.archidata.db.DBEntry; import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.ConfigBaseVariable;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -46,29 +48,56 @@ public class SqlWrapper {
} }
public static String convertTypeInSQL(Class<?> type) throws Exception { public static String convertTypeInSQL(Class<?> type) throws Exception {
if (type == Long.class || type == long.class ) { if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
return "bigint"; if (type == Long.class || type == long.class ) {
} return "bigint";
if (type == Integer.class || type == int.class ) { }
return "int"; if (type == Integer.class || type == int.class ) {
} return "int";
if (type == Boolean.class || type == boolean.class) { }
return "tinyint(1)"; if (type == Boolean.class || type == boolean.class) {
} return "tinyint(1)";
if (type == Float.class || type == float.class) { }
return "float"; if (type == Float.class || type == float.class) {
} return "float";
if (type == Double.class || type == double.class) { }
return "double"; if (type == Double.class || type == double.class) {
} return "double";
if (type == Timestamp.class) { }
return "timestamp(3)"; if (type == Timestamp.class) {
} return "timestamp(3)";
if (type == Date.class) { }
return "date"; if (type == Date.class) {
} return "date";
if (type == String.class) { }
return "text"; if (type == String.class) {
return "text";
}
} else {
if (type == Long.class || type == long.class ) {
return "INTEGER";
}
if (type == Integer.class || type == int.class ) {
return "INTEGER";
}
if (type == Boolean.class || type == boolean.class) {
return "INTEGER";
}
if (type == Float.class || type == float.class) {
return "REAL";
}
if (type == Double.class || type == double.class) {
return "REAL";
}
if (type == Timestamp.class) {
return "INTEGER";
}
if (type == Date.class) {
return "NUMERIC";
}
if (type == String.class) {
return "text";
}
} }
throw new Exception("Imcompatible type of element in object for: " + type.getCanonicalName()); throw new Exception("Imcompatible type of element in object for: " + type.getCanonicalName());
} }
@ -76,11 +105,11 @@ public class SqlWrapper {
protected static <T> void setValuedb(Class<?> type, T data, int index, Field field, PreparedStatement ps) throws IllegalArgumentException, IllegalAccessException, SQLException { protected static <T> void setValuedb(Class<?> type, T data, int index, Field field, PreparedStatement ps) throws IllegalArgumentException, IllegalAccessException, SQLException {
if (type == Long.class) { if (type == Long.class) {
Object tmp = field.get(data); Object tmp = field.get(data);
if (tmp == null) { if (tmp == null) {
ps.setNull(index++, Types.BIGINT); ps.setNull(index++, Types.BIGINT);
} else { } else {
ps.setLong(index++, (Long)tmp); ps.setLong(index++, (Long)tmp);
} }
} else if (type == long.class ) { } else if (type == long.class ) {
ps.setLong(index++, field.getLong(data)); ps.setLong(index++, field.getLong(data));
} else if (type == Integer.class) { } else if (type == Integer.class) {
@ -656,6 +685,12 @@ public class SqlWrapper {
} }
} }
public static void executeSimpleQuerry(String querry) throws SQLException, IOException {
DBEntry entry = new DBEntry(GlobalConfiguration.dbConfig);
Statement stmt = entry.connection.createStatement();
stmt.executeUpdate(querry);
}
public static <T> List<T> getsWhere(Class<T> clazz, List<WhereCondition> condition, String orderBy, boolean full, Integer linit) throws Exception { public static <T> List<T> getsWhere(Class<T> clazz, List<WhereCondition> condition, String orderBy, boolean full, Integer linit) throws Exception {
DBEntry entry = new DBEntry(GlobalConfiguration.dbConfig); DBEntry entry = new DBEntry(GlobalConfiguration.dbConfig);
List<T> outs = new ArrayList<>(); List<T> outs = new ArrayList<>();
@ -1111,34 +1146,55 @@ public class SqlWrapper {
} }
// special case with external link table: // special case with external link table:
if (linkGeneric == ModelLink.EXTERNAL) { if (linkGeneric == ModelLink.EXTERNAL) {
String localName = name; String localName = name;
if (name.endsWith("s")) { if (name.endsWith("s")) {
localName = name.substring(0, name.length()-1); localName = name.substring(0, name.length()-1);
} }
if (createIfNotExist) { if (createIfNotExist) {
otherTable.append("DROP TABLE IF EXISTS `"); otherTable.append("DROP TABLE IF EXISTS `");
otherTable.append(tableName); otherTable.append(tableName);
otherTable.append("_link_"); otherTable.append("_link_");
otherTable.append(localName); otherTable.append(localName);
otherTable.append("`;\n"); otherTable.append("`;\n");
} }
otherTable.append("CREATE TABLE `"); otherTable.append("CREATE TABLE `");
otherTable.append(tableName); otherTable.append(tableName);
otherTable.append("_link_"); otherTable.append("_link_");
otherTable.append(localName); otherTable.append(localName);
otherTable.append("`(\n"); otherTable.append("`(\n");
otherTable.append("\t\t`id` bigint NOT NULL AUTO_INCREMENT,\n"); if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
otherTable.append("\t\t`deleted` tinyint(1) NOT NULL DEFAULT '0',\n"); otherTable.append("\t\t`id` bigint NOT NULL AUTO_INCREMENT,\n");
otherTable.append("\t\t`create_date` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),\n"); otherTable.append("\t\t`deleted` tinyint(1) NOT NULL DEFAULT '0',\n");
otherTable.append("\t\t`modify_date` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),\n"); otherTable.append("\t\t`create_date` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),\n");
otherTable.append("\t\t`"); otherTable.append("\t\t`modify_date` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),\n");
otherTable.append(tableName); } else {
otherTable.append("_id` bigint NOT NULL,\n"); otherTable.append("\t\t`id` INTEGER PRIMARY KEY AUTOINCREMENT,\n");
otherTable.append("\t\t`"); otherTable.append("\t\t`deleted` INTEGER NOT NULL DEFAULT '0',\n");
otherTable.append(localName); otherTable.append("\t\t`create_date` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP(3),\n");
otherTable.append("_id` bigint NOT NULL,\n"); otherTable.append("\t\t`modify_date` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP(3),\n");
otherTable.append("\tPRIMARY KEY (`id`)\n"); }
otherTable.append("\t) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;\n\n"); otherTable.append("\t\t`");
otherTable.append(tableName);
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
otherTable.append("_id` bigint NOT NULL,\n");
} else {
otherTable.append("_id` INTEGER NOT NULL,\n");
}
otherTable.append("\t\t`");
otherTable.append(localName);
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
otherTable.append("_id` bigint NOT NULL,\n");
} else {
otherTable.append("_id` INTEGER NOT NULL,\n");
}
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
otherTable.append("\tPRIMARY KEY (`id`)\n");
}
otherTable.append("\t)");
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
otherTable.append(" ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;\n\n");
}
otherTable.append(";\n\n");
} else { } else {
if (firstField) { if (firstField) {
out.append("\n\t\t`"); out.append("\n\t\t`");
@ -1154,13 +1210,16 @@ public class SqlWrapper {
out.append(typeValue); out.append(typeValue);
} else { } else {
typeValue = convertTypeInSQL(elem.getType()); typeValue = convertTypeInSQL(elem.getType());
if (typeValue.equals("text")) { if (typeValue.equals("text") && !ConfigBaseVariable.getDBType().equals("sqlite")) {
if (limitSize != null) { if (limitSize != null) {
out.append("varchar("); out.append("varchar(");
out.append(limitSize); out.append(limitSize);
out.append(")"); out.append(")");
} else { } else {
out.append("text CHARACTER SET utf8"); out.append("text");
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
out.append(" CHARACTER SET utf8");
}
} }
} else { } else {
out.append(typeValue); out.append(typeValue);
@ -1168,14 +1227,20 @@ public class SqlWrapper {
} }
out.append(" "); out.append(" ");
if (notNull) { if (notNull) {
out.append("NOT NULL "); if (!name.equals(primaryKeyValue) || !ConfigBaseVariable.getDBType().equals("sqlite")) {
out.append("NOT NULL ");
}
if (defaultValue == null) { if (defaultValue == null) {
if (updateTime || createTime) { if (updateTime || createTime) {
out.append("DEFAULT CURRENT_TIMESTAMP(3) "); out.append("DEFAULT CURRENT_TIMESTAMP(3) ");
} }
} else { } else {
out.append("DEFAULT "); out.append("DEFAULT ");
out.append(defaultValue); if ("CURRENT_TIMESTAMP(3)".equals(defaultValue) && ConfigBaseVariable.getDBType().equals("sqlite")) {
out.append("CURRENT_TIMESTAMP");
} else {
out.append(defaultValue);
}
out.append(" "); out.append(" ");
} }
} else if (defaultValue == null) { } else if (defaultValue == null) {
@ -1189,24 +1254,36 @@ public class SqlWrapper {
out.append(defaultValue); out.append(defaultValue);
out.append(" "); out.append(" ");
}
if (name.equals(primaryKeyValue) && ConfigBaseVariable.getDBType().equals("sqlite")) {
out.append("PRIMARY KEY ");
} }
if (autoIncrement) { if (autoIncrement) {
out.append("AUTO_INCREMENT "); if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
out.append("AUTO_INCREMENT ");
} else {
out.append("AUTOINCREMENT ");
}
} }
if (comment != null) { if (comment != null && !ConfigBaseVariable.getDBType().equals("sqlite")) {
out.append("COMMENT '"); out.append("COMMENT '");
out.append(comment.replaceAll("'", "\'")); out.append(comment.replaceAll("'", "\'"));
out.append("' "); out.append("' ");
} }
} }
} }
if (primaryKeyValue != null) { if (primaryKeyValue != null && !ConfigBaseVariable.getDBType().equals("sqlite")) {
out.append(",\n\tPRIMARY KEY (`"); out.append(",\n\tPRIMARY KEY (`");
out.append(primaryKeyValue); out.append(primaryKeyValue);
out.append("`)"); out.append("`)");
} }
out.append("\n\t) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;\n"); out.append("\n\t)");
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
out.append(" ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci");
}
out.append(";\n");
return out.toString() + otherTable.toString(); return out.toString() + otherTable.toString();
} }

View File

@ -1,13 +1,19 @@
package org.kar.archidata.db; package org.kar.archidata.db;
public class DBConfig { public class DBConfig {
private final String type;
private final String hostname; private final String hostname;
private final int port; private final int port;
private final String login; private final String login;
private final String password; private final String password;
private final String dbName; private final String dbName;
public DBConfig(String hostname, Integer port, String login, String password, String dbName) { public DBConfig(String type, String hostname, Integer port, String login, String password, String dbName) {
if (type == null) {
this.type = "mysql";
} else {
this.type = type;
}
if (hostname == null) { if (hostname == null) {
this.hostname = "localhost"; this.hostname = "localhost";
} else { } else {
@ -26,7 +32,8 @@ public class DBConfig {
@Override @Override
public String toString() { public String toString() {
return "DBConfig{" + return "DBConfig{" +
"hostname='" + hostname + '\'' + "type='" + type + '\'' +
", hostname='" + hostname + '\'' +
", port=" + port + ", port=" + port +
", login='" + login + '\'' + ", login='" + login + '\'' +
", password='" + password + '\'' + ", password='" + password + '\'' +
@ -55,6 +62,9 @@ public class DBConfig {
} }
public String getUrl() { public String getUrl() {
return "jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.dbName + "?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC"; if (type.equals("sqlite")) {
return "jdbc:sqlite:" + this.hostname + ".db";
}
return "jdbc:" + this.type + "://" + this.hostname + ":" + this.port + "/" + this.dbName + "?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC";
} }
} }

View File

@ -26,12 +26,12 @@ public class GenericTable {
@SQLCreateTime @SQLCreateTime
@SQLNotNull @SQLNotNull
@SQLComment("Create time of the object") @SQLComment("Create time of the object")
@SQLDefault("now(3)") @SQLDefault("CURRENT_TIMESTAMP(3)")
public Timestamp create_date = null; public Timestamp create_date = null;
@SQLNotRead @SQLNotRead
@SQLUpdateTime @SQLUpdateTime
@SQLNotNull @SQLNotNull
@SQLComment("When update the object") @SQLComment("When update the object")
@SQLDefault("now(3)") @SQLDefault("CURRENT_TIMESTAMP(3)")
public Timestamp modify_date = null; public Timestamp modify_date = null;
} }

View File

@ -3,6 +3,7 @@ package org.kar.archidata.util;
public class ConfigBaseVariable { public class ConfigBaseVariable {
static public String tmpDataFolder = System.getenv("DATA_TMP_FOLDER"); static public String tmpDataFolder = System.getenv("DATA_TMP_FOLDER");
static public String dataFolder = System.getenv("DATA_FOLDER"); static public String dataFolder = System.getenv("DATA_FOLDER");
static public String dbType = System.getenv("DB_TYPE");
static public String dbHost = System.getenv("DB_HOST"); static public String dbHost = System.getenv("DB_HOST");
static public String dbPort = System.getenv("DB_PORT"); static public String dbPort = System.getenv("DB_PORT");
static public String dbUser = System.getenv("DB_USER"); static public String dbUser = System.getenv("DB_USER");
@ -26,6 +27,13 @@ public class ConfigBaseVariable {
return dataFolder; return dataFolder;
} }
public static String getDBType() {
if (dbType == null) {
return "mysql";
}
return dbType;
}
public static String getDBHost() { public static String getDBHost() {
if (dbHost == null) { if (dbHost == null) {
return "localhost"; return "localhost";