[FEAT] add dependency of the primary key and rework quer(r)y

This commit is contained in:
Edouard DUPIN 2024-04-28 10:27:30 +02:00
parent 4fd13ceb1d
commit c43dfa097a
33 changed files with 217 additions and 174 deletions

View File

@ -126,7 +126,7 @@ public class DataAccess {
return true; return true;
} }
try { try {
return 1 == DataAccess.executeSimpleQuerry("CREATE DATABASE `" + name + "`;", new DBInterfaceRoot(true)); return 1 == DataAccess.executeSimpleQuery("CREATE DATABASE `" + name + "`;", new DBInterfaceRoot(true));
} catch (final SQLException | IOException ex) { } catch (final SQLException | IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
LOGGER.error("Can not check if the DB exist!!! {}", ex.getMessage()); LOGGER.error("Can not check if the DB exist!!! {}", ex.getMessage());
@ -873,7 +873,7 @@ public class DataAccess {
query.append(")"); query.append(")");
final OrderBy orders = options.get(OrderBy.class); final OrderBy orders = options.get(OrderBy.class);
if (orders != null) { if (orders != null) {
orders.generateQuerry(query, tableName); orders.generateQuery(query, tableName);
} }
LOGGER.warn("generate the query: '{}'", query.toString()); LOGGER.warn("generate the query: '{}'", query.toString());
// prepare the request: // prepare the request:
@ -1148,7 +1148,7 @@ public class DataAccess {
query.append(" "); query.append(" ");
final OrderBy orders = options.get(OrderBy.class); final OrderBy orders = options.get(OrderBy.class);
if (orders != null) { if (orders != null) {
orders.generateQuerry(query, tableName); orders.generateQuery(query, tableName);
} }
query.append(" "); query.append(" ");
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
@ -1188,7 +1188,7 @@ public class DataAccess {
addOn.insertData(ps, field, data, iii); addOn.insertData(ps, field, data, iii);
} }
} }
condition.injectQuerry(ps, iii); condition.injectQuery(ps, iii);
return ps.executeUpdate(); return ps.executeUpdate();
} }
} catch (final SQLException ex) { } catch (final SQLException ex) {
@ -1248,14 +1248,14 @@ public class DataAccess {
} }
} }
public static int executeSimpleQuerry(final String query, final QueryOption... option) throws SQLException, IOException { public static int executeSimpleQuery(final String query, final QueryOption... option) throws SQLException, IOException {
final QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
final DBEntry entry = DBInterfaceOption.getAutoEntry(options); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final Statement stmt = entry.connection.createStatement(); final Statement stmt = entry.connection.createStatement();
return stmt.executeUpdate(query); return stmt.executeUpdate(query);
} }
public static boolean executeQuerry(final String query, final QueryOption... option) throws SQLException, IOException { public static boolean executeQuery(final String query, final QueryOption... option) throws SQLException, IOException {
final QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
final DBEntry entry = DBInterfaceOption.getAutoEntry(options); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final Statement stmt = entry.connection.createStatement(); final Statement stmt = entry.connection.createStatement();
@ -1272,9 +1272,16 @@ public class DataAccess {
return values.get(0); return values.get(0);
} }
public static void generateSelectField(final StringBuilder querySelect, final StringBuilder query, final Class<?> clazz, final QueryOptions options, final CountInOut count) throws Exception { public static void generateSelectField(//
final StringBuilder querySelect, //
final StringBuilder query, //
final Class<?> clazz, //
final QueryOptions options, //
final CountInOut count//
) throws Exception {
final boolean readAllfields = QueryOptions.readAllColomn(options); final boolean readAllfields = QueryOptions.readAllColomn(options);
final String tableName = AnnotationTools.getTableName(clazz, options); final String tableName = AnnotationTools.getTableName(clazz, options);
final String primaryKey = AnnotationTools.getPrimaryKeyField(clazz).getName();
boolean firstField = true; boolean firstField = true;
for (final Field elem : clazz.getFields()) { for (final Field elem : clazz.getFields()) {
@ -1298,7 +1305,7 @@ public class DataAccess {
} }
querySelect.append(" "); querySelect.append(" ");
if (addOn != null) { if (addOn != null) {
addOn.generateQuerry(tableName, elem, querySelect, query, name, count, options); addOn.generateQuery(tableName, primaryKey, elem, querySelect, query, name, count, options);
} else { } else {
querySelect.append(tableName); querySelect.append(tableName);
querySelect.append("."); querySelect.append(".");
@ -1340,23 +1347,23 @@ public class DataAccess {
condition.whereAppendQuery(query, tableName, options, deletedFieldName); condition.whereAppendQuery(query, tableName, options, deletedFieldName);
final GroupBy groups = options.get(GroupBy.class); final GroupBy groups = options.get(GroupBy.class);
if (groups != null) { if (groups != null) {
groups.generateQuerry(query, null); groups.generateQuery(query, null);
} }
final OrderBy orders = options.get(OrderBy.class); final OrderBy orders = options.get(OrderBy.class);
if (orders != null) { if (orders != null) {
orders.generateQuerry(query, tableName); orders.generateQuery(query, tableName);
} }
final Limit limit = options.get(Limit.class); final Limit limit = options.get(Limit.class);
if (limit != null) { if (limit != null) {
limit.generateQuerry(query, tableName); limit.generateQuery(query, tableName);
} }
LOGGER.warn("generate the query: '{}'", query.toString()); LOGGER.warn("generate the query: '{}'", query.toString());
// prepare the request: // prepare the request:
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS); final PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
final CountInOut iii = new CountInOut(1); final CountInOut iii = new CountInOut(1);
condition.injectQuerry(ps, iii); condition.injectQuery(ps, iii);
if (limit != null) { if (limit != null) {
limit.injectQuerry(ps, iii); limit.injectQuery(ps, iii);
} }
// execute the request // execute the request
final ResultSet rs = ps.executeQuery(); final ResultSet rs = ps.executeQuery();
@ -1409,7 +1416,7 @@ public class DataAccess {
continue; continue;
} }
if (addOn != null) { if (addOn != null) {
addOn.fillFromQuerry(rs, elem, data, count, options, lazyCall); addOn.fillFromQuery(rs, elem, data, count, options, lazyCall);
} else { } else {
setValueFromDb(elem.getType(), data, count, elem, rs, countNotNull); setValueFromDb(elem.getType(), data, count, elem, rs, countNotNull);
} }
@ -1440,15 +1447,15 @@ public class DataAccess {
condition.whereAppendQuery(query, tableName, options, deletedFieldName); condition.whereAppendQuery(query, tableName, options, deletedFieldName);
final Limit limit = options.get(Limit.class); final Limit limit = options.get(Limit.class);
if (limit != null) { if (limit != null) {
limit.generateQuerry(query, tableName); limit.generateQuery(query, tableName);
} }
LOGGER.warn("generate the query: '{}'", query.toString()); LOGGER.warn("generate the query: '{}'", query.toString());
// prepare the request: // prepare the request:
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS); final PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
final CountInOut iii = new CountInOut(1); final CountInOut iii = new CountInOut(1);
condition.injectQuerry(ps, iii); condition.injectQuery(ps, iii);
if (limit != null) { if (limit != null) {
limit.injectQuerry(ps, iii); limit.injectQuery(ps, iii);
} }
// execute the request // execute the request
final ResultSet rs = ps.executeQuery(); final ResultSet rs = ps.executeQuery();
@ -1540,7 +1547,7 @@ public class DataAccess {
LOGGER.debug("APPLY: {}", query.toString()); LOGGER.debug("APPLY: {}", query.toString());
final PreparedStatement ps = entry.connection.prepareStatement(query.toString()); final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
final CountInOut iii = new CountInOut(1); final CountInOut iii = new CountInOut(1);
condition.injectQuerry(ps, iii); condition.injectQuery(ps, iii);
return ps.executeUpdate(); return ps.executeUpdate();
} finally { } finally {
entry.close(); entry.close();
@ -1577,7 +1584,7 @@ public class DataAccess {
LOGGER.debug("APPLY UPDATE: {}", query.toString()); LOGGER.debug("APPLY UPDATE: {}", query.toString());
final PreparedStatement ps = entry.connection.prepareStatement(query.toString()); final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
final CountInOut iii = new CountInOut(1); final CountInOut iii = new CountInOut(1);
condition.injectQuerry(ps, iii); condition.injectQuery(ps, iii);
return ps.executeUpdate(); return ps.executeUpdate();
} finally { } finally {
entry.close(); entry.close();
@ -1618,7 +1625,7 @@ public class DataAccess {
try { try {
final PreparedStatement ps = entry.connection.prepareStatement(query.toString()); final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
final CountInOut iii = new CountInOut(1); final CountInOut iii = new CountInOut(1);
condition.injectQuerry(ps, iii); condition.injectQuery(ps, iii);
return ps.executeUpdate(); return ps.executeUpdate();
} finally { } finally {
entry.close(); entry.close();
@ -1634,7 +1641,7 @@ public class DataAccess {
query.append(tableName); query.append(tableName);
query.append("`"); query.append("`");
try { try {
LOGGER.trace("Execute Querry: {}", query.toString()); LOGGER.trace("Execute Query: {}", query.toString());
// Remove main table // Remove main table
final PreparedStatement ps = entry.connection.prepareStatement(query.toString()); final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
ps.executeUpdate(); ps.executeUpdate();
@ -1666,7 +1673,7 @@ public class DataAccess {
query.append(tableName); query.append(tableName);
query.append("`"); query.append("`");
try { try {
LOGGER.trace("Execute Querry: {}", query.toString()); LOGGER.trace("Execute Query: {}", query.toString());
// Remove main table // Remove main table
final PreparedStatement ps = entry.connection.prepareStatement(query.toString()); final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
ps.executeUpdate(); ps.executeUpdate();
@ -1721,15 +1728,15 @@ public class DataAccess {
final GroupBy groups = options.get(GroupBy.class); final GroupBy groups = options.get(GroupBy.class);
if (groups != null) { if (groups != null) {
groups.generateQuerry(query, null); groups.generateQuery(query, null);
} }
final OrderBy orders = options.get(OrderBy.class); final OrderBy orders = options.get(OrderBy.class);
if (orders != null) { if (orders != null) {
orders.generateQuerry(query, null); orders.generateQuery(query, null);
} }
final Limit limit = options.get(Limit.class); final Limit limit = options.get(Limit.class);
if (limit != null) { if (limit != null) {
limit.generateQuerry(query, null); limit.generateQuery(query, null);
} }
LOGGER.warn("generate the query: '{}'", query.toString()); LOGGER.warn("generate the query: '{}'", query.toString());
// prepare the request: // prepare the request:
@ -1741,9 +1748,9 @@ public class DataAccess {
} }
iii.inc(); iii.inc();
} }
condition.injectQuerry(ps, iii); condition.injectQuery(ps, iii);
if (limit != null) { if (limit != null) {
limit.injectQuerry(ps, iii); limit.injectQuery(ps, iii);
} }
// execute the request // execute the request
final ResultSet rs = ps.executeQuery(); final ResultSet rs = ps.executeQuery();
@ -1756,7 +1763,7 @@ public class DataAccess {
// find field name ... // find field name ...
final Field field = AnnotationTools.getFieldNamed(clazz, label); final Field field = AnnotationTools.getFieldNamed(clazz, label);
if (field == null) { if (field == null) {
throw new DataAccessException("Querry with unknown field: '" + label + "'"); throw new DataAccessException("Query with unknown field: '" + label + "'");
} }
// create the callback... // create the callback...
final RetreiveFromDB element = createSetValueFromDbCallback(jjj + 1, field); final RetreiveFromDB element = createSetValueFromDbCallback(jjj + 1, field);

View File

@ -45,11 +45,19 @@ public interface DataAccessAddOn {
return false; return false;
} }
void generateQuerry(@NotNull String tableName, @NotNull Field field, @NotNull final StringBuilder querySelect, @NotNull final StringBuilder query, @NotNull String name, @NotNull CountInOut count, void generateQuery(//
QueryOptions options) throws Exception; @NotNull String tableName, //
@NotNull final String primaryKey, //
@NotNull Field field, //
@NotNull final StringBuilder querySelect, //
@NotNull final StringBuilder query, //
@NotNull String name, //
@NotNull CountInOut count, //
QueryOptions options//
) throws Exception;
// Return the number of colomn read // Return the number of colomn read
void fillFromQuerry(ResultSet rs, Field field, Object data, CountInOut count, QueryOptions options, final List<LazyGetter> lazyCall) void fillFromQuery(ResultSet rs, Field field, Object data, CountInOut count, QueryOptions options, final List<LazyGetter> lazyCall)
throws Exception, SQLException, IllegalArgumentException, IllegalAccessException; throws Exception, SQLException, IllegalArgumentException, IllegalAccessException;
/** Create associated table of the specific element. /** Create associated table of the specific element.

View File

@ -234,7 +234,7 @@ public class DataExport {
return iii; return iii;
} }
} }
throw new DataAccessException("Querry with unknown field: '" + name + "'"); throw new DataAccessException("Query with unknown field: '" + name + "'");
} }
public static TableQuery queryTable(final List<TableQueryTypes> headers, final String query, final List<Object> parameters, final QueryOption... option) throws Exception { public static TableQuery queryTable(final List<TableQueryTypes> headers, final String query, final List<Object> parameters, final QueryOption... option) throws Exception {
@ -260,15 +260,15 @@ public class DataExport {
final GroupBy groups = options.get(GroupBy.class); final GroupBy groups = options.get(GroupBy.class);
if (groups != null) { if (groups != null) {
groups.generateQuerry(query, null); groups.generateQuery(query, null);
} }
final OrderBy orders = options.get(OrderBy.class); final OrderBy orders = options.get(OrderBy.class);
if (orders != null) { if (orders != null) {
orders.generateQuerry(query, null); orders.generateQuery(query, null);
} }
final Limit limit = options.get(Limit.class); final Limit limit = options.get(Limit.class);
if (limit != null) { if (limit != null) {
limit.generateQuerry(query, null); limit.generateQuery(query, null);
} }
LOGGER.warn("generate the query: '{}'", query.toString()); LOGGER.warn("generate the query: '{}'", query.toString());
// prepare the request: // prepare the request:
@ -280,9 +280,9 @@ public class DataExport {
} }
iii.inc(); iii.inc();
} }
condition.injectQuerry(ps, iii); condition.injectQuery(ps, iii);
if (limit != null) { if (limit != null) {
limit.injectQuerry(ps, iii); limit.injectQuery(ps, iii);
} }
// execute the request // execute the request
final ResultSet rs = ps.executeQuery(); final ResultSet rs = ps.executeQuery();

View File

@ -14,7 +14,7 @@ public class GroupBy extends QueryOption {
this.childs = List.of(childs); this.childs = List.of(childs);
} }
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuery(final StringBuilder query, final String tableName) {
if (this.childs.size() == 0) { if (this.childs.size() == 0) {
return; return;
} }
@ -33,7 +33,7 @@ public class GroupBy extends QueryOption {
query.append("\n"); query.append("\n");
} }
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception { public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
// nothing to add. // nothing to add.
} }
} }

View File

@ -9,11 +9,11 @@ public class Limit extends QueryOption {
this.limit = limit; this.limit = limit;
} }
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuery(final StringBuilder query, final String tableName) {
query.append(" LIMIT ? \n"); query.append(" LIMIT ? \n");
} }
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception { public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
DataAccess.addElement(ps, this.limit, iii); DataAccess.addElement(ps, this.limit, iii);
iii.inc(); iii.inc();
} }

View File

@ -14,7 +14,7 @@ public class OrderBy extends QueryOption {
this.childs = List.of(childs); this.childs = List.of(childs);
} }
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuery(final StringBuilder query, final String tableName) {
if (this.childs.size() == 0) { if (this.childs.size() == 0) {
return; return;
} }
@ -35,7 +35,7 @@ public class OrderBy extends QueryOption {
query.append("\n"); query.append("\n");
} }
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception { public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
// nothing to add. // nothing to add.
} }
} }

View File

@ -22,7 +22,7 @@ public class QueryAnd implements QueryItem {
} }
@Override @Override
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuery(final StringBuilder query, final String tableName) {
if (this.childs.size() >= 1) { if (this.childs.size() >= 1) {
query.append(" ("); query.append(" (");
} }
@ -33,7 +33,7 @@ public class QueryAnd implements QueryItem {
} else { } else {
query.append(" AND "); query.append(" AND ");
} }
elem.generateQuerry(query, tableName); elem.generateQuery(query, tableName);
} }
if (this.childs.size() >= 1) { if (this.childs.size() >= 1) {
query.append(")"); query.append(")");
@ -41,10 +41,10 @@ public class QueryAnd implements QueryItem {
} }
@Override @Override
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception { public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
for (final QueryItem elem : this.childs) { for (final QueryItem elem : this.childs) {
elem.injectQuerry(ps, iii); elem.injectQuery(ps, iii);
} }
} }

View File

@ -14,7 +14,7 @@ public class QueryCondition implements QueryItem {
} }
@Override @Override
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuery(final StringBuilder query, final String tableName) {
if (tableName != null) { if (tableName != null) {
query.append(tableName); query.append(tableName);
query.append("."); query.append(".");
@ -26,7 +26,7 @@ public class QueryCondition implements QueryItem {
} }
@Override @Override
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception { public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
DataAccess.addElement(ps, this.value, iii); DataAccess.addElement(ps, this.value, iii);
iii.inc(); iii.inc();
} }

View File

@ -23,7 +23,7 @@ public class QueryInList<T> implements QueryItem {
} }
@Override @Override
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuery(final StringBuilder query, final String tableName) {
if (tableName != null) { if (tableName != null) {
query.append(tableName); query.append(tableName);
query.append("."); query.append(".");
@ -44,7 +44,7 @@ public class QueryInList<T> implements QueryItem {
} }
@Override @Override
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception { public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
for (final Object elem : this.value) { for (final Object elem : this.value) {
DataAccess.addElement(ps, elem, iii); DataAccess.addElement(ps, elem, iii);
iii.inc(); iii.inc();

View File

@ -3,7 +3,7 @@ package org.kar.archidata.dataAccess;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
public interface QueryItem { public interface QueryItem {
void generateQuerry(StringBuilder query, String tableName); void generateQuery(StringBuilder query, String tableName);
void injectQuerry(PreparedStatement ps, CountInOut iii) throws Exception; void injectQuery(PreparedStatement ps, CountInOut iii) throws Exception;
} }

View File

@ -10,7 +10,7 @@ public class QueryNotNull implements QueryItem {
} }
@Override @Override
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuery(final StringBuilder query, final String tableName) {
if (tableName != null) { if (tableName != null) {
query.append(tableName); query.append(tableName);
query.append("."); query.append(".");
@ -20,5 +20,5 @@ public class QueryNotNull implements QueryItem {
} }
@Override @Override
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {} public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {}
} }

View File

@ -10,7 +10,7 @@ public class QueryNull implements QueryItem {
} }
@Override @Override
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuery(final StringBuilder query, final String tableName) {
if (tableName != null) { if (tableName != null) {
query.append(tableName); query.append(tableName);
query.append("."); query.append(".");
@ -20,5 +20,5 @@ public class QueryNull implements QueryItem {
} }
@Override @Override
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {} public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {}
} }

View File

@ -15,7 +15,7 @@ public class QueryOr implements QueryItem {
} }
@Override @Override
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuery(final StringBuilder query, final String tableName) {
if (this.childs.size() >= 1) { if (this.childs.size() >= 1) {
query.append(" ("); query.append(" (");
} }
@ -26,7 +26,7 @@ public class QueryOr implements QueryItem {
} else { } else {
query.append(" OR "); query.append(" OR ");
} }
elem.generateQuerry(query, tableName); elem.generateQuery(query, tableName);
} }
if (this.childs.size() >= 1) { if (this.childs.size() >= 1) {
query.append(")"); query.append(")");
@ -34,9 +34,9 @@ public class QueryOr implements QueryItem {
} }
@Override @Override
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception { public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
for (final QueryItem elem : this.childs) { for (final QueryItem elem : this.childs) {
elem.injectQuerry(ps, iii); elem.injectQuery(ps, iii);
} }
} }
} }

View File

@ -79,18 +79,26 @@ public class AddOnDataJson implements DataAccessAddOn {
} }
@Override @Override
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name, public void generateQuery(//
@NotNull final CountInOut elemCount, final QueryOptions options) throws Exception { @NotNull final String tableName, //
querrySelect.append(" "); @NotNull final String primaryKey, //
querrySelect.append(tableName); @NotNull final Field field, //
querrySelect.append("."); @NotNull final StringBuilder querySelect, //
querrySelect.append(name); @NotNull final StringBuilder query, //
elemCount.inc(); @NotNull final String name, //
@NotNull final CountInOut count, //
final QueryOptions options//
) throws Exception {
querySelect.append(" ");
querySelect.append(tableName);
querySelect.append(".");
querySelect.append(name);
count.inc();
return; return;
} }
@Override @Override
public void fillFromQuerry(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) throws Exception { public void fillFromQuery(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) throws Exception {
final String jsonData = rs.getString(count.value); final String jsonData = rs.getString(count.value);
count.inc(); count.inc();
if (!rs.wasNull()) { if (!rs.wasNull()) {

View File

@ -9,7 +9,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.apache.velocity.tools.generic.ResourceTool.Key;
import org.kar.archidata.annotation.AnnotationTools; import org.kar.archidata.annotation.AnnotationTools;
import org.kar.archidata.dataAccess.CountInOut; import org.kar.archidata.dataAccess.CountInOut;
import org.kar.archidata.dataAccess.DataAccess; import org.kar.archidata.dataAccess.DataAccess;
@ -98,76 +97,79 @@ public class AddOnManyToMany implements DataAccessAddOn {
return tableName + "_link_" + localName; return tableName + "_link_" + localName;
} }
public void generateConcatQuerry( // public void generateConcatQuery(//
@NotNull final String tableName, // @NotNull final String tableName, //
@NotNull final String primaryKey, //
@NotNull final Field field, // @NotNull final Field field, //
@NotNull final StringBuilder querrySelect, // @NotNull final StringBuilder querySelect, //
@NotNull final StringBuilder querry, // @NotNull final StringBuilder query, //
@NotNull final String name, // @NotNull final String name, //
@NotNull final CountInOut elemCount, // @NotNull final CountInOut count, //
final QueryOptions options// final QueryOptions options//
) throws Exception { ) throws Exception {
final String linkTableName = generateLinkTableName(tableName, name); final String linkTableName = generateLinkTableName(tableName, name);
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
final String tmpVariable = "tmp_" + Integer.toString(elemCount.value); final String tmpVariable = "tmp_" + Integer.toString(count.value);
querrySelect.append(" (SELECT GROUP_CONCAT("); querySelect.append(" (SELECT GROUP_CONCAT(");
querrySelect.append(tmpVariable); querySelect.append(tmpVariable);
querrySelect.append(".object2Id "); querySelect.append(".object2Id ");
if ("sqlite".equals(ConfigBaseVariable.getDBType())) { if ("sqlite".equals(ConfigBaseVariable.getDBType())) {
querrySelect.append(", "); querySelect.append(", ");
} else { } else {
querrySelect.append("SEPARATOR "); querySelect.append("SEPARATOR ");
} }
querrySelect.append("'"); querySelect.append("'");
if (objectClass == Long.class) { if (objectClass == Long.class) {
querrySelect.append(SEPARATOR_LONG); querySelect.append(SEPARATOR_LONG);
} else if (objectClass == UUID.class) {} else { } else if (objectClass == UUID.class) {} else {
final Class<?> foreignKeyType = AnnotationTools.getPrimaryKeyField(objectClass).getType(); final Class<?> foreignKeyType = AnnotationTools.getPrimaryKeyField(objectClass).getType();
if (foreignKeyType == Long.class) { if (foreignKeyType == Long.class) {
querrySelect.append(SEPARATOR_LONG); querySelect.append(SEPARATOR_LONG);
} }
} }
querrySelect.append("') FROM "); querySelect.append("') FROM ");
querrySelect.append(linkTableName); querySelect.append(linkTableName);
querrySelect.append(" "); querySelect.append(" ");
querrySelect.append(tmpVariable); querySelect.append(tmpVariable);
querrySelect.append(" WHERE "); querySelect.append(" WHERE ");
/* querrySelect.append(tmpVariable); querrySelect.append(".deleted = false AND "); */ /* querySelect.append(tmpVariable); querySelect.append(".deleted = false AND "); */
querrySelect.append(tableName); querySelect.append(tableName);
final la il faut final retouvrt la primary final Key de la final table courante ... querySelect.append(".");
querrySelect.append(".id = "); querySelect.append(primaryKey);
querrySelect.append(tmpVariable); querySelect.append(" = ");
querrySelect.append("."); querySelect.append(tmpVariable);
querrySelect.append("object1Id "); querySelect.append(".");
querySelect.append("object1Id ");
if (!"sqlite".equals(ConfigBaseVariable.getDBType())) { if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
querrySelect.append(" GROUP BY "); querySelect.append(" GROUP BY ");
querrySelect.append(tmpVariable); querySelect.append(tmpVariable);
querrySelect.append(".object1Id"); querySelect.append(".object1Id");
} }
querrySelect.append(") AS "); querySelect.append(") AS ");
querrySelect.append(name); querySelect.append(name);
querrySelect.append(" "); querySelect.append(" ");
/* " (SELECT GROUP_CONCAT(tmp.data_id SEPARATOR '-')" + " FROM cover_link_node tmp" + " WHERE tmp.deleted = false" + /* " (SELECT GROUP_CONCAT(tmp.data_id SEPARATOR '-')" + " FROM cover_link_node tmp" + " WHERE tmp.deleted = false" +
* " AND node.id = tmp.node_id" + " GROUP BY tmp.node_id) AS covers" + */ * " AND node.id = tmp.node_id" + " GROUP BY tmp.node_id) AS covers" + */
elemCount.inc(); count.inc();
} }
@Override @Override
public void generateQuerry( // public void generateQuery(//
@NotNull final String tableName, // @NotNull final String tableName, //
@NotNull final String primaryKey, //
@NotNull final Field field, // @NotNull final Field field, //
@NotNull final StringBuilder querrySelect, // @NotNull final StringBuilder querySelect, //
@NotNull final StringBuilder querry, // @NotNull final StringBuilder query, //
@NotNull final String name, // @NotNull final String name, //
@NotNull final CountInOut elemCount, // @NotNull final CountInOut count, //
final QueryOptions options // final QueryOptions options//
) throws Exception { ) throws Exception {
if (field.getType() != List.class) { if (field.getType() != List.class) {
return; return;
} }
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
if (objectClass == Long.class || objectClass == UUID.class) { if (objectClass == Long.class || objectClass == UUID.class) {
generateConcatQuerry(tableName, field, querrySelect, querry, name, elemCount, options); generateConcatQuery(tableName, primaryKey, field, querySelect, query, name, count, options);
} }
final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class); final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class);
if (decorators == null) { if (decorators == null) {
@ -177,13 +179,13 @@ public class AddOnManyToMany implements DataAccessAddOn {
if (decorators.fetch() == FetchType.EAGER) { if (decorators.fetch() == FetchType.EAGER) {
throw new DataAccessException("EAGER is not supported for list of element..."); throw new DataAccessException("EAGER is not supported for list of element...");
} else { } else {
generateConcatQuerry(tableName, field, querrySelect, querry, name, elemCount, options); generateConcatQuery(tableName, primaryKey, field, querySelect, query, name, count, options);
} }
} }
} }
@Override @Override
public void fillFromQuerry( // public void fillFromQuery( //
final ResultSet rs, // final ResultSet rs, //
final Field field, // final Field field, //
final Object data, // final Object data, //

View File

@ -132,13 +132,14 @@ public class AddOnManyToOne implements DataAccessAddOn {
} }
@Override @Override
public void generateQuerry( // public void generateQuery(//
@NotNull final String tableName, // @NotNull final String tableName, //
@NotNull final String primaryKey, //
@NotNull final Field field, // @NotNull final Field field, //
@NotNull final StringBuilder querrySelect, // @NotNull final StringBuilder querySelect, //
@NotNull final StringBuilder querry, // @NotNull final StringBuilder query, //
@NotNull final String name, // @NotNull final String name, //
@NotNull final CountInOut elemCount, // @NotNull final CountInOut count, //
final QueryOptions options// final QueryOptions options//
) throws Exception { ) throws Exception {
if (field.getType() == Long.class // if (field.getType() == Long.class //
@ -146,37 +147,37 @@ public class AddOnManyToOne implements DataAccessAddOn {
|| field.getType() == Short.class // || field.getType() == Short.class //
|| field.getType() == String.class // || field.getType() == String.class //
|| field.getType() == UUID.class) { || field.getType() == UUID.class) {
querrySelect.append(" "); querySelect.append(" ");
querrySelect.append(tableName); querySelect.append(tableName);
querrySelect.append("."); querySelect.append(".");
querrySelect.append(name); querySelect.append(name);
elemCount.inc(); count.inc();
return; return;
} }
final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class); final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class);
if (field.getType() == decorators.targetEntity()) { if (field.getType() == decorators.targetEntity()) {
if (decorators.fetch() == FetchType.EAGER) { if (decorators.fetch() == FetchType.EAGER) {
// TODO: rework this to have a lazy mode ... // TODO: rework this to have a lazy mode ...
DataAccess.generateSelectField(querrySelect, querry, field.getType(), options, elemCount); DataAccess.generateSelectField(querySelect, query, field.getType(), options, count);
final Class<?> subType = field.getType(); final Class<?> subType = field.getType();
final String subTableName = AnnotationTools.getTableName(subType); final String subTableName = AnnotationTools.getTableName(subType);
final Field idField = AnnotationTools.getFieldOfId(subType); final Field idField = AnnotationTools.getFieldOfId(subType);
querry.append("LEFT OUTER JOIN `"); query.append("LEFT OUTER JOIN `");
querry.append(subTableName); query.append(subTableName);
querry.append("` ON "); query.append("` ON ");
querry.append(subTableName); query.append(subTableName);
querry.append("."); query.append(".");
querry.append(AnnotationTools.getFieldName(idField)); query.append(AnnotationTools.getFieldName(idField));
querry.append(" = "); query.append(" = ");
querry.append(tableName); query.append(tableName);
querry.append("."); query.append(".");
querry.append(AnnotationTools.getFieldName(field)); query.append(AnnotationTools.getFieldName(field));
} else { } else {
querrySelect.append(" "); querySelect.append(" ");
querrySelect.append(tableName); querySelect.append(tableName);
querrySelect.append("."); querySelect.append(".");
querrySelect.append(name); querySelect.append(name);
elemCount.inc(); count.inc();
return; return;
} }
} }
@ -185,7 +186,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
} }
@Override @Override
public void fillFromQuerry(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) throws Exception { public void fillFromQuery(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) throws Exception {
if (field.getType() == Long.class) { if (field.getType() == Long.class) {
final Long foreignKey = rs.getLong(count.value); final Long foreignKey = rs.getLong(count.value);
count.inc(); count.inc();

View File

@ -103,17 +103,25 @@ public class AddOnOneToMany implements DataAccessAddOn {
} }
@Override @Override
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name, public void generateQuery(//
@NotNull final CountInOut elemCount, final QueryOptions options) { @NotNull final String tableName, //
querrySelect.append(" "); @NotNull final String primaryKey, //
querrySelect.append(tableName); @NotNull final Field field, //
querrySelect.append("."); @NotNull final StringBuilder querySelect, //
querrySelect.append(name); @NotNull final StringBuilder query, //
elemCount.inc(); @NotNull final String name, //
@NotNull final CountInOut count, //
final QueryOptions options//
) {
querySelect.append(" ");
querySelect.append(tableName);
querySelect.append(".");
querySelect.append(name);
count.inc();
} }
@Override @Override
public void fillFromQuerry(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) public void fillFromQuery(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall)
throws SQLException, IllegalArgumentException, IllegalAccessException { throws SQLException, IllegalArgumentException, IllegalAccessException {
final Long foreignKey = rs.getLong(count.value); final Long foreignKey = rs.getLong(count.value);
count.inc(); count.inc();

View File

@ -87,17 +87,25 @@ public class AddOnSQLTableExternalForeinKeyAsList implements DataAccessAddOn {
} }
@Override @Override
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name, public void generateQuery(//
@NotNull final CountInOut elemCount, final QueryOptions options) { @NotNull final String tableName, //
elemCount.inc(); @NotNull final String primaryKey, //
querrySelect.append(" "); @NotNull final Field field, //
querrySelect.append(tableName); @NotNull final StringBuilder querySelect, //
querrySelect.append("."); @NotNull final StringBuilder query, //
querrySelect.append(name); @NotNull final String name, //
@NotNull final CountInOut count, //
final QueryOptions options//
) {
count.inc();
querySelect.append(" ");
querySelect.append(tableName);
querySelect.append(".");
querySelect.append(name);
} }
@Override @Override
public void fillFromQuerry(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) public void fillFromQuery(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall)
throws SQLException, IllegalArgumentException, IllegalAccessException { throws SQLException, IllegalArgumentException, IllegalAccessException {
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR); final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR);
field.set(data, idList); field.set(data, idList);

View File

@ -19,15 +19,15 @@ public class Condition extends QueryOption {
this.condition = null; this.condition = null;
} }
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuery(final StringBuilder query, final String tableName) {
if (this.condition != null) { if (this.condition != null) {
this.condition.generateQuerry(query, tableName); this.condition.generateQuery(query, tableName);
} }
} }
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception { public void injectQuery(final PreparedStatement ps, final CountInOut iii) throws Exception {
if (this.condition != null) { if (this.condition != null) {
this.condition.injectQuerry(ps, iii); this.condition.injectQuery(ps, iii);
} }
} }
@ -48,7 +48,7 @@ public class Condition extends QueryOption {
return; return;
} }
query.append(" WHERE ("); query.append(" WHERE (");
this.condition.generateQuerry(query, tableName); this.condition.generateQuery(query, tableName);
query.append(") "); query.append(") ");
if (exclude_deleted && deletedFieldName != null) { if (exclude_deleted && deletedFieldName != null) {
query.append("AND "); query.append("AND ");

View File

@ -166,7 +166,7 @@ public class MigrationEngine {
} }
LOGGER.info("Create Table with : {}", sqlQuery.get(0)); LOGGER.info("Create Table with : {}", sqlQuery.get(0));
try { try {
DataAccess.executeQuerry(sqlQuery.get(0)); DataAccess.executeQuery(sqlQuery.get(0));
} catch (SQLException | IOException ex) { } catch (SQLException | IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
throw new MigrationException("Fail to create the local DB model for migaration ==> wait administrator interventions"); throw new MigrationException("Fail to create the local DB model for migaration ==> wait administrator interventions");

View File

@ -98,7 +98,7 @@ public class MigrationSqlStep implements MigrationInterface {
} }
try { try {
if (action.action() != null) { if (action.action() != null) {
DataAccess.executeQuerry(action.action()); DataAccess.executeQuery(action.action());
} else { } else {
action.async().doRequest(); action.async().doRequest();
} }

View File

@ -135,6 +135,7 @@ public class RESTApi {
if (clazz.equals(String.class)) { if (clazz.equals(String.class)) {
return (T) httpResponse.body(); return (T) httpResponse.body();
} }
LOGGER.trace("Receive model: {} with data: '{}'", clazz.getCanonicalName(), httpResponse.body());
return this.mapper.readValue(httpResponse.body(), clazz); return this.mapper.readValue(httpResponse.body(), clazz);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -52,7 +52,7 @@ public class TestRawQuery {
final List<String> sqlCommand = DataFactory.createTable(TypesTable.class); final List<String> sqlCommand = DataFactory.createTable(TypesTable.class);
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuerry(elem); DataAccess.executeSimpleQuery(elem);
} }
} }
@ -83,7 +83,7 @@ public class TestRawQuery {
test.floatData = 7.0F; test.floatData = 7.0F;
DataAccess.insert(test); DataAccess.insert(test);
{ {
String querry = """ String query = """
SELECT * SELECT *
FROM TypesTable FROM TypesTable
WHERE `intData` = ? WHERE `intData` = ?
@ -91,7 +91,7 @@ public class TestRawQuery {
"""; """;
List<Object> parameters = List.of(Integer.valueOf(99)); List<Object> parameters = List.of(Integer.valueOf(99));
// Try to retrieve all the data: // Try to retrieve all the data:
final List<TypesTable> retrieve = DataAccess.query(TypesTable.class, querry, parameters); final List<TypesTable> retrieve = DataAccess.query(TypesTable.class, query, parameters);
Assertions.assertNotNull(retrieve); Assertions.assertNotNull(retrieve);
Assertions.assertEquals(3, retrieve.size()); Assertions.assertEquals(3, retrieve.size());
@ -102,7 +102,7 @@ public class TestRawQuery {
} }
{ {
String querry = """ String query = """
SELECT DISTINCT intData SELECT DISTINCT intData
FROM TypesTable FROM TypesTable
WHERE `intData` = ? WHERE `intData` = ?
@ -110,7 +110,7 @@ public class TestRawQuery {
"""; """;
List<Object> parameters = List.of(Integer.valueOf(99)); List<Object> parameters = List.of(Integer.valueOf(99));
// Try to retrieve all the data: // Try to retrieve all the data:
final List<TypesTable> retrieve = DataAccess.query(TypesTable.class, querry, parameters); final List<TypesTable> retrieve = DataAccess.query(TypesTable.class, query, parameters);
Assertions.assertNotNull(retrieve); Assertions.assertNotNull(retrieve);
Assertions.assertEquals(1, retrieve.size()); Assertions.assertEquals(1, retrieve.size());

View File

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

View File

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

View File

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

View File

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

View File

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