[API,FEAT] continue integration of mongo (refacto of the API
This commit is contained in:
parent
c144cd378e
commit
c682cc7803
50
pom.xml
50
pom.xml
@ -188,33 +188,29 @@
|
||||
<version>4.8.6</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<!-- Morphia -->
|
||||
<dependency>
|
||||
<groupId>dev.morphia.morphia</groupId>
|
||||
<artifactId>morphia-core</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- MongoDB Java Driver -->
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-sync</artifactId>
|
||||
<version>4.3.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Bean Validation (JSR 303 / 380) -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>7.0.0.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>2.0.1.Final</version>
|
||||
</dependency>
|
||||
<!-- Morphia -->
|
||||
<dependency>
|
||||
<groupId>dev.morphia.morphia</groupId>
|
||||
<artifactId>morphia-core</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
<!-- MongoDB Java Driver -->
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-sync</artifactId>
|
||||
<version>4.3.0</version>
|
||||
</dependency>
|
||||
<!-- Bean Validation (JSR 303 / 380) -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>7.0.0.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>2.0.1.Final</version>
|
||||
</dependency>
|
||||
<!--
|
||||
************************************************************
|
||||
** TEST dependency **
|
||||
|
@ -1,15 +1,28 @@
|
||||
package org.kar.archidata;
|
||||
|
||||
import org.kar.archidata.db.DBConfig;
|
||||
import org.kar.archidata.exception.DataAccessException;
|
||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||
|
||||
public class GlobalConfiguration {
|
||||
public static final DBConfig dbConfig;
|
||||
private static final DBConfig dbConfig;
|
||||
|
||||
static {
|
||||
dbConfig = new DBConfig(ConfigBaseVariable.getDBType(), ConfigBaseVariable.getDBHost(),
|
||||
Integer.parseInt(ConfigBaseVariable.getDBPort()), ConfigBaseVariable.getDBLogin(),
|
||||
ConfigBaseVariable.getDBPassword(), ConfigBaseVariable.getDBName(),
|
||||
ConfigBaseVariable.getDBKeepConnected());
|
||||
DBConfig dbConfigTmp = null;
|
||||
try {
|
||||
dbConfigTmp = new DBConfig(ConfigBaseVariable.getDBType(), ConfigBaseVariable.getDBHost(),
|
||||
Integer.parseInt(ConfigBaseVariable.getDBPort()), ConfigBaseVariable.getDBLogin(),
|
||||
ConfigBaseVariable.getDBPassword(), ConfigBaseVariable.getDBName(),
|
||||
ConfigBaseVariable.getDBKeepConnected());
|
||||
} catch (NumberFormatException | DataAccessException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
System.out.println("Fail to configure db access ... in static GlobalConfiguration");
|
||||
}
|
||||
dbConfig = dbConfigTmp;
|
||||
}
|
||||
|
||||
public static DBConfig getDbconfig() {
|
||||
return dbConfig;
|
||||
}
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
package org.kar.archidata;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
import org.kar.archidata.dataAccess.options.DBInterfaceOption;
|
||||
import org.kar.archidata.dataAccess.options.QueryOption;
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
import org.kar.archidata.model.User;
|
||||
|
||||
public class UserDB {
|
||||
|
||||
public UserDB() {}
|
||||
|
||||
public static User getUsers(final long userId, QueryOption... option) throws Exception {
|
||||
return DataAccess.get(User.class, userId, option);
|
||||
}
|
||||
|
||||
public static User getUserOrCreate(final long userId, final String userLogin, QueryOption... option)
|
||||
throws Exception {
|
||||
final User user = getUsers(userId);
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
createUsersInfoFromOAuth(userId, userLogin, option);
|
||||
return getUsers(userId);
|
||||
}
|
||||
|
||||
private static void createUsersInfoFromOAuth(final long userId, final String login, QueryOption... option)
|
||||
throws IOException {
|
||||
QueryOptions options = new QueryOptions(option);
|
||||
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
||||
final String query = "INSERT INTO `user` (`id`, `login`, `lastConnection`, `admin`, `blocked`, `removed`) VALUE (?,?,now(3),'0','0','0')";
|
||||
try {
|
||||
final PreparedStatement ps = entry.connection.prepareStatement(query);
|
||||
ps.setLong(1, userId);
|
||||
ps.setString(2, login);
|
||||
ps.executeUpdate();
|
||||
} catch (final SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
} finally {
|
||||
entry.close();
|
||||
}
|
||||
}
|
||||
}
|
@ -56,7 +56,7 @@ import jakarta.ws.rs.core.StreamingOutput;
|
||||
// https://stackoverflow.com/questions/35367113/jersey-webservice-scalable-approach-to-download-file-and-reply-to-client
|
||||
// https://gist.github.com/aitoroses/4f7a2b197b732a6a691d
|
||||
|
||||
@Path("/data")
|
||||
// TODO: must be inherited and set the default dataAccess interface @Path("/data")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class DataResource {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DataResource.class);
|
||||
@ -64,6 +64,7 @@ public class DataResource {
|
||||
private final static int CHUNK_SIZE_IN = 50 * 1024 * 1024; // 1MB chunks
|
||||
/** Upload some datas */
|
||||
private static long tmpFolderId = 1;
|
||||
protected final DataAccess da = DataAccess.createInterface();
|
||||
|
||||
private static void createFolder(final String path) throws IOException {
|
||||
if (!Files.exists(java.nio.file.Path.of(path))) {
|
||||
@ -118,10 +119,10 @@ public class DataResource {
|
||||
return getFileData(uuid) + ".json";
|
||||
}
|
||||
|
||||
public static Data getWithSha512(final String sha512) {
|
||||
public Data getWithSha512(final String sha512) {
|
||||
LOGGER.info("find sha512 = {}", sha512);
|
||||
try {
|
||||
return DataAccess.getWhere(Data.class, new Condition(new QueryCondition("sha512", "=", sha512)));
|
||||
return this.da.getWhere(Data.class, new Condition(new QueryCondition("sha512", "=", sha512)));
|
||||
} catch (final Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
@ -129,10 +130,10 @@ public class DataResource {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Data getWithId(final long id) {
|
||||
public Data getWithId(final long id) {
|
||||
LOGGER.info("find id = {}", id);
|
||||
try {
|
||||
return DataAccess.get(Data.class, id);
|
||||
return this.da.get(Data.class, id);
|
||||
} catch (final Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
@ -140,7 +141,7 @@ public class DataResource {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Data createNewData(final long tmpUID, final String originalFileName, final String sha512)
|
||||
public Data createNewData(final long tmpUID, final String originalFileName, final String sha512)
|
||||
throws IOException {
|
||||
// determine mime type:
|
||||
Data injectedData = new Data();
|
||||
@ -161,7 +162,7 @@ public class DataResource {
|
||||
injectedData.size = Files.size(Paths.get(tmpPath));
|
||||
|
||||
try {
|
||||
injectedData = DataAccess.insert(injectedData);
|
||||
injectedData = this.da.insert(injectedData);
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
@ -254,7 +255,7 @@ public class DataResource {
|
||||
|
||||
public Data getSmall(final UUID id) {
|
||||
try {
|
||||
return DataAccess.get(Data.class, id);
|
||||
return this.da.get(Data.class, id);
|
||||
} catch (final Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
@ -500,8 +501,8 @@ public class DataResource {
|
||||
}
|
||||
}
|
||||
|
||||
public static void undelete(final Long id) throws Exception {
|
||||
DataAccess.unsetDelete(Data.class, id);
|
||||
public void undelete(final Long id) throws Exception {
|
||||
this.da.unsetDelete(Data.class, id);
|
||||
}
|
||||
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -29,7 +29,7 @@ public interface DataAccessAddOn {
|
||||
* @param iii The index of injection
|
||||
* @return the new index of injection in case of multiple value management
|
||||
* @throws SQLException */
|
||||
void insertData(PreparedStatement ps, final Field field, Object data, CountInOut iii)
|
||||
void insertData(final DataAccessSQL ioDb, PreparedStatement ps, final Field field, Object data, CountInOut iii)
|
||||
throws Exception, SQLException, IllegalArgumentException, IllegalAccessException;
|
||||
|
||||
/** Element can insert in the single request
|
||||
@ -58,6 +58,7 @@ public interface DataAccessAddOn {
|
||||
|
||||
// Return the number of colomn read
|
||||
void fillFromQuery(
|
||||
final DataAccessSQL ioDb,
|
||||
ResultSet rs,
|
||||
Field field,
|
||||
Object data,
|
||||
@ -100,6 +101,7 @@ public interface DataAccessAddOn {
|
||||
* @param data Data that might be inserted.
|
||||
* @param actions Asynchronous action to do after main request. */
|
||||
default void asyncInsert(
|
||||
final DataAccessSQL ioDb,
|
||||
final String tableName,
|
||||
final Object localId,
|
||||
final Field field,
|
||||
@ -122,6 +124,7 @@ public interface DataAccessAddOn {
|
||||
* @param data Data that might be inserted.
|
||||
* @param actions Asynchronous action to do after main request. */
|
||||
default void asyncUpdate(
|
||||
final DataAccessSQL ioDb,
|
||||
final String tableName,
|
||||
final Object localId,
|
||||
final Field field,
|
||||
@ -130,11 +133,11 @@ public interface DataAccessAddOn {
|
||||
|
||||
}
|
||||
|
||||
default void drop(final String tableName, final Field field) throws Exception {
|
||||
default void drop(final DataAccessSQL ioDb, final String tableName, final Field field) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
default void cleanAll(final String tableName, final Field field) throws Exception {
|
||||
default void cleanAll(final DataAccessSQL ioDb, final String tableName, final Field field) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,6 @@ import java.lang.reflect.Field;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
@ -25,24 +24,17 @@ import org.kar.archidata.annotation.CreationTimestamp;
|
||||
import org.kar.archidata.annotation.UpdateTimestamp;
|
||||
import org.kar.archidata.dataAccess.options.CheckFunction;
|
||||
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.Limit;
|
||||
import org.kar.archidata.dataAccess.options.OrderBy;
|
||||
import org.kar.archidata.dataAccess.options.QueryOption;
|
||||
import org.kar.archidata.dataAccess.options.TransmitKey;
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
import org.kar.archidata.db.DbInterfaceMorphia;
|
||||
import org.kar.archidata.exception.DataAccessException;
|
||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||
import org.kar.archidata.tools.DateTools;
|
||||
import org.kar.archidata.tools.UuidUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
@ -61,7 +53,7 @@ import jakarta.ws.rs.InternalServerErrorException;
|
||||
|
||||
/** Data access is an abstraction class that permit to access on the DB with a function wrapping that permit to minimize the SQL writing of SQL code. This interface support the SQL and SQLite
|
||||
* back-end. */
|
||||
public class DataAccessMorphia {
|
||||
public class DataAccessMorphia extends DataAccess {
|
||||
static final Logger LOGGER = LoggerFactory.getLogger(DataAccessMorphia.class);
|
||||
// by default we manage some add-on that permit to manage non-native model (like json serialization, List of external key as String list...)
|
||||
static final List<DataAccessAddOn> addOn = new ArrayList<>();
|
||||
@ -87,105 +79,23 @@ public class DataAccessMorphia {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
public DbInterfaceMorphia getInterface() {
|
||||
return this.db;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDBExist(final String name, final QueryOption... option) throws InternalServerErrorException {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
if ("sqlite".equals(ConfigBaseVariable.getDBType())) {
|
||||
// no base manage in sqLite ...
|
||||
// TODO: check if the file exist or not ...
|
||||
return true;
|
||||
}
|
||||
DBEntry entry;
|
||||
try {
|
||||
entry = DBInterfaceOption.getAutoEntry(options);
|
||||
} catch (final IOException ex) {
|
||||
ex.printStackTrace();
|
||||
LOGGER.error("Can not check if the DB exist!!! {}", ex.getMessage());
|
||||
|
||||
// TODO: TO test
|
||||
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
// TODO : Maybe connect with a temporary not specified connection interface to a db ...
|
||||
final PreparedStatement ps = entry.connection.prepareStatement("show databases");
|
||||
final ResultSet rs = ps.executeQuery();
|
||||
// LOGGER.info("List all tables: equals? '{}'", name);
|
||||
while (rs.next()) {
|
||||
final String data = rs.getString(1);
|
||||
// LOGGER.info(" - '{}'", data);
|
||||
if (name.equals(data)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (final SQLException ex) {
|
||||
LOGGER.error("Can not check if the DB exist SQL-error !!! {}", ex.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
entry.close();
|
||||
} catch (final IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
entry = null;
|
||||
}
|
||||
throw new InternalServerErrorException("Can Not manage the DB-access");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createDB(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("CREATE DATABASE `" + name + "`;", new DBInterfaceRoot(true));
|
||||
} catch (final SQLException | IOException ex) {
|
||||
ex.printStackTrace();
|
||||
LOGGER.error("Can not check if the DB exist!!! {}", ex.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTableExist(final String name, final QueryOption... option) throws InternalServerErrorException {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
try {
|
||||
String request = "";
|
||||
if ("sqlite".equals(ConfigBaseVariable.getDBType())) {
|
||||
request = """
|
||||
SELECT COUNT(*) AS total
|
||||
FROM sqlite_master
|
||||
WHERE type = 'table'
|
||||
AND name = ?;
|
||||
""";
|
||||
// PreparedStatement ps = entry.connection.prepareStatement("show tables");
|
||||
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
||||
final PreparedStatement ps = entry.connection.prepareStatement(request);
|
||||
ps.setString(1, name);
|
||||
final ResultSet ret = ps.executeQuery();
|
||||
final int count = ret.getInt("total");
|
||||
return count == 1;
|
||||
} else {
|
||||
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
||||
// TODO : Maybe connect with a temporary not specified connection interface to a db ...
|
||||
final PreparedStatement ps = entry.connection.prepareStatement("show tables");
|
||||
final ResultSet rs = ps.executeQuery();
|
||||
// LOGGER.info("List all tables: equals? '{}'", name);
|
||||
while (rs.next()) {
|
||||
final String data = rs.getString(1);
|
||||
// LOGGER.info(" - '{}'", data);
|
||||
if (name.equals(data)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} catch (final SQLException ex) {
|
||||
LOGGER.error("Can not check if the table exist SQL-error !!! {}", ex.getMessage());
|
||||
} catch (final IOException ex) {
|
||||
LOGGER.error("Can not check if the table exist!!! {}", ex.getMessage());
|
||||
}
|
||||
throw new InternalServerErrorException("Can Not manage the DB-access");
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Extract a list of Long with "-" separated element from a SQL input data.
|
||||
@ -270,7 +180,6 @@ public class DataAccessMorphia {
|
||||
final Document docSet,
|
||||
final Document docUnSet) throws Exception {
|
||||
if (type == long.class) {
|
||||
|
||||
docSet.append(fieldName, field.getLong(data));
|
||||
return;
|
||||
}
|
||||
@ -497,7 +406,7 @@ public class DataAccessMorphia {
|
||||
final String value = doc.getString(fieldName);
|
||||
field.set(data, value);
|
||||
return;
|
||||
|
||||
|
||||
}
|
||||
if (type.isEnum()) {
|
||||
final String value = doc.getString(fieldName);
|
||||
@ -749,16 +658,6 @@ public class DataAccessMorphia {
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO: manage insert batch...
|
||||
public <T> List<T> insertMultiple(final List<T> data, final QueryOption... options) throws Exception {
|
||||
final List<T> out = new ArrayList<>();
|
||||
for (final T elem : data) {
|
||||
final T tmp = insert(elem, options);
|
||||
out.add(tmp);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public long getNextSequenceLongValue(final String collectionName, String fieldName) {
|
||||
if (fieldName == null || fieldName.isEmpty()) {
|
||||
fieldName = "sequence_id";
|
||||
@ -784,6 +683,7 @@ public class DataAccessMorphia {
|
||||
return updatedCounter.getLong(fieldName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T insert(final T data, final QueryOption... option) throws Exception {
|
||||
final Class<?> clazz = data.getClass();
|
||||
@ -792,10 +692,9 @@ public class DataAccessMorphia {
|
||||
// External checker of data:
|
||||
final List<CheckFunction> checks = options.get(CheckFunction.class);
|
||||
for (final CheckFunction check : checks) {
|
||||
check.getChecker().check("", data, AnnotationTools.getFieldsNames(clazz), options);
|
||||
check.getChecker().check(this, "", data, AnnotationTools.getFieldsNames(clazz), options);
|
||||
}
|
||||
|
||||
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
||||
final List<Field> asyncFieldUpdate = new ArrayList<>();
|
||||
final String collectionName = AnnotationTools.getCollectionName(clazz, options);
|
||||
Field primaryKeyField = null;
|
||||
@ -872,8 +771,6 @@ public class DataAccessMorphia {
|
||||
LOGGER.error("Fail SQL request: {}", ex.getMessage());
|
||||
ex.printStackTrace();
|
||||
throw new DataAccessException("Fail to Insert data in DB : " + ex.getMessage());
|
||||
} finally {
|
||||
entry.close();
|
||||
}
|
||||
final List<LazyGetter> asyncActions = new ArrayList<>();
|
||||
for (final Field field : asyncFieldUpdate) {
|
||||
@ -892,102 +789,7 @@ public class DataAccessMorphia {
|
||||
return (T) getWhere(clazz, new Condition(new QueryCondition("_id", "=", insertedId)));
|
||||
}
|
||||
|
||||
// seems a good idea, but very dangerous if we not filter input data... if set an id it can be complicated...
|
||||
public <T> T insertWithJson(final Class<T> clazz, final String jsonData) throws Exception {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
// parse the object to be sure the data are valid:
|
||||
final T data = mapper.readValue(jsonData, clazz);
|
||||
return insert(data);
|
||||
}
|
||||
|
||||
public <ID_TYPE> QueryCondition getTableIdCondition(final Class<?> clazz, final ID_TYPE idKey)
|
||||
throws DataAccessException {
|
||||
// Find the ID field type ....
|
||||
final Field idField = AnnotationTools.getIdField(clazz);
|
||||
if (idField == null) {
|
||||
throw new DataAccessException(
|
||||
"The class have no annotation @Id ==> can not determine the default type searching");
|
||||
}
|
||||
// check the compatibility of the id and the declared ID
|
||||
final Class<?> typeClass = idField.getType();
|
||||
if (idKey == null) {
|
||||
throw new DataAccessException("Try to identify the ID type and object wa null.");
|
||||
}
|
||||
if (idKey.getClass() != typeClass) {
|
||||
if (idKey.getClass() == Condition.class) {
|
||||
throw new DataAccessException(
|
||||
"Try to identify the ID type on a condition 'close' internal API error use xxxWhere(...) instead.");
|
||||
}
|
||||
throw new DataAccessException("Request update with the wrong type ...");
|
||||
}
|
||||
return new QueryCondition(AnnotationTools.getFieldName(idField), "=", idKey);
|
||||
}
|
||||
|
||||
/** Update an object with the inserted json data
|
||||
*
|
||||
* @param <T> Type of the object to insert
|
||||
* @param <ID_TYPE> Master key on the object manage with @Id
|
||||
* @param clazz Class reference of the insertion model
|
||||
* @param id Key to insert data
|
||||
* @param jsonData Json data (partial) values to update
|
||||
* @return the number of object updated
|
||||
* @throws Exception */
|
||||
public <T, ID_TYPE> long updateWithJson(
|
||||
final Class<T> clazz,
|
||||
final ID_TYPE id,
|
||||
final String jsonData,
|
||||
final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
options.add(new Condition(getTableIdCondition(clazz, id)));
|
||||
options.add(new TransmitKey(id));
|
||||
return updateWhereWithJson(clazz, jsonData, options.getAllArray());
|
||||
}
|
||||
|
||||
public <T> long updateWhereWithJson(final Class<T> clazz, final String jsonData, final QueryOption... option)
|
||||
throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
if (options.get(Condition.class).size() == 0) {
|
||||
throw new DataAccessException("request a updateWhereWithJson without any condition");
|
||||
}
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
// parse the object to be sure the data are valid:
|
||||
final T data = mapper.readValue(jsonData, clazz);
|
||||
// Read the tree to filter injection of data:
|
||||
final JsonNode root = mapper.readTree(jsonData);
|
||||
final List<String> keys = new ArrayList<>();
|
||||
final var iterator = root.fieldNames();
|
||||
iterator.forEachRemaining(e -> keys.add(e));
|
||||
options.add(new FilterValue(keys));
|
||||
return updateWhere(data, options.getAllArray());
|
||||
}
|
||||
|
||||
public <T, ID_TYPE> long update(final T data, final ID_TYPE id) throws Exception {
|
||||
return update(data, id, AnnotationTools.getFieldsNames(data.getClass()));
|
||||
}
|
||||
|
||||
/** @param <T>
|
||||
* @param data
|
||||
* @param id
|
||||
* @param filterValue
|
||||
* @return the affected rows.
|
||||
* @throws Exception */
|
||||
public <T, ID_TYPE> long update(
|
||||
final T data,
|
||||
final ID_TYPE id,
|
||||
final List<String> updateColomn,
|
||||
final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
options.add(new Condition(getTableIdCondition(data.getClass(), id)));
|
||||
options.add(new FilterValue(updateColomn));
|
||||
options.add(new TransmitKey(id));
|
||||
return updateWhere(data, options);
|
||||
}
|
||||
|
||||
public <T> long updateWhere(final T data, final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
return updateWhere(data, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> long updateWhere(final T data, QueryOptions options) throws Exception {
|
||||
final Class<?> clazz = data.getClass();
|
||||
if (options == null) {
|
||||
@ -1003,7 +805,7 @@ public class DataAccessMorphia {
|
||||
if (options != null) {
|
||||
final List<CheckFunction> checks = options.get(CheckFunction.class);
|
||||
for (final CheckFunction check : checks) {
|
||||
check.getChecker().check("", data, filterKey.getValues(), options);
|
||||
check.getChecker().check(this, "", data, filterKey.getValues(), options);
|
||||
}
|
||||
}
|
||||
final List<LazyGetter> asyncActions = new ArrayList<>();
|
||||
@ -1011,7 +813,7 @@ public class DataAccessMorphia {
|
||||
// real add in the BDD:
|
||||
try {
|
||||
final String collectionName = AnnotationTools.getCollectionName(clazz, options);
|
||||
|
||||
|
||||
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||
final Bson filters = condition.getFilter(collectionName, options, deletedFieldName);
|
||||
final Document docSet = new Document();
|
||||
@ -1090,6 +892,7 @@ public class DataAccessMorphia {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addElement(final PreparedStatement ps, final Object value, final CountInOut iii) throws Exception {
|
||||
if (value instanceof final UUID tmp) {
|
||||
final byte[] dataByte = UuidUtils.asBytes(tmp);
|
||||
@ -1138,37 +941,6 @@ public class DataAccessMorphia {
|
||||
}
|
||||
}
|
||||
|
||||
public int executeSimpleQuery(final String query, final QueryOption... option) throws SQLException, IOException {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
||||
LOGGER.info("Query : '{}'", query);
|
||||
try (final Statement stmt = entry.connection.createStatement()) {
|
||||
return stmt.executeUpdate(query);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean executeQuery(final String query, final QueryOption... option) throws SQLException, IOException {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
||||
try (final Statement stmt = entry.connection.createStatement()) {
|
||||
return stmt.execute(query);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T getWhere(final Class<T> clazz, final QueryOptions options) throws Exception {
|
||||
options.add(new Limit(1));
|
||||
final List<T> values = getsWhere(clazz, options);
|
||||
if (values.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
return values.get(0);
|
||||
}
|
||||
|
||||
public <T> T getWhere(final Class<T> clazz, final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
return getWhere(clazz, options);
|
||||
}
|
||||
|
||||
// This must be refactored to manage the .project of Morphia.
|
||||
public void generateSelectField(//
|
||||
final StringBuilder querySelect, //
|
||||
@ -1216,11 +988,13 @@ public class DataAccessMorphia {
|
||||
*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> getsWhere(final Class<T> clazz, final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
return getsWhere(clazz, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition conditionFusionOrEmpty(final QueryOptions options, final boolean throwIfEmpty)
|
||||
throws DataAccessException {
|
||||
if (options == null) {
|
||||
@ -1247,10 +1021,11 @@ public class DataAccessMorphia {
|
||||
return condition;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> List<T> getsWhere(final Class<T> clazz, final QueryOptions options)
|
||||
throws DataAccessException, IOException {
|
||||
|
||||
|
||||
final Condition condition = conditionFusionOrEmpty(options, false);
|
||||
final List<LazyGetter> lazyCall = new ArrayList<>();
|
||||
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||
@ -1284,7 +1059,7 @@ public class DataAccessMorphia {
|
||||
}
|
||||
retFind = retFind.sort(sorts);
|
||||
}
|
||||
|
||||
|
||||
final List<Limit> limits = options.get(Limit.class);
|
||||
if (limits.size() == 1) {
|
||||
retFind = retFind.limit((int) limits.get(0).getValue());
|
||||
@ -1363,17 +1138,20 @@ public class DataAccessMorphia {
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <ID_TYPE> long count(final Class<?> clazz, final ID_TYPE id, final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
options.add(new Condition(getTableIdCondition(clazz, id)));
|
||||
return this.countWhere(clazz, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long countWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
return countWhere(clazz, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long countWhere(final Class<?> clazz, final QueryOptions options) throws Exception {
|
||||
final Condition condition = conditionFusionOrEmpty(options, false);
|
||||
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||
@ -1392,16 +1170,19 @@ public class DataAccessMorphia {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, ID_TYPE> T get(final Class<T> clazz, final ID_TYPE id, final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
options.add(new Condition(getTableIdCondition(clazz, id)));
|
||||
return this.getWhere(clazz, options.getAllArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> gets(final Class<T> clazz) throws Exception {
|
||||
return getsWhere(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> gets(final Class<T> clazz, final QueryOption... option) throws Exception {
|
||||
return getsWhere(clazz, option);
|
||||
}
|
||||
@ -1412,6 +1193,7 @@ public class DataAccessMorphia {
|
||||
* @param id Unique Id of the model
|
||||
* @param options (Optional) Options of the request
|
||||
* @return Number of element that is removed. */
|
||||
@Override
|
||||
public <ID_TYPE> long delete(final Class<?> clazz, final ID_TYPE id, final QueryOption... options)
|
||||
throws Exception {
|
||||
final String hasDeletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||
@ -1427,6 +1209,7 @@ public class DataAccessMorphia {
|
||||
* @param condition Condition to remove elements.
|
||||
* @param options (Optional) Options of the request.
|
||||
* @return Number of element that is removed. */
|
||||
@Override
|
||||
public long deleteWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
|
||||
final String hasDeletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||
if (hasDeletedFieldName != null) {
|
||||
@ -1436,6 +1219,7 @@ public class DataAccessMorphia {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <ID_TYPE> long deleteHard(final Class<?> clazz, final ID_TYPE id, final QueryOption... option)
|
||||
throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
@ -1443,6 +1227,7 @@ public class DataAccessMorphia {
|
||||
return deleteHardWhere(clazz, options.getAllArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long deleteHardWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
final Condition condition = conditionFusionOrEmpty(options, true);
|
||||
@ -1466,6 +1251,7 @@ public class DataAccessMorphia {
|
||||
return deleteSoftWhere(clazz, options.getAllArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long deleteSoftWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
final Condition condition = conditionFusionOrEmpty(options, true);
|
||||
@ -1479,10 +1265,12 @@ public class DataAccessMorphia {
|
||||
return ret.getModifiedCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <ID_TYPE> long unsetDelete(final Class<?> clazz, final ID_TYPE id) throws DataAccessException {
|
||||
return unsetDeleteWhere(clazz, new Condition(getTableIdCondition(clazz, id)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <ID_TYPE> long unsetDelete(final Class<?> clazz, final ID_TYPE id, final QueryOption... option)
|
||||
throws DataAccessException {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
@ -1490,6 +1278,7 @@ public class DataAccessMorphia {
|
||||
return unsetDeleteWhere(clazz, options.getAllArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long unsetDeleteWhere(final Class<?> clazz, final QueryOption... option) throws DataAccessException {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
final Condition condition = conditionFusionOrEmpty(options, true);
|
||||
@ -1506,6 +1295,7 @@ public class DataAccessMorphia {
|
||||
return ret.getModifiedCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(final Class<?> clazz, final QueryOption... option) throws Exception {
|
||||
/*
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
@ -1541,6 +1331,7 @@ public class DataAccessMorphia {
|
||||
*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanAll(final Class<?> clazz, final QueryOption... option) throws Exception {
|
||||
/*
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -19,12 +19,10 @@ import java.util.UUID;
|
||||
import org.kar.archidata.dataAccess.exportTools.TableQuery;
|
||||
import org.kar.archidata.dataAccess.exportTools.TableQueryTypes;
|
||||
import org.kar.archidata.dataAccess.options.Condition;
|
||||
import org.kar.archidata.dataAccess.options.DBInterfaceOption;
|
||||
import org.kar.archidata.dataAccess.options.GroupBy;
|
||||
import org.kar.archidata.dataAccess.options.Limit;
|
||||
import org.kar.archidata.dataAccess.options.OrderBy;
|
||||
import org.kar.archidata.dataAccess.options.QueryOption;
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
import org.kar.archidata.exception.DataAccessException;
|
||||
import org.kar.archidata.tools.DateTools;
|
||||
import org.slf4j.Logger;
|
||||
@ -246,24 +244,24 @@ public class DataExport {
|
||||
}
|
||||
|
||||
public static TableQuery queryTable(
|
||||
final DataAccessSQL ioDb,
|
||||
final List<TableQueryTypes> headers,
|
||||
final String query,
|
||||
final List<Object> parameters,
|
||||
final QueryOption... option) throws Exception {
|
||||
final QueryOptions options = new QueryOptions(option);
|
||||
return queryTable(headers, query, parameters, options);
|
||||
return queryTable(ioDb, headers, query, parameters, options);
|
||||
}
|
||||
|
||||
public static TableQuery queryTable(
|
||||
final DataAccessSQL ioDb,
|
||||
final List<TableQueryTypes> headers,
|
||||
final String queryBase,
|
||||
final List<Object> parameters,
|
||||
final QueryOptions options) throws Exception {
|
||||
final List<LazyGetter> lazyCall = new ArrayList<>();
|
||||
// TODO ... final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
||||
|
||||
final Condition condition = DataAccess.conditionFusionOrEmpty(options, false);
|
||||
final Condition condition = ioDb.conditionFusionOrEmpty(options, false);
|
||||
final StringBuilder query = new StringBuilder(queryBase);
|
||||
final TableQuery out = new TableQuery(headers);
|
||||
// real add in the BDD:
|
||||
@ -286,18 +284,18 @@ public class DataExport {
|
||||
}
|
||||
LOGGER.warn("generate the query: '{}'", query.toString());
|
||||
// prepare the request:
|
||||
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(),
|
||||
final PreparedStatement ps = ioDb.getConnection().prepareStatement(query.toString(),
|
||||
Statement.RETURN_GENERATED_KEYS);
|
||||
final CountInOut iii = new CountInOut(1);
|
||||
if (parameters != null) {
|
||||
for (final Object elem : parameters) {
|
||||
DataAccess.addElement(ps, elem, iii);
|
||||
ioDb.addElement(ps, elem, iii);
|
||||
}
|
||||
iii.inc();
|
||||
}
|
||||
condition.injectQuery(ps, iii);
|
||||
condition.injectQuery(ioDb, ps, iii);
|
||||
if (limits.size() == 1) {
|
||||
limits.get(0).injectQuery(ps, iii);
|
||||
limits.get(0).injectQuery(ioDb, ps, iii);
|
||||
}
|
||||
// execute the request
|
||||
final ResultSet rs = ps.executeQuery();
|
||||
@ -332,8 +330,6 @@ public class DataExport {
|
||||
throw ex;
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
entry.close();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
@ -383,8 +383,8 @@ public class DataFactory {
|
||||
}
|
||||
alreadyAdded.add(dataName);
|
||||
LOGGER.trace(" + '{}'", elem.getName());
|
||||
if (DataAccess.isAddOnField(elem)) {
|
||||
final DataAccessAddOn addOn = DataAccess.findAddOnforField(elem);
|
||||
if (DataAccessSQL.isAddOnField(elem)) {
|
||||
final DataAccessAddOn addOn = DataAccessSQL.findAddOnforField(elem);
|
||||
LOGGER.trace("Create type for: {} ==> {} (ADD-ON)", AnnotationTools.getFieldName(elem),
|
||||
elem.getType());
|
||||
if (addOn != null) {
|
||||
@ -439,4 +439,4 @@ public class DataFactory {
|
||||
return preActionList;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -11,20 +11,20 @@ import com.mongodb.client.model.Filters;
|
||||
|
||||
public class QueryAnd implements QueryItem {
|
||||
protected final List<QueryItem> childs;
|
||||
|
||||
|
||||
public QueryAnd(final List<QueryItem> child) {
|
||||
this.childs = child;
|
||||
}
|
||||
|
||||
|
||||
public QueryAnd(final QueryItem... child) {
|
||||
this.childs = new ArrayList<>();
|
||||
Collections.addAll(this.childs, child);
|
||||
}
|
||||
|
||||
|
||||
public void add(final QueryItem... child) {
|
||||
Collections.addAll(this.childs, child);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void generateQuery(final StringBuilder query, final String tableName) {
|
||||
if (this.childs.size() >= 1) {
|
||||
@ -43,19 +43,19 @@ public class QueryAnd implements QueryItem {
|
||||
query.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||
|
||||
public void injectQuery(final DataAccessSQL ioDb, final PreparedStatement ps, final CountInOut iii)
|
||||
throws Exception {
|
||||
for (final QueryItem elem : this.childs) {
|
||||
elem.injectQuery(ps, iii);
|
||||
elem.injectQuery(ioDb, ps, iii);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int size() {
|
||||
return this.childs.size();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void generateFilter(final List<Bson> filters) {
|
||||
final List<Bson> filtersLocal = new ArrayList<>();
|
||||
|
@ -14,7 +14,7 @@ public class QueryCondition implements QueryItem {
|
||||
private final String key;
|
||||
private final String comparator;
|
||||
private final Object value;
|
||||
|
||||
|
||||
/**
|
||||
* Simple DB comparison element. Note the injected object is injected in the statement and not in the query directly.
|
||||
* @param key Field to check (the Model property name)
|
||||
@ -26,7 +26,7 @@ public class QueryCondition implements QueryItem {
|
||||
this.comparator = comparator;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void generateQuery(final StringBuilder query, final String tableName) {
|
||||
if (tableName != null) {
|
||||
@ -38,10 +38,11 @@ public class QueryCondition implements QueryItem {
|
||||
query.append(this.comparator);
|
||||
query.append(" ?");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||
DataAccess.addElement(ps, this.value, iii);
|
||||
public void injectQuery(final DataAccessSQL ioDb, final PreparedStatement ps, final CountInOut iii)
|
||||
throws Exception {
|
||||
ioDb.addElement(ps, this.value, iii);
|
||||
iii.inc();
|
||||
}
|
||||
|
||||
@ -62,6 +63,6 @@ public class QueryCondition implements QueryItem {
|
||||
} else {
|
||||
LOGGER.error("Not manage comparison: '{}'", this.key);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -48,9 +48,10 @@ public class QueryInList<T> implements QueryItem {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||
public void injectQuery(final DataAccessSQL ioDb, final PreparedStatement ps, final CountInOut iii)
|
||||
throws Exception {
|
||||
for (final Object elem : this.value) {
|
||||
DataAccess.addElement(ps, elem, iii);
|
||||
ioDb.addElement(ps, elem, iii);
|
||||
iii.inc();
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ public interface QueryItem {
|
||||
void generateQuery(StringBuilder query, String tableName);
|
||||
|
||||
// For SQL mode query injection
|
||||
void injectQuery(PreparedStatement ps, CountInOut iii) throws Exception;
|
||||
void injectQuery(DataAccessSQL ioDb, PreparedStatement ps, CountInOut iii) throws Exception;
|
||||
|
||||
// For No-SQL mode filter creation
|
||||
void generateFilter(List<Bson> filters);
|
||||
|
@ -25,7 +25,8 @@ public class QueryNotNull implements QueryItem {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {}
|
||||
public void injectQuery(final DataAccessSQL ioDb, final PreparedStatement ps, final CountInOut iii)
|
||||
throws Exception {}
|
||||
|
||||
@Override
|
||||
public void generateFilter(final List<Bson> filters) {
|
||||
|
@ -9,11 +9,11 @@ import com.mongodb.client.model.Filters;
|
||||
|
||||
public class QueryNull implements QueryItem {
|
||||
private final String key;
|
||||
|
||||
|
||||
public QueryNull(final String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void generateQuery(final StringBuilder query, final String tableName) {
|
||||
if (tableName != null) {
|
||||
@ -23,10 +23,11 @@ public class QueryNull implements QueryItem {
|
||||
query.append(this.key);
|
||||
query.append(" IS NULL");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {}
|
||||
|
||||
public void injectQuery(final DataAccessSQL ioDb, final PreparedStatement ps, final CountInOut iii)
|
||||
throws Exception {}
|
||||
|
||||
@Override
|
||||
public void generateFilter(final List<Bson> filters) {
|
||||
// Not sure of the result ... maybe check it ...
|
||||
|
@ -10,15 +10,15 @@ import com.mongodb.client.model.Filters;
|
||||
|
||||
public class QueryOr implements QueryItem {
|
||||
protected final List<QueryItem> childs;
|
||||
|
||||
|
||||
public QueryOr(final List<QueryItem> childs) {
|
||||
this.childs = childs;
|
||||
}
|
||||
|
||||
|
||||
public QueryOr(final QueryItem... childs) {
|
||||
this.childs = List.of(childs);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void generateQuery(final StringBuilder query, final String tableName) {
|
||||
if (this.childs.size() >= 1) {
|
||||
@ -37,14 +37,15 @@ public class QueryOr implements QueryItem {
|
||||
query.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||
public void injectQuery(final DataAccessSQL ioDb, final PreparedStatement ps, final CountInOut iii)
|
||||
throws Exception {
|
||||
for (final QueryItem elem : this.childs) {
|
||||
elem.injectQuery(ps, iii);
|
||||
elem.injectQuery(ioDb, ps, iii);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void generateFilter(final List<Bson> filters) {
|
||||
final List<Bson> filtersLocal = new ArrayList<>();
|
||||
|
@ -10,11 +10,14 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.annotation.DataJson;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
||||
import org.kar.archidata.dataAccess.DataAccessMorphia;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.LazyGetter;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
@ -31,6 +34,8 @@ import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.model.Filters;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
@ -55,7 +60,12 @@ public class AddOnDataJson implements DataAccessAddOn {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii)
|
||||
public void insertData(
|
||||
final DataAccessSQL ioDb,
|
||||
final PreparedStatement ps,
|
||||
final Field field,
|
||||
final Object rootObject,
|
||||
final CountInOut iii)
|
||||
throws IllegalArgumentException, IllegalAccessException, SQLException, JsonProcessingException {
|
||||
final Object data = field.get(rootObject);
|
||||
if (data == null) {
|
||||
@ -102,6 +112,7 @@ public class AddOnDataJson implements DataAccessAddOn {
|
||||
|
||||
@Override
|
||||
public void fillFromQuery(
|
||||
final DataAccessSQL ioDb,
|
||||
final ResultSet rs,
|
||||
final Field field,
|
||||
final Object data,
|
||||
@ -172,11 +183,14 @@ public class AddOnDataJson implements DataAccessAddOn {
|
||||
postActionList, createIfNotExist, createDrop, fieldId, JsonValue.class);
|
||||
}
|
||||
|
||||
public static void addLink(final Class<?> clazz, final Long id, final String column, final Long remoteKey)
|
||||
throws Exception {
|
||||
public static void addLink(
|
||||
final DataAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final Long id,
|
||||
final String column,
|
||||
final Long remoteKey) throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversLongLong data = DataAccess.get(TableCoversLongLong.class, id,
|
||||
new OverrideTableName(tableName));
|
||||
final TableCoversLongLong data = ioDb.get(TableCoversLongLong.class, id, new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
data.covers = new ArrayList<>();
|
||||
}
|
||||
@ -186,7 +200,7 @@ public class AddOnDataJson implements DataAccessAddOn {
|
||||
}
|
||||
}
|
||||
data.covers.add(remoteKey);
|
||||
DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));
|
||||
ioDb.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -200,22 +214,31 @@ public class AddOnDataJson implements DataAccessAddOn {
|
||||
* @param remoteKey The UUID to add to the covers list.
|
||||
* @throws Exception If an error occurs during data retrieval or update.
|
||||
*/
|
||||
public static void addLink(final Class<?> clazz, final Long id, final String column, final UUID remoteKey)
|
||||
throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
// TODO: Get primary key name
|
||||
final TableCoversLongUUID data = DataAccess.get(TableCoversLongUUID.class, id,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
data.covers = new ArrayList<>();
|
||||
}
|
||||
for (final UUID elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
return;
|
||||
public static void addLink(
|
||||
final DataAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final Long id,
|
||||
final String column,
|
||||
final UUID remoteKey) throws Exception {
|
||||
if (ioDb instanceof final DataAccessSQL daSQL) {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
// TODO: Get primary key name
|
||||
final TableCoversLongUUID data = ioDb.get(TableCoversLongUUID.class, id, new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
data.covers = new ArrayList<>();
|
||||
}
|
||||
for (final UUID elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
data.covers.add(remoteKey);
|
||||
ioDb.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));// TODO: ,new OverrideFieldName("covers", column));
|
||||
} else if (ioDb instanceof final DataAccessMorphia dam) {
|
||||
|
||||
} else {
|
||||
throw new DataAccessException("DataAccess Not managed");
|
||||
}
|
||||
data.covers.add(remoteKey);
|
||||
DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));// TODO: ,new OverrideFieldName("covers", column));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -229,113 +252,210 @@ public class AddOnDataJson implements DataAccessAddOn {
|
||||
* @param remoteKey The UUID to add to the covers list.
|
||||
* @throws Exception If an error occurs during data retrieval or update.
|
||||
*/
|
||||
public static void addLink(final Class<?> clazz, final UUID uuid, final String column, final UUID remoteKey)
|
||||
throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversUUIDUUID data = DataAccess.get(TableCoversUUIDUUID.class, uuid,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
data.covers = new ArrayList<>();
|
||||
public static void addLink(
|
||||
final DataAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final UUID uuid,
|
||||
final String column,
|
||||
final UUID remoteKey) throws Exception {
|
||||
if (ioDb instanceof final DataAccessSQL daSQL) {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversUUIDUUID data = ioDb.get(TableCoversUUIDUUID.class, uuid,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
data.covers = new ArrayList<>();
|
||||
}
|
||||
for (final UUID elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
data.covers.add(remoteKey);
|
||||
ioDb.update(data, data.uuid, List.of("covers"), new OverrideTableName(tableName));
|
||||
} else if (ioDb instanceof final DataAccessMorphia dam) {
|
||||
|
||||
} else {
|
||||
throw new DataAccessException("DataAccess Not managed");
|
||||
}
|
||||
for (final UUID elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
}
|
||||
|
||||
public static void addLink(
|
||||
final DataAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final UUID uuid,
|
||||
final String column,
|
||||
final Long remoteKey) throws Exception {
|
||||
if (ioDb instanceof final DataAccessSQL daSQL) {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversUUIDLong data = ioDb.get(TableCoversUUIDLong.class, uuid,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
data.covers = new ArrayList<>();
|
||||
}
|
||||
for (final Long elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
data.covers.add(remoteKey);
|
||||
ioDb.update(data, data.uuid, List.of("covers"), new OverrideTableName(tableName));
|
||||
} else if (ioDb instanceof final DataAccessMorphia dam) {
|
||||
|
||||
} else {
|
||||
throw new DataAccessException("DataAccess Not managed");
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeLink(
|
||||
final DataAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final UUID uuid,
|
||||
final String column,
|
||||
final Long remoteKey) throws Exception {
|
||||
if (ioDb instanceof final DataAccessSQL daSQL) {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversUUIDLong data = ioDb.get(TableCoversUUIDLong.class, uuid,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
final List<Long> newList = new ArrayList<>();
|
||||
for (final Long elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
continue;
|
||||
}
|
||||
newList.add(elem);
|
||||
}
|
||||
data.covers = newList;
|
||||
ioDb.update(data, data.uuid, List.of("covers"), new OverrideTableName(tableName));
|
||||
} else if (ioDb instanceof final DataAccessMorphia dam) {
|
||||
|
||||
} else {
|
||||
throw new DataAccessException("DataAccess Not managed");
|
||||
}
|
||||
data.covers.add(remoteKey);
|
||||
DataAccess.update(data, data.uuid, List.of("covers"), new OverrideTableName(tableName));
|
||||
}
|
||||
|
||||
public static void addLink(final Class<?> clazz, final UUID uuid, final String column, final Long remoteKey)
|
||||
throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversUUIDLong data = DataAccess.get(TableCoversUUIDLong.class, uuid,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
data.covers = new ArrayList<>();
|
||||
}
|
||||
for (final Long elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
public static void removeLink(
|
||||
final DataAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final UUID uuid,
|
||||
final String column,
|
||||
final UUID remoteKey) throws Exception {
|
||||
if (ioDb instanceof final DataAccessSQL daSQL) {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversUUIDUUID data = ioDb.get(TableCoversUUIDUUID.class, uuid,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
final List<UUID> newList = new ArrayList<>();
|
||||
for (final UUID elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
continue;
|
||||
}
|
||||
newList.add(elem);
|
||||
}
|
||||
data.covers = newList;
|
||||
ioDb.update(data, data.uuid, List.of("covers"), new OverrideTableName(tableName));
|
||||
} else if (ioDb instanceof final DataAccessMorphia dam) {
|
||||
|
||||
} else {
|
||||
throw new DataAccessException("DataAccess Not managed");
|
||||
}
|
||||
data.covers.add(remoteKey);
|
||||
DataAccess.update(data, data.uuid, List.of("covers"), new OverrideTableName(tableName));
|
||||
}
|
||||
|
||||
public static void removeLink(final Class<?> clazz, final UUID uuid, final String column, final Long remoteKey)
|
||||
throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversUUIDLong data = DataAccess.get(TableCoversUUIDLong.class, uuid,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
final List<Long> newList = new ArrayList<>();
|
||||
for (final Long elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
continue;
|
||||
public static void removeLink(
|
||||
final DataAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final Long id,
|
||||
final String column,
|
||||
final Long remoteKey) throws Exception {
|
||||
if (ioDb instanceof final DataAccessSQL daSQL) {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversLongLong data = ioDb.get(TableCoversLongLong.class, id, new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
newList.add(elem);
|
||||
final List<Long> newList = new ArrayList<>();
|
||||
for (final Long elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
continue;
|
||||
}
|
||||
newList.add(elem);
|
||||
}
|
||||
data.covers = newList;
|
||||
ioDb.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));
|
||||
} else if (ioDb instanceof final DataAccessMorphia dam) {
|
||||
|
||||
} else {
|
||||
throw new DataAccessException("DataAccess Not managed");
|
||||
}
|
||||
data.covers = newList;
|
||||
DataAccess.update(data, data.uuid, List.of("covers"), new OverrideTableName(tableName));
|
||||
}
|
||||
|
||||
public static void removeLink(final Class<?> clazz, final UUID uuid, final String column, final UUID remoteKey)
|
||||
throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversUUIDUUID data = DataAccess.get(TableCoversUUIDUUID.class, uuid,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
final List<UUID> newList = new ArrayList<>();
|
||||
for (final UUID elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
continue;
|
||||
public static void removeLink(
|
||||
final DataAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final Long id,
|
||||
final String column,
|
||||
final UUID remoteKey) throws Exception {
|
||||
if (ioDb instanceof final DataAccessSQL daSQL) {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversLongUUID data = ioDb.get(TableCoversLongUUID.class, id, new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
newList.add(elem);
|
||||
}
|
||||
data.covers = newList;
|
||||
DataAccess.update(data, data.uuid, List.of("covers"), new OverrideTableName(tableName));
|
||||
}
|
||||
final List<UUID> newList = new ArrayList<>();
|
||||
for (final UUID elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
continue;
|
||||
}
|
||||
newList.add(elem);
|
||||
}
|
||||
data.covers = newList;
|
||||
} else if (ioDb instanceof final DataAccessMorphia dam) {
|
||||
final String collectionName = AnnotationTools.getCollectionName(clazz);
|
||||
final Field primaryfield = AnnotationTools.getPrimaryKeyField(clazz);
|
||||
final String primaryFieldName = AnnotationTools.getFieldName(primaryfield);
|
||||
|
||||
public static void removeLink(final Class<?> clazz, final Long id, final String column, final Long remoteKey)
|
||||
throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversLongLong data = DataAccess.get(TableCoversLongLong.class, id,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
final List<Long> newList = new ArrayList<>();
|
||||
for (final Long elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
continue;
|
||||
final MongoCollection<Document> collection = dam.getInterface().getDatastore().getDatabase()
|
||||
.getCollection(collectionName);
|
||||
// retrieve previous value:
|
||||
final Document ret = collection.find(Filters.eq(primaryFieldName, id)).first();
|
||||
if (ret == null) {
|
||||
throw new DataAccessException("Element does not exist ...");
|
||||
}
|
||||
newList.add(elem);
|
||||
}
|
||||
data.covers = newList;
|
||||
DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));
|
||||
}
|
||||
final List<UUID> newList = new ArrayList<>();
|
||||
final List listValues = ret.get(remoteKey, newList.getClass());
|
||||
/*
|
||||
final Document actions = new Document();
|
||||
|
||||
public static void removeLink(final Class<?> clazz, final Long id, final String column, final UUID remoteKey)
|
||||
throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final TableCoversLongUUID data = DataAccess.get(TableCoversLongUUID.class, id,
|
||||
new OverrideTableName(tableName));
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
final List<UUID> newList = new ArrayList<>();
|
||||
for (final UUID elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
continue;
|
||||
// update value:
|
||||
final Document actions = new Document();
|
||||
if (!docSet.isEmpty()) {
|
||||
actions.append("$set", docSet);
|
||||
}
|
||||
newList.add(elem);
|
||||
if (!docUnSet.isEmpty()) {
|
||||
actions.append("$unset", docUnSet);
|
||||
}
|
||||
LOGGER.info("update some values: {}", actions.toJson());
|
||||
final UpdateResult ret = collection.updateMany(filters, actions);
|
||||
return ret.getModifiedCount();
|
||||
|
||||
final TableCoversLongUUID data = ioDb.getDocument(tableName, id);
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
final List<UUID> newList = new ArrayList<>();
|
||||
for (final UUID elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
continue;
|
||||
}
|
||||
newList.add(elem);
|
||||
}
|
||||
data.covers = newList;
|
||||
*/
|
||||
} else {
|
||||
throw new DataAccessException("DataAccess Not managed");
|
||||
}
|
||||
data.covers = newList;
|
||||
DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,8 @@ import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
||||
import org.kar.archidata.dataAccess.DataAccessMorphia;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.LazyGetter;
|
||||
import org.kar.archidata.dataAccess.QueryAnd;
|
||||
@ -56,8 +58,12 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii)
|
||||
throws SQLException, IllegalArgumentException, IllegalAccessException {
|
||||
public void insertData(
|
||||
final DataAccessSQL ioDb,
|
||||
final PreparedStatement ps,
|
||||
final Field field,
|
||||
final Object rootObject,
|
||||
final CountInOut iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
||||
|
||||
}
|
||||
|
||||
@ -209,6 +215,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
|
||||
@Override
|
||||
public void fillFromQuery(
|
||||
final DataAccessSQL ioDb,
|
||||
final ResultSet rs,
|
||||
final Field field,
|
||||
final Object data,
|
||||
@ -222,12 +229,12 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType())
|
||||
.getActualTypeArguments()[0];
|
||||
if (objectClass == Long.class) {
|
||||
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR_LONG);
|
||||
final List<Long> idList = ioDb.getListOfIds(rs, count.value, SEPARATOR_LONG);
|
||||
field.set(data, idList);
|
||||
count.inc();
|
||||
return;
|
||||
} else if (objectClass == UUID.class) {
|
||||
final List<UUID> idList = DataAccess.getListOfRawUUIDs(rs, count.value);
|
||||
final List<UUID> idList = ioDb.getListOfRawUUIDs(rs, count.value);
|
||||
field.set(data, idList);
|
||||
count.inc();
|
||||
return;
|
||||
@ -241,7 +248,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
if (decorators.fetch() == FetchType.EAGER) {
|
||||
throw new DataAccessException("EAGER is not supported for list of element...");
|
||||
} else if (foreignKeyType == Long.class) {
|
||||
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR_LONG);
|
||||
final List<Long> idList = ioDb.getListOfIds(rs, count.value, SEPARATOR_LONG);
|
||||
// field.set(data, idList);
|
||||
count.inc();
|
||||
if (idList != null && idList.size() > 0) {
|
||||
@ -251,7 +258,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
final List<Long> childs = new ArrayList<>(idList);
|
||||
// TODO: update to have get with abstract types ....
|
||||
@SuppressWarnings("unchecked")
|
||||
final Object foreignData = DataAccess.getsWhere(decorators.targetEntity(),
|
||||
final Object foreignData = ioDb.getsWhere(decorators.targetEntity(),
|
||||
new Condition(new QueryInList<>(idField, childs)));
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
@ -261,7 +268,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
lazyCall.add(lambda);
|
||||
}
|
||||
} else if (foreignKeyType == UUID.class) {
|
||||
final List<UUID> idList = DataAccess.getListOfRawUUIDs(rs, count.value);
|
||||
final List<UUID> idList = ioDb.getListOfRawUUIDs(rs, count.value);
|
||||
// field.set(data, idList);
|
||||
count.inc();
|
||||
if (idList != null && idList.size() > 0) {
|
||||
@ -271,7 +278,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
final List<UUID> childs = new ArrayList<>(idList);
|
||||
// TODO: update to have get with abstract types ....
|
||||
@SuppressWarnings("unchecked")
|
||||
final Object foreignData = DataAccess.getsWhere(decorators.targetEntity(),
|
||||
final Object foreignData = ioDb.getsWhere(decorators.targetEntity(),
|
||||
new Condition(new QueryInList<>(idField, childs)));
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
@ -291,6 +298,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
|
||||
@Override
|
||||
public void asyncUpdate(
|
||||
final DataAccessSQL ioDb,
|
||||
final String tableName,
|
||||
final Object localKey,
|
||||
final Field field,
|
||||
@ -312,30 +320,30 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
if (localKey instanceof final Long localKeyLong) {
|
||||
if (objectClass == Long.class) {
|
||||
actions.add(() -> {
|
||||
DataAccess.deleteWhere(LinkTableLongLong.class, new OverrideTableName(linkTableName),
|
||||
ioDb.deleteWhere(LinkTableLongLong.class, new OverrideTableName(linkTableName),
|
||||
new Condition(new QueryCondition("object1Id", "=", localKeyLong)));
|
||||
});
|
||||
asyncInsert(tableName, localKey, field, data, actions);
|
||||
asyncInsert(ioDb, tableName, localKey, field, data, actions);
|
||||
} else {
|
||||
actions.add(() -> {
|
||||
DataAccess.deleteWhere(LinkTableLongUUID.class, new OverrideTableName(linkTableName),
|
||||
ioDb.deleteWhere(LinkTableLongUUID.class, new OverrideTableName(linkTableName),
|
||||
new Condition(new QueryCondition("object1Id", "=", localKeyLong)));
|
||||
});
|
||||
asyncInsert(tableName, localKey, field, data, actions);
|
||||
asyncInsert(ioDb, tableName, localKey, field, data, actions);
|
||||
}
|
||||
} else if (localKey instanceof final UUID localKeyUUID) {
|
||||
if (objectClass == Long.class) {
|
||||
actions.add(() -> {
|
||||
DataAccess.deleteWhere(LinkTableUUIDLong.class, new OverrideTableName(linkTableName),
|
||||
ioDb.deleteWhere(LinkTableUUIDLong.class, new OverrideTableName(linkTableName),
|
||||
new Condition(new QueryCondition("object1Id", "=", localKeyUUID)));
|
||||
});
|
||||
asyncInsert(tableName, localKey, field, data, actions);
|
||||
asyncInsert(ioDb, tableName, localKey, field, data, actions);
|
||||
} else {
|
||||
actions.add(() -> {
|
||||
DataAccess.deleteWhere(LinkTableUUIDUUID.class, new OverrideTableName(linkTableName),
|
||||
ioDb.deleteWhere(LinkTableUUIDUUID.class, new OverrideTableName(linkTableName),
|
||||
new Condition(new QueryCondition("object1Id", "=", localKeyUUID)));
|
||||
});
|
||||
asyncInsert(tableName, localKey, field, data, actions);
|
||||
asyncInsert(ioDb, tableName, localKey, field, data, actions);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -347,6 +355,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
|
||||
@Override
|
||||
public void asyncInsert(
|
||||
final DataAccessSQL ioDb,
|
||||
final String tableName,
|
||||
final Object localKey,
|
||||
final Field field,
|
||||
@ -389,7 +398,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
return;
|
||||
}
|
||||
actions.add(() -> {
|
||||
DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName));
|
||||
ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName));
|
||||
});
|
||||
} else {
|
||||
// ========================================================
|
||||
@ -412,7 +421,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
return;
|
||||
}
|
||||
actions.add(() -> {
|
||||
DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName));
|
||||
ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName));
|
||||
});
|
||||
}
|
||||
} else if (localKey instanceof final UUID localKeyUUID) {
|
||||
@ -437,7 +446,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
return;
|
||||
}
|
||||
actions.add(() -> {
|
||||
DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName));
|
||||
ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName));
|
||||
});
|
||||
} else {
|
||||
// ========================================================
|
||||
@ -460,7 +469,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
return;
|
||||
}
|
||||
actions.add(() -> {
|
||||
DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName));
|
||||
ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName));
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@ -471,7 +480,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(final String tableName, final Field field) throws Exception {
|
||||
public void drop(final DataAccessSQL ioDb, final String tableName, final Field field) throws Exception {
|
||||
final String columnName = AnnotationTools.getFieldName(field);
|
||||
final String linkTableName = generateLinkTableName(tableName, columnName);
|
||||
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType())
|
||||
@ -480,11 +489,11 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
throw new DataAccessException("Can not ManyToMany with other than List<Long> or List<UUID> Model: List<"
|
||||
+ objectClass.getCanonicalName() + ">");
|
||||
}
|
||||
DataAccess.drop(LinkTableLongLong.class, new OverrideTableName(linkTableName));
|
||||
ioDb.drop(LinkTableLongLong.class, new OverrideTableName(linkTableName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanAll(final String tableName, final Field field) throws Exception {
|
||||
public void cleanAll(final DataAccessSQL ioDb, final String tableName, final Field field) throws Exception {
|
||||
final String columnName = AnnotationTools.getFieldName(field);
|
||||
final String linkTableName = generateLinkTableName(tableName, columnName);
|
||||
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType())
|
||||
@ -493,27 +502,47 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
throw new DataAccessException("Can not ManyToMany with other than List<Long> or List<UUID> Model: List<"
|
||||
+ objectClass.getCanonicalName() + ">");
|
||||
}
|
||||
DataAccess.cleanAll(LinkTableLongLong.class, new OverrideTableName(linkTableName));
|
||||
ioDb.cleanAll(LinkTableLongLong.class, new OverrideTableName(linkTableName));
|
||||
}
|
||||
|
||||
public static void addLink(final Class<?> clazz, final long localKey, final String column, final long remoteKey)
|
||||
throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final String linkTableName = generateLinkTableName(tableName, column);
|
||||
/* final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; if (objectClass != Long.class && objectClass != UUID.class) { throw new
|
||||
* DataAccessException("Can not ManyToMany with other than List<Long> or List<UUID> Model: List<" + objectClass.getCanonicalName() + ">"); } */
|
||||
final LinkTableLongLong insertElement = new LinkTableLongLong(localKey, remoteKey);
|
||||
DataAccess.insert(insertElement, new OverrideTableName(linkTableName));
|
||||
public static void addLink(
|
||||
final DataAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final long localKey,
|
||||
final String column,
|
||||
final long remoteKey) throws Exception {
|
||||
if (ioDb instanceof final DataAccessSQL daSQL) {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final String linkTableName = generateLinkTableName(tableName, column);
|
||||
/* final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; if (objectClass != Long.class && objectClass != UUID.class) { throw new
|
||||
* DataAccessException("Can not ManyToMany with other than List<Long> or List<UUID> Model: List<" + objectClass.getCanonicalName() + ">"); } */
|
||||
final LinkTableLongLong insertElement = new LinkTableLongLong(localKey, remoteKey);
|
||||
daSQL.insert(insertElement, new OverrideTableName(linkTableName));
|
||||
} else if (ioDb instanceof final DataAccessMorphia dam) {
|
||||
|
||||
} else {
|
||||
throw new DataAccessException("DataAccess Not managed");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static int removeLink(final Class<?> clazz, final long localKey, final String column, final long remoteKey)
|
||||
throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final String linkTableName = generateLinkTableName(tableName, column);
|
||||
return DataAccess.deleteWhere(LinkTableLongLong.class, new OverrideTableName(linkTableName),
|
||||
new Condition(new QueryAnd(new QueryCondition("object1Id", "=", localKey),
|
||||
new QueryCondition("object2Id", "=", remoteKey))));
|
||||
public static long removeLink(
|
||||
final DataAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final long localKey,
|
||||
final String column,
|
||||
final long remoteKey) throws Exception {
|
||||
if (ioDb instanceof final DataAccessSQL daSQL) {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final String linkTableName = generateLinkTableName(tableName, column);
|
||||
return daSQL.deleteWhere(LinkTableLongLong.class, new OverrideTableName(linkTableName),
|
||||
new Condition(new QueryAnd(new QueryCondition("object1Id", "=", localKey),
|
||||
new QueryCondition("object2Id", "=", remoteKey))));
|
||||
} else if (ioDb instanceof final DataAccessMorphia dam) {
|
||||
return 0L;
|
||||
} else {
|
||||
throw new DataAccessException("DataAccess Not managed");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -9,8 +9,8 @@ import java.util.UUID;
|
||||
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.LazyGetter;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
@ -48,8 +48,12 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii)
|
||||
throws Exception {
|
||||
public void insertData(
|
||||
final DataAccessSQL ioDb,
|
||||
final PreparedStatement ps,
|
||||
final Field field,
|
||||
final Object rootObject,
|
||||
final CountInOut iii) throws Exception {
|
||||
final Object data = field.get(rootObject);
|
||||
if (data == null) {
|
||||
if (field.getType() == Long.class) {
|
||||
@ -149,7 +153,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
if (field.getType() == decorators.targetEntity()) {
|
||||
if (decorators.fetch() == FetchType.EAGER) {
|
||||
// TODO: rework this to have a lazy mode ...
|
||||
DataAccess.generateSelectField(querySelect, query, field.getType(), options, count);
|
||||
DataAccessSQL.generateSelectField(querySelect, query, field.getType(), options, count);
|
||||
final Class<?> subType = field.getType();
|
||||
final String subTableName = AnnotationTools.getTableName(subType);
|
||||
final Field idField = AnnotationTools.getFieldOfId(subType);
|
||||
@ -178,6 +182,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
|
||||
@Override
|
||||
public void fillFromQuery(
|
||||
final DataAccessSQL ioDb,
|
||||
final ResultSet rs,
|
||||
final Field field,
|
||||
final Object data,
|
||||
@ -233,8 +238,8 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
if (objectClass == decorators.targetEntity()) {
|
||||
if (decorators.fetch() == FetchType.EAGER) {
|
||||
final CountInOut countNotNull = new CountInOut(0);
|
||||
final Object dataNew = DataAccess.createObjectFromSQLRequest(rs, objectClass, count, countNotNull,
|
||||
options, lazyCall);
|
||||
final Object dataNew = ioDb.createObjectFromSQLRequest(rs, objectClass, count, countNotNull, options,
|
||||
lazyCall);
|
||||
if (dataNew != null && countNotNull.value != 0) {
|
||||
field.set(data, dataNew);
|
||||
}
|
||||
@ -250,7 +255,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
|
||||
final LazyGetter lambda = () -> {
|
||||
// TODO: update to have get with abstract types ....
|
||||
final Object foreignData = DataAccess.get(decorators.targetEntity(), foreignKey);
|
||||
final Object foreignData = ioDb.get(decorators.targetEntity(), foreignKey);
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
}
|
||||
@ -260,13 +265,13 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
}
|
||||
} else if (remotePrimaryKeyType == UUID.class) {
|
||||
// here we have the field, the data and the the remote value ==> can create callback that generate the update of the value ...
|
||||
final UUID foreignKey = DataAccess.getListOfRawUUID(rs, count.value);
|
||||
final UUID foreignKey = ioDb.getListOfRawUUID(rs, count.value);
|
||||
count.inc();
|
||||
if (foreignKey != null) {
|
||||
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
|
||||
final LazyGetter lambda = () -> {
|
||||
// TODO: update to have get with abstract types ....
|
||||
final Object foreignData = DataAccess.get(decorators.targetEntity(), foreignKey);
|
||||
final Object foreignData = ioDb.get(decorators.targetEntity(), foreignKey);
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.LazyGetter;
|
||||
import org.kar.archidata.dataAccess.QueryCondition;
|
||||
@ -83,8 +83,12 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii)
|
||||
throws SQLException, IllegalArgumentException, IllegalAccessException {
|
||||
public void insertData(
|
||||
final DataAccessSQL ioDb,
|
||||
final PreparedStatement ps,
|
||||
final Field field,
|
||||
final Object rootObject,
|
||||
final CountInOut iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
||||
throw new IllegalAccessException("Can not generate an inset of @OneToMany");
|
||||
}
|
||||
|
||||
@ -219,6 +223,7 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
|
||||
@Override
|
||||
public void fillFromQuery(
|
||||
final DataAccessSQL ioDb,
|
||||
final ResultSet rs,
|
||||
final Field field,
|
||||
final Object data,
|
||||
@ -236,12 +241,12 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
return;
|
||||
}
|
||||
if (objectClass == Long.class) {
|
||||
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR_LONG);
|
||||
final List<Long> idList = ioDb.getListOfIds(rs, count.value, SEPARATOR_LONG);
|
||||
field.set(data, idList);
|
||||
count.inc();
|
||||
return;
|
||||
} else if (objectClass == UUID.class) {
|
||||
final List<UUID> idList = DataAccess.getListOfRawUUIDs(rs, count.value);
|
||||
final List<UUID> idList = ioDb.getListOfRawUUIDs(rs, count.value);
|
||||
field.set(data, idList);
|
||||
count.inc();
|
||||
return;
|
||||
@ -255,7 +260,7 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
parentIdTmp = Long.valueOf(modelData);
|
||||
count.inc();
|
||||
} catch (final NumberFormatException ex) {
|
||||
final List<UUID> idList = DataAccess.getListOfRawUUIDs(rs, count.value);
|
||||
final List<UUID> idList = ioDb.getListOfRawUUIDs(rs, count.value);
|
||||
parendUuidTmp = idList.get(0);
|
||||
count.inc();
|
||||
}
|
||||
@ -279,7 +284,7 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
|
||||
final LazyGetter lambda = () -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
final Object foreignData = DataAccess.getsWhere(decorators.targetEntity(),
|
||||
final Object foreignData = ioDb.getsWhere(decorators.targetEntity(),
|
||||
new Condition(new QueryCondition(mappingKey, "=", parentId)));
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
@ -290,7 +295,7 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
} else if (parendUuid != null) {
|
||||
final LazyGetter lambda = () -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
final Object foreignData = DataAccess.getsWhere(decorators.targetEntity(),
|
||||
final Object foreignData = ioDb.getsWhere(decorators.targetEntity(),
|
||||
new Condition(new QueryCondition(mappingKey, "=", parendUuid)));
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
|
@ -3,6 +3,7 @@ package org.kar.archidata.dataAccess.options;
|
||||
import java.util.List;
|
||||
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
|
||||
/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */
|
||||
@ -12,11 +13,16 @@ public interface CheckFunctionInterface {
|
||||
* @param data The object that might be injected.
|
||||
* @param filterValue List of fields that might be check. If null, then all column must be checked.
|
||||
* @throws Exception Exception is generate if the data are incorrect. */
|
||||
void check(final String baseName, Object data, List<String> filterValue, final QueryOptions options)
|
||||
throws Exception;
|
||||
void check(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
Object data,
|
||||
List<String> filterValue,
|
||||
final QueryOptions options) throws Exception;
|
||||
|
||||
default void checkAll(final String baseName, final Object data, final QueryOptions options) throws Exception {
|
||||
check(baseName, data, AnnotationTools.getAllFieldsNames(data.getClass()), options);
|
||||
default void checkAll(final DataAccess ioDb, final String baseName, final Object data, final QueryOptions options)
|
||||
throws Exception {
|
||||
check(ioDb, baseName, data, AnnotationTools.getAllFieldsNames(data.getClass()), options);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,12 +2,14 @@ package org.kar.archidata.dataAccess.options;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
|
||||
/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */
|
||||
public class CheckFunctionVoid implements CheckFunctionInterface {
|
||||
@Override
|
||||
public void check(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final Object data,
|
||||
final List<String> filterValue,
|
||||
|
@ -28,21 +28,21 @@ import jakarta.persistence.ManyToOne;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CheckJPA.class);
|
||||
private final Class<?> clazz;
|
||||
|
||||
|
||||
/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */
|
||||
public interface CheckInterface<K> {
|
||||
/** This function implementation is design to check if the updated class is valid of not for insertion
|
||||
* @param data The object that might be injected.
|
||||
* @param filterValue List of fields that might be check. If null, then all column must be checked.
|
||||
* @throws Exception Exception is generate if the data are incorrect. */
|
||||
void check(final String baseName, final K data, final QueryOptions options) throws Exception;
|
||||
void check(DataAccess ioDb, final String baseName, final K data, final QueryOptions options) throws Exception;
|
||||
}
|
||||
|
||||
|
||||
protected Map<String, List<CheckInterface<T>>> checking = null;
|
||||
|
||||
|
||||
protected void add(final String field, final CheckInterface<T> checkFunction) {
|
||||
List<CheckInterface<T>> actions = this.checking.get(field);
|
||||
if (actions == null) {
|
||||
@ -51,11 +51,11 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
}
|
||||
actions.add(checkFunction);
|
||||
}
|
||||
|
||||
|
||||
public CheckJPA(final Class<T> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
|
||||
public void initialize() throws Exception {
|
||||
if (this.checking != null) {
|
||||
return;
|
||||
@ -67,243 +67,336 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
for (final Field field : this.clazz.getFields()) {
|
||||
final String fieldName = field.getName(); // AnnotationTools.getFieldName(field);
|
||||
if (AnnotationTools.isPrimaryKey(field)) {
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"This is a '@Id' (primaryKey) ==> can not be change");
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"This is a '@Id' (primaryKey) ==> can not be change");
|
||||
});
|
||||
}
|
||||
if (AnnotationTools.getConstraintsNotNull(field)) {
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
if (field.get(data) == null) {
|
||||
throw new InputException(baseName + fieldName, "Can not be null");
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
if (field.get(data) == null) {
|
||||
throw new InputException(baseName + fieldName, "Can not be null");
|
||||
}
|
||||
});
|
||||
}
|
||||
if (AnnotationTools.isCreatedAtField(field) || AnnotationTools.isUpdateAtField(field)) {
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
throw new InputException(baseName + fieldName, "It is forbidden to change this field");
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
throw new InputException(baseName + fieldName, "It is forbidden to change this field");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
final Class<?> type = field.getType();
|
||||
if (type == Long.class || type == long.class) {
|
||||
final Long maxValue = AnnotationTools.getConstraintsMax(field);
|
||||
if (maxValue != null) {
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Long elemTyped = (Long) elem;
|
||||
if (elemTyped > maxValue) {
|
||||
throw new InputException(baseName + fieldName, "Value too height max: " + maxValue);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Long elemTyped = (Long) elem;
|
||||
if (elemTyped > maxValue) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Value too height max: " + maxValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
final Long minValue = AnnotationTools.getConstraintsMin(field);
|
||||
if (minValue != null) {
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Long elemTyped = (Long) elem;
|
||||
if (elemTyped < minValue) {
|
||||
throw new InputException(baseName + fieldName, "Value too Low min: " + minValue);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Long elemTyped = (Long) elem;
|
||||
if (elemTyped < minValue) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Value too Low min: " + minValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field);
|
||||
if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) {
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final List<ConditionChecker> condCheckers = options.get(ConditionChecker.class);
|
||||
final Condition conditionCheck = condCheckers.isEmpty() ? null
|
||||
: condCheckers.get(0).toCondition();
|
||||
final long count = DataAccess.count(annotationManyToOne.targetEntity(), elem,
|
||||
conditionCheck);
|
||||
if (count == 0) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Foreign element does not exist in the DB:" + elem);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final List<ConditionChecker> condCheckers = options.get(ConditionChecker.class);
|
||||
final Condition conditionCheck = condCheckers.isEmpty() ? null
|
||||
: condCheckers.get(0).toCondition();
|
||||
final long count = ioDb.count(annotationManyToOne.targetEntity(), elem,
|
||||
conditionCheck);
|
||||
if (count == 0) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Foreign element does not exist in the DB:" + elem);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
} else if (type == Integer.class || type == int.class) {
|
||||
final Long maxValueRoot = AnnotationTools.getConstraintsMax(field);
|
||||
if (maxValueRoot != null) {
|
||||
final int maxValue = maxValueRoot.intValue();
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Integer elemTyped = (Integer) elem;
|
||||
if (elemTyped > maxValue) {
|
||||
throw new InputException(baseName + fieldName, "Value too height max: " + maxValue);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Integer elemTyped = (Integer) elem;
|
||||
if (elemTyped > maxValue) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Value too height max: " + maxValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
final Long minValueRoot = AnnotationTools.getConstraintsMin(field);
|
||||
if (minValueRoot != null) {
|
||||
final int minValue = minValueRoot.intValue();
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Integer elemTyped = (Integer) elem;
|
||||
if (elemTyped < minValue) {
|
||||
throw new InputException(baseName + fieldName, "Value too Low min: " + minValue);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Integer elemTyped = (Integer) elem;
|
||||
if (elemTyped < minValue) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Value too Low min: " + minValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field);
|
||||
if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) {
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final long count = DataAccess.count(annotationManyToOne.targetEntity(), elem);
|
||||
if (count == 0) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Foreign element does not exist in the DB:" + elem);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final long count = ioDb.count(annotationManyToOne.targetEntity(), elem);
|
||||
if (count == 0) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Foreign element does not exist in the DB:" + elem);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (type == UUID.class) {
|
||||
final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field);
|
||||
if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) {
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final long count = DataAccess.count(annotationManyToOne.targetEntity(), elem);
|
||||
if (count == 0) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Foreign element does not exist in the DB:" + elem);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final long count = ioDb.count(annotationManyToOne.targetEntity(), elem);
|
||||
if (count == 0) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Foreign element does not exist in the DB:" + elem);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (type == Boolean.class || type == boolean.class) {
|
||||
|
||||
|
||||
} else if (type == Float.class || type == float.class) {
|
||||
final Long maxValueRoot = AnnotationTools.getConstraintsMax(field);
|
||||
if (maxValueRoot != null) {
|
||||
final float maxValue = maxValueRoot.floatValue();
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Float elemTyped = (Float) elem;
|
||||
if (elemTyped > maxValue) {
|
||||
throw new InputException(baseName + fieldName, "Value too height max: " + maxValue);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Float elemTyped = (Float) elem;
|
||||
if (elemTyped > maxValue) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Value too height max: " + maxValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
final Long minValueRoot = AnnotationTools.getConstraintsMin(field);
|
||||
if (minValueRoot != null) {
|
||||
final float minValue = minValueRoot.floatValue();
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Float elemTyped = (Float) elem;
|
||||
if (elemTyped < minValue) {
|
||||
throw new InputException(baseName + fieldName, "Value too Low min: " + minValue);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Float elemTyped = (Float) elem;
|
||||
if (elemTyped < minValue) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Value too Low min: " + minValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (type == Double.class || type == double.class) {
|
||||
final Long maxValueRoot = AnnotationTools.getConstraintsMax(field);
|
||||
if (maxValueRoot != null) {
|
||||
final double maxValue = maxValueRoot.doubleValue();
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Double elemTyped = (Double) elem;
|
||||
if (elemTyped > maxValue) {
|
||||
throw new InputException(baseName + fieldName, "Value too height max: " + maxValue);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Double elemTyped = (Double) elem;
|
||||
if (elemTyped > maxValue) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Value too height max: " + maxValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
final Long minValueRoot = AnnotationTools.getConstraintsMin(field);
|
||||
if (minValueRoot != null) {
|
||||
final double minValue = minValueRoot.doubleValue();
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Double elemTyped = (Double) elem;
|
||||
if (elemTyped < minValue) {
|
||||
throw new InputException(baseName + fieldName, "Value too Low min: " + minValue);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final Double elemTyped = (Double) elem;
|
||||
if (elemTyped < minValue) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Value too Low min: " + minValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (type == Date.class || type == Timestamp.class) {
|
||||
|
||||
|
||||
} else if (type == LocalDate.class) {
|
||||
|
||||
|
||||
} else if (type == LocalTime.class) {
|
||||
|
||||
|
||||
} else if (type == String.class) {
|
||||
final int maxSizeString = AnnotationTools.getLimitSize(field);
|
||||
if (maxSizeString > 0) {
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final String elemTyped = (String) elem;
|
||||
if (elemTyped.length() > maxSizeString) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Too long size must be <= " + maxSizeString);
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final String elemTyped = (String) elem;
|
||||
if (elemTyped.length() > maxSizeString) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Too long size must be <= " + maxSizeString);
|
||||
}
|
||||
});
|
||||
}
|
||||
final Size limitSize = AnnotationTools.getConstraintsSize(field);
|
||||
if (limitSize != null) {
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final String elemTyped = (String) elem;
|
||||
if (elemTyped.length() > limitSize.max()) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Too long size (constraints) must be <= " + limitSize.max());
|
||||
}
|
||||
if (elemTyped.length() < limitSize.min()) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Too small size (constraints) must be >= " + limitSize.min());
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final String elemTyped = (String) elem;
|
||||
if (elemTyped.length() > limitSize.max()) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Too long size (constraints) must be <= " + limitSize.max());
|
||||
}
|
||||
if (elemTyped.length() < limitSize.min()) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"Too small size (constraints) must be >= " + limitSize.min());
|
||||
}
|
||||
});
|
||||
}
|
||||
final String patternString = AnnotationTools.getConstraintsPattern(field);
|
||||
if (patternString != null) {
|
||||
final Pattern pattern = Pattern.compile(patternString);
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final String elemTyped = (String) elem;
|
||||
if (!pattern.matcher(elemTyped).find()) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"does not match the required pattern (constraints) must be '" + patternString
|
||||
+ "'");
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final String elemTyped = (String) elem;
|
||||
if (!pattern.matcher(elemTyped).find()) {
|
||||
throw new InputException(baseName + fieldName,
|
||||
"does not match the required pattern (constraints) must be '"
|
||||
+ patternString + "'");
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (type == JsonValue.class) {
|
||||
final DataJson jsonAnnotation = AnnotationTools.getDataJson(field);
|
||||
@ -311,9 +404,14 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
// Here if we have an error it crash at start and no new instance after creation...
|
||||
final CheckFunctionInterface instance = jsonAnnotation.checker().getDeclaredConstructor()
|
||||
.newInstance();
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
instance.checkAll(baseName + fieldName + ".", field.get(data), options);
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
instance.checkAll(ioDb, baseName + fieldName + ".", field.get(data), options);
|
||||
});
|
||||
}
|
||||
} else if (type.isEnum()) {
|
||||
// nothing to do.
|
||||
@ -321,40 +419,47 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
// keep this is last ==> take more time...
|
||||
if (AnnotationTools.isUnique(field)) {
|
||||
// Create the request ...
|
||||
add(fieldName, (final String baseName, final T data, final QueryOptions options) -> {
|
||||
final List<ConditionChecker> condCheckers = options.get(ConditionChecker.class);
|
||||
Object other = null;
|
||||
if (condCheckers.isEmpty()) {
|
||||
other = DataAccess.getWhere(this.clazz,
|
||||
new Condition(new QueryCondition(fieldName, "==", field.get(data))));
|
||||
} else {
|
||||
other = DataAccess.getWhere(this.clazz,
|
||||
new Condition(new QueryCondition(fieldName, "==", field.get(data))),
|
||||
condCheckers.get(0).toCondition());
|
||||
}
|
||||
if (other != null) {
|
||||
throw new InputException(baseName + fieldName, "Name already exist in the DB");
|
||||
}
|
||||
});
|
||||
add(fieldName,
|
||||
(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final QueryOptions options) -> {
|
||||
final List<ConditionChecker> condCheckers = options.get(ConditionChecker.class);
|
||||
Object other = null;
|
||||
if (condCheckers.isEmpty()) {
|
||||
other = ioDb.getWhere(this.clazz,
|
||||
new Condition(new QueryCondition(fieldName, "==", field.get(data))));
|
||||
} else {
|
||||
other = ioDb.getWhere(this.clazz,
|
||||
new Condition(new QueryCondition(fieldName, "==", field.get(data))),
|
||||
condCheckers.get(0).toCondition());
|
||||
}
|
||||
if (other != null) {
|
||||
throw new InputException(baseName + fieldName, "Name already exist in the DB");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (final Exception ex) {
|
||||
this.checking = null;
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public void check(final String baseName, final Object data) throws Exception {
|
||||
check(baseName, data, null, null);
|
||||
|
||||
public void check(final DataAccess ioDb, final String baseName, final Object data) throws Exception {
|
||||
check(ioDb, baseName, data, null, null);
|
||||
}
|
||||
|
||||
public void check(final String baseName, final Object data, final List<String> filterValue) throws Exception {
|
||||
check(baseName, data, filterValue, null);
|
||||
|
||||
public void check(final DataAccess ioDb, final String baseName, final Object data, final List<String> filterValue)
|
||||
throws Exception {
|
||||
check(ioDb, baseName, data, filterValue, null);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void check(
|
||||
final DataAccess ioDb,
|
||||
final String baseName,
|
||||
final Object data,
|
||||
final List<String> filterValue,
|
||||
@ -373,12 +478,12 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
continue;
|
||||
}
|
||||
for (final CheckInterface<T> action : actions) {
|
||||
action.check(baseName, dataCasted, options);
|
||||
action.check(ioDb, baseName, dataCasted, options);
|
||||
}
|
||||
}
|
||||
checkTyped(dataCasted, filterValue, options);
|
||||
}
|
||||
|
||||
|
||||
public void checkTyped(final T data, final List<String> filterValue, final QueryOptions options) throws Exception {
|
||||
// nothing to do ...
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import java.util.List;
|
||||
|
||||
import org.bson.conversions.Bson;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.QueryItem;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
|
||||
@ -14,27 +15,28 @@ import com.mongodb.client.model.Filters;
|
||||
/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */
|
||||
public class Condition extends QueryOption {
|
||||
public final QueryItem condition;
|
||||
|
||||
|
||||
public Condition(final QueryItem items) {
|
||||
this.condition = items;
|
||||
}
|
||||
|
||||
|
||||
public Condition() {
|
||||
this.condition = null;
|
||||
}
|
||||
|
||||
|
||||
public void generateQuery(final StringBuilder query, final String tableName) {
|
||||
if (this.condition != null) {
|
||||
this.condition.generateQuery(query, tableName);
|
||||
}
|
||||
}
|
||||
|
||||
public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||
|
||||
public void injectQuery(final DataAccessSQL ioDb, final PreparedStatement ps, final CountInOut iii)
|
||||
throws Exception {
|
||||
if (this.condition != null) {
|
||||
this.condition.injectQuery(ps, iii);
|
||||
this.condition.injectQuery(ioDb, ps, iii);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void whereAppendQuery(
|
||||
final StringBuilder query,
|
||||
final String tableName,
|
||||
@ -67,7 +69,7 @@ public class Condition extends QueryOption {
|
||||
}
|
||||
query.append("\n");
|
||||
}
|
||||
|
||||
|
||||
public Bson getFilter(final String collectionName, final QueryOptions options, final String deletedFieldName) {
|
||||
boolean exclude_deleted = true;
|
||||
if (options != null) {
|
||||
|
@ -1,14 +1,8 @@
|
||||
package org.kar.archidata.dataAccess.options;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.kar.archidata.GlobalConfiguration;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
import org.kar.archidata.db.DBConfig;
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
|
||||
@Deprecated
|
||||
public class DBInterfaceOption extends QueryOption {
|
||||
/*
|
||||
private DBEntry entry = null;
|
||||
private final DBConfig config;
|
||||
private final boolean root;
|
||||
@ -47,5 +41,6 @@ public class DBInterfaceOption extends QueryOption {
|
||||
return dbOption.get(0).getEntry(options);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
@ -3,24 +3,25 @@ package org.kar.archidata.dataAccess.options;
|
||||
import java.sql.PreparedStatement;
|
||||
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
|
||||
public class Limit extends QueryOption {
|
||||
protected final long limit;
|
||||
|
||||
|
||||
public Limit(final long limit) {
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
|
||||
public void generateQuery(final StringBuilder query, final String tableName) {
|
||||
query.append(" LIMIT ? \n");
|
||||
}
|
||||
|
||||
public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||
DataAccess.addElement(ps, this.limit, iii);
|
||||
|
||||
public void injectQuery(final DataAccessSQL ioDb, final PreparedStatement ps, final CountInOut iii)
|
||||
throws Exception {
|
||||
ioDb.addElement(ps, this.limit, iii);
|
||||
iii.inc();
|
||||
}
|
||||
|
||||
|
||||
public long getValue() {
|
||||
return this.limit;
|
||||
}
|
||||
|
@ -9,15 +9,15 @@ import org.kar.archidata.dataAccess.options.OrderItem.Order;
|
||||
|
||||
public class OrderBy extends QueryOption {
|
||||
protected final List<OrderItem> childs;
|
||||
|
||||
|
||||
public OrderBy(final List<OrderItem> childs) {
|
||||
this.childs = childs;
|
||||
}
|
||||
|
||||
|
||||
public OrderBy(final OrderItem... childs) {
|
||||
this.childs = List.of(childs);
|
||||
}
|
||||
|
||||
|
||||
public void generateQuery(final StringBuilder query, final String tableName) {
|
||||
if (this.childs.size() == 0) {
|
||||
return;
|
||||
@ -38,11 +38,11 @@ public class OrderBy extends QueryOption {
|
||||
}
|
||||
query.append("\n");
|
||||
}
|
||||
|
||||
|
||||
public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||
// nothing to add.
|
||||
}
|
||||
|
||||
|
||||
public void generateSort(final Document data) {
|
||||
for (final OrderItem elem : this.childs) {
|
||||
data.append(elem.value, elem.order == Order.ASC ? 1 : -1);
|
||||
|
@ -43,7 +43,7 @@ public class DBConfig {
|
||||
this.password = password;
|
||||
this.dbName = dbName;
|
||||
this.keepConnected = keepConnected;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -57,6 +57,10 @@ public class DBConfig {
|
||||
return this.hostname;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
@ -92,7 +96,7 @@ public class DBConfig {
|
||||
return "jdbc:sqlite:" + this.hostname + ".db";
|
||||
}
|
||||
if ("mongo".equals(this.type)) {
|
||||
return "mongodb:" + getLogin() + ":" + getPassword() + "//" + this.hostname + ":" + this.port;
|
||||
return "mongodb://" + getLogin() + ":" + getPassword() + "@" + this.hostname + ":" + this.port;
|
||||
}
|
||||
if ("mysql".equals(this.type)) {
|
||||
if (isRoot) {
|
||||
|
95
src/org/kar/archidata/db/DBEntry.java
Normal file
95
src/org/kar/archidata/db/DBEntry.java
Normal file
@ -0,0 +1,95 @@
|
||||
package org.kar.archidata.db;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DBEntry implements Closeable {
|
||||
final static Logger LOGGER = LoggerFactory.getLogger(DBEntry.class);
|
||||
private final DBConfig config;
|
||||
private DbInterface ioDb;
|
||||
private static List<DBEntry> stored = new ArrayList<>();
|
||||
|
||||
private DBEntry(final DBConfig config, final boolean root, final Class<?>... classes) throws IOException {
|
||||
this.config = config;
|
||||
if (root) {
|
||||
connectRoot();
|
||||
} else {
|
||||
connect();
|
||||
}
|
||||
}
|
||||
|
||||
public static DBEntry createInterface(final DBConfig config) throws IOException {
|
||||
return createInterface(config, false);
|
||||
}
|
||||
|
||||
public static DBEntry createInterface(final DBConfig config, final boolean root) throws IOException {
|
||||
if (config.getKeepConnected()) {
|
||||
for (final DBEntry elem : stored) {
|
||||
if (elem == null) {
|
||||
continue;
|
||||
}
|
||||
if (elem.config.getUrl().equals(config.getUrl())) {
|
||||
return elem;
|
||||
}
|
||||
}
|
||||
final DBEntry tmp = new DBEntry(config, root);
|
||||
stored.add(tmp);
|
||||
return tmp;
|
||||
} else {
|
||||
return new DBEntry(config, root);
|
||||
}
|
||||
}
|
||||
|
||||
public void connectRoot() throws IOException {
|
||||
// TODO: maybe better check for root connection ...
|
||||
if ("mysql".equals(this.config.getType())) {
|
||||
this.ioDb = new DbInterfaceSQL(this.config);
|
||||
} else if ("sqlite".equals(this.config.getType())) {
|
||||
this.ioDb = new DbInterfaceSQL(this.config);
|
||||
} else if ("mongo".equals(this.config.getType())) {
|
||||
this.ioDb = new DbInterfaceMorphia(this.config);
|
||||
} else {
|
||||
throw new IOException("DB type: '" + this.config.getType() + "'is not managed");
|
||||
}
|
||||
}
|
||||
|
||||
public void connect() throws IOException {
|
||||
if ("mysql".equals(this.config.getType())) {
|
||||
this.ioDb = new DbInterfaceSQL(this.config);
|
||||
} else if ("sqlite".equals(this.config.getType())) {
|
||||
this.ioDb = new DbInterfaceSQL(this.config);
|
||||
} else if ("mongo".equals(this.config.getType())) {
|
||||
this.ioDb = new DbInterfaceMorphia(this.config);
|
||||
} else {
|
||||
throw new IOException("DB type: '" + this.config.getType() + "'is not managed");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (this.config.getKeepConnected()) {
|
||||
return;
|
||||
}
|
||||
closeForce();
|
||||
}
|
||||
|
||||
public void closeForce() throws IOException {
|
||||
this.ioDb.close();
|
||||
}
|
||||
|
||||
public DbInterface getDbInterface() {
|
||||
return this.ioDb;
|
||||
}
|
||||
|
||||
public static void closeAllForceMode() throws IOException {
|
||||
for (final DBEntry entry : stored) {
|
||||
entry.closeForce();
|
||||
}
|
||||
stored = new ArrayList<>();
|
||||
}
|
||||
}
|
@ -1,3 +1,16 @@
|
||||
package org.kar.archidata.db;
|
||||
|
||||
public class DbInterface {}
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DbInterface implements Closeable {
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(DbInterface.class);
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
LOGGER.error("Check db interface close implementation !!! " + this.getClass().getCanonicalName());
|
||||
}
|
||||
}
|
||||
|
@ -19,14 +19,18 @@ import dev.morphia.Datastore;
|
||||
import dev.morphia.Morphia;
|
||||
|
||||
public class DbInterfaceMorphia extends DbInterface implements Closeable {
|
||||
final static Logger LOGGER = LoggerFactory.getLogger(DbInterfaceMorphia.class);
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(DbInterfaceMorphia.class);
|
||||
private final MongoClient mongoClient;
|
||||
private final Datastore datastore;
|
||||
|
||||
|
||||
public DbInterfaceMorphia(final DBConfig config, final Class<?>... classes) throws IOException {
|
||||
this(config.getUrl(), config.getDbName(), classes);
|
||||
}
|
||||
|
||||
public DbInterfaceMorphia(final String dbUrl, final String dbName, final Class<?>... classes) {
|
||||
// Connect to MongoDB (simple form):
|
||||
// final MongoClient mongoClient = MongoClients.create(dbUrl);
|
||||
|
||||
LOGGER.info("Connect on the DB: {}", dbUrl);
|
||||
// Connect to MongoDB (complex form):
|
||||
final ConnectionString connectionString = new ConnectionString(dbUrl);
|
||||
// Créer un CodecRegistry pour UUID
|
||||
@ -37,7 +41,7 @@ public class DbInterfaceMorphia extends DbInterface implements Closeable {
|
||||
// Ajouter le CodecRegistry par défaut, le codec UUID et celui pour POJOs
|
||||
//final CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
|
||||
// MongoClientSettings.getDefaultCodecRegistry(), /*uuidCodecRegistry, */ pojoCodecRegistry);
|
||||
|
||||
|
||||
final CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
|
||||
MongoClientSettings.getDefaultCodecRegistry(),
|
||||
CodecRegistries.fromCodecs(new org.bson.codecs.UuidCodec(UuidRepresentation.STANDARD)),
|
||||
@ -55,11 +59,11 @@ public class DbInterfaceMorphia extends DbInterface implements Closeable {
|
||||
// Ensure indexes
|
||||
this.datastore.ensureIndexes();
|
||||
}
|
||||
|
||||
|
||||
public Datastore getDatastore() {
|
||||
return this.datastore;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.mongoClient.close();
|
||||
|
@ -11,13 +11,13 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DbInterfaceSQL extends DbInterface implements Closeable {
|
||||
final static Logger LOGGER = LoggerFactory.getLogger(DbInterfaceSQL.class);
|
||||
|
||||
private final Connection connection;
|
||||
|
||||
public DbInterfaceSQL(final DBConfig config, final String dbName, final Class<?>... classes) throws IOException {
|
||||
|
||||
private Connection connection = null;
|
||||
|
||||
public DbInterfaceSQL(final DBConfig config) throws IOException {
|
||||
this(config.getUrl(), config.getLogin(), config.getPassword());
|
||||
}
|
||||
|
||||
|
||||
public DbInterfaceSQL(final String dbUrl, final String login, final String password) throws IOException {
|
||||
try {
|
||||
this.connection = DriverManager.getConnection(dbUrl, login, password);
|
||||
@ -26,7 +26,7 @@ public class DbInterfaceSQL extends DbInterface implements Closeable {
|
||||
throw new IOException("Connection db fail: " + ex.getMessage() + " On URL: " + dbUrl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Connection getConnection() {
|
||||
return this.connection;
|
||||
}
|
||||
@ -35,6 +35,7 @@ public class DbInterfaceSQL extends DbInterface implements Closeable {
|
||||
public void close() throws IOException {
|
||||
try {
|
||||
this.connection.close();
|
||||
this.connection = null;
|
||||
} catch (final SQLException ex) {
|
||||
throw new IOException("Dis-connection db fail: " + ex.getMessage());
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package org.kar.archidata.migration;
|
||||
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
|
||||
public interface AsyncCall {
|
||||
void doRequest() throws Exception;
|
||||
void doRequest(DataAccess da) throws Exception;
|
||||
}
|
||||
|
@ -6,10 +6,10 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
import org.kar.archidata.db.DBConfig;
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
import org.kar.archidata.migration.model.Migration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -24,17 +24,20 @@ public class MigrationEngine {
|
||||
// initialization of the migration if the DB is not present...
|
||||
private MigrationInterface init;
|
||||
|
||||
protected final DataAccess da;
|
||||
|
||||
/** Migration engine constructor (empty). */
|
||||
public MigrationEngine() {
|
||||
this(new ArrayList<>(), null);
|
||||
public MigrationEngine(final DataAccess da) {
|
||||
this(da, new ArrayList<>(), null);
|
||||
}
|
||||
|
||||
/** Migration engine constructor (specific mode).
|
||||
* @param datas All the migration ordered.
|
||||
* @param init Initialization migration model. */
|
||||
public MigrationEngine(final List<MigrationInterface> datas, final MigrationInterface init) {
|
||||
public MigrationEngine(final DataAccess da, final List<MigrationInterface> datas, final MigrationInterface init) {
|
||||
this.datas = datas;
|
||||
this.init = init;
|
||||
this.da = da;
|
||||
}
|
||||
|
||||
/** Add a Migration in the list
|
||||
@ -53,16 +56,16 @@ public class MigrationEngine {
|
||||
* @return Model represent the last migration. If null then no migration has been done.
|
||||
* @throws MigrationException */
|
||||
public Migration getCurrentVersion() throws MigrationException {
|
||||
if (!DataAccess.isTableExist("KAR_migration")) {
|
||||
if (!this.da.isTableExist("KAR_migration")) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
List<Migration> data = null;
|
||||
try {
|
||||
data = DataAccess.gets(Migration.class, QueryOptions.READ_ALL_COLOMN);
|
||||
data = this.da.gets(Migration.class, QueryOptions.READ_ALL_COLOMN);
|
||||
} catch (final Exception e) {
|
||||
// Previous version does not have the same timeCode...
|
||||
data = DataAccess.gets(Migration.class);
|
||||
data = this.da.gets(Migration.class);
|
||||
}
|
||||
if (data == null) {
|
||||
LOGGER.error("Can not collect the migration table in the DB:{}");
|
||||
@ -135,13 +138,13 @@ public class MigrationEngine {
|
||||
|
||||
// STEP 1: Check the DB exist:
|
||||
LOGGER.info("Verify existance of '{}'", config.getDbName());
|
||||
boolean exist = DataAccess.isDBExist(config.getDbName());
|
||||
boolean exist = this.da.isDBExist(config.getDbName());
|
||||
if (!exist) {
|
||||
LOGGER.warn("DB: '{}' DOES NOT EXIST ==> create one", config.getDbName());
|
||||
// create the local DB:
|
||||
DataAccess.createDB(config.getDbName());
|
||||
this.da.createDB(config.getDbName());
|
||||
}
|
||||
exist = DataAccess.isDBExist(config.getDbName());
|
||||
exist = this.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...");
|
||||
@ -151,31 +154,32 @@ public class MigrationEngine {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
exist = DataAccess.isDBExist(config.getDbName());
|
||||
exist = this.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");
|
||||
// TODO: set the class in parameters instead of string...
|
||||
exist = DataAccess.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 {
|
||||
DataAccess.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");
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
exist = this.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();
|
||||
@ -217,18 +221,10 @@ public class MigrationEngine {
|
||||
}
|
||||
}
|
||||
}
|
||||
DBEntry entry;
|
||||
try {
|
||||
entry = DBEntry.createInterface(config);
|
||||
final int id = 0;
|
||||
final int count = toApply.size();
|
||||
for (final MigrationInterface elem : toApply) {
|
||||
migrateSingle(entry, elem, id, count);
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new MigrationException("An error occured in the migration (can not access to the DB): '"
|
||||
+ currentVersion.name + "' defect @" + currentVersion.stepId + "/" + currentVersion.count);
|
||||
final int id = 0;
|
||||
final int count = toApply.size();
|
||||
for (final MigrationInterface elem : toApply) {
|
||||
migrateSingle(elem, id, count);
|
||||
}
|
||||
if (needPlaceholder) {
|
||||
if (this.datas.size() == 0) {
|
||||
@ -244,7 +240,7 @@ public class MigrationEngine {
|
||||
migrationResult.count = 0;
|
||||
migrationResult.log = "Place-holder for first initialization";
|
||||
try {
|
||||
migrationResult = DataAccess.insert(migrationResult);
|
||||
migrationResult = this.da.insert(migrationResult);
|
||||
} catch (final Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
@ -254,8 +250,7 @@ public class MigrationEngine {
|
||||
LOGGER.info("Execute migration ... [ END ]");
|
||||
}
|
||||
|
||||
public void migrateSingle(final DBEntry entry, final MigrationInterface elem, final int id, final int count)
|
||||
throws MigrationException {
|
||||
public void migrateSingle(final MigrationInterface elem, final int id, final int count) throws MigrationException {
|
||||
LOGGER.info("---------------------------------------------------------");
|
||||
LOGGER.info("-- Migrate: [{}/{}] {} [BEGIN]", id, count, elem.getName());
|
||||
LOGGER.info("---------------------------------------------------------");
|
||||
@ -274,7 +269,7 @@ public class MigrationEngine {
|
||||
}
|
||||
migrationResult.log = log.toString();
|
||||
try {
|
||||
migrationResult = DataAccess.insert(migrationResult);
|
||||
migrationResult = this.da.insert(migrationResult);
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new MigrationException(
|
||||
@ -282,7 +277,7 @@ public class MigrationEngine {
|
||||
}
|
||||
boolean ret = true;
|
||||
try {
|
||||
ret = elem.applyMigration(entry, log, migrationResult);
|
||||
ret = elem.applyMigration(this.da, log, migrationResult);
|
||||
} catch (final Exception e) {
|
||||
log.append("\nFail in the migration apply ");
|
||||
log.append(e.getLocalizedMessage());
|
||||
@ -293,7 +288,7 @@ public class MigrationEngine {
|
||||
if (ret) {
|
||||
migrationResult.terminated = true;
|
||||
try {
|
||||
DataAccess.update(migrationResult, migrationResult.id, List.of("terminated"));
|
||||
this.da.update(migrationResult, migrationResult.id, List.of("terminated"));
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new MigrationException(
|
||||
@ -303,7 +298,7 @@ public class MigrationEngine {
|
||||
try {
|
||||
log.append("Fail in the migration engine...");
|
||||
migrationResult.log = log.toString();
|
||||
DataAccess.update(migrationResult, migrationResult.id, List.of("log"));
|
||||
this.da.update(migrationResult, migrationResult.id, List.of("log"));
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new MigrationException("Fail to update migration Log in the migration table: "
|
||||
@ -316,7 +311,7 @@ public class MigrationEngine {
|
||||
LOGGER.info("Migrate: [{}/{}] {} [ END ]", id, count, elem.getName());
|
||||
}
|
||||
|
||||
public void revertTo(final DBEntry entry, final String migrationName) throws MigrationException {
|
||||
public void revertTo(final String migrationName) throws MigrationException {
|
||||
final Migration currentVersion = getCurrentVersion();
|
||||
final List<MigrationInterface> toApply = new ArrayList<>();
|
||||
boolean find = false;
|
||||
@ -335,11 +330,11 @@ public class MigrationEngine {
|
||||
final int id = 0;
|
||||
final int count = toApply.size();
|
||||
for (final MigrationInterface elem : toApply) {
|
||||
revertSingle(entry, elem, id, count);
|
||||
revertSingle(elem, id, count);
|
||||
}
|
||||
}
|
||||
|
||||
public void revertSingle(final DBEntry entry, final MigrationInterface elem, final int id, final int count) {
|
||||
public void revertSingle(final MigrationInterface elem, final int id, final int count) {
|
||||
LOGGER.info("Revert migration: {} [BEGIN]", elem.getName());
|
||||
|
||||
LOGGER.info("Revert migration: {} [ END ]", elem.getName());
|
||||
|
@ -1,6 +1,6 @@
|
||||
package org.kar.archidata.migration;
|
||||
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.migration.model.Migration;
|
||||
|
||||
public interface MigrationInterface {
|
||||
@ -13,13 +13,13 @@ public interface MigrationInterface {
|
||||
* @param log Stored data in the BDD for the migration progression.
|
||||
* @param migration Migration post data on each step...
|
||||
* @return true if migration is finished. */
|
||||
boolean applyMigration(DBEntry entry, StringBuilder log, Migration model) throws Exception;
|
||||
boolean applyMigration(DataAccess entry, StringBuilder log, Migration model) throws Exception;
|
||||
|
||||
/** Remove a migration the system to the previous version.
|
||||
* @param entry DB interface for the migration.
|
||||
* @param log Stored data in the BDD for the migration progression.
|
||||
* @return true if migration is finished. */
|
||||
boolean revertMigration(DBEntry entry, StringBuilder log) throws Exception;
|
||||
boolean revertMigration(DataAccess entry, StringBuilder log) throws Exception;
|
||||
|
||||
/** Get the number of step in the migration process.
|
||||
* @return count of SQL access. */
|
||||
|
@ -6,8 +6,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
import org.kar.archidata.migration.model.Migration;
|
||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||
import org.slf4j.Logger;
|
||||
@ -70,7 +70,7 @@ public class MigrationSqlStep implements MigrationInterface {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean applyMigration(final DBEntry entry, final StringBuilder log, final Migration model)
|
||||
public boolean applyMigration(final DataAccess da, final StringBuilder log, final Migration model)
|
||||
throws Exception {
|
||||
if (!this.isGenerated) {
|
||||
this.isGenerated = true;
|
||||
@ -106,9 +106,11 @@ public class MigrationSqlStep implements MigrationInterface {
|
||||
}
|
||||
try {
|
||||
if (action.action() != null) {
|
||||
DataAccess.executeQuery(action.action());
|
||||
if (da instanceof final DataAccessSQL ioDBSQL) {
|
||||
ioDBSQL.executeQuery(action.action());
|
||||
}
|
||||
} else {
|
||||
action.async().doRequest();
|
||||
action.async().doRequest(da);
|
||||
}
|
||||
} catch (SQLException | IOException ex) {
|
||||
ex.printStackTrace();
|
||||
@ -117,7 +119,7 @@ public class MigrationSqlStep implements MigrationInterface {
|
||||
model.stepId = iii + 1;
|
||||
model.log = log.toString();
|
||||
try {
|
||||
DataAccess.update(model, model.id, List.of("stepId", "log"));
|
||||
da.update(model, model.id, List.of("stepId", "log"));
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -128,7 +130,7 @@ public class MigrationSqlStep implements MigrationInterface {
|
||||
model.stepId = iii + 1;
|
||||
model.log = log.toString();
|
||||
try {
|
||||
DataAccess.update(model, model.id, List.of("stepId", "log"));
|
||||
da.update(model, model.id, List.of("stepId", "log"));
|
||||
} catch (final Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
@ -143,7 +145,7 @@ public class MigrationSqlStep implements MigrationInterface {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean revertMigration(final DBEntry entry, final StringBuilder log) throws Exception {
|
||||
public boolean revertMigration(final DataAccess da, final StringBuilder log) throws Exception {
|
||||
generateRevertStep();
|
||||
return false;
|
||||
}
|
||||
|
@ -72,6 +72,9 @@ public class ConfigBaseVariable {
|
||||
|
||||
public static String getDBPort() {
|
||||
if (dbPort == null) {
|
||||
if (getDBType().equals("mongo")) {
|
||||
return "27017";
|
||||
}
|
||||
return "3306";
|
||||
}
|
||||
return dbPort;
|
||||
|
@ -77,9 +77,9 @@ public class DataTools {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public static Data getWithSha512(final String sha512) {
|
||||
public static Data getWithSha512(final DataAccess ioDb, final String sha512) {
|
||||
try {
|
||||
return DataAccess.getWhere(Data.class, new Condition(new QueryCondition("sha512", "=", sha512)),
|
||||
return ioDb.getWhere(Data.class, new Condition(new QueryCondition("sha512", "=", sha512)),
|
||||
new ReadAllColumn());
|
||||
} catch (final Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
@ -88,9 +88,9 @@ public class DataTools {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Data getWithId(final long id) {
|
||||
public static Data getWithId(final DataAccess ioDb, final long id) {
|
||||
try {
|
||||
return DataAccess.getWhere(Data.class, new Condition(new QueryAnd(
|
||||
return ioDb.getWhere(Data.class, new Condition(new QueryAnd(
|
||||
List.of(new QueryCondition("deleted", "=", false), new QueryCondition("id", "=", id)))));
|
||||
} catch (final Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
@ -100,6 +100,7 @@ public class DataTools {
|
||||
}
|
||||
|
||||
public static Data createNewData(
|
||||
final DataAccess ioDb,
|
||||
final long tmpUID,
|
||||
final String originalFileName,
|
||||
final String sha512,
|
||||
@ -113,7 +114,7 @@ public class DataTools {
|
||||
out.sha512 = sha512;
|
||||
out.mimeType = mimeType;
|
||||
out.size = fileSize;
|
||||
out = DataAccess.insert(out);
|
||||
out = ioDb.insert(out);
|
||||
} catch (final Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
@ -130,8 +131,11 @@ public class DataTools {
|
||||
return out;
|
||||
}
|
||||
|
||||
public static Data createNewData(final long tmpUID, final String originalFileName, final String sha512)
|
||||
throws IOException, SQLException {
|
||||
public static Data createNewData(
|
||||
final DataAccess ioDb,
|
||||
final long tmpUID,
|
||||
final String originalFileName,
|
||||
final String sha512) throws IOException, SQLException {
|
||||
// determine mime type:
|
||||
String mimeType = "";
|
||||
final String extension = originalFileName.substring(originalFileName.lastIndexOf('.') + 1);
|
||||
@ -144,12 +148,12 @@ public class DataTools {
|
||||
case "webm" -> "video/webm";
|
||||
default -> throw new IOException("Can not find the mime type of data input: '" + extension + "'");
|
||||
};
|
||||
return createNewData(tmpUID, originalFileName, sha512, mimeType);
|
||||
return createNewData(ioDb, tmpUID, originalFileName, sha512, mimeType);
|
||||
}
|
||||
|
||||
public static void undelete(final UUID id) {
|
||||
public static void undelete(final DataAccess ioDb, final UUID id) {
|
||||
try {
|
||||
DataAccess.unsetDelete(Data.class, id);
|
||||
ioDb.unsetDelete(Data.class, id);
|
||||
} catch (final Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
@ -272,13 +276,14 @@ public class DataTools {
|
||||
}
|
||||
|
||||
public static <CLASS_TYPE, ID_TYPE> void uploadCoverFromUri(
|
||||
final DataAccess ioDb,
|
||||
final Class<CLASS_TYPE> clazz,
|
||||
final ID_TYPE id,
|
||||
final String url) throws Exception {
|
||||
|
||||
LOGGER.info(" - id: {}", id);
|
||||
LOGGER.info(" - url: {} ", url);
|
||||
final CLASS_TYPE media = DataAccess.get(clazz, id);
|
||||
final CLASS_TYPE media = ioDb.get(clazz, id);
|
||||
if (media == null) {
|
||||
throw new InputException(clazz.getCanonicalName(),
|
||||
"[" + id.toString() + "] Id does not exist or removed...");
|
||||
@ -310,7 +315,7 @@ public class DataTools {
|
||||
|
||||
final long tmpUID = getTmpDataId();
|
||||
final String sha512 = saveTemporaryFile(dataResponse, tmpUID);
|
||||
Data data = getWithSha512(sha512);
|
||||
Data data = getWithSha512(ioDb, sha512);
|
||||
final String mimeType = getMimeType(dataResponse);
|
||||
if (!Arrays.asList(SUPPORTED_IMAGE_MIME_TYPE).contains(mimeType)) {
|
||||
throw new FailException(Response.Status.NOT_ACCEPTABLE,
|
||||
@ -321,7 +326,7 @@ public class DataTools {
|
||||
if (data == null) {
|
||||
LOGGER.info("Need to add the data in the BDD ... ");
|
||||
try {
|
||||
data = createNewData(tmpUID, url, sha512, mimeType);
|
||||
data = createNewData(ioDb, tmpUID, url, sha512, mimeType);
|
||||
} catch (final IOException ex) {
|
||||
removeTemporaryFile(tmpUID);
|
||||
throw new FailException(Response.Status.NOT_MODIFIED,
|
||||
@ -333,7 +338,7 @@ public class DataTools {
|
||||
}
|
||||
} else if (data.deleted) {
|
||||
LOGGER.error("Data already exist but deleted");
|
||||
undelete(data.uuid);
|
||||
undelete(ioDb, data.uuid);
|
||||
data.deleted = false;
|
||||
} else {
|
||||
LOGGER.error("Data already exist ... all good");
|
||||
@ -341,15 +346,16 @@ public class DataTools {
|
||||
// Fist step: retrieve all the Id of each parents:...
|
||||
LOGGER.info("Find typeNode");
|
||||
if (id instanceof final Long idLong) {
|
||||
AddOnDataJson.addLink(clazz, idLong, "covers", data.uuid);
|
||||
AddOnDataJson.addLink(ioDb, clazz, idLong, "covers", data.uuid);
|
||||
} else if (id instanceof final UUID idUUID) {
|
||||
AddOnDataJson.addLink(clazz, idUUID, "covers", data.uuid);
|
||||
AddOnDataJson.addLink(ioDb, clazz, idUUID, "covers", data.uuid);
|
||||
} else {
|
||||
throw new IOException("Fail to add Cover can not detect type...");
|
||||
}
|
||||
}
|
||||
|
||||
public static <CLASS_TYPE, ID_TYPE> void uploadCover(
|
||||
final DataAccess ioDb,
|
||||
final Class<CLASS_TYPE> clazz,
|
||||
final ID_TYPE id,
|
||||
final InputStream fileInputStream,
|
||||
@ -360,7 +366,7 @@ public class DataTools {
|
||||
LOGGER.info(" - file_name: {} ", fileMetaData.getFileName());
|
||||
LOGGER.info(" - fileInputStream: {}", fileInputStream);
|
||||
LOGGER.info(" - fileMetaData: {}", fileMetaData);
|
||||
final CLASS_TYPE media = DataAccess.get(clazz, id);
|
||||
final CLASS_TYPE media = ioDb.get(clazz, id);
|
||||
if (media == null) {
|
||||
throw new InputException(clazz.getCanonicalName(),
|
||||
"[" + id.toString() + "] Id does not exist or removed...");
|
||||
@ -368,11 +374,11 @@ public class DataTools {
|
||||
|
||||
final long tmpUID = getTmpDataId();
|
||||
final String sha512 = saveTemporaryFile(fileInputStream, tmpUID);
|
||||
Data data = getWithSha512(sha512);
|
||||
Data data = getWithSha512(ioDb, sha512);
|
||||
if (data == null) {
|
||||
LOGGER.info("Need to add the data in the BDD ... ");
|
||||
try {
|
||||
data = createNewData(tmpUID, fileMetaData.getFileName(), sha512);
|
||||
data = createNewData(ioDb, tmpUID, fileMetaData.getFileName(), sha512);
|
||||
} catch (final IOException ex) {
|
||||
removeTemporaryFile(tmpUID);
|
||||
throw new FailException(Response.Status.NOT_MODIFIED,
|
||||
@ -384,7 +390,7 @@ public class DataTools {
|
||||
}
|
||||
} else if (data.deleted) {
|
||||
LOGGER.error("Data already exist but deleted");
|
||||
undelete(data.uuid);
|
||||
undelete(ioDb, data.uuid);
|
||||
data.deleted = false;
|
||||
} else {
|
||||
LOGGER.error("Data already exist ... all good");
|
||||
@ -392,9 +398,9 @@ public class DataTools {
|
||||
// Fist step: retrieve all the Id of each parents:...
|
||||
LOGGER.info("Find typeNode");
|
||||
if (id instanceof final Long idLong) {
|
||||
AddOnDataJson.addLink(clazz, idLong, "covers", data.uuid);
|
||||
AddOnDataJson.addLink(ioDb, clazz, idLong, "covers", data.uuid);
|
||||
} else if (id instanceof final UUID idUUID) {
|
||||
AddOnDataJson.addLink(clazz, idUUID, "covers", data.uuid);
|
||||
AddOnDataJson.addLink(ioDb, clazz, idUUID, "covers", data.uuid);
|
||||
} else {
|
||||
throw new IOException("Fail to add Cover can not detect type...");
|
||||
}
|
||||
|
@ -12,22 +12,38 @@ public class ConfigureDb {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(ConfigureDb.class);
|
||||
|
||||
public static void configure() throws IOException {
|
||||
if (true) {
|
||||
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
|
||||
ConfigBaseVariable.dbType = "sqlite";
|
||||
ConfigBaseVariable.dbHost = "memory";
|
||||
// for test we need to connect all time the DB
|
||||
ConfigBaseVariable.dbKeepConnected = "true";
|
||||
}
|
||||
} else {
|
||||
// Enable this if you want to access to a local MySQL base to test with an adminer
|
||||
String modeTest = System.getenv("TEST_E2E_MODE");
|
||||
if (modeTest == null || modeTest.isEmpty() || "false".equalsIgnoreCase(modeTest)) {
|
||||
modeTest = "SQLITE-MEMORY";
|
||||
} else if ("true".equalsIgnoreCase(modeTest)) {
|
||||
modeTest = "MY-SQL";
|
||||
}
|
||||
// override the local test:
|
||||
modeTest = "MONGO";
|
||||
if ("SQLITE-MEMORY".equalsIgnoreCase(modeTest)) {
|
||||
ConfigBaseVariable.dbType = "sqlite";
|
||||
ConfigBaseVariable.dbHost = "memory";
|
||||
// for test we need to connect all time the DB
|
||||
ConfigBaseVariable.dbKeepConnected = "true";
|
||||
} else if ("SQLITE".equalsIgnoreCase(modeTest)) {
|
||||
ConfigBaseVariable.dbType = "sqlite";
|
||||
ConfigBaseVariable.dbKeepConnected = "true";
|
||||
} else if ("MY-SQL".equalsIgnoreCase(modeTest)) {
|
||||
ConfigBaseVariable.dbType = "mysql";
|
||||
ConfigBaseVariable.bdDatabase = "test_db";
|
||||
ConfigBaseVariable.dbPort = "3906";
|
||||
ConfigBaseVariable.dbUser = "root";
|
||||
} else if ("MONGO".equalsIgnoreCase(modeTest)) {
|
||||
ConfigBaseVariable.dbType = "mongo";
|
||||
ConfigBaseVariable.bdDatabase = "test_mongo_db";
|
||||
} else {
|
||||
// User local modification ...
|
||||
ConfigBaseVariable.bdDatabase = "test_db";
|
||||
ConfigBaseVariable.dbPort = "3906";
|
||||
ConfigBaseVariable.dbUser = "root";
|
||||
//ConfigBaseVariable.dbPassword = "password";
|
||||
}
|
||||
// Connect the dataBase...
|
||||
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.getDbconfig());
|
||||
entry.connect();
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -24,6 +25,8 @@ import test.kar.archidata.model.SimpleTable;
|
||||
public class TestJson {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestJson.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
ConfigureDb.configure();
|
||||
@ -34,13 +37,19 @@ public class TestJson {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestJson() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testTableInsertAndRetrieve() throws Exception {
|
||||
final List<String> sqlCommand = DataFactory.createTable(SerializeAsJson.class);
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,7 +60,7 @@ public class TestJson {
|
||||
test.data = new SimpleTable();
|
||||
test.data.data = "plopppopql";
|
||||
|
||||
final SerializeAsJson insertedData = DataAccess.insert(test);
|
||||
final SerializeAsJson insertedData = this.da.insert(test);
|
||||
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
@ -61,7 +70,7 @@ public class TestJson {
|
||||
Assertions.assertEquals(test.data.data, insertedData.data.data);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final SerializeAsJson retrieve = DataAccess.get(SerializeAsJson.class, insertedData.id);
|
||||
final SerializeAsJson retrieve = this.da.get(SerializeAsJson.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
|
@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -24,6 +25,8 @@ import test.kar.archidata.model.SerializeListAsJson;
|
||||
public class TestListJson {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestListJson.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
ConfigureDb.configure();
|
||||
@ -34,13 +37,19 @@ public class TestListJson {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestListJson() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testTableInsertAndRetrieve() throws Exception {
|
||||
final List<String> sqlCommand = DataFactory.createTable(SerializeListAsJson.class);
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,7 +64,7 @@ public class TestListJson {
|
||||
test.data.add(6);
|
||||
test.data.add(51);
|
||||
|
||||
final SerializeListAsJson insertedData = DataAccess.insert(test);
|
||||
final SerializeListAsJson insertedData = this.da.insert(test);
|
||||
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
@ -69,7 +78,7 @@ public class TestListJson {
|
||||
Assertions.assertEquals(test.data.get(4), insertedData.data.get(4));
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final SerializeListAsJson retrieve = DataAccess.get(SerializeListAsJson.class, insertedData.id);
|
||||
final SerializeListAsJson retrieve = this.da.get(SerializeListAsJson.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
|
@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
|
||||
import org.slf4j.Logger;
|
||||
@ -26,6 +27,8 @@ import test.kar.archidata.model.TypeManyToManyRootExpand;
|
||||
public class TestManyToMany {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestManyToMany.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
ConfigureDb.configure();
|
||||
@ -36,15 +39,21 @@ public class TestManyToMany {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestManyToMany() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testCreateTable() throws Exception {
|
||||
final List<String> sqlCommand2 = DataFactory.createTable(TypeManyToManyRoot.class);
|
||||
final List<String> sqlCommand = DataFactory.createTable(TypeManyToManyRemote.class);
|
||||
sqlCommand.addAll(sqlCommand2);
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,14 +62,14 @@ public class TestManyToMany {
|
||||
public void testSimpleInsertAndRetieve() throws Exception {
|
||||
final TypeManyToManyRoot test = new TypeManyToManyRoot();
|
||||
test.otherData = "kjhlkjlkj";
|
||||
final TypeManyToManyRoot insertedData = DataAccess.insert(test);
|
||||
final TypeManyToManyRoot insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
Assertions.assertNull(insertedData.remote);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypeManyToManyRoot retrieve = DataAccess.get(TypeManyToManyRoot.class, insertedData.id);
|
||||
final TypeManyToManyRoot retrieve = this.da.get(TypeManyToManyRoot.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -69,7 +78,7 @@ public class TestManyToMany {
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
|
||||
Assertions.assertNull(retrieve.remote);
|
||||
|
||||
DataAccess.delete(TypeManyToManyRoot.class, insertedData.id);
|
||||
this.da.delete(TypeManyToManyRoot.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(3)
|
||||
@ -78,24 +87,24 @@ public class TestManyToMany {
|
||||
|
||||
TypeManyToManyRemote remote = new TypeManyToManyRemote();
|
||||
remote.data = "remote1";
|
||||
final TypeManyToManyRemote insertedRemote1 = DataAccess.insert(remote);
|
||||
final TypeManyToManyRemote insertedRemote1 = this.da.insert(remote);
|
||||
Assertions.assertEquals(insertedRemote1.data, remote.data);
|
||||
|
||||
remote = new TypeManyToManyRemote();
|
||||
remote.data = "remote2";
|
||||
final TypeManyToManyRemote insertedRemote2 = DataAccess.insert(remote);
|
||||
final TypeManyToManyRemote insertedRemote2 = this.da.insert(remote);
|
||||
Assertions.assertEquals(insertedRemote2.data, remote.data);
|
||||
|
||||
final TypeManyToManyRoot test = new TypeManyToManyRoot();
|
||||
test.otherData = "kjhlkjlkj";
|
||||
final TypeManyToManyRoot insertedData = DataAccess.insert(test);
|
||||
final TypeManyToManyRoot insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
Assertions.assertNull(insertedData.remote);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
TypeManyToManyRoot retrieve = DataAccess.get(TypeManyToManyRoot.class, insertedData.id);
|
||||
TypeManyToManyRoot retrieve = this.da.get(TypeManyToManyRoot.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -105,10 +114,10 @@ public class TestManyToMany {
|
||||
Assertions.assertNull(retrieve.remote);
|
||||
|
||||
// Add remote elements
|
||||
AddOnManyToMany.addLink(TypeManyToManyRoot.class, retrieve.id, "remote", insertedRemote1.id);
|
||||
AddOnManyToMany.addLink(TypeManyToManyRoot.class, retrieve.id, "remote", insertedRemote2.id);
|
||||
AddOnManyToMany.addLink(this.da, TypeManyToManyRoot.class, retrieve.id, "remote", insertedRemote1.id);
|
||||
AddOnManyToMany.addLink(this.da, TypeManyToManyRoot.class, retrieve.id, "remote", insertedRemote2.id);
|
||||
|
||||
retrieve = DataAccess.get(TypeManyToManyRoot.class, insertedData.id);
|
||||
retrieve = this.da.get(TypeManyToManyRoot.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -120,7 +129,7 @@ public class TestManyToMany {
|
||||
Assertions.assertEquals(retrieve.remote.get(0), insertedRemote1.id);
|
||||
Assertions.assertEquals(retrieve.remote.get(1), insertedRemote2.id);
|
||||
|
||||
final TypeManyToManyRootExpand retrieveExpand = DataAccess.get(TypeManyToManyRootExpand.class, insertedData.id);
|
||||
final TypeManyToManyRootExpand retrieveExpand = this.da.get(TypeManyToManyRootExpand.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieveExpand);
|
||||
Assertions.assertNotNull(retrieveExpand.id);
|
||||
@ -133,10 +142,11 @@ public class TestManyToMany {
|
||||
Assertions.assertEquals(retrieveExpand.remote.get(1).id, insertedRemote2.id);
|
||||
|
||||
// Remove an element
|
||||
int count = AddOnManyToMany.removeLink(TypeManyToManyRoot.class, retrieve.id, "remote", insertedRemote1.id);
|
||||
long count = AddOnManyToMany.removeLink(this.da, TypeManyToManyRoot.class, retrieve.id, "remote",
|
||||
insertedRemote1.id);
|
||||
Assertions.assertEquals(1, count);
|
||||
|
||||
retrieve = DataAccess.get(TypeManyToManyRoot.class, insertedData.id);
|
||||
retrieve = this.da.get(TypeManyToManyRoot.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -148,10 +158,11 @@ public class TestManyToMany {
|
||||
Assertions.assertEquals(retrieve.remote.get(0), insertedRemote2.id);
|
||||
|
||||
// Remove the second element
|
||||
count = AddOnManyToMany.removeLink(TypeManyToManyRoot.class, retrieve.id, "remote", insertedRemote2.id);
|
||||
count = AddOnManyToMany.removeLink(this.da, TypeManyToManyRoot.class, retrieve.id, "remote",
|
||||
insertedRemote2.id);
|
||||
Assertions.assertEquals(1, count);
|
||||
|
||||
retrieve = DataAccess.get(TypeManyToManyRoot.class, insertedData.id);
|
||||
retrieve = this.da.get(TypeManyToManyRoot.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -160,9 +171,9 @@ public class TestManyToMany {
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
|
||||
Assertions.assertNull(retrieve.remote);
|
||||
|
||||
DataAccess.delete(TypeManyToManyRoot.class, insertedData.id);
|
||||
this.da.delete(TypeManyToManyRoot.class, insertedData.id);
|
||||
}
|
||||
|
||||
/* API TODO: - Replace list (permet de les ordonnées) - remove all links - delete en cascade .... (compliqué...) */
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -28,6 +29,8 @@ import test.kar.archidata.model.TypeManyToOneUUIDRootExpand;
|
||||
public class TestManyToOne {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestManyToOne.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
ConfigureDb.configure();
|
||||
@ -38,6 +41,10 @@ public class TestManyToOne {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestManyToOne() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testCreateTable() throws Exception {
|
||||
@ -45,9 +52,11 @@ public class TestManyToOne {
|
||||
sqlCommand.addAll(DataFactory.createTable(TypeManyToOneRoot.class));
|
||||
sqlCommand.addAll(DataFactory.createTable(TypeManyToOneUUIDRoot.class));
|
||||
sqlCommand.addAll(DataFactory.createTable(TypeManyToOneUUIDRemote.class));
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,32 +65,32 @@ public class TestManyToOne {
|
||||
public void testRemoteLong() throws Exception {
|
||||
TypeManyToOneRemote remote = new TypeManyToOneRemote();
|
||||
remote.data = "remote1";
|
||||
final TypeManyToOneRemote insertedRemote1 = DataAccess.insert(remote);
|
||||
final TypeManyToOneRemote insertedRemote1 = this.da.insert(remote);
|
||||
Assertions.assertEquals(insertedRemote1.data, remote.data);
|
||||
|
||||
remote = new TypeManyToOneRemote();
|
||||
remote.data = "remote2";
|
||||
final TypeManyToOneRemote insertedRemote2 = DataAccess.insert(remote);
|
||||
final TypeManyToOneRemote insertedRemote2 = this.da.insert(remote);
|
||||
Assertions.assertEquals(insertedRemote2.data, remote.data);
|
||||
|
||||
final TypeManyToOneRoot test = new TypeManyToOneRoot();
|
||||
test.otherData = "kjhlkjlkj";
|
||||
test.remoteId = insertedRemote2.id;
|
||||
final TypeManyToOneRoot insertedData = DataAccess.insert(test);
|
||||
final TypeManyToOneRoot insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
Assertions.assertEquals(test.otherData, insertedData.otherData);
|
||||
Assertions.assertEquals(insertedRemote2.id, insertedData.remoteId);
|
||||
|
||||
TypeManyToOneRoot retrieve = DataAccess.get(TypeManyToOneRoot.class, insertedData.id);
|
||||
TypeManyToOneRoot retrieve = this.da.get(TypeManyToOneRoot.class, insertedData.id);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
Assertions.assertEquals(insertedData.id, retrieve.id);
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
|
||||
Assertions.assertEquals(insertedRemote2.id, retrieve.remoteId);
|
||||
|
||||
TypeManyToOneRootExpand retrieve2 = DataAccess.get(TypeManyToOneRootExpand.class, insertedData.id);
|
||||
TypeManyToOneRootExpand retrieve2 = this.da.get(TypeManyToOneRootExpand.class, insertedData.id);
|
||||
Assertions.assertNotNull(retrieve2);
|
||||
Assertions.assertNotNull(retrieve2.id);
|
||||
Assertions.assertEquals(insertedData.id, retrieve2.id);
|
||||
@ -91,19 +100,19 @@ public class TestManyToOne {
|
||||
Assertions.assertEquals(insertedRemote2.data, retrieve2.remote.data);
|
||||
|
||||
// remove values:
|
||||
final int count = DataAccess.delete(TypeManyToOneRemote.class, remote.id);
|
||||
Assertions.assertEquals(1, count);
|
||||
final long count = this.da.delete(TypeManyToOneRemote.class, remote.id);
|
||||
Assertions.assertEquals(1L, count);
|
||||
|
||||
// check fail:
|
||||
|
||||
retrieve = DataAccess.get(TypeManyToOneRoot.class, insertedData.id);
|
||||
retrieve = this.da.get(TypeManyToOneRoot.class, insertedData.id);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
Assertions.assertEquals(insertedData.id, retrieve.id);
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
|
||||
Assertions.assertEquals(insertedRemote2.id, retrieve.remoteId);
|
||||
|
||||
retrieve2 = DataAccess.get(TypeManyToOneRootExpand.class, insertedData.id);
|
||||
retrieve2 = this.da.get(TypeManyToOneRootExpand.class, insertedData.id);
|
||||
Assertions.assertNotNull(retrieve2);
|
||||
Assertions.assertNotNull(retrieve2.id);
|
||||
Assertions.assertEquals(insertedData.id, retrieve2.id);
|
||||
@ -116,31 +125,31 @@ public class TestManyToOne {
|
||||
public void testRemoteUUID() throws Exception {
|
||||
TypeManyToOneUUIDRemote remote = new TypeManyToOneUUIDRemote();
|
||||
remote.data = "remote1";
|
||||
final TypeManyToOneUUIDRemote insertedRemote1 = DataAccess.insert(remote);
|
||||
final TypeManyToOneUUIDRemote insertedRemote1 = this.da.insert(remote);
|
||||
Assertions.assertEquals(insertedRemote1.data, remote.data);
|
||||
|
||||
remote = new TypeManyToOneUUIDRemote();
|
||||
remote.data = "remote2";
|
||||
final TypeManyToOneUUIDRemote insertedRemote2 = DataAccess.insert(remote);
|
||||
final TypeManyToOneUUIDRemote insertedRemote2 = this.da.insert(remote);
|
||||
Assertions.assertEquals(insertedRemote2.data, remote.data);
|
||||
|
||||
final TypeManyToOneUUIDRoot test = new TypeManyToOneUUIDRoot();
|
||||
test.otherData = "kjhlkjlkj";
|
||||
test.remoteUuid = insertedRemote2.uuid;
|
||||
final TypeManyToOneUUIDRoot insertedData = DataAccess.insert(test);
|
||||
final TypeManyToOneUUIDRoot insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.uuid);
|
||||
Assertions.assertEquals(test.otherData, insertedData.otherData);
|
||||
Assertions.assertEquals(insertedRemote2.uuid, insertedData.remoteUuid);
|
||||
|
||||
TypeManyToOneUUIDRoot retrieve = DataAccess.get(TypeManyToOneUUIDRoot.class, insertedData.uuid);
|
||||
TypeManyToOneUUIDRoot retrieve = this.da.get(TypeManyToOneUUIDRoot.class, insertedData.uuid);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.uuid);
|
||||
Assertions.assertEquals(insertedData.uuid, retrieve.uuid);
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
|
||||
Assertions.assertEquals(insertedRemote2.uuid, retrieve.remoteUuid);
|
||||
|
||||
TypeManyToOneUUIDRootExpand retrieve2 = DataAccess.get(TypeManyToOneUUIDRootExpand.class, insertedData.uuid);
|
||||
TypeManyToOneUUIDRootExpand retrieve2 = this.da.get(TypeManyToOneUUIDRootExpand.class, insertedData.uuid);
|
||||
Assertions.assertNotNull(retrieve2);
|
||||
Assertions.assertNotNull(retrieve2.uuid);
|
||||
Assertions.assertEquals(insertedData.uuid, retrieve2.uuid);
|
||||
@ -150,23 +159,23 @@ public class TestManyToOne {
|
||||
Assertions.assertEquals(insertedRemote2.data, retrieve2.remote.data);
|
||||
|
||||
// remove values:
|
||||
final int count = DataAccess.delete(TypeManyToOneUUIDRemote.class, remote.uuid);
|
||||
final long count = this.da.delete(TypeManyToOneUUIDRemote.class, remote.uuid);
|
||||
Assertions.assertEquals(1, count);
|
||||
|
||||
// check fail:
|
||||
|
||||
retrieve = DataAccess.get(TypeManyToOneUUIDRoot.class, insertedData.uuid);
|
||||
retrieve = this.da.get(TypeManyToOneUUIDRoot.class, insertedData.uuid);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.uuid);
|
||||
Assertions.assertEquals(insertedData.uuid, retrieve.uuid);
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
|
||||
Assertions.assertEquals(insertedRemote2.uuid, retrieve.remoteUuid);
|
||||
|
||||
retrieve2 = DataAccess.get(TypeManyToOneUUIDRootExpand.class, insertedData.uuid);
|
||||
retrieve2 = this.da.get(TypeManyToOneUUIDRootExpand.class, insertedData.uuid);
|
||||
Assertions.assertNotNull(retrieve2);
|
||||
Assertions.assertNotNull(retrieve2.uuid);
|
||||
Assertions.assertEquals(insertedData.uuid, retrieve2.uuid);
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve2.otherData);
|
||||
Assertions.assertNull(retrieve2.remote);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -28,6 +29,8 @@ import test.kar.archidata.model.TypeOneToManyUUIDRootExpand;
|
||||
public class TestOneToMany {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestOneToMany.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
ConfigureDb.configure();
|
||||
@ -38,6 +41,10 @@ public class TestOneToMany {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestOneToMany() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testCreateTable() throws Exception {
|
||||
@ -45,9 +52,11 @@ public class TestOneToMany {
|
||||
sqlCommand.addAll(DataFactory.createTable(TypeOneToManyRoot.class));
|
||||
sqlCommand.addAll(DataFactory.createTable(TypeOneToManyUUIDRemote.class));
|
||||
sqlCommand.addAll(DataFactory.createTable(TypeOneToManyUUIDRoot.class));
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,13 +67,13 @@ public class TestOneToMany {
|
||||
|
||||
final TypeOneToManyRoot root = new TypeOneToManyRoot();
|
||||
root.otherData = "plouf";
|
||||
final TypeOneToManyRoot insertedRoot = DataAccess.insert(root);
|
||||
final TypeOneToManyRoot insertedRoot = this.da.insert(root);
|
||||
Assertions.assertEquals(insertedRoot.otherData, root.otherData);
|
||||
Assertions.assertNull(insertedRoot.remoteIds);
|
||||
|
||||
final TypeOneToManyRoot root2 = new TypeOneToManyRoot();
|
||||
root2.otherData = "plouf 2";
|
||||
final TypeOneToManyRoot insertedRoot2 = DataAccess.insert(root2);
|
||||
final TypeOneToManyRoot insertedRoot2 = this.da.insert(root2);
|
||||
Assertions.assertEquals(insertedRoot2.otherData, root2.otherData);
|
||||
Assertions.assertNull(insertedRoot2.remoteIds);
|
||||
|
||||
@ -73,34 +82,34 @@ public class TestOneToMany {
|
||||
final TypeOneToManyRemote remote10 = new TypeOneToManyRemote();
|
||||
remote10.data = "remote10";
|
||||
remote10.rootId = insertedRoot.id;
|
||||
final TypeOneToManyRemote insertedRemote10 = DataAccess.insert(remote10);
|
||||
final TypeOneToManyRemote insertedRemote10 = this.da.insert(remote10);
|
||||
Assertions.assertEquals(insertedRemote10.data, remote10.data);
|
||||
Assertions.assertEquals(insertedRemote10.rootId, remote10.rootId);
|
||||
|
||||
final TypeOneToManyRemote remote11 = new TypeOneToManyRemote();
|
||||
remote11.data = "remote11";
|
||||
remote11.rootId = insertedRoot.id;
|
||||
final TypeOneToManyRemote insertedRemote11 = DataAccess.insert(remote11);
|
||||
final TypeOneToManyRemote insertedRemote11 = this.da.insert(remote11);
|
||||
Assertions.assertEquals(insertedRemote11.data, remote11.data);
|
||||
Assertions.assertEquals(insertedRemote11.rootId, remote11.rootId);
|
||||
|
||||
final TypeOneToManyRemote remote20 = new TypeOneToManyRemote();
|
||||
remote20.data = "remote20";
|
||||
remote20.rootId = insertedRoot2.id;
|
||||
final TypeOneToManyRemote insertedRemote20 = DataAccess.insert(remote20);
|
||||
final TypeOneToManyRemote insertedRemote20 = this.da.insert(remote20);
|
||||
Assertions.assertEquals(insertedRemote20.data, remote20.data);
|
||||
Assertions.assertEquals(insertedRemote20.rootId, remote20.rootId);
|
||||
|
||||
// Check remote are inserted
|
||||
|
||||
final TypeOneToManyRoot retreiveRoot1 = DataAccess.get(TypeOneToManyRoot.class, insertedRoot.id);
|
||||
final TypeOneToManyRoot retreiveRoot1 = this.da.get(TypeOneToManyRoot.class, insertedRoot.id);
|
||||
Assertions.assertEquals(retreiveRoot1.otherData, insertedRoot.otherData);
|
||||
Assertions.assertNotNull(retreiveRoot1.remoteIds);
|
||||
Assertions.assertEquals(2, retreiveRoot1.remoteIds.size());
|
||||
Assertions.assertEquals(insertedRemote10.id, retreiveRoot1.remoteIds.get(0));
|
||||
Assertions.assertEquals(insertedRemote11.id, retreiveRoot1.remoteIds.get(1));
|
||||
|
||||
final TypeOneToManyRoot retreiveRoot2 = DataAccess.get(TypeOneToManyRoot.class, insertedRoot2.id);
|
||||
final TypeOneToManyRoot retreiveRoot2 = this.da.get(TypeOneToManyRoot.class, insertedRoot2.id);
|
||||
Assertions.assertEquals(retreiveRoot2.otherData, insertedRoot2.otherData);
|
||||
Assertions.assertNotNull(retreiveRoot2.remoteIds);
|
||||
Assertions.assertEquals(1, retreiveRoot2.remoteIds.size());
|
||||
@ -108,8 +117,7 @@ public class TestOneToMany {
|
||||
|
||||
// Check remote are inserted and expandable
|
||||
|
||||
final TypeOneToManyRootExpand retreiveRootExpand1 = DataAccess.get(TypeOneToManyRootExpand.class,
|
||||
insertedRoot.id);
|
||||
final TypeOneToManyRootExpand retreiveRootExpand1 = this.da.get(TypeOneToManyRootExpand.class, insertedRoot.id);
|
||||
Assertions.assertEquals(retreiveRootExpand1.otherData, insertedRoot.otherData);
|
||||
Assertions.assertNotNull(retreiveRootExpand1.remotes);
|
||||
Assertions.assertEquals(2, retreiveRootExpand1.remotes.size());
|
||||
@ -120,7 +128,7 @@ public class TestOneToMany {
|
||||
Assertions.assertEquals(insertedRemote11.rootId, retreiveRootExpand1.remotes.get(1).rootId);
|
||||
Assertions.assertEquals(insertedRemote11.data, retreiveRootExpand1.remotes.get(1).data);
|
||||
|
||||
final TypeOneToManyRootExpand retreiveRootExpand2 = DataAccess.get(TypeOneToManyRootExpand.class,
|
||||
final TypeOneToManyRootExpand retreiveRootExpand2 = this.da.get(TypeOneToManyRootExpand.class,
|
||||
insertedRoot2.id);
|
||||
Assertions.assertEquals(retreiveRootExpand2.otherData, insertedRoot2.otherData);
|
||||
Assertions.assertNotNull(retreiveRootExpand2.remotes);
|
||||
@ -138,13 +146,13 @@ public class TestOneToMany {
|
||||
|
||||
final TypeOneToManyUUIDRoot root = new TypeOneToManyUUIDRoot();
|
||||
root.otherData = "plouf";
|
||||
final TypeOneToManyUUIDRoot insertedRoot = DataAccess.insert(root);
|
||||
final TypeOneToManyUUIDRoot insertedRoot = this.da.insert(root);
|
||||
Assertions.assertEquals(insertedRoot.otherData, root.otherData);
|
||||
Assertions.assertNull(insertedRoot.remoteIds);
|
||||
|
||||
final TypeOneToManyUUIDRoot root2 = new TypeOneToManyUUIDRoot();
|
||||
root2.otherData = "plouf 2";
|
||||
final TypeOneToManyUUIDRoot insertedRoot2 = DataAccess.insert(root2);
|
||||
final TypeOneToManyUUIDRoot insertedRoot2 = this.da.insert(root2);
|
||||
Assertions.assertEquals(insertedRoot2.otherData, root2.otherData);
|
||||
Assertions.assertNull(insertedRoot2.remoteIds);
|
||||
|
||||
@ -153,34 +161,34 @@ public class TestOneToMany {
|
||||
final TypeOneToManyUUIDRemote remote10 = new TypeOneToManyUUIDRemote();
|
||||
remote10.data = "remote10";
|
||||
remote10.rootUuid = insertedRoot.uuid;
|
||||
final TypeOneToManyUUIDRemote insertedRemote10 = DataAccess.insert(remote10);
|
||||
final TypeOneToManyUUIDRemote insertedRemote10 = this.da.insert(remote10);
|
||||
Assertions.assertEquals(insertedRemote10.data, remote10.data);
|
||||
Assertions.assertEquals(insertedRemote10.rootUuid, remote10.rootUuid);
|
||||
|
||||
final TypeOneToManyUUIDRemote remote11 = new TypeOneToManyUUIDRemote();
|
||||
remote11.data = "remote11";
|
||||
remote11.rootUuid = insertedRoot.uuid;
|
||||
final TypeOneToManyUUIDRemote insertedRemote11 = DataAccess.insert(remote11);
|
||||
final TypeOneToManyUUIDRemote insertedRemote11 = this.da.insert(remote11);
|
||||
Assertions.assertEquals(insertedRemote11.data, remote11.data);
|
||||
Assertions.assertEquals(insertedRemote11.rootUuid, remote11.rootUuid);
|
||||
|
||||
final TypeOneToManyUUIDRemote remote20 = new TypeOneToManyUUIDRemote();
|
||||
remote20.data = "remote20";
|
||||
remote20.rootUuid = insertedRoot2.uuid;
|
||||
final TypeOneToManyUUIDRemote insertedRemote20 = DataAccess.insert(remote20);
|
||||
final TypeOneToManyUUIDRemote insertedRemote20 = this.da.insert(remote20);
|
||||
Assertions.assertEquals(insertedRemote20.data, remote20.data);
|
||||
Assertions.assertEquals(insertedRemote20.rootUuid, remote20.rootUuid);
|
||||
|
||||
// Check remote are inserted
|
||||
|
||||
final TypeOneToManyUUIDRoot retreiveRoot1 = DataAccess.get(TypeOneToManyUUIDRoot.class, insertedRoot.uuid);
|
||||
final TypeOneToManyUUIDRoot retreiveRoot1 = this.da.get(TypeOneToManyUUIDRoot.class, insertedRoot.uuid);
|
||||
Assertions.assertEquals(retreiveRoot1.otherData, insertedRoot.otherData);
|
||||
Assertions.assertNotNull(retreiveRoot1.remoteIds);
|
||||
Assertions.assertEquals(2, retreiveRoot1.remoteIds.size());
|
||||
Assertions.assertEquals(insertedRemote10.uuid, retreiveRoot1.remoteIds.get(0));
|
||||
Assertions.assertEquals(insertedRemote11.uuid, retreiveRoot1.remoteIds.get(1));
|
||||
|
||||
final TypeOneToManyUUIDRoot retreiveRoot2 = DataAccess.get(TypeOneToManyUUIDRoot.class, insertedRoot2.uuid);
|
||||
final TypeOneToManyUUIDRoot retreiveRoot2 = this.da.get(TypeOneToManyUUIDRoot.class, insertedRoot2.uuid);
|
||||
Assertions.assertEquals(retreiveRoot2.otherData, insertedRoot2.otherData);
|
||||
Assertions.assertNotNull(retreiveRoot2.remoteIds);
|
||||
Assertions.assertEquals(1, retreiveRoot2.remoteIds.size());
|
||||
@ -188,7 +196,7 @@ public class TestOneToMany {
|
||||
|
||||
// Check remote are inserted and expandable
|
||||
|
||||
final TypeOneToManyUUIDRootExpand retreiveRootExpand1 = DataAccess.get(TypeOneToManyUUIDRootExpand.class,
|
||||
final TypeOneToManyUUIDRootExpand retreiveRootExpand1 = this.da.get(TypeOneToManyUUIDRootExpand.class,
|
||||
insertedRoot.uuid);
|
||||
Assertions.assertEquals(retreiveRootExpand1.otherData, insertedRoot.otherData);
|
||||
Assertions.assertNotNull(retreiveRootExpand1.remotes);
|
||||
@ -200,7 +208,7 @@ public class TestOneToMany {
|
||||
Assertions.assertEquals(insertedRemote11.rootUuid, retreiveRootExpand1.remotes.get(1).rootUuid);
|
||||
Assertions.assertEquals(insertedRemote11.data, retreiveRootExpand1.remotes.get(1).data);
|
||||
|
||||
final TypeOneToManyUUIDRootExpand retreiveRootExpand2 = DataAccess.get(TypeOneToManyUUIDRootExpand.class,
|
||||
final TypeOneToManyUUIDRootExpand retreiveRootExpand2 = this.da.get(TypeOneToManyUUIDRootExpand.class,
|
||||
insertedRoot2.uuid);
|
||||
Assertions.assertEquals(retreiveRootExpand2.otherData, insertedRoot2.otherData);
|
||||
Assertions.assertNotNull(retreiveRootExpand2.remotes);
|
||||
@ -210,4 +218,4 @@ public class TestOneToMany {
|
||||
Assertions.assertEquals(insertedRemote20.data, retreiveRootExpand2.remotes.get(0).data);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -23,6 +24,8 @@ import test.kar.archidata.model.TypesTable;
|
||||
public class TestRawQuery {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestTypes.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
ConfigureDb.configure();
|
||||
@ -33,75 +36,88 @@ public class TestRawQuery {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestRawQuery() {
|
||||
this.da = DataAccess.createInterface();
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
LOGGER.error("lkjddlkj");
|
||||
}
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testCreateTable() throws Exception {
|
||||
final List<String> sqlCommand = DataFactory.createTable(TypesTable.class);
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Order(2)
|
||||
@Test
|
||||
public void testGet() throws Exception {
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
|
||||
final TypesTable test = new TypesTable();
|
||||
test.intData = 95;
|
||||
test.floatData = 1.0F;
|
||||
DataAccess.insert(test);
|
||||
test.intData = 96;
|
||||
test.floatData = 2.0F;
|
||||
DataAccess.insert(test);
|
||||
test.intData = 97;
|
||||
test.floatData = 3.0F;
|
||||
DataAccess.insert(test);
|
||||
test.intData = 98;
|
||||
test.floatData = 4.0F;
|
||||
DataAccess.insert(test);
|
||||
test.intData = 99;
|
||||
test.floatData = 5.0F;
|
||||
DataAccess.insert(test);
|
||||
test.intData = 99;
|
||||
test.floatData = 6.0F;
|
||||
DataAccess.insert(test);
|
||||
test.intData = 99;
|
||||
test.floatData = 7.0F;
|
||||
DataAccess.insert(test);
|
||||
{
|
||||
final String query = """
|
||||
SELECT *
|
||||
FROM TypesTable
|
||||
WHERE `intData` = ?
|
||||
ORDER BY id DESC
|
||||
""";
|
||||
final List<Object> parameters = List.of(Integer.valueOf(99));
|
||||
// Try to retrieve all the data:
|
||||
final List<TypesTable> retrieve = DataAccess.query(TypesTable.class, query, parameters);
|
||||
final TypesTable test = new TypesTable();
|
||||
test.intData = 95;
|
||||
test.floatData = 1.0F;
|
||||
this.da.insert(test);
|
||||
test.intData = 96;
|
||||
test.floatData = 2.0F;
|
||||
this.da.insert(test);
|
||||
test.intData = 97;
|
||||
test.floatData = 3.0F;
|
||||
this.da.insert(test);
|
||||
test.intData = 98;
|
||||
test.floatData = 4.0F;
|
||||
this.da.insert(test);
|
||||
test.intData = 99;
|
||||
test.floatData = 5.0F;
|
||||
this.da.insert(test);
|
||||
test.intData = 99;
|
||||
test.floatData = 6.0F;
|
||||
this.da.insert(test);
|
||||
test.intData = 99;
|
||||
test.floatData = 7.0F;
|
||||
this.da.insert(test);
|
||||
{
|
||||
final String query = """
|
||||
SELECT *
|
||||
FROM TypesTable
|
||||
WHERE `intData` = ?
|
||||
ORDER BY id DESC
|
||||
""";
|
||||
final List<Object> parameters = List.of(Integer.valueOf(99));
|
||||
// Try to retrieve all the data:
|
||||
final List<TypesTable> retrieve = daSQL.query(TypesTable.class, query, parameters);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertEquals(3, retrieve.size());
|
||||
Assertions.assertEquals(99, retrieve.get(0).intData);
|
||||
Assertions.assertEquals(7.0F, retrieve.get(0).floatData);
|
||||
Assertions.assertEquals(6.0F, retrieve.get(1).floatData);
|
||||
Assertions.assertEquals(5.0F, retrieve.get(2).floatData);
|
||||
}
|
||||
{
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertEquals(3, retrieve.size());
|
||||
Assertions.assertEquals(99, retrieve.get(0).intData);
|
||||
Assertions.assertEquals(7.0F, retrieve.get(0).floatData);
|
||||
Assertions.assertEquals(6.0F, retrieve.get(1).floatData);
|
||||
Assertions.assertEquals(5.0F, retrieve.get(2).floatData);
|
||||
}
|
||||
{
|
||||
|
||||
final String query = """
|
||||
SELECT DISTINCT intData
|
||||
FROM TypesTable
|
||||
WHERE `intData` = ?
|
||||
ORDER BY id DESC
|
||||
""";
|
||||
final List<Object> parameters = List.of(Integer.valueOf(99));
|
||||
// Try to retrieve all the data:
|
||||
final List<TypesTable> retrieve = DataAccess.query(TypesTable.class, query, parameters);
|
||||
final String query = """
|
||||
SELECT DISTINCT intData
|
||||
FROM TypesTable
|
||||
WHERE `intData` = ?
|
||||
ORDER BY id DESC
|
||||
""";
|
||||
final List<Object> parameters = List.of(Integer.valueOf(99));
|
||||
// Try to retrieve all the data:
|
||||
final List<TypesTable> retrieve = daSQL.query(TypesTable.class, query, parameters);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertEquals(1, retrieve.size());
|
||||
Assertions.assertEquals(99, retrieve.get(0).intData);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertEquals(1, retrieve.size());
|
||||
Assertions.assertEquals(99, retrieve.get(0).intData);
|
||||
}
|
||||
} else {
|
||||
LOGGER.warn("Not a SQL DB ...");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
import org.slf4j.Logger;
|
||||
@ -31,6 +32,8 @@ public class TestSimpleTable {
|
||||
private static Long idOfTheObject = null;
|
||||
private static Timestamp startAction = null;
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
// Clear the static test:
|
||||
@ -44,25 +47,31 @@ public class TestSimpleTable {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestSimpleTable() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testTableInsertAndRetrieve() throws Exception {
|
||||
TestSimpleTable.startAction = Timestamp.from(Instant.now());
|
||||
final List<String> sqlCommand = DataFactory.createTable(SimpleTable.class);
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
final SimpleTable test = new SimpleTable();
|
||||
test.data = TestSimpleTable.DATA_INJECTED;
|
||||
final SimpleTable insertedData = DataAccess.insert(test);
|
||||
final SimpleTable insertedData = this.da.insert(test);
|
||||
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, insertedData.id);
|
||||
final SimpleTable retrieve = this.da.get(SimpleTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -77,7 +86,7 @@ public class TestSimpleTable {
|
||||
@Test
|
||||
public void testReadAllValuesUnreadable() throws Exception {
|
||||
// check the full values
|
||||
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject,
|
||||
final SimpleTable retrieve = this.da.get(SimpleTable.class, TestSimpleTable.idOfTheObject,
|
||||
QueryOptions.READ_ALL_COLOMN);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
@ -100,8 +109,8 @@ public class TestSimpleTable {
|
||||
// Delete the entry:
|
||||
final SimpleTable test = new SimpleTable();
|
||||
test.data = TestSimpleTable.DATA_INJECTED_2;
|
||||
DataAccess.update(test, TestSimpleTable.idOfTheObject, List.of("data"));
|
||||
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject,
|
||||
this.da.update(test, TestSimpleTable.idOfTheObject, List.of("data"));
|
||||
final SimpleTable retrieve = this.da.get(SimpleTable.class, TestSimpleTable.idOfTheObject,
|
||||
QueryOptions.READ_ALL_COLOMN);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -117,8 +126,8 @@ public class TestSimpleTable {
|
||||
@Test
|
||||
public void testDeleteTheObject() throws Exception {
|
||||
// Delete the entry:
|
||||
DataAccess.delete(SimpleTable.class, TestSimpleTable.idOfTheObject);
|
||||
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject);
|
||||
this.da.delete(SimpleTable.class, TestSimpleTable.idOfTheObject);
|
||||
final SimpleTable retrieve = this.da.get(SimpleTable.class, TestSimpleTable.idOfTheObject);
|
||||
Assertions.assertNull(retrieve);
|
||||
}
|
||||
|
||||
@ -127,7 +136,7 @@ public class TestSimpleTable {
|
||||
public void testReadDeletedObject() throws Exception {
|
||||
|
||||
// check if we set get deleted element
|
||||
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject,
|
||||
final SimpleTable retrieve = this.da.get(SimpleTable.class, TestSimpleTable.idOfTheObject,
|
||||
QueryOptions.ACCESS_DELETED_ITEMS);
|
||||
Assertions.assertNull(retrieve);
|
||||
|
||||
@ -137,7 +146,7 @@ public class TestSimpleTable {
|
||||
@Test
|
||||
public void testReadAllValuesUnreadableOfDeletedObject() throws Exception {
|
||||
// check if we set get deleted element with all data
|
||||
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject,
|
||||
final SimpleTable retrieve = this.da.get(SimpleTable.class, TestSimpleTable.idOfTheObject,
|
||||
QueryOptions.ACCESS_DELETED_ITEMS, QueryOptions.READ_ALL_COLOMN);
|
||||
Assertions.assertNull(retrieve);
|
||||
|
||||
|
@ -15,6 +15,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||
@ -32,6 +33,8 @@ public class TestSimpleTableSoftDelete {
|
||||
private static Long idOfTheObject = null;
|
||||
private static Timestamp startAction = null;
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
// Clear the static test:
|
||||
@ -46,25 +49,31 @@ public class TestSimpleTableSoftDelete {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestSimpleTableSoftDelete() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testTableInsertAndRetrieve() throws Exception {
|
||||
TestSimpleTableSoftDelete.startAction = Timestamp.from(Instant.now());
|
||||
final List<String> sqlCommand = DataFactory.createTable(SimpleTableSoftDelete.class);
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
final SimpleTableSoftDelete test = new SimpleTableSoftDelete();
|
||||
test.data = TestSimpleTableSoftDelete.DATA_INJECTED;
|
||||
final SimpleTableSoftDelete insertedData = DataAccess.insert(test);
|
||||
final SimpleTableSoftDelete insertedData = this.da.insert(test);
|
||||
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class, insertedData.id);
|
||||
final SimpleTableSoftDelete retrieve = this.da.get(SimpleTableSoftDelete.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -80,7 +89,7 @@ public class TestSimpleTableSoftDelete {
|
||||
@Test
|
||||
public void testReadAllValuesUnreadable() throws Exception {
|
||||
// check the full values
|
||||
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class,
|
||||
final SimpleTableSoftDelete retrieve = this.da.get(SimpleTableSoftDelete.class,
|
||||
TestSimpleTableSoftDelete.idOfTheObject, QueryOptions.READ_ALL_COLOMN);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
@ -107,8 +116,8 @@ public class TestSimpleTableSoftDelete {
|
||||
// Delete the entry:
|
||||
final SimpleTableSoftDelete test = new SimpleTableSoftDelete();
|
||||
test.data = TestSimpleTableSoftDelete.DATA_INJECTED_2;
|
||||
DataAccess.update(test, TestSimpleTableSoftDelete.idOfTheObject, List.of("data"));
|
||||
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class,
|
||||
this.da.update(test, TestSimpleTableSoftDelete.idOfTheObject, List.of("data"));
|
||||
final SimpleTableSoftDelete retrieve = this.da.get(SimpleTableSoftDelete.class,
|
||||
TestSimpleTableSoftDelete.idOfTheObject, QueryOptions.ACCESS_DELETED_ITEMS,
|
||||
QueryOptions.READ_ALL_COLOMN);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
@ -132,8 +141,8 @@ public class TestSimpleTableSoftDelete {
|
||||
Thread.sleep(Duration.ofMillis(15));
|
||||
}
|
||||
// Delete the entry:
|
||||
DataAccess.delete(SimpleTableSoftDelete.class, TestSimpleTableSoftDelete.idOfTheObject);
|
||||
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class,
|
||||
this.da.delete(SimpleTableSoftDelete.class, TestSimpleTableSoftDelete.idOfTheObject);
|
||||
final SimpleTableSoftDelete retrieve = this.da.get(SimpleTableSoftDelete.class,
|
||||
TestSimpleTableSoftDelete.idOfTheObject);
|
||||
Assertions.assertNull(retrieve);
|
||||
}
|
||||
@ -143,7 +152,7 @@ public class TestSimpleTableSoftDelete {
|
||||
public void testReadDeletedObject() throws Exception {
|
||||
|
||||
// check if we set get deleted element
|
||||
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class,
|
||||
final SimpleTableSoftDelete retrieve = this.da.get(SimpleTableSoftDelete.class,
|
||||
TestSimpleTableSoftDelete.idOfTheObject, QueryOptions.ACCESS_DELETED_ITEMS);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -159,7 +168,7 @@ public class TestSimpleTableSoftDelete {
|
||||
@Test
|
||||
public void testReadAllValuesUnreadableOfDeletedObject() throws Exception {
|
||||
// check if we set get deleted element with all data
|
||||
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class,
|
||||
final SimpleTableSoftDelete retrieve = this.da.get(SimpleTableSoftDelete.class,
|
||||
TestSimpleTableSoftDelete.idOfTheObject, QueryOptions.ACCESS_DELETED_ITEMS,
|
||||
QueryOptions.READ_ALL_COLOMN);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
|
@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -24,6 +25,8 @@ import test.kar.archidata.model.TypesEnum1;
|
||||
public class TestTypeEnum1 {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestTypeEnum1.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
ConfigureDb.configure();
|
||||
@ -34,13 +37,19 @@ public class TestTypeEnum1 {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestTypeEnum1() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testCreateTable() throws Exception {
|
||||
final List<String> sqlCommand = DataFactory.createTable(TypesEnum1.class);
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,13 +59,13 @@ public class TestTypeEnum1 {
|
||||
|
||||
final TypesEnum1 test = new TypesEnum1();
|
||||
test.data = Enum1ForTest.ENUM_VALUE_3;
|
||||
final TypesEnum1 insertedData = DataAccess.insert(test);
|
||||
final TypesEnum1 insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesEnum1 retrieve = DataAccess.get(TypesEnum1.class, insertedData.id);
|
||||
final TypesEnum1 retrieve = this.da.get(TypesEnum1.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -64,6 +73,6 @@ public class TestTypeEnum1 {
|
||||
Assertions.assertNotNull(retrieve.data);
|
||||
Assertions.assertEquals(insertedData.data, retrieve.data);
|
||||
|
||||
DataAccess.delete(TypesEnum1.class, insertedData.id);
|
||||
this.da.delete(TypesEnum1.class, insertedData.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -24,6 +25,8 @@ import test.kar.archidata.model.TypesEnum2;
|
||||
public class TestTypeEnum2 {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestTypeEnum2.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
ConfigureDb.configure();
|
||||
@ -34,13 +37,19 @@ public class TestTypeEnum2 {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestTypeEnum2() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testCreateTable() throws Exception {
|
||||
final List<String> sqlCommand = DataFactory.createTable(TypesEnum2.class);
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,13 +59,13 @@ public class TestTypeEnum2 {
|
||||
|
||||
final TypesEnum2 test = new TypesEnum2();
|
||||
test.data = Enum2ForTest.ENUM_VALUE_4;
|
||||
final TypesEnum2 insertedData = DataAccess.insert(test);
|
||||
final TypesEnum2 insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
TypesEnum2 retrieve = DataAccess.get(TypesEnum2.class, insertedData.id);
|
||||
TypesEnum2 retrieve = this.da.get(TypesEnum2.class, insertedData.id);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
Assertions.assertEquals(insertedData.id, retrieve.id);
|
||||
@ -65,22 +74,22 @@ public class TestTypeEnum2 {
|
||||
|
||||
// Update data to null
|
||||
retrieve.data = null;
|
||||
int ret = DataAccess.update(retrieve, retrieve.id);
|
||||
Assertions.assertEquals(1, ret);
|
||||
long ret = this.da.update(retrieve, retrieve.id);
|
||||
Assertions.assertEquals(1L, ret);
|
||||
|
||||
// get new data
|
||||
retrieve = DataAccess.get(TypesEnum2.class, insertedData.id);
|
||||
retrieve = this.da.get(TypesEnum2.class, insertedData.id);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
Assertions.assertEquals(insertedData.id, retrieve.id);
|
||||
Assertions.assertNull(retrieve.data);
|
||||
|
||||
// Remove the data
|
||||
ret = DataAccess.delete(TypesEnum2.class, insertedData.id);
|
||||
Assertions.assertEquals(1, ret);
|
||||
ret = this.da.delete(TypesEnum2.class, insertedData.id);
|
||||
Assertions.assertEquals(1L, ret);
|
||||
|
||||
// Get the removed data:
|
||||
retrieve = DataAccess.get(TypesEnum2.class, insertedData.id);
|
||||
retrieve = this.da.get(TypesEnum2.class, insertedData.id);
|
||||
Assertions.assertNull(retrieve);
|
||||
}
|
||||
|
||||
@ -90,19 +99,19 @@ public class TestTypeEnum2 {
|
||||
|
||||
final TypesEnum2 test = new TypesEnum2();
|
||||
test.data = null;
|
||||
final TypesEnum2 insertedData = DataAccess.insert(test);
|
||||
final TypesEnum2 insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesEnum2 retrieve = DataAccess.get(TypesEnum2.class, insertedData.id);
|
||||
final TypesEnum2 retrieve = this.da.get(TypesEnum2.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
Assertions.assertEquals(insertedData.id, retrieve.id);
|
||||
Assertions.assertNull(retrieve.data);
|
||||
|
||||
DataAccess.delete(TypesEnum2.class, insertedData.id);
|
||||
this.da.delete(TypesEnum2.class, insertedData.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -28,6 +29,8 @@ import test.kar.archidata.model.TypesTable;
|
||||
public class TestTypes {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestTypes.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
ConfigureDb.configure();
|
||||
@ -38,13 +41,19 @@ public class TestTypes {
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
public TestTypes() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testCreateTable() throws Exception {
|
||||
final List<String> sqlCommand = DataFactory.createTable(TypesTable.class);
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
DataAccess.executeSimpleQuery(elem);
|
||||
if (this.da instanceof final DataAccessSQL daSQL) {
|
||||
for (final String elem : sqlCommand) {
|
||||
LOGGER.debug("request: '{}'", elem);
|
||||
daSQL.executeSimpleQuery(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,13 +63,13 @@ public class TestTypes {
|
||||
|
||||
final TypesTable test = new TypesTable();
|
||||
test.intData = 95;
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -68,7 +77,7 @@ public class TestTypes {
|
||||
Assertions.assertNotNull(retrieve.intData);
|
||||
Assertions.assertEquals(insertedData.intData, retrieve.intData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(3)
|
||||
@ -77,13 +86,13 @@ public class TestTypes {
|
||||
|
||||
final TypesTable test = new TypesTable();
|
||||
test.longData = 541684354354L;
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -91,7 +100,7 @@ public class TestTypes {
|
||||
Assertions.assertNotNull(retrieve.longData);
|
||||
Assertions.assertEquals(insertedData.longData, retrieve.longData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(4)
|
||||
@ -100,13 +109,13 @@ public class TestTypes {
|
||||
|
||||
final TypesTable test = new TypesTable();
|
||||
test.floatData = 153154.0f;
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -114,7 +123,7 @@ public class TestTypes {
|
||||
Assertions.assertNotNull(retrieve.floatData);
|
||||
Assertions.assertEquals(insertedData.floatData, retrieve.floatData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(5)
|
||||
@ -123,13 +132,13 @@ public class TestTypes {
|
||||
|
||||
final TypesTable test = new TypesTable();
|
||||
test.doubleData = 153152654654.0;
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -137,7 +146,7 @@ public class TestTypes {
|
||||
Assertions.assertNotNull(retrieve.doubleData);
|
||||
Assertions.assertEquals(insertedData.doubleData, retrieve.doubleData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(6)
|
||||
@ -146,13 +155,13 @@ public class TestTypes {
|
||||
|
||||
final TypesTable test = new TypesTable();
|
||||
test.textData = "lkjlkjlkjmlkqjsdùkljqsùmckljvùwxmckvmwlkdnfqmsjdvnmclkwsjdn;vbcm <wkdjncvm<wk:dnxcm<lwkdnc mqs<wdn:cx,<nm wlx!k:cn<;wmlx:!c;,<wmlx!:c;n<wm ldx:;c,<nwmlx:c,;<wmlx!:c;,< w";
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -160,7 +169,7 @@ public class TestTypes {
|
||||
Assertions.assertNotNull(retrieve.textData);
|
||||
Assertions.assertEquals(insertedData.textData, retrieve.textData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(7)
|
||||
@ -169,13 +178,13 @@ public class TestTypes {
|
||||
|
||||
final TypesTable test = new TypesTable();
|
||||
test.varcharData = "123456789123456789";
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -183,7 +192,7 @@ public class TestTypes {
|
||||
Assertions.assertNotNull(retrieve.varcharData);
|
||||
Assertions.assertEquals(insertedData.varcharData, retrieve.varcharData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(8)
|
||||
@ -192,13 +201,13 @@ public class TestTypes {
|
||||
|
||||
final TypesTable test = new TypesTable();
|
||||
test.booleanData = true;
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -206,7 +215,7 @@ public class TestTypes {
|
||||
Assertions.assertNotNull(retrieve.booleanData);
|
||||
Assertions.assertEquals(insertedData.booleanData, retrieve.booleanData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(9)
|
||||
@ -215,13 +224,13 @@ public class TestTypes {
|
||||
|
||||
final TypesTable test = new TypesTable();
|
||||
test.booleanData = false;
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -229,7 +238,7 @@ public class TestTypes {
|
||||
Assertions.assertNotNull(retrieve.booleanData);
|
||||
Assertions.assertEquals(insertedData.booleanData, retrieve.booleanData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(10)
|
||||
@ -239,13 +248,13 @@ public class TestTypes {
|
||||
final TypesTable test = new TypesTable();
|
||||
test.timeStampData = Timestamp.from(Instant.now());
|
||||
LOGGER.debug("Timestamp = {}", test.timeStampData);
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -257,7 +266,7 @@ public class TestTypes {
|
||||
Assertions.assertEquals(insertedData.timeStampData.toInstant().toEpochMilli(),
|
||||
retrieve.timeStampData.toInstant().toEpochMilli());
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(11)
|
||||
@ -267,13 +276,13 @@ public class TestTypes {
|
||||
final TypesTable test = new TypesTable();
|
||||
test.dateFullData = Date.from(Instant.now());
|
||||
LOGGER.debug("Date = {}", test.dateFullData);
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -282,7 +291,7 @@ public class TestTypes {
|
||||
Assertions.assertNotNull(retrieve.dateFullData);
|
||||
Assertions.assertEquals(insertedData.dateFullData, retrieve.dateFullData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(12)
|
||||
@ -292,13 +301,13 @@ public class TestTypes {
|
||||
final TypesTable test = new TypesTable();
|
||||
test.dateData = LocalDate.now();
|
||||
LOGGER.debug("LocalDate = {}", test.dateData);
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -307,7 +316,7 @@ public class TestTypes {
|
||||
Assertions.assertNotNull(retrieve.dateData);
|
||||
Assertions.assertEquals(insertedData.dateData, retrieve.dateData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(13)
|
||||
@ -317,13 +326,13 @@ public class TestTypes {
|
||||
final TypesTable test = new TypesTable();
|
||||
test.timeData = LocalTime.now();
|
||||
LOGGER.debug("LocalTime = {}", test.timeData);
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -334,7 +343,7 @@ public class TestTypes {
|
||||
Assertions.assertEquals(insertedData.timeData.getMinute(), retrieve.timeData.getMinute());
|
||||
Assertions.assertEquals(insertedData.timeData.getSecond(), retrieve.timeData.getSecond());
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(14)
|
||||
@ -345,13 +354,13 @@ public class TestTypes {
|
||||
test.textData = "test 1";
|
||||
test.booleanData = null;
|
||||
test.varcharData = "plop";
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -366,11 +375,11 @@ public class TestTypes {
|
||||
retrieve.textData = "test 2";
|
||||
retrieve.booleanData = true;
|
||||
retrieve.varcharData = null;
|
||||
final int nbUpdate = DataAccess.update(retrieve, insertedData.id);
|
||||
Assertions.assertEquals(1, nbUpdate);
|
||||
final long nbUpdate = this.da.update(retrieve, insertedData.id);
|
||||
Assertions.assertEquals(1L, nbUpdate);
|
||||
|
||||
// Get new data
|
||||
final TypesTable retrieve2 = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve2 = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve2);
|
||||
Assertions.assertNotNull(retrieve2.id);
|
||||
@ -385,11 +394,11 @@ public class TestTypes {
|
||||
retrieve.textData = "test 3";
|
||||
retrieve.booleanData = false;
|
||||
retrieve.varcharData = "test3";
|
||||
final int nbUpdate2 = DataAccess.update(retrieve, insertedData.id, List.of("textData"));
|
||||
Assertions.assertEquals(1, nbUpdate2);
|
||||
final long nbUpdate2 = this.da.update(retrieve, insertedData.id, List.of("textData"));
|
||||
Assertions.assertEquals(1L, nbUpdate2);
|
||||
|
||||
// Get new data
|
||||
final TypesTable retrieve3 = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve3 = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve3);
|
||||
Assertions.assertNotNull(retrieve3.id);
|
||||
@ -401,7 +410,7 @@ public class TestTypes {
|
||||
Assertions.assertEquals(retrieve2.booleanData, retrieve3.booleanData);
|
||||
Assertions.assertNull(retrieve3.varcharData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
@Order(15)
|
||||
@ -412,13 +421,13 @@ public class TestTypes {
|
||||
test.textData = "test 1";
|
||||
test.booleanData = null;
|
||||
test.varcharData = "plop";
|
||||
final TypesTable insertedData = DataAccess.insert(test);
|
||||
final TypesTable insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.id);
|
||||
Assertions.assertTrue(insertedData.id >= 0);
|
||||
|
||||
// Try to retrieve all the data:
|
||||
final TypesTable retrieve = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.id);
|
||||
@ -437,11 +446,11 @@ public class TestTypes {
|
||||
"varcharData": null
|
||||
}
|
||||
""";
|
||||
final int nbUpdate = DataAccess.updateWithJson(TypesTable.class, insertedData.id, jsonData);
|
||||
Assertions.assertEquals(1, nbUpdate);
|
||||
final long nbUpdate = this.da.updateWithJson(TypesTable.class, insertedData.id, jsonData);
|
||||
Assertions.assertEquals(1L, nbUpdate);
|
||||
|
||||
// Get new data
|
||||
final TypesTable retrieve2 = DataAccess.get(TypesTable.class, insertedData.id);
|
||||
final TypesTable retrieve2 = this.da.get(TypesTable.class, insertedData.id);
|
||||
|
||||
Assertions.assertNotNull(retrieve2);
|
||||
Assertions.assertNotNull(retrieve2.id);
|
||||
@ -452,7 +461,7 @@ public class TestTypes {
|
||||
Assertions.assertEquals(true, retrieve2.booleanData);
|
||||
Assertions.assertNull(retrieve2.varcharData);
|
||||
|
||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||
this.da.delete(TypesTable.class, insertedData.id);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,13 +12,12 @@ import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.GlobalConfiguration;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
import org.kar.archidata.migration.MigrationEngine;
|
||||
import org.kar.archidata.migration.MigrationException;
|
||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import test.kar.archidata.ConfigureDb;
|
||||
import test.kar.archidata.StepwiseExtension;
|
||||
import test.kar.archidata.migration.model.TypesMigrationInitialisationFirst;
|
||||
|
||||
@ -27,37 +26,33 @@ import test.kar.archidata.migration.model.TypesMigrationInitialisationFirst;
|
||||
public class TestMigrationFail {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestMigrationFirstInit.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
public TestMigrationFail() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
|
||||
ConfigBaseVariable.dbType = "sqlite";
|
||||
ConfigBaseVariable.dbHost = "memory";
|
||||
// for test we need to connect all time the DB
|
||||
ConfigBaseVariable.dbKeepConnected = "true";
|
||||
}
|
||||
// Connect the dataBase...
|
||||
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||
entry.connect();
|
||||
ConfigureDb.configure();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void removeDataBase() throws IOException {
|
||||
LOGGER.info("Remove the test db");
|
||||
DBEntry.closeAllForceMode();
|
||||
ConfigBaseVariable.clearAllValue();
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testInitializeTable() throws Exception {
|
||||
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||
final MigrationEngine migrationEngine = new MigrationEngine(this.da);
|
||||
// add initialization:
|
||||
migrationEngine.setInit(new InitializationFirst());
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.getDbconfig());
|
||||
|
||||
final TypesMigrationInitialisationFirst test = new TypesMigrationInitialisationFirst();
|
||||
test.testData = 95.0;
|
||||
final TypesMigrationInitialisationFirst insertedData = DataAccess.insert(test);
|
||||
final TypesMigrationInitialisationFirst insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertEquals(95.0, insertedData.testData);
|
||||
}
|
||||
@ -65,14 +60,14 @@ public class TestMigrationFail {
|
||||
@Order(2)
|
||||
@Test
|
||||
public void testUpdateTwoMigration() throws Exception {
|
||||
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||
final MigrationEngine migrationEngine = new MigrationEngine(this.da);
|
||||
// add initialization:
|
||||
migrationEngine.setInit(new InitializationCurrent());
|
||||
migrationEngine.add(new Migration1());
|
||||
migrationEngine.add(new MigrationFail());
|
||||
Assertions.assertThrows(MigrationException.class, () -> {
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.getDbconfig());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -12,12 +12,11 @@ import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.GlobalConfiguration;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
import org.kar.archidata.migration.MigrationEngine;
|
||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import test.kar.archidata.ConfigureDb;
|
||||
import test.kar.archidata.StepwiseExtension;
|
||||
import test.kar.archidata.migration.model.TypesMigrationInitialisationCurrent;
|
||||
import test.kar.archidata.migration.model.TypesMigrationInitialisationFirst;
|
||||
@ -27,37 +26,33 @@ import test.kar.archidata.migration.model.TypesMigrationInitialisationFirst;
|
||||
public class TestMigrationFirstInit {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestMigrationFail.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
public TestMigrationFirstInit() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
|
||||
ConfigBaseVariable.dbType = "sqlite";
|
||||
ConfigBaseVariable.dbHost = "memory";
|
||||
// for test we need to connect all time the DB
|
||||
ConfigBaseVariable.dbKeepConnected = "true";
|
||||
}
|
||||
// Connect the dataBase...
|
||||
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||
entry.connect();
|
||||
ConfigureDb.configure();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void removeDataBase() throws IOException {
|
||||
LOGGER.info("Remove the test db");
|
||||
DBEntry.closeAllForceMode();
|
||||
ConfigBaseVariable.clearAllValue();
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testInitialMigration() throws Exception {
|
||||
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||
final MigrationEngine migrationEngine = new MigrationEngine(this.da);
|
||||
// add initialization:
|
||||
migrationEngine.setInit(new InitializationFirst());
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.getDbconfig());
|
||||
|
||||
final TypesMigrationInitialisationFirst test = new TypesMigrationInitialisationFirst();
|
||||
test.testData = 95.0;
|
||||
final TypesMigrationInitialisationFirst insertedData = DataAccess.insert(test);
|
||||
final TypesMigrationInitialisationFirst insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertEquals(95.0, insertedData.testData);
|
||||
}
|
||||
@ -65,14 +60,14 @@ public class TestMigrationFirstInit {
|
||||
@Order(2)
|
||||
@Test
|
||||
public void testInitialMigrationReopen() throws Exception {
|
||||
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||
final MigrationEngine migrationEngine = new MigrationEngine(this.da);
|
||||
// add initialization:
|
||||
migrationEngine.setInit(new InitializationFirst());
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.getDbconfig());
|
||||
|
||||
final TypesMigrationInitialisationFirst test = new TypesMigrationInitialisationFirst();
|
||||
test.testData = 99.0;
|
||||
final TypesMigrationInitialisationFirst insertedData = DataAccess.insert(test);
|
||||
final TypesMigrationInitialisationFirst insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertEquals(99.0, insertedData.testData);
|
||||
}
|
||||
@ -80,16 +75,16 @@ public class TestMigrationFirstInit {
|
||||
@Order(3)
|
||||
@Test
|
||||
public void testUpdateTwoMigration() throws Exception {
|
||||
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||
final MigrationEngine migrationEngine = new MigrationEngine(this.da);
|
||||
// add initialization:
|
||||
migrationEngine.setInit(new InitializationCurrent());
|
||||
migrationEngine.add(new Migration1());
|
||||
migrationEngine.add(new Migration2());
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.getDbconfig());
|
||||
|
||||
final TypesMigrationInitialisationCurrent test = new TypesMigrationInitialisationCurrent();
|
||||
test.testDataMigration2 = 125.0;
|
||||
final TypesMigrationInitialisationCurrent insertedData = DataAccess.insert(test);
|
||||
final TypesMigrationInitialisationCurrent insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertEquals(125.0, insertedData.testDataMigration2);
|
||||
}
|
||||
@ -97,17 +92,17 @@ public class TestMigrationFirstInit {
|
||||
@Order(4)
|
||||
@Test
|
||||
public void testUpdateTwoMigrationReopen() throws Exception {
|
||||
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||
final MigrationEngine migrationEngine = new MigrationEngine(this.da);
|
||||
// add initialization:
|
||||
migrationEngine.setInit(new InitializationCurrent());
|
||||
migrationEngine.add(new Migration1());
|
||||
migrationEngine.add(new Migration2());
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.getDbconfig());
|
||||
|
||||
final TypesMigrationInitialisationCurrent test = new TypesMigrationInitialisationCurrent();
|
||||
test.testDataMigration2 = 2563.0;
|
||||
final TypesMigrationInitialisationCurrent insertedData = DataAccess.insert(test);
|
||||
final TypesMigrationInitialisationCurrent insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertEquals(2563.0, insertedData.testDataMigration2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,13 +13,12 @@ import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.kar.archidata.GlobalConfiguration;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
import org.kar.archidata.migration.MigrationEngine;
|
||||
import org.kar.archidata.migration.model.Migration;
|
||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import test.kar.archidata.ConfigureDb;
|
||||
import test.kar.archidata.StepwiseExtension;
|
||||
import test.kar.archidata.migration.model.TypesMigrationInitialisationCurrent;
|
||||
|
||||
@ -28,44 +27,40 @@ import test.kar.archidata.migration.model.TypesMigrationInitialisationCurrent;
|
||||
public class TestMigrationFirstInitWithMigration {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestMigrationFirstInitWithMigration.class);
|
||||
|
||||
private DataAccess da = null;
|
||||
|
||||
public TestMigrationFirstInitWithMigration() {
|
||||
this.da = DataAccess.createInterface();
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public static void configureWebServer() throws Exception {
|
||||
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
|
||||
ConfigBaseVariable.dbType = "sqlite";
|
||||
ConfigBaseVariable.dbHost = "memory";
|
||||
// for test we need to connect all time the DB
|
||||
ConfigBaseVariable.dbKeepConnected = "true";
|
||||
}
|
||||
// Connect the dataBase...
|
||||
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||
entry.connect();
|
||||
ConfigureDb.configure();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void removeDataBase() throws IOException {
|
||||
LOGGER.info("Remove the test db");
|
||||
DBEntry.closeAllForceMode();
|
||||
ConfigBaseVariable.clearAllValue();
|
||||
ConfigureDb.clear();
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testInitialMigration() throws Exception {
|
||||
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||
final MigrationEngine migrationEngine = new MigrationEngine(this.da);
|
||||
// add initialization:
|
||||
migrationEngine.setInit(new InitializationCurrent());
|
||||
// add migration for old version
|
||||
migrationEngine.add(new Migration1());
|
||||
migrationEngine.add(new Migration2());
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.getDbconfig());
|
||||
|
||||
final TypesMigrationInitialisationCurrent test = new TypesMigrationInitialisationCurrent();
|
||||
test.testDataMigration2 = 95.0;
|
||||
final TypesMigrationInitialisationCurrent insertedData = DataAccess.insert(test);
|
||||
final TypesMigrationInitialisationCurrent insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertEquals(95.0, insertedData.testDataMigration2);
|
||||
|
||||
final List<Migration> elements = DataAccess.gets(Migration.class);
|
||||
final List<Migration> elements = this.da.gets(Migration.class);
|
||||
LOGGER.info("List of migrations:");
|
||||
for (final Migration elem : elements) {
|
||||
LOGGER.info(" - {} => {}", elem.id, elem.name);
|
||||
@ -75,18 +70,18 @@ public class TestMigrationFirstInitWithMigration {
|
||||
@Order(2)
|
||||
@Test
|
||||
public void testInitialMigrationReopen() throws Exception {
|
||||
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||
final MigrationEngine migrationEngine = new MigrationEngine(this.da);
|
||||
// add initialization:
|
||||
migrationEngine.setInit(new InitializationCurrent());
|
||||
// add migration for old version
|
||||
migrationEngine.add(new Migration1());
|
||||
migrationEngine.add(new Migration2());
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||
migrationEngine.migrateErrorThrow(GlobalConfiguration.getDbconfig());
|
||||
|
||||
final TypesMigrationInitialisationCurrent test = new TypesMigrationInitialisationCurrent();
|
||||
test.testDataMigration2 = 99.0;
|
||||
final TypesMigrationInitialisationCurrent insertedData = DataAccess.insert(test);
|
||||
final TypesMigrationInitialisationCurrent insertedData = this.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertEquals(99.0, insertedData.testDataMigration2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user