[DEV] configure mode to access on multiple database

This commit is contained in:
Edouard DUPIN 2024-01-28 23:49:12 +01:00
parent 04114aa0cf
commit aef4cdabc3
15 changed files with 376 additions and 324 deletions

View File

@ -5,6 +5,7 @@ import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import org.kar.archidata.dataAccess.DataAccess; import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.options.DBInterfaceOption;
import org.kar.archidata.db.DBEntry; import org.kar.archidata.db.DBEntry;
import org.kar.archidata.model.User; import org.kar.archidata.model.User;
@ -26,7 +27,7 @@ public class UserDB {
} }
private static void createUsersInfoFromOAuth(final long userId, final String login) throws IOException { private static void createUsersInfoFromOAuth(final long userId, final String login) throws IOException {
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final String query = "INSERT INTO `user` (`id`, `login`, `lastConnection`, `admin`, `blocked`, `removed`) VALUE (?,?,now(3),'0','0','0')"; final String query = "INSERT INTO `user` (`id`, `login`, `lastConnection`, `admin`, `blocked`, `removed`) VALUE (?,?,now(3),'0','0','0')";
try { try {
final PreparedStatement ps = entry.connection.prepareStatement(query); final PreparedStatement ps = entry.connection.prepareStatement(query);

View File

@ -15,7 +15,6 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.kar.archidata.GlobalConfiguration;
import org.kar.archidata.annotation.AnnotationTools; import org.kar.archidata.annotation.AnnotationTools;
import org.kar.archidata.annotation.CreationTimestamp; import org.kar.archidata.annotation.CreationTimestamp;
import org.kar.archidata.annotation.DataDefault; import org.kar.archidata.annotation.DataDefault;
@ -26,6 +25,8 @@ import org.kar.archidata.dataAccess.addOn.AddOnManyToOne;
import org.kar.archidata.dataAccess.addOn.AddOnSQLTableExternalForeinKeyAsList; import org.kar.archidata.dataAccess.addOn.AddOnSQLTableExternalForeinKeyAsList;
import org.kar.archidata.dataAccess.options.CheckFunction; import org.kar.archidata.dataAccess.options.CheckFunction;
import org.kar.archidata.dataAccess.options.Condition; import org.kar.archidata.dataAccess.options.Condition;
import org.kar.archidata.dataAccess.options.DBInterfaceOption;
import org.kar.archidata.dataAccess.options.DBInterfaceRoot;
import org.kar.archidata.dataAccess.options.FilterValue; import org.kar.archidata.dataAccess.options.FilterValue;
import org.kar.archidata.dataAccess.options.TransmitKey; import org.kar.archidata.dataAccess.options.TransmitKey;
import org.kar.archidata.db.DBEntry; import org.kar.archidata.db.DBEntry;
@ -73,7 +74,8 @@ public class DataAccess {
} }
public static boolean isDBExist(final String name) throws InternalServerErrorException { public static boolean isDBExist(final String name, final QueryOption... option) throws InternalServerErrorException {
final QueryOptions options = new QueryOptions(option);
if ("sqlite".equals(ConfigBaseVariable.getDBType())) { if ("sqlite".equals(ConfigBaseVariable.getDBType())) {
// no base manage in sqLite ... // no base manage in sqLite ...
// TODO: check if the file exist or not ... // TODO: check if the file exist or not ...
@ -81,7 +83,7 @@ public class DataAccess {
} }
DBEntry entry; DBEntry entry;
try { try {
entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, true); entry = DBInterfaceOption.getAutoEntry(options);
} catch (final IOException ex) { } catch (final IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
LOGGER.error("Can not check if the DB exist!!! {}", ex.getMessage()); LOGGER.error("Can not check if the DB exist!!! {}", ex.getMessage());
@ -124,7 +126,7 @@ public class DataAccess {
return true; return true;
} }
try { try {
return 1 == DataAccess.executeSimpleQuerry("CREATE DATABASE `" + name + "`;", true); return 1 == DataAccess.executeSimpleQuerry("CREATE DATABASE `" + name + "`;", new DBInterfaceRoot(true));
} catch (final SQLException | IOException ex) { } catch (final SQLException | IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
LOGGER.error("Can not check if the DB exist!!! {}", ex.getMessage()); LOGGER.error("Can not check if the DB exist!!! {}", ex.getMessage());
@ -132,7 +134,8 @@ public class DataAccess {
} }
} }
public static boolean isTableExist(final String name) throws InternalServerErrorException { public static boolean isTableExist(final String name, final QueryOption... option) throws InternalServerErrorException {
final QueryOptions options = new QueryOptions(option);
try { try {
String request = ""; String request = "";
if ("sqlite".equals(ConfigBaseVariable.getDBType())) { if ("sqlite".equals(ConfigBaseVariable.getDBType())) {
@ -143,14 +146,14 @@ public class DataAccess {
AND name = ?; AND name = ?;
"""; """;
// PreparedStatement ps = entry.connection.prepareStatement("show tables"); // PreparedStatement ps = entry.connection.prepareStatement("show tables");
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final PreparedStatement ps = entry.connection.prepareStatement(request); final PreparedStatement ps = entry.connection.prepareStatement(request);
ps.setString(1, name); ps.setString(1, name);
final ResultSet ret = ps.executeQuery(); final ResultSet ret = ps.executeQuery();
final int count = ret.getInt("total"); final int count = ret.getInt("total");
return count == 1; return count == 1;
} else { } else {
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
// TODO : Maybe connect with a temporary not specified connection interface to a db ... // TODO : Maybe connect with a temporary not specified connection interface to a db ...
final PreparedStatement ps = entry.connection.prepareStatement("show tables"); final PreparedStatement ps = entry.connection.prepareStatement("show tables");
final ResultSet rs = ps.executeQuery(); final ResultSet rs = ps.executeQuery();
@ -480,7 +483,7 @@ public class DataAccess {
check.getChecker().check("", data, AnnotationTools.getFieldsNames(clazz)); check.getChecker().check("", data, AnnotationTools.getFieldsNames(clazz));
} }
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final List<Field> asyncFieldUpdate = new ArrayList<>(); final List<Field> asyncFieldUpdate = new ArrayList<>();
Long uniqueSQLID = null; Long uniqueSQLID = null;
final String tableName = AnnotationTools.getTableName(clazz, options); final String tableName = AnnotationTools.getTableName(clazz, options);
@ -622,7 +625,6 @@ public class DataAccess {
ex.printStackTrace(); ex.printStackTrace();
} finally { } finally {
entry.close(); entry.close();
entry = null;
} }
final List<LazyGetter> asyncActions = new ArrayList<>(); final List<LazyGetter> asyncActions = new ArrayList<>();
for (final Field field : asyncFieldUpdate) { for (final Field field : asyncFieldUpdate) {
@ -734,9 +736,9 @@ public class DataAccess {
} }
} }
final List<LazyGetter> asyncActions = new ArrayList<>(); final List<LazyGetter> asyncActions = new ArrayList<>();
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
// real add in the BDD: // real add in the BDD:
try { try (entry) {
final String tableName = AnnotationTools.getTableName(clazz, options); final String tableName = AnnotationTools.getTableName(clazz, options);
// boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0; // boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
final StringBuilder query = new StringBuilder(); final StringBuilder query = new StringBuilder();
@ -830,9 +832,6 @@ public class DataAccess {
} }
} catch (final SQLException ex) { } catch (final SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
} finally {
entry.close();
entry = null;
} }
for (final LazyGetter action : asyncActions) { for (final LazyGetter action : asyncActions) {
action.doRequest(); action.doRequest();
@ -885,26 +884,22 @@ public class DataAccess {
} }
} }
public static int executeSimpleQuerry(final String query, final boolean root) throws SQLException, IOException { public static int executeSimpleQuerry(final String query, final QueryOption... option) throws SQLException, IOException {
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, root); final QueryOptions options = new QueryOptions(option);
// .... TODO final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, root);
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final Statement stmt = entry.connection.createStatement(); final Statement stmt = entry.connection.createStatement();
return stmt.executeUpdate(query); return stmt.executeUpdate(query);
} }
public static int executeSimpleQuerry(final String query) throws SQLException, IOException { public static boolean executeQuerry(final String query, final QueryOption... option) throws SQLException, IOException {
return executeSimpleQuerry(query, false); final QueryOptions options = new QueryOptions(option);
} // .... TODO final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, root);
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
public static boolean executeQuerry(final String query, final boolean root) throws SQLException, IOException {
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, root);
final Statement stmt = entry.connection.createStatement(); final Statement stmt = entry.connection.createStatement();
return stmt.execute(query); return stmt.execute(query);
} }
public static boolean executeQuerry(final String query) throws SQLException, IOException {
return executeQuerry(query, false);
}
public static <T> T getWhere(final Class<T> clazz, final QueryOption... option) throws Exception { public static <T> T getWhere(final Class<T> clazz, final QueryOption... option) throws Exception {
final QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
options.add(new Limit(1)); options.add(new Limit(1));
@ -966,7 +961,7 @@ public class DataAccess {
} }
final List<LazyGetter> lazyCall = new ArrayList<>(); final List<LazyGetter> lazyCall = new ArrayList<>();
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final List<T> outs = new ArrayList<>(); final List<T> outs = new ArrayList<>();
// real add in the BDD: // real add in the BDD:
try { try {
@ -1019,7 +1014,6 @@ public class DataAccess {
ex.printStackTrace(); ex.printStackTrace();
} finally { } finally {
entry.close(); entry.close();
entry = null;
} }
return outs; return outs;
} }
@ -1070,7 +1064,7 @@ public class DataAccess {
condition = new Condition(); condition = new Condition();
} }
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); DBEntry entry = DBInterfaceOption.getAutoEntry(options);
long count = 0; long count = 0;
// real add in the BDD: // real add in the BDD:
try { try {
@ -1172,8 +1166,7 @@ public class DataAccess {
final String tableName = AnnotationTools.getTableName(clazz, options); final String tableName = AnnotationTools.getTableName(clazz, options);
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
// find the deleted field // find the deleted field
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
final StringBuilder query = new StringBuilder(); final StringBuilder query = new StringBuilder();
query.append("DELETE FROM `"); query.append("DELETE FROM `");
query.append(tableName); query.append(tableName);
@ -1187,7 +1180,6 @@ public class DataAccess {
return ps.executeUpdate(); return ps.executeUpdate();
} finally { } finally {
entry.close(); entry.close();
entry = null;
} }
} }
@ -1207,8 +1199,7 @@ public class DataAccess {
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
/* String updateFieldName = null; if ("sqlite".equalsIgnoreCase(ConfigBaseVariable.getDBType())) { updateFieldName = AnnotationTools.getUpdatedFieldName(clazz); } */ /* String updateFieldName = null; if ("sqlite".equalsIgnoreCase(ConfigBaseVariable.getDBType())) { updateFieldName = AnnotationTools.getUpdatedFieldName(clazz); } */
// find the deleted field // find the deleted field
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
final StringBuilder query = new StringBuilder(); final StringBuilder query = new StringBuilder();
query.append("UPDATE `"); query.append("UPDATE `");
query.append(tableName); query.append(tableName);
@ -1226,7 +1217,6 @@ public class DataAccess {
return ps.executeUpdate(); return ps.executeUpdate();
} finally { } finally {
entry.close(); entry.close();
entry = null;
} }
} }
@ -1251,7 +1241,7 @@ public class DataAccess {
if (deletedFieldName == null) { if (deletedFieldName == null) {
throw new DataAccessException("The class " + clazz.getCanonicalName() + " has no deleted field"); throw new DataAccessException("The class " + clazz.getCanonicalName() + " has no deleted field");
} }
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final StringBuilder query = new StringBuilder(); final StringBuilder query = new StringBuilder();
query.append("UPDATE `"); query.append("UPDATE `");
query.append(tableName); query.append(tableName);
@ -1268,14 +1258,13 @@ public class DataAccess {
return ps.executeUpdate(); return ps.executeUpdate();
} finally { } finally {
entry.close(); entry.close();
entry = null;
} }
} }
public static void drop(final Class<?> clazz, final QueryOption... option) throws Exception { public static void drop(final Class<?> clazz, final QueryOption... option) throws Exception {
final QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
final String tableName = AnnotationTools.getTableName(clazz, options); final String tableName = AnnotationTools.getTableName(clazz, options);
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final StringBuilder query = new StringBuilder(); final StringBuilder query = new StringBuilder();
query.append("DROP TABLE IF EXISTS `"); query.append("DROP TABLE IF EXISTS `");
query.append(tableName); query.append(tableName);
@ -1301,14 +1290,13 @@ public class DataAccess {
} }
} finally { } finally {
entry.close(); entry.close();
entry = null;
} }
} }
public static void cleanAll(final Class<?> clazz, final QueryOption... option) throws Exception { public static void cleanAll(final Class<?> clazz, final QueryOption... option) throws Exception {
final QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
final String tableName = AnnotationTools.getTableName(clazz, options); final String tableName = AnnotationTools.getTableName(clazz, options);
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final StringBuilder query = new StringBuilder(); final StringBuilder query = new StringBuilder();
query.append("DELETE FROM `"); query.append("DELETE FROM `");
query.append(tableName); query.append(tableName);

View File

@ -64,5 +64,4 @@ public class QueryOptions {
} }
return false; return false;
} }
} }

