diff --git a/src/org/kar/archidata/dataAccess/DataAccess.java b/src/org/kar/archidata/dataAccess/DataAccess.java index 9f5b136..57c1130 100644 --- a/src/org/kar/archidata/dataAccess/DataAccess.java +++ b/src/org/kar/archidata/dataAccess/DataAccess.java @@ -25,9 +25,9 @@ import org.kar.archidata.dataAccess.addOn.AddOnDataJson; import org.kar.archidata.dataAccess.addOn.AddOnManyToMany; import org.kar.archidata.dataAccess.addOn.AddOnManyToOne; import org.kar.archidata.dataAccess.addOn.AddOnSQLTableExternalForeinKeyAsList; -import org.kar.archidata.dataAccess.options.AccessDeletedItems; import org.kar.archidata.dataAccess.options.CheckFunction; import org.kar.archidata.dataAccess.options.Condition; +import org.kar.archidata.dataAccess.options.FilterValue; import org.kar.archidata.db.DBEntry; import org.kar.archidata.exception.DataAccessException; import org.kar.archidata.tools.ConfigBaseVariable; @@ -679,8 +679,8 @@ public class DataAccess { return update(data, id, null); } - public static int updateWhere(final T data, final QueryItem condition) throws Exception { - return updateWhere(data, condition, null, null); + public static int updateWhere(final T data, final QueryOptions options) throws Exception { + return updateWhere(data, options, null); } /** @param @@ -690,11 +690,22 @@ public class DataAccess { * @return the affected rows. * @throws Exception */ public static int update(final T data, final ID_TYPE id, final List filterValue) throws Exception { - return updateWhere(data, getTableIdCondition(data.getClass(), id), null, filterValue); + return updateWhere(data, new Condition(getTableIdCondition(data.getClass(), id)), filterValue); } - public static int updateWhere(final T data, final QueryItem condition, final QueryOptions options, final List filterValue) throws Exception { + // il y avait: final List filterValue + public static int updateWhere(final T data, final QueryOption... option) throws Exception { final Class clazz = data.getClass(); + QueryOptions options = new QueryOptions(option); + Condition condition = options.get(Condition.class); + if (condition == null) { + throw new DataAccessException("request a gets without any condition"); + } + FilterValue filter = options.get(FilterValue.class); + if (filter == null) { + throw new DataAccessException("request a gets without any filter values"); + } + // public static NodeSmall createNode(String typeInNode, String name, String description, Long parentId) { // External checker of data: @@ -755,7 +766,7 @@ public class DataAccess { } query.append(" "); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); - whereAppendQuery(query, tableName, condition, null, deletedFieldName); + condition.whereAppendQuery(query, tableName, null, deletedFieldName); firstField = true; LOGGER.debug("generate the query: '{}'", query.toString()); // prepare the request: @@ -791,7 +802,7 @@ public class DataAccess { addOn.insertData(ps, field, data, iii); } } - whereInjectValue(ps, condition, iii); + condition.injectQuerry(ps, iii); return ps.executeUpdate(); } catch (final SQLException ex) { ex.printStackTrace(); @@ -834,45 +845,6 @@ public class DataAccess { } } - public static void whereAppendQuery(final StringBuilder query, final String tableName, final QueryItem condition, final QueryOptions options, final String deletedFieldName) { - boolean exclude_deleted = true; - if (options != null) { - exclude_deleted = !options.exist(AccessDeletedItems.class); - } - // Check if we have a condition to generate - if (condition == null) { - if (exclude_deleted && deletedFieldName != null) { - query.append(" WHERE "); - query.append(tableName); - query.append("."); - query.append(deletedFieldName); - query.append(" = false "); - } - return; - } - query.append(" WHERE ("); - condition.generateQuerry(query, tableName); - - query.append(") "); - if (exclude_deleted && deletedFieldName != null) { - query.append("AND "); - query.append(tableName); - query.append("."); - query.append(deletedFieldName); - query.append(" = false "); - } - } - - public static void whereInjectValue(final PreparedStatement ps, final QueryOptions options, final CountInOut iii) throws Exception { - // Check if we have a condition to generate - if (options != null) { - final Condition condition = options.get(Condition.class); - if (condition != null) { - condition.injectQuerry(ps, iii); - } - } - } - public static int executeSimpleQuerry(final String query, final boolean root) throws SQLException, IOException { final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, root); final Statement stmt = entry.connection.createStatement(); @@ -893,15 +865,9 @@ public class DataAccess { return executeQuerry(query, false); } - public static T getWhere(final Class clazz, QueryOptions options) throws Exception { - return getWhere(clazz, options); - if (options == null) { - options = new QueryOptions(); - } - final Limit limit = options.get(Limit.class); - if (limit != null) { - options.add(new Limit(1)); - } + public static T getWhere(final Class clazz, QueryOption... option) throws Exception { + QueryOptions options = new QueryOptions(option); + options.add(new Limit(1)); final List values = getsWhere(clazz, options); if (values.size() == 0) { return null; @@ -909,9 +875,7 @@ public class DataAccess { return values.get(0); } - public static List getsWhere(final Class clazz, final QueryItem condition) throws Exception { - return getsWhere(clazz, condition, null); - } + /* public static List getsWhere(final Class clazz, final QueryItem condition) throws Exception { return getsWhere(clazz, condition, null); } */ 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); @@ -949,8 +913,17 @@ public class DataAccess { } } + public static List getsWhere(final Class clazz, final QueryOption... option) throws Exception { + QueryOptions options = new QueryOptions(option); + return getsWhere(clazz, options); + } + @SuppressWarnings("unchecked") - public static List getsWhere(final Class clazz, final QueryItem condition, final QueryOptions options) throws Exception { + public static List getsWhere(final Class clazz, final QueryOptions options) throws Exception { + Condition condition = options.get(Condition.class); + if (condition == null) { + throw new DataAccessException("request a gets without any condition"); + } final List lazyCall = new ArrayList<>(); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); @@ -969,7 +942,7 @@ public class DataAccess { generateSelectField(querySelect, query, clazz, options, count); querySelect.append(query.toString()); query = querySelect; - whereAppendQuery(query, tableName, condition, options, deletedFieldName); + condition.whereAppendQuery(query, tableName, options, deletedFieldName); final OrderBy orders = options.get(OrderBy.class); if (orders != null) { orders.generateQuerry(query, tableName); @@ -982,7 +955,7 @@ public class DataAccess { // prepare the request: final PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS); final CountInOut iii = new CountInOut(1); - whereInjectValue(ps, condition, iii); + condition.injectQuerry(ps, iii); // execute the request final ResultSet rs = ps.executeQuery(); while (rs.next()) { @@ -1039,8 +1012,10 @@ public class DataAccess { return get(clazz, id, null); } - public static T get(final Class clazz, final ID_TYPE id, final QueryOptions options) throws Exception { - return DataAccess.getWhere(clazz, getTableIdCondition(clazz, id), options); + public static T get(final Class clazz, final ID_TYPE id, final QueryOption... option) throws Exception { + QueryOptions options = new QueryOptions(option); + options.add(new Condition(getTableIdCondition(clazz, id))); + return DataAccess.getWhere(clazz, options.getAllArray()); } public static String getCurrentTimeStamp() { @@ -1048,11 +1023,11 @@ public class DataAccess { } public static List gets(final Class clazz) throws Exception { - return getsWhere(clazz, null); + return getsWhere(clazz); } - public static List gets(final Class clazz, final QueryOptions options) throws Exception { - return getsWhere(clazz, null, options); + public static List gets(final Class clazz, final QueryOption... option) throws Exception { + return getsWhere(clazz, option); } public static int delete(final Class clazz, final ID_TYPE id) throws Exception { @@ -1065,7 +1040,7 @@ public class DataAccess { * @param id Unique Id of the model * @param options (Optional) Options of the request * @return Number of element that is removed. */ - public static int delete(final Class clazz, final ID_TYPE id, final QueryOptions options) throws Exception { + public static int delete(final Class clazz, final ID_TYPE id, final QueryOption... options) throws Exception { final String hasDeletedFieldName = AnnotationTools.getDeletedFieldName(clazz); if (hasDeletedFieldName != null) { return deleteSoft(clazz, id, options); @@ -1079,20 +1054,28 @@ public class DataAccess { * @param condition Condition to remove elements. * @param options (Optional) Options of the request. * @return Number of element that is removed. */ - public static int deleteWhere(final Class clazz, final QueryItem condition, final QueryOptions options) throws Exception { + public static int deleteWhere(final Class clazz, final QueryOption... option) throws Exception { + final String hasDeletedFieldName = AnnotationTools.getDeletedFieldName(clazz); if (hasDeletedFieldName != null) { - return deleteSoftWhere(clazz, condition, options); + return deleteSoftWhere(clazz, option); } else { - return deleteHardWhere(clazz, condition, options); + return deleteHardWhere(clazz, option); } } - public static int deleteHard(final Class clazz, final ID_TYPE id, final QueryOptions options) throws Exception { - return deleteHardWhere(clazz, getTableIdCondition(clazz, id), options); + public static int deleteHard(final Class clazz, final ID_TYPE id, final QueryOption... option) throws Exception { + QueryOptions options = new QueryOptions(option); + options.add(new Condition(getTableIdCondition(clazz, id))); + return deleteHardWhere(clazz, options.getAllArray()); } - public static int deleteHardWhere(final Class clazz, final QueryItem condition, final QueryOptions options) throws Exception { + public static int deleteHardWhere(final Class clazz, final QueryOption... option) throws Exception { + QueryOptions options = new QueryOptions(option); + Condition condition = options.get(Condition.class); + if (condition == null) { + throw new DataAccessException("request a gets without any condition"); + } final String tableName = AnnotationTools.getTableName(clazz, options); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); // find the deleted field @@ -1102,12 +1085,12 @@ public class DataAccess { query.append("DELETE FROM `"); query.append(tableName); query.append("` "); - whereAppendQuery(query, tableName, condition, null, deletedFieldName); + condition.whereAppendQuery(query, tableName, null, deletedFieldName); try { LOGGER.debug("APPLY: {}", query.toString()); final PreparedStatement ps = entry.connection.prepareStatement(query.toString()); final CountInOut iii = new CountInOut(1); - whereInjectValue(ps, condition, iii); + condition.injectQuerry(ps, iii); return ps.executeUpdate(); } finally { entry.close(); @@ -1115,11 +1098,18 @@ public class DataAccess { } } - private static int deleteSoft(final Class clazz, final ID_TYPE id, final QueryOptions options) throws Exception { - return deleteSoftWhere(clazz, getTableIdCondition(clazz, id), options); + private static int deleteSoft(final Class clazz, final ID_TYPE id, final QueryOption... option) throws Exception { + QueryOptions options = new QueryOptions(option); + options.add(new Condition(getTableIdCondition(clazz, id))); + return deleteSoftWhere(clazz, options.getAllArray()); } - public static int deleteSoftWhere(final Class clazz, final QueryItem condition, final QueryOptions options) throws Exception { + public static int deleteSoftWhere(final Class clazz, final QueryOption... option) throws Exception { + QueryOptions options = new QueryOptions(option); + Condition condition = options.get(Condition.class); + if (condition == null) { + throw new DataAccessException("request a gets without any condition"); + } final String tableName = AnnotationTools.getTableName(clazz, options); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); /* String updateFieldName = null; if ("sqlite".equalsIgnoreCase(ConfigBaseVariable.getDBType())) { updateFieldName = AnnotationTools.getUpdatedFieldName(clazz); } */ @@ -1134,12 +1124,12 @@ public class DataAccess { query.append("`=true "); /* The trigger work well, but the timestamp is store @ seconds... if (updateFieldName != null) { // done only in SQLite (the trigger does not work... query.append(", `"); * query.append(updateFieldName); query.append("`=DATE()"); } */ - whereAppendQuery(query, tableName, condition, null, deletedFieldName); + condition.whereAppendQuery(query, tableName, null, deletedFieldName); try { LOGGER.debug("APPLY UPDATE: {}", query.toString()); final PreparedStatement ps = entry.connection.prepareStatement(query.toString()); final CountInOut iii = new CountInOut(1); - whereInjectValue(ps, condition, iii); + condition.injectQuerry(ps, iii); return ps.executeUpdate(); } finally { entry.close(); @@ -1148,14 +1138,21 @@ public class DataAccess { } public static int unsetDelete(final Class clazz, final ID_TYPE id) throws Exception { - return unsetDeleteWhere(clazz, getTableIdCondition(clazz, id), null); + return unsetDeleteWhere(clazz, new Condition(getTableIdCondition(clazz, id))); } - public static int unsetDelete(final Class clazz, final ID_TYPE id, final QueryOptions options) throws Exception { - return unsetDeleteWhere(clazz, getTableIdCondition(clazz, id), options); + public static int unsetDelete(final Class clazz, final ID_TYPE id, final QueryOption... option) throws Exception { + QueryOptions options = new QueryOptions(option); + options.add(new Condition(getTableIdCondition(clazz, id))); + return unsetDeleteWhere(clazz, options.getAllArray()); } - public static int unsetDeleteWhere(final Class clazz, final QueryItem condition, final QueryOptions options) throws Exception { + public static int unsetDeleteWhere(final Class clazz, final QueryOption... option) throws Exception { + QueryOptions options = new QueryOptions(option); + Condition condition = options.get(Condition.class); + if (condition == null) { + throw new DataAccessException("request a gets without any condition"); + } final String tableName = AnnotationTools.getTableName(clazz, options); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); if (deletedFieldName == null) { @@ -1170,11 +1167,11 @@ public class DataAccess { query.append("`=false "); // need to disable the deleted false because the model must be unselected to be updated. options.add(QueryOptions.ACCESS_DELETED_ITEMS); - whereAppendQuery(query, tableName, condition, options, deletedFieldName); + condition.whereAppendQuery(query, tableName, options, deletedFieldName); try { final PreparedStatement ps = entry.connection.prepareStatement(query.toString()); final CountInOut iii = new CountInOut(1); - whereInjectValue(ps, condition, iii); + condition.injectQuerry(ps, iii); return ps.executeUpdate(); } finally { entry.close(); diff --git a/src/org/kar/archidata/dataAccess/QueryOptions.java b/src/org/kar/archidata/dataAccess/QueryOptions.java index 4028218..d552e58 100644 --- a/src/org/kar/archidata/dataAccess/QueryOptions.java +++ b/src/org/kar/archidata/dataAccess/QueryOptions.java @@ -32,6 +32,10 @@ public class QueryOptions { return this.options; } + public QueryOption[] getAllArray() { + return this.options.toArray(new QueryOption[0]); + } + @SuppressWarnings("unchecked") public T get(final Class type) { for (final QueryOption elem : this.options) { diff --git a/src/org/kar/archidata/dataAccess/addOn/AddOnManyToMany.java b/src/org/kar/archidata/dataAccess/addOn/AddOnManyToMany.java index fdd1557..e0718ff 100644 --- a/src/org/kar/archidata/dataAccess/addOn/AddOnManyToMany.java +++ b/src/org/kar/archidata/dataAccess/addOn/AddOnManyToMany.java @@ -19,6 +19,7 @@ import org.kar.archidata.dataAccess.QueryCondition; import org.kar.archidata.dataAccess.QueryInList; import org.kar.archidata.dataAccess.QueryOptions; import org.kar.archidata.dataAccess.addOn.model.LinkTable; +import org.kar.archidata.dataAccess.options.Condition; import org.kar.archidata.dataAccess.options.OverrideTableName; import org.kar.archidata.exception.DataAccessException; import org.kar.archidata.tools.ConfigBaseVariable; @@ -85,7 +86,7 @@ public class AddOnManyToMany implements DataAccessAddOn { querrySelect.append(" (SELECT GROUP_CONCAT("); querrySelect.append(tmpVariable); querrySelect.append(".object2Id "); - if (ConfigBaseVariable.getDBType().equals("sqlite")) { + if ("sqlite".equals(ConfigBaseVariable.getDBType())) { querrySelect.append(", "); } else { querrySelect.append("SEPARATOR "); @@ -104,7 +105,7 @@ public class AddOnManyToMany implements DataAccessAddOn { querrySelect.append(tmpVariable); querrySelect.append("."); querrySelect.append("object1Id "); - if (!ConfigBaseVariable.getDBType().equals("sqlite")) { + if (!"sqlite".equals(ConfigBaseVariable.getDBType())) { querrySelect.append(" GROUP BY "); querrySelect.append(tmpVariable); querrySelect.append(".object1Id"); @@ -169,7 +170,7 @@ public class AddOnManyToMany implements DataAccessAddOn { final List childs = new ArrayList<>(idList); // TODO: update to have get with abstract types .... @SuppressWarnings("unchecked") - final Object foreignData = DataAccess.getsWhere(decorators.targetEntity(), new QueryInList<>(idField, childs), null); + final Object foreignData = DataAccess.getsWhere(decorators.targetEntity(), new Condition(new QueryInList<>(idField, childs))); if (foreignData == null) { return; } diff --git a/src/org/kar/archidata/dataAccess/options/Condition.java b/src/org/kar/archidata/dataAccess/options/Condition.java index 3071fcb..bc1944b 100644 --- a/src/org/kar/archidata/dataAccess/options/Condition.java +++ b/src/org/kar/archidata/dataAccess/options/Condition.java @@ -5,6 +5,7 @@ import java.sql.PreparedStatement; import org.kar.archidata.dataAccess.CountInOut; import org.kar.archidata.dataAccess.QueryItem; import org.kar.archidata.dataAccess.QueryOption; +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 Condition extends QueryOption { @@ -25,4 +26,33 @@ public class Condition extends QueryOption { this.condition.injectQuerry(ps, iii); } } + + public void whereAppendQuery(final StringBuilder query, final String tableName, final QueryOptions options, final String deletedFieldName) { + boolean exclude_deleted = true; + if (options != null) { + exclude_deleted = !options.exist(AccessDeletedItems.class); + } + // Check if we have a condition to generate + if (this.condition == null) { + if (exclude_deleted && deletedFieldName != null) { + query.append(" WHERE "); + query.append(tableName); + query.append("."); + query.append(deletedFieldName); + query.append(" = false "); + } + return; + } + query.append(" WHERE ("); + this.condition.generateQuerry(query, tableName); + + query.append(") "); + if (exclude_deleted && deletedFieldName != null) { + query.append("AND "); + query.append(tableName); + query.append("."); + query.append(deletedFieldName); + query.append(" = false "); + } + } } diff --git a/src/org/kar/archidata/dataAccess/options/FilterValue.java b/src/org/kar/archidata/dataAccess/options/FilterValue.java new file mode 100644 index 0000000..de20c0f --- /dev/null +++ b/src/org/kar/archidata/dataAccess/options/FilterValue.java @@ -0,0 +1,22 @@ +package org.kar.archidata.dataAccess.options; + +import java.util.List; + +import org.kar.archidata.dataAccess.QueryOption; + +/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */ +public class FilterValue extends QueryOption { + public final List filterValue; + + public FilterValue(final List filterValue) { + this.filterValue = filterValue; + } + + public FilterValue(final String... filterValue) { + this.filterValue = List.of(filterValue); + } + + public List getValues() { + return this.filterValue; + } +}