[DEV] Add drop table to improve testing

This commit is contained in:
Edouard DUPIN 2024-01-02 12:05:36 +01:00
parent 66796d9591
commit eff5513705
3 changed files with 53 additions and 7 deletions

View File

@ -720,7 +720,6 @@ public class DataAccess {
return updateWhere(data, new Condition(getTableIdCondition(data.getClass(), id)), new FilterValue(updateColomn), new TransmitKey(id));
}
// il y avait: final List<String> filterValue
public static <T> int updateWhere(final T data, final QueryOption... option) throws Exception {
final Class<?> clazz = data.getClass();
final QueryOptions options = new QueryOptions(option);
@ -834,15 +833,15 @@ public class DataAccess {
condition.injectQuerry(ps, iii);
return ps.executeUpdate();
}
for (final LazyGetter action : asyncActions) {
action.doRequest();
}
} catch (final SQLException ex) {
ex.printStackTrace();
} finally {
entry.close();
entry = null;
}
for (final LazyGetter action : asyncActions) {
action.doRequest();
}
return 0;
}
@ -1276,4 +1275,37 @@ public class DataAccess {
}
}
public static void drop(final Class<?> clazz, final QueryOption... option) throws Exception {
final QueryOptions options = new QueryOptions(option);
final String tableName = AnnotationTools.getTableName(clazz, options);
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
final StringBuilder query = new StringBuilder();
query.append("DROP TABLE `");
query.append(tableName);
query.append("`");
try {
LOGGER.trace("Execute Querry: {}", query.toString());
// Remove main table
final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
ps.executeUpdate();
// search subTable:
for (final Field field : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
continue;
}
if (AnnotationTools.isGenericField(field)) {
continue;
}
final DataAccessAddOn addOn = findAddOnforField(field);
if (addOn != null && !addOn.canInsert(field)) {
addOn.drop(tableName, field);
}
}
} finally {
entry.close();
entry = null;
}
}
}

View File

@ -98,4 +98,8 @@ public interface DataAccessAddOn {
}
default void drop(final String tableName, final Field field) throws Exception {
}
}

View File

@ -151,9 +151,10 @@ public class AddOnManyToMany implements DataAccessAddOn {
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR);
field.set(data, idList);
count.inc();
} else {
LOGGER.error("Can not ManyToMany with other than List<Long> Model: List<{}>", objectClass.getCanonicalName());
return;
// } else {
// LOGGER.error("Can not ManyToMany with other than List<Long> Model: List<{}>", objectClass.getCanonicalName());
// return;
}
final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class);
if (decorators == null) {
@ -245,7 +246,16 @@ public class AddOnManyToMany implements DataAccessAddOn {
LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted);
return;
}
DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName));
actions.add(() -> {
DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName));
});
}
@Override
public void drop(final String tableName, final Field field) throws Exception {
final String columnName = AnnotationTools.getFieldName(field);
final String linkTableName = generateLinkTableName(tableName, columnName);
DataAccess.drop(LinkTable.class, new OverrideTableName(linkTableName));
}
public static void addLink(final Class<?> clazz, final long localKey, final String column, final long remoteKey) throws Exception {