View File

@ -0,0 +1,48 @@
package org.kar.archidata.dataAccess.options;
import java.io.IOException;
import org.kar.archidata.GlobalConfiguration;
import org.kar.archidata.dataAccess.QueryOption;
import org.kar.archidata.dataAccess.QueryOptions;
import org.kar.archidata.db.DBConfig;
import org.kar.archidata.db.DBEntry;
public class DBInterfaceOption extends QueryOption {
private DBEntry entry = null;
private final DBConfig config;
private final boolean root;
public DBInterfaceOption(final DBConfig config) {
this.config = config;
this.root = false;
}
public DBInterfaceOption(final DBConfig config, boolean root) {
this.config = config;
this.root = root;
}
public DBEntry getEntry(QueryOptions options) throws IOException {
if (this.entry == null) {
final DBInterfaceRoot isRoot = options.get(DBInterfaceRoot.class);
this.entry = DBEntry.createInterface(this.config, isRoot != null && isRoot.getRoot());
}
return this.entry;
}
public boolean getRoot() {
return this.root;
}
public static DBEntry getAutoEntry(QueryOptions options) throws IOException {
final DBInterfaceOption dbOption = options.get(DBInterfaceOption.class);
if (dbOption == null) {
final DBInterfaceRoot isRoot = options.get(DBInterfaceRoot.class);
return DBEntry.createInterface(GlobalConfiguration.dbConfig, isRoot != null && isRoot.getRoot());
} else {
return dbOption.getEntry(options);
}
}
}

