[FEAT] correct the SQL DB creation but acces does not work anymore

This commit is contained in:
Edouard DUPIN 2024-12-22 10:28:56 +01:00
parent df12cd7413
commit e8c6771b79
8 changed files with 55 additions and 21 deletions

View File

@ -72,7 +72,7 @@ public abstract class DBAccess implements Closeable {
throw new InternalServerErrorException("Can Not manage the DB-access");
}
public void deleteDB(final String name) {
public boolean deleteDB(final String name) {
throw new InternalServerErrorException("Can Not manage the DB-access");
}

View File

@ -103,9 +103,10 @@ public class DBAccessMorphia extends DBAccess {
}
@Override
public void deleteDB(final String name) {
public boolean deleteDB(final String name) {
final MongoDatabase database = this.db.getClient().getDatabase(name);
database.drop();
return true;
}
@Override

View File

@ -131,6 +131,22 @@ public class DBAccessSQL extends DBAccess {
}
}
@Override
public boolean deleteDB(final String name) {
if ("sqlite".equals(ConfigBaseVariable.getDBType())) {
// no base manage in sqLite ...
// TODO: check if the file exist or not ...
return true;
}
try {
return 1 == executeSimpleQuery("DROP DATABASE `" + name + "`;", new DBInterfaceRoot(true));
} catch (final SQLException | IOException ex) {
//ex.printStackTrace();
LOGGER.error("Can not drop the DB!!! {}", ex.getMessage());
}
return false;
}
@Override
public boolean isTableExist(final String name, final QueryOption... option) throws InternalServerErrorException {
final QueryOptions options = new QueryOptions(option);
@ -150,7 +166,8 @@ public class DBAccessSQL extends DBAccess {
return count == 1;
} else {
// TODO : Maybe connect with a temporary not specified connection interface to a db ...
final PreparedStatement ps = this.db.getConnection().prepareStatement("show tables");
final PreparedStatement ps = this.db.getConnection()
.prepareStatement("SHOW TABLES IN `" + this.db.getCongig().getDbName() + "`");
final ResultSet rs = ps.executeQuery();
// LOGGER.info("List all tables: equals? '{}'", name);
while (rs.next()) {
@ -785,7 +802,7 @@ public class DBAccessSQL extends DBAccess {
final List<Field> asyncFieldUpdate = new ArrayList<>();
Long uniqueSQLID = null;
UUID uniqueSQLUUID = null;
final String tableName = AnnotationTools.getTableName(clazz, options);
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
Field primaryKeyField = null;
boolean generateUUID = false;
// real add in the BDD:
@ -1014,7 +1031,8 @@ public class DBAccessSQL extends DBAccess {
final List<LazyGetter> asyncActions = new ArrayList<>();
// real add in the BDD:
try {
final String tableName = AnnotationTools.getTableName(clazz, options);
final String tableName = this.db.getCongig().getDbName() + "."
+ AnnotationTools.getTableName(clazz, options);
// boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
final StringBuilder query = new StringBuilder();
query.append("UPDATE `");
@ -1189,6 +1207,7 @@ 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;
@ -1235,7 +1254,8 @@ public class DBAccessSQL extends DBAccess {
final CountInOut count = new CountInOut();
final StringBuilder querySelect = new StringBuilder();
StringBuilder query = new StringBuilder();
final String tableName = AnnotationTools.getTableName(clazz, options);
final String tableName = this.db.getCongig().getDbName() + "."
+ AnnotationTools.getTableName(clazz, options);
querySelect.append("SELECT ");
query.append(" FROM `");
query.append(tableName);
@ -1341,7 +1361,8 @@ public class DBAccessSQL extends DBAccess {
// real add in the BDD:
try {
final StringBuilder query = new StringBuilder();
final String tableName = AnnotationTools.getTableName(clazz, options);
final String tableName = this.db.getCongig().getDbName() + "."
+ AnnotationTools.getTableName(clazz, options);
query.append("SELECT COUNT(*) AS count FROM `");
query.append(tableName);
query.append("` ");
@ -1379,7 +1400,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 = AnnotationTools.getTableName(clazz, options);
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
// find the deleted field
final StringBuilder query = new StringBuilder();
@ -1398,7 +1419,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 = AnnotationTools.getTableName(clazz, options);
final String tableName = this.db.getCongig().getDbName() + "." + 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
@ -1424,7 +1445,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 = AnnotationTools.getTableName(clazz, options);
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
if (deletedFieldName == null) {
throw new DataAccessException("The class " + clazz.getCanonicalName() + " has no deleted field");
@ -1452,7 +1473,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 = AnnotationTools.getTableName(clazz, options);
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final StringBuilder query = new StringBuilder();
query.append("DROP TABLE IF EXISTS `");
query.append(tableName);
@ -1480,7 +1501,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 = AnnotationTools.getTableName(clazz, options);
final String tableName = this.db.getCongig().getDbName() + "." + AnnotationTools.getTableName(clazz, options);
final StringBuilder query = new StringBuilder();
query.append("DELETE FROM `");
query.append(tableName);

View File

@ -26,10 +26,13 @@ import com.fasterxml.jackson.annotation.JsonValue;
import jakarta.persistence.GenerationType;
public class DataFactory {
static final Logger LOGGER = LoggerFactory.getLogger(DataFactory.class);
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 {
if ("sql".equals(ConfigBaseVariable.getDBType())) {
final String typelocal = ConfigBaseVariable.getDBType();
if ("mysql".equals(typelocal)) {
if (type == UUID.class) {
return "binary(16)";
}
@ -83,7 +86,7 @@ public class DataFactory {
out.append(")");
return out.toString();
}
} else if ("sqlite".equals(ConfigBaseVariable.getDBType())) {
} else if ("sqlite".equals(typelocal)) {
if (type == UUID.class) {
return "BINARY(16)";
}
@ -139,7 +142,7 @@ public class DataFactory {
out.append(" ) )");
return out.toString();
}
} else if ("mongo".equals(ConfigBaseVariable.getDBType())) {
} else if ("mongo".equals(typelocal)) {
// no importance for mango ...
return "text";
}
@ -328,7 +331,7 @@ public class DataFactory {
}
public static List<String> createTable(final Class<?> clazz, final QueryOptions options) throws Exception {
final String tableName = AnnotationTools.getTableName(clazz, options);
final String tableName = DEFAULT_DB_NAME_INVALID + "." + AnnotationTools.getTableName(clazz, options);
boolean createDrop = false;
if (options != null) {

View File

@ -116,11 +116,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) {
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";

View File

@ -73,6 +73,10 @@ public abstract class DbIo implements Closeable {
protected abstract void openImplement() throws IOException;
public boolean compatible(final DbConfig config) {
return config.equals(config);
return this.config.equals(config);
}
public DbConfig getCongig() {
return this.config;
}
}

View File

@ -4,6 +4,7 @@ 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;
@ -182,6 +183,10 @@ public class MigrationEngine {
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));

View File

@ -36,7 +36,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 = null;//"MONGO";
final static private String modeTestForced = "MY-SQL";
public static DBAccess da = null;
public static void configure() throws IOException, InternalServerErrorException, DataAccessException {
@ -91,7 +91,6 @@ public class ConfigureDb {
} else if ("MONGO".equalsIgnoreCase(modeTest)) {
ConfigBaseVariable.dbType = "mongo";
ConfigBaseVariable.bdDatabase = "test_db";
ConfigBaseVariable.bdDatabase = "test_mongo_db";
} else {
// User local modification ...
ConfigBaseVariable.bdDatabase = "test_db";