[DEV] implement base of create and check db exist

This commit is contained in:
Edouard DUPIN 2023-05-06 23:58:21 +02:00
parent 1501b7a21e
commit 7c37b65842
5 changed files with 164 additions and 36 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry including="**/*.java" kind="src" output="out/maven/classes" path="src">
<classpathentry kind="src" output="out/maven/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
@ -13,7 +13,7 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
@ -23,22 +23,5 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="out/maven/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="out/maven/test-classes" path="out/maven/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="out/maven/classes"/>
</classpath>

View File

@ -30,13 +30,14 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.ws.rs.InternalServerErrorException;
import org.kar.archidata.annotation.SQLCreateTime;
import org.kar.archidata.annotation.SQLDefault;
import org.kar.archidata.annotation.SQLDeleted;
public class SqlWrapper {
static final Logger logger = LoggerFactory.getLogger(SqlWrapper.class);
static final Logger LOGGER = LoggerFactory.getLogger(SqlWrapper.class);
public static class ExceptionDBInterface extends Exception {
private static final long serialVersionUID = 1L;
@ -50,6 +51,112 @@ public class SqlWrapper {
public SqlWrapper() {
}
public static boolean isDBExist(String name) throws InternalServerErrorException {
if (ConfigBaseVariable.getDBType().equals("sqlite")) {
// no base manage in sqLite ...
// TODO: check if the file exist or not ...
return true;
}
DBEntry entry;
try {
entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, true);
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
LOGGER.error("Can not check if the DB exist!!! {}", ex.getMessage());
return false;
}
try {
// TODO : Maybe connect with a temporary not specified connection interface to a db ...
PreparedStatement ps = entry.connection.prepareStatement("show databases");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String data = rs.getString(1);
if (name.equals(data)) {
return true;
}
}
//int count = ret.getInt("total");
return false;
} catch (SQLException ex) {
LOGGER.error("Can not check if the DB exist SQL-error !!! {}", ex.getMessage());
} finally {
try {
entry.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
entry = null;
}
throw new InternalServerErrorException("Can Not manage the DB-access");
}
public static boolean createDB(String name) throws InternalServerErrorException {
if (ConfigBaseVariable.getDBType().equals("sqlite")) {
// no base manage in sqLite ...
// TODO: check if the file exist or not ...
return true;
}
DBEntry entry;
try {
entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, true);
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
LOGGER.error("Can not Create the DB {}", ex.getMessage());
return false;
}
try {
PreparedStatement ps = entry.connection.prepareStatement("CREATE DATABASE ?");
ps.setString(1, name);
int ret = ps.executeUpdate();
return ret == 1;
} catch (SQLException ex) {
LOGGER.error("Can not Create the DB SQL-error !!! {}", ex.getMessage());
} finally {
try {
entry.close();
} catch (IOException e) {
e.printStackTrace();
}
entry = null;
}
throw new InternalServerErrorException("Can Not manage the DB-access");
}
public static boolean isTableExist(String name) throws InternalServerErrorException {
try {
String request = "";
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
request = """
SELECT count(*) AS total
FROM information_schema.tables
WHERE table_name = ?;
LIMIT 1;
""";
} else {
request = """
SELECT COUNT(*) AS total
FROM sqlite_master
WHERE type = 'table'
AND name = ?;
""";
}
// PreparedStatement ps = entry.connection.prepareStatement("show tables");
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
PreparedStatement ps = entry.connection.prepareStatement(request);
ps.setString(1, name);
ResultSet ret = ps.executeQuery();
int count = ret.getInt("total");
return count == 1;
} catch (SQLException ex) {
LOGGER.error("Can not check if the table exist SQL-error !!! {}", ex.getMessage());
} catch (IOException ex) {
LOGGER.error("Can not check if the table exist!!! {}", ex.getMessage());
}
throw new InternalServerErrorException("Can Not manage the DB-access");
}
public static String convertTypeInSQL(Class<?> type) throws Exception {
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
@ -398,7 +505,7 @@ public class SqlWrapper {
throw new SQLException("Creating node failed, no ID obtained (1).");
}
} catch (Exception ex) {
logger.error("Can not get the UID key inserted ... ");
LOGGER.error("Can not get the UID key inserted ... ");
ex.printStackTrace();
throw new SQLException("Creating node failed, no ID obtained (2).");
}
@ -408,7 +515,7 @@ public class SqlWrapper {
} else if (primaryKeyField.getType() == long.class) {
primaryKeyField.setLong(data, uniqueSQLID);
} else {
logger.error("Can not manage the primary filed !!!");
LOGGER.error("Can not manage the primary filed !!!");
}
}
//ps.execute();
@ -823,7 +930,7 @@ public class SqlWrapper {
}
public static <T> List<T> gets(Class<T> clazz, boolean full) throws Exception {
logger.debug("request get {} start @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
LOGGER.debug("request get {} start @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
List<T> out = new ArrayList<>();
// real add in the BDD:
@ -917,15 +1024,15 @@ public class SqlWrapper {
query.append(tableName);
query.append(".deleted = false ");
firstField = true;
logger.info("generate the querry: '{}'", query.toString());
logger.info("request get {} prepare @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
LOGGER.info("generate the querry: '{}'", query.toString());
LOGGER.info("request get {} prepare @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
// prepare the request:
PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
logger.info("request get {} query @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
LOGGER.info("request get {} query @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
// execute the request
ResultSet rs = ps.executeQuery();
logger.info("request get {} transform @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
LOGGER.info("request get {} transform @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
while (rs.next()) {
indexAutoClasify = 0;
@ -956,7 +1063,7 @@ public class SqlWrapper {
out.add((T)data);
}
logger.info("request get {} ready @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
LOGGER.info("request get {} ready @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
} catch (SQLException ex) {
ex.printStackTrace();
@ -994,7 +1101,7 @@ public class SqlWrapper {
throw new SQLException("Creating user failed, no ID obtained (1).");
}
} catch (Exception ex) {
logger.debug("Can not get the UID key inserted ... ");
LOGGER.debug("Can not get the UID key inserted ... ");
ex.printStackTrace();
throw new SQLException("Creating user failed, no ID obtained (2).");
}
@ -1138,7 +1245,7 @@ public class SqlWrapper {
out.append(tableName);
out.append("` (");
boolean firstField = true;
logger.debug("===> TABLE `{}`", tableName);
LOGGER.debug("===> TABLE `{}`", tableName);
String primaryKeyValue = null;
for (Field elem : clazz.getFields()) {

View File

@ -1,6 +1,11 @@
package org.kar.archidata.db;
import org.kar.archidata.SqlWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DBConfig {
static final Logger LOGGER = LoggerFactory.getLogger(SqlWrapper.class);
private final String type;
private final String hostname;
private final int port;
@ -67,11 +72,20 @@ public class DBConfig {
}
public String getUrl() {
return getUrl(false);
}
public String getUrl(boolean isRoot) {
if (type.equals("sqlite")) {
if (isRoot == true) {
LOGGER.error("Can not manage root connection on SQLite...");
}
if (this.hostname.equals("memory")) {
return "jdbc:sqlite::memory:";
}
return "jdbc:sqlite:" + this.hostname + ".db";
}
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 + "?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC";
}

View File

@ -15,12 +15,19 @@ public class DBEntry implements Closeable {
public Connection connection;
private static List<DBEntry> stored = new ArrayList<>();
private DBEntry(DBConfig config) throws IOException {
private DBEntry(DBConfig config, boolean root) throws IOException {
this.config = config;
connect();
if (root) {
connectRoot();
} else {
connect();
}
}
public static DBEntry createInterface(DBConfig config) throws IOException {
return createInterface(config, false);
}
public static DBEntry createInterface(DBConfig config, boolean root) throws IOException {
if (config.getKeepConnected()) {
for (DBEntry elem : stored) {
if (elem == null) {
@ -30,14 +37,23 @@ public class DBEntry implements Closeable {
return elem;
}
}
DBEntry tmp = new DBEntry(config);
DBEntry tmp = new DBEntry(config, root);
stored.add(tmp);
return tmp;
} else {
return new DBEntry(config);
return new DBEntry(config, root);
}
}
public void connectRoot() throws IOException {
try {
connection = DriverManager.getConnection(config.getUrl(true), config.getLogin(), config.getPassword());
} catch (SQLException ex) {
throw new IOException("Connection db fail: " + ex.getMessage());
}
}
public void connect() throws IOException {
try {
connection = DriverManager.getConnection(config.getUrl(), config.getLogin(), config.getPassword());

View File

@ -35,6 +35,14 @@ public class MigrationEngine {
* @return String represent the last migration. If null then no migration has been done.
*/
public String getCurrentVersion() {
// TODO: check if the DB exist :
if (SqlWrapper.isTableExist("migration")) {
}
// check if migration table exist:
// get the current migration
try {
List<MigrationModel> data = SqlWrapper.gets(MigrationModel.class, false);
if (data == null) {