View File

@ -0,0 +1,16 @@
package org.kar.archidata.dataAccess.options;
import org.kar.archidata.dataAccess.QueryOption;
public class DBInterfaceRoot extends QueryOption {
private final boolean root;
public DBInterfaceRoot(boolean root) {
this.root = root;
}
public boolean getRoot() {
return this.root;
}
}

View File

@ -53,7 +53,7 @@ public class TestJson {
final List<String> sqlCommand = DataFactory.createTable(SerializeAsJson.class); final List<String> sqlCommand = DataFactory.createTable(SerializeAsJson.class);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem, false); DataAccess.executeSimpleQuerry(elem);
} }
} }

View File

@ -53,7 +53,7 @@ public class TestListJson {
final List<String> sqlCommand = DataFactory.createTable(SerializeListAsJson.class); final List<String> sqlCommand = DataFactory.createTable(SerializeListAsJson.class);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem, false); DataAccess.executeSimpleQuerry(elem);
} }
} }

View File

@ -57,7 +57,7 @@ public class TestManyToMany {
sqlCommand.addAll(sqlCommand2); sqlCommand.addAll(sqlCommand2);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem, false); DataAccess.executeSimpleQuerry(elem);
} }
} }

View File

@ -56,7 +56,7 @@ public class TestManyToOne {
sqlCommand.addAll(sqlCommand2); sqlCommand.addAll(sqlCommand2);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem, false); DataAccess.executeSimpleQuerry(elem);
} }
} }

