[FIX] back compatibility of mySQL and SQLite

This commit is contained in:
Edouard DUPIN 2024-12-29 16:13:34 +01:00
parent e8c6771b79
commit 820cf7d3d2
8 changed files with 208 additions and 170 deletions

View File

@ -802,7 +802,7 @@ public class DBAccessSQL extends DBAccess {
final List<Field> asyncFieldUpdate = new ArrayList<>();
Long uniqueSQLID = null;
UUID uniqueSQLUUID = null;
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final String tableName = AnnotationTools.getTableName(clazz, options);
Field primaryKeyField = null;
boolean generateUUID = false;
// real add in the BDD:
@ -1031,8 +1031,7 @@ public class DBAccessSQL extends DBAccess {
final List<LazyGetter> asyncActions = new ArrayList<>();
// real add in the BDD:
try {
final String tableName = this.db.getCongig().getDbName() + "."
+ AnnotationTools.getTableName(clazz, options);
final String tableName = AnnotationTools.getTableName(clazz, options);
// boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
final StringBuilder query = new StringBuilder();
query.append("UPDATE `");
@ -1207,7 +1206,6 @@ public class DBAccessSQL extends DBAccess {
) throws Exception {
final boolean readAllfields = QueryOptions.readAllColomn(options);
final String tableName = AnnotationTools.getTableName(clazz, options);
//final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final String primaryKey = AnnotationTools.getPrimaryKeyField(clazz).getName();
boolean firstField = true;
@ -1254,8 +1252,7 @@ public class DBAccessSQL extends DBAccess {
final CountInOut count = new CountInOut();
final StringBuilder querySelect = new StringBuilder();
StringBuilder query = new StringBuilder();
final String tableName = this.db.getCongig().getDbName() + "."
+ AnnotationTools.getTableName(clazz, options);
final String tableName = AnnotationTools.getTableName(clazz, options);
querySelect.append("SELECT ");
query.append(" FROM `");
query.append(tableName);
@ -1361,8 +1358,7 @@ public class DBAccessSQL extends DBAccess {
// real add in the BDD:
try {
final StringBuilder query = new StringBuilder();
final String tableName = this.db.getCongig().getDbName() + "."
+ AnnotationTools.getTableName(clazz, options);
final String tableName = AnnotationTools.getTableName(clazz, options);
query.append("SELECT COUNT(*) AS count FROM `");
query.append(tableName);
query.append("` ");
@ -1400,7 +1396,7 @@ public class DBAccessSQL extends DBAccess {
public long deleteHardWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
final QueryOptions options = new QueryOptions(option);
final Condition condition = conditionFusionOrEmpty(options, true);
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final String tableName = AnnotationTools.getTableName(clazz, options);
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
// find the deleted field
final StringBuilder query = new StringBuilder();
@ -1419,7 +1415,7 @@ public class DBAccessSQL extends DBAccess {
public long deleteSoftWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
final QueryOptions options = new QueryOptions(option);
final Condition condition = conditionFusionOrEmpty(options, true);
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final String tableName = AnnotationTools.getTableName(clazz, options);
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
/* String updateFieldName = null; if ("sqlite".equalsIgnoreCase(ConfigBaseVariable.getDBType())) { updateFieldName = AnnotationTools.getUpdatedFieldName(clazz); } */
// find the deleted field
@ -1445,7 +1441,7 @@ public class DBAccessSQL extends DBAccess {
public long unsetDeleteWhere(final Class<?> clazz, final QueryOption... option) throws DataAccessException {
final QueryOptions options = new QueryOptions(option);
final Condition condition = conditionFusionOrEmpty(options, true);
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final String tableName = AnnotationTools.getTableName(clazz, options);
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
if (deletedFieldName == null) {
throw new DataAccessException("The class " + clazz.getCanonicalName() + " has no deleted field");
@ -1473,7 +1469,7 @@ public class DBAccessSQL extends DBAccess {
@Override
public void drop(final Class<?> clazz, final QueryOption... option) throws Exception {
final QueryOptions options = new QueryOptions(option);
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final String tableName = AnnotationTools.getTableName(clazz, options);
final StringBuilder query = new StringBuilder();
query.append("DROP TABLE IF EXISTS `");
query.append(tableName);
@ -1501,7 +1497,7 @@ public class DBAccessSQL extends DBAccess {
@Override
public void cleanAll(final Class<?> clazz, final QueryOption... option) throws Exception {
final QueryOptions options = new QueryOptions(option);
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final String tableName = AnnotationTools.getTableName(clazz, options);
final StringBuilder query = new StringBuilder();
query.append("DELETE FROM `");
query.append(tableName);

View File

@ -28,8 +28,6 @@ import jakarta.persistence.GenerationType;
public class DataFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(DataFactory.class);
public static final String DEFAULT_DB_NAME_INVALID = "@__ZZZ__TABLE_NAME__ZZZ__@.";
public static String convertTypeInSQL(final Class<?> type, final String fieldName) throws DataAccessException {
final String typelocal = ConfigBaseVariable.getDBType();
if ("mysql".equals(typelocal)) {
@ -331,7 +329,7 @@ public class DataFactory {
}
public static List<String> createTable(final Class<?> clazz, final QueryOptions options) throws Exception {
final String tableName = DEFAULT_DB_NAME_INVALID + "." + AnnotationTools.getTableName(clazz, options);
final String tableName = AnnotationTools.getTableName(clazz, options);
boolean createDrop = false;
if (options != null) {

View File

@ -13,10 +13,10 @@ public class DbConfig {
static final Logger LOGGER = LoggerFactory.getLogger(DBAccess.class);
private final String type;
private final String hostname;
private final int port;
private final Short port;
private final String login;
private final String password;
private final String dbName;
private String dbName;
private final boolean keepConnected;
private final List<Class<?>> classes;
@ -90,6 +90,10 @@ public class DbConfig {
return this.dbName;
}
public void setDbName(final String dbName) {
this.dbName = dbName;
}
public boolean getKeepConnected() {
return this.keepConnected;
}
@ -99,14 +103,7 @@ public class DbConfig {
}
public String getUrl() {
return getUrl(false);
}
public String getUrl(final boolean isRoot) {
if (this.type.equals("sqlite")) {
if (isRoot) {
LOGGER.error("Can not manage root connection on SQLite...");
}
if (this.hostname.equals("memory")) {
return "jdbc:sqlite::memory:";
}
@ -116,12 +113,12 @@ public class DbConfig {
return "mongodb://" + getLogin() + ":" + getPassword() + "@" + this.hostname + ":" + this.port;
}
if ("mysql".equals(this.type)) {
LOGGER.warn("Request log on SQL: isRoot={}", isRoot);
if (isRoot) {
if (this.dbName == null || this.dbName.isEmpty()) {
LOGGER.warn("Request log on SQL: root");
return "jdbc:" + this.type + "://" + this.hostname + ":" + this.port
+ "/?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC";
}
return "jdbc:" + this.type + "://" + this.hostname + ":" + this.port // + "/" + this.dbName
return "jdbc:" + this.type + "://" + this.hostname + ":" + this.port + "/" + this.dbName
+ "?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC";
}
return "dead_code";
@ -136,7 +133,7 @@ public class DbConfig {
return false;
}
if (other instanceof final DbConfig dbConfig) {
return this.port == dbConfig.port //
return Objects.equals(this.port, dbConfig.port) //
&& this.keepConnected == dbConfig.keepConnected //
&& Objects.equals(this.type, dbConfig.type) //
&& Objects.equals(this.hostname, dbConfig.hostname) //
@ -153,4 +150,16 @@ public class DbConfig {
return Objects.hash(this.type, this.hostname, this.port, this.login, this.password, this.dbName,
this.keepConnected, this.classes);
}
@Override
public DbConfig clone() {
try {
return new DbConfig(this.type, this.hostname, this.port, this.login, this.password, this.dbName,
this.keepConnected, this.classes);
} catch (final DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

View File

@ -4,7 +4,6 @@ import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.kar.archidata.dataAccess.DBAccess;
import org.kar.archidata.dataAccess.DBAccessSQL;
@ -104,25 +103,7 @@ public class MigrationEngine {
}
}
public void migrateErrorThrow(final DbConfig config) throws MigrationException {
try (final DBAccess da = DBAccess.createInterface(config)) {
migrateErrorThrow(config, da);
} catch (final InternalServerErrorException e) {
e.printStackTrace();
throw new MigrationException("TODO ...");
} catch (final IOException e) {
e.printStackTrace();
throw new MigrationException("TODO ...");
}
}
/** Process the automatic migration of the system
* @param config SQL connection for the migration
* @throws IOException Error if access on the DB */
@SuppressFBWarnings({ "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING",
"SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE" })
public void migrateErrorThrow(final DbConfig config, final DBAccess da) throws MigrationException {
LOGGER.info("Execute migration ... [BEGIN]");
private void listAvailableMigration() throws MigrationException {
// check the integrity of the migrations:
LOGGER.info("List of availlable Migration: ");
for (final MigrationInterface elem : this.datas) {
@ -146,123 +127,151 @@ public class MigrationEngine {
}
}
}
}
// STEP 1: Check the DB exist:
LOGGER.info("Verify existance of '{}'", config.getDbName());
boolean exist = da.isDBExist(config.getDbName());
if (!exist) {
LOGGER.warn("DB: '{}' DOES NOT EXIST ==> create one", config.getDbName());
// create the local DB:
da.createDB(config.getDbName());
}
exist = da.isDBExist(config.getDbName());
while (!exist) {
LOGGER.error("DB: '{}' DOES NOT EXIST after trying to create one ", config.getDbName());
LOGGER.error("Waiting administrator create a new one, we check after 30 seconds...");
try {
Thread.sleep(30000);
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
exist = da.isDBExist(config.getDbName());
}
LOGGER.info("DB '{}' exist.", config.getDbName());
// STEP 2: Check migration table exist:
LOGGER.info("Verify existance of migration table '{}'", "KAR_migration");
if (da instanceof final DBAccessSQL daSQL) {
exist = da.isTableExist("KAR_migration");
private void createTableIfAbleOrWaitAdmin(final DbConfig configInput) throws MigrationException {
final DbConfig config = configInput.clone();
config.setDbName(null);
final String dbName = configInput.getDbName();
LOGGER.info("Verify existance of '{}'", dbName);
try (final DBAccess da = DBAccess.createInterface(config)) {
boolean exist = da.isDBExist(dbName);
if (!exist) {
LOGGER.info("'{}' Does not exist create a new one...", "KAR_migration");
// create the table:
List<String> sqlQuery;
LOGGER.warn("DB: '{}' DOES NOT EXIST ==> create one", dbName);
// create the local DB:
da.createDB(dbName);
}
exist = da.isDBExist(dbName);
while (!exist) {
LOGGER.error("DB: '{}' DOES NOT EXIST after trying to create one ", dbName);
LOGGER.error("Waiting administrator create a new one, we check after 30 seconds...");
try {
sqlQuery = DataFactory.createTable(Migration.class);
} catch (final Exception ex) {
ex.printStackTrace();
throw new MigrationException(
"Fail to create the local DB SQL model for migaration ==> wait administrator interventions");
}
sqlQuery = sqlQuery.stream()
.map(query -> query.replace(DataFactory.DEFAULT_DB_NAME_INVALID, config.getDbName()))
.collect(Collectors.toList());
LOGGER.info("Create Table with : {}", sqlQuery.get(0));
try {
daSQL.executeQuery(sqlQuery.get(0));
} catch (SQLException | IOException ex) {
ex.printStackTrace();
throw new MigrationException(
"Fail to create the local DB model for migaration ==> wait administrator interventions");
}
}
}
final Migration currentVersion = getCurrentVersion(da);
List<MigrationInterface> toApply = new ArrayList<>();
boolean needPlaceholder = false;
if (currentVersion == null) {
// This is a first migration
LOGGER.info("First installation of the system ==> Create the DB");
if (this.init == null) {
// No initialization class ==> manage a historical creation mode...
toApply = this.datas;
} else {
// Select Initialization class if it exist
toApply.add(this.init);
needPlaceholder = true;
}
} else {
if (!currentVersion.terminated) {
throw new MigrationException("An error occured in the last migration: '" + currentVersion.name
+ "' defect @" + currentVersion.stepId + "/" + currentVersion.count);
}
LOGGER.info("Upgrade the system Current version: {}", currentVersion.name);
boolean find = this.init != null && this.init.getName().equals(currentVersion.name);
if (find) {
toApply = this.datas;
} else {
LOGGER.info(" ===> Check what must be apply:");
for (final MigrationInterface elem : this.datas) {
LOGGER.info(" - {}", elem.getName());
if (!find) {
if (currentVersion.name.equals(elem.getName())) {
LOGGER.info(" == current version");
find = true;
}
continue;
}
LOGGER.info(" ++ add ");
toApply.add(elem);
}
}
}
final int id = 0;
final int count = toApply.size();
for (final MigrationInterface elem : toApply) {
migrateSingle(da, elem, id, count);
}
if (needPlaceholder) {
if (this.datas.size() == 0) {
// No placeholder needed, the model have no migration in the current version...
} else {
// we insert a placeholder to simulate the last migration is well done.
final String placeholderName = this.datas.get(this.datas.size() - 1).getName();
Migration migrationResult = new Migration();
migrationResult.id = 1000L;
migrationResult.name = placeholderName;
migrationResult.stepId = 0;
migrationResult.terminated = true;
migrationResult.count = 0;
migrationResult.log = "Place-holder for first initialization";
try {
migrationResult = da.insert(migrationResult);
} catch (final Exception e) {
Thread.sleep(30000);
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
exist = da.isDBExist(dbName);
}
} catch (final InternalServerErrorException e) {
e.printStackTrace();
throw new MigrationException("TODO ...");
} catch (final IOException e) {
e.printStackTrace();
throw new MigrationException("TODO ...");
}
}
/** Process the automatic migration of the system
* @param config SQL connection for the migration
* @throws IOException Error if access on the DB */
@SuppressFBWarnings({ "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING",
"SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE" })
public void migrateErrorThrow(final DbConfig config) throws MigrationException {
LOGGER.info("Execute migration ... [BEGIN]");
listAvailableMigration();
// STEP 1: Check the DB exist:
createTableIfAbleOrWaitAdmin(config);
LOGGER.info("DB '{}' exist.", config.getDbName());
try (final DBAccess da = DBAccess.createInterface(config)) {
// STEP 2: Check migration table exist:
LOGGER.info("Verify existance of migration table '{}'", "KAR_migration");
if (da instanceof final DBAccessSQL daSQL) {
final boolean exist = da.isTableExist("KAR_migration");
if (!exist) {
LOGGER.info("'{}' Does not exist create a new one...", "KAR_migration");
// create the table:
List<String> sqlQuery;
try {
sqlQuery = DataFactory.createTable(Migration.class);
} catch (final Exception ex) {
ex.printStackTrace();
throw new MigrationException(
"Fail to create the local DB SQL model for migaration ==> wait administrator interventions");
}
LOGGER.info("Create Table with : {}", sqlQuery.get(0));
try {
daSQL.executeQuery(sqlQuery.get(0));
} catch (SQLException | IOException ex) {
ex.printStackTrace();
throw new MigrationException(
"Fail to create the local DB model for migaration ==> wait administrator interventions");
}
}
}
final Migration currentVersion = getCurrentVersion(da);
List<MigrationInterface> toApply = new ArrayList<>();
boolean needPlaceholder = false;
if (currentVersion == null) {
// This is a first migration
LOGGER.info("First installation of the system ==> Create the DB");
if (this.init == null) {
// No initialization class ==> manage a historical creation mode...
toApply = this.datas;
} else {
// Select Initialization class if it exist
toApply.add(this.init);
needPlaceholder = true;
}
} else {
if (!currentVersion.terminated) {
throw new MigrationException("An error occured in the last migration: '" + currentVersion.name
+ "' defect @" + currentVersion.stepId + "/" + currentVersion.count);
}
LOGGER.info("Upgrade the system Current version: {}", currentVersion.name);
boolean find = this.init != null && this.init.getName().equals(currentVersion.name);
if (find) {
toApply = this.datas;
} else {
LOGGER.info(" ===> Check what must be apply:");
for (final MigrationInterface elem : this.datas) {
LOGGER.info(" - {}", elem.getName());
if (!find) {
if (currentVersion.name.equals(elem.getName())) {
LOGGER.info(" == current version");
find = true;
}
continue;
}
LOGGER.info(" ++ add ");
toApply.add(elem);
}
}
}
final int id = 0;
final int count = toApply.size();
for (final MigrationInterface elem : toApply) {
migrateSingle(da, elem, id, count);
}
if (needPlaceholder) {
if (this.datas.size() == 0) {
// No placeholder needed, the model have no migration in the current version...
} else {
// we insert a placeholder to simulate the last migration is well done.
final String placeholderName = this.datas.get(this.datas.size() - 1).getName();
Migration migrationResult = new Migration();
migrationResult.id = 1000L;
migrationResult.name = placeholderName;
migrationResult.stepId = 0;
migrationResult.terminated = true;
migrationResult.count = 0;
migrationResult.log = "Place-holder for first initialization";
try {
migrationResult = da.insert(migrationResult);
} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
LOGGER.info("Execute migration ... [ END ]");
} catch (final InternalServerErrorException e) {
e.printStackTrace();
throw new MigrationException("TODO ...");
} catch (final IOException e) {
e.printStackTrace();
throw new MigrationException("TODO ...");
}
LOGGER.info("Execute migration ... [ END ]");
}
public void migrateSingle(final DBAccess da, final MigrationInterface elem, final int id, final int count)

View File

@ -4,6 +4,7 @@ import java.io.IOException;
import java.util.List;
import org.kar.archidata.dataAccess.DBAccess;
import org.kar.archidata.db.DbConfig;
import org.kar.archidata.db.DbIoFactory;
import org.kar.archidata.exception.DataAccessException;
import org.kar.archidata.tools.ConfigBaseVariable;
@ -36,7 +37,7 @@ import test.kar.archidata.dataAccess.model.TypesTable;
public class ConfigureDb {
final static private Logger LOGGER = LoggerFactory.getLogger(ConfigureDb.class);
final static private String modeTestForced = "MY-SQL";
final static private String modeTestForced = null; // "MONGO";
public static DBAccess da = null;
public static void configure() throws IOException, InternalServerErrorException, DataAccessException {
@ -97,9 +98,9 @@ public class ConfigureDb {
ConfigBaseVariable.dbPort = "3906";
ConfigBaseVariable.dbUser = "root";
}
removeDB();
// Connect the dataBase...
da = DBAccess.createInterface();
removeDB();
}
public static void removeDB() {
@ -113,14 +114,35 @@ public class ConfigureDb {
if (modeTestForced != null) {
modeTest = modeTestForced;
}
if ("SQLITE-MEMORY".equalsIgnoreCase(modeTest)) {
// nothing to do ...
} else if ("SQLITE".equalsIgnoreCase(modeTest)) {
da.deleteDB(ConfigBaseVariable.bdDatabase);
} else if ("MY-SQL".equalsIgnoreCase(modeTest)) {
da.deleteDB(ConfigBaseVariable.bdDatabase);
} else if ("MONGO".equalsIgnoreCase(modeTest)) {
da.deleteDB(ConfigBaseVariable.bdDatabase);
DbConfig config = null;
try {
config = new DbConfig();
} catch (final DataAccessException e) {
e.printStackTrace();
LOGGER.error("Fail to clean the DB");
return;
}
config.setDbName(null);
LOGGER.info("Remove the DB and create a new one '{}'", config.getDbName());
try (final DBAccess daRoot = DBAccess.createInterface(config)) {
if ("SQLITE-MEMORY".equalsIgnoreCase(modeTest)) {
// nothing to do ...
} else if ("SQLITE".equalsIgnoreCase(modeTest)) {
daRoot.deleteDB(ConfigBaseVariable.bdDatabase);
} else if ("MY-SQL".equalsIgnoreCase(modeTest)) {
daRoot.deleteDB(ConfigBaseVariable.bdDatabase);
} else if ("MONGO".equalsIgnoreCase(modeTest)) {
daRoot.deleteDB(ConfigBaseVariable.bdDatabase);
}
daRoot.createDB(ConfigBaseVariable.bdDatabase);
} catch (final InternalServerErrorException e) {
e.printStackTrace();
LOGGER.error("Fail to clean the DB");
return;
} catch (final IOException e) {
e.printStackTrace();
LOGGER.error("Fail to clean the DB");
return;
}
}

View File

@ -95,7 +95,7 @@ public class TestRawQuery {
{
final String query = """
SELECT DISTINCT intData
SELECT id, intData
FROM TypesTable
WHERE `intData` = ?
ORDER BY id DESC
@ -105,7 +105,7 @@ public class TestRawQuery {
final List<TypesTable> retrieve = daSQL.query(TypesTable.class, query, parameters);
Assertions.assertNotNull(retrieve);
Assertions.assertEquals(1, retrieve.size());
Assertions.assertEquals(3, retrieve.size());
Assertions.assertEquals(99, retrieve.get(0).intData);
}
} else {

View File

@ -3,9 +3,11 @@ package test.kar.archidata.dataAccess.model;
import org.kar.archidata.model.GenericData;
import dev.morphia.annotations.Entity;
import jakarta.persistence.Column;
@Entity
public class SimpleTable extends GenericData {
@Column(length = 0)
public String data;
}

View File

@ -3,8 +3,10 @@ package test.kar.archidata.dataAccess.model;
import org.kar.archidata.model.GenericDataSoftDelete;
import dev.morphia.annotations.Entity;
import jakarta.persistence.Column;
@Entity
public class SimpleTableSoftDelete extends GenericDataSoftDelete {
@Column(length = 0)
public String data;
}