View File

@ -51,7 +51,7 @@ public class TestOneToMany {
final List<String> sqlCommand = DataFactory.createTable(TypesTable.class); final List<String> sqlCommand = DataFactory.createTable(TypesTable.class);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem, false); DataAccess.executeSimpleQuerry(elem);
} }
} }

View File

@ -65,7 +65,7 @@ public class TestSimpleTable {
final List<String> sqlCommand = DataFactory.createTable(SimpleTable.class); final List<String> sqlCommand = DataFactory.createTable(SimpleTable.class);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem, false); DataAccess.executeSimpleQuerry(elem);
} }
final SimpleTable test = new SimpleTable(); final SimpleTable test = new SimpleTable();
test.data = TestSimpleTable.DATA_INJECTED; test.data = TestSimpleTable.DATA_INJECTED;

View File

@ -65,7 +65,7 @@ public class TestSimpleTableSoftDelete {
final List<String> sqlCommand = DataFactory.createTable(SimpleTableSoftDelete.class); final List<String> sqlCommand = DataFactory.createTable(SimpleTableSoftDelete.class);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem, false); DataAccess.executeSimpleQuerry(elem);
} }
final SimpleTableSoftDelete test = new SimpleTableSoftDelete(); final SimpleTableSoftDelete test = new SimpleTableSoftDelete();
test.data = TestSimpleTableSoftDelete.DATA_INJECTED; test.data = TestSimpleTableSoftDelete.DATA_INJECTED;

View File

@ -53,7 +53,7 @@ public class TestTypeEnum1 {
final List<String> sqlCommand = DataFactory.createTable(TypesEnum1.class); final List<String> sqlCommand = DataFactory.createTable(TypesEnum1.class);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem, false); DataAccess.executeSimpleQuerry(elem);
} }
} }

View File

@ -53,7 +53,7 @@ public class TestTypeEnum2 {
final List<String> sqlCommand = DataFactory.createTable(TypesEnum2.class); final List<String> sqlCommand = DataFactory.createTable(TypesEnum2.class);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem, false); DataAccess.executeSimpleQuerry(elem);
} }
} }

View File

@ -57,7 +57,7 @@ public class TestTypes {
final List<String> sqlCommand = DataFactory.createTable(TypesTable.class); final List<String> sqlCommand = DataFactory.createTable(TypesTable.class);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem, false); DataAccess.executeSimpleQuerry(elem);
} }
} }