[DEV] add condition in row request and better name naming in condition

This commit is contained in:
Edouard DUPIN 2024-02-12 18:20:27 +01:00
parent 6f1dd991c5
commit 15125e2d6d
25 changed files with 484 additions and 412 deletions

View File

@ -30,5 +30,16 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

View File

@ -448,9 +448,9 @@ public class DataAccess {
// TODO: this function will replace the previous one !!! // TODO: this function will replace the previous one !!!
protected static RetreiveFromDB createSetValueFromDbCallback(final int count, final Field field) throws Exception { protected static RetreiveFromDB createSetValueFromDbCallback(final int count, final Field field) throws Exception {
Class<?> type = field.getType(); final Class<?> type = field.getType();
if (type == Long.class) { if (type == Long.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Long tmp = rs.getLong(count); final Long tmp = rs.getLong(count);
if (rs.wasNull()) { if (rs.wasNull()) {
field.set(obj, null); field.set(obj, null);
@ -460,7 +460,7 @@ public class DataAccess {
}; };
} }
if (type == long.class) { if (type == long.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Long tmp = rs.getLong(count); final Long tmp = rs.getLong(count);
if (rs.wasNull()) { if (rs.wasNull()) {
// field.set(data, null); // field.set(data, null);
@ -470,7 +470,7 @@ public class DataAccess {
}; };
} }
if (type == Integer.class) { if (type == Integer.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Integer tmp = rs.getInt(count); final Integer tmp = rs.getInt(count);
if (rs.wasNull()) { if (rs.wasNull()) {
field.set(obj, null); field.set(obj, null);
@ -480,7 +480,7 @@ public class DataAccess {
}; };
} }
if (type == int.class) { if (type == int.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Integer tmp = rs.getInt(count); final Integer tmp = rs.getInt(count);
if (rs.wasNull()) { if (rs.wasNull()) {
// field.set(obj, null); // field.set(obj, null);
@ -490,7 +490,7 @@ public class DataAccess {
}; };
} }
if (type == Float.class) { if (type == Float.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Float tmp = rs.getFloat(count); final Float tmp = rs.getFloat(count);
if (rs.wasNull()) { if (rs.wasNull()) {
field.set(obj, null); field.set(obj, null);
@ -500,7 +500,7 @@ public class DataAccess {
}; };
} }
if (type == float.class) { if (type == float.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Float tmp = rs.getFloat(count); final Float tmp = rs.getFloat(count);
if (rs.wasNull()) { if (rs.wasNull()) {
// field.set(obj, null); // field.set(obj, null);
@ -510,7 +510,7 @@ public class DataAccess {
}; };
} }
if (type == Double.class) { if (type == Double.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Double tmp = rs.getDouble(count); final Double tmp = rs.getDouble(count);
if (rs.wasNull()) { if (rs.wasNull()) {
field.set(obj, null); field.set(obj, null);
@ -520,7 +520,7 @@ public class DataAccess {
}; };
} }
if (type == double.class) { if (type == double.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Double tmp = rs.getDouble(count); final Double tmp = rs.getDouble(count);
if (rs.wasNull()) { if (rs.wasNull()) {
// field.set(obj, null); // field.set(obj, null);
@ -530,7 +530,7 @@ public class DataAccess {
}; };
} }
if (type == Boolean.class) { if (type == Boolean.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Boolean tmp = rs.getBoolean(count); final Boolean tmp = rs.getBoolean(count);
if (rs.wasNull()) { if (rs.wasNull()) {
field.set(obj, null); field.set(obj, null);
@ -540,7 +540,7 @@ public class DataAccess {
}; };
} }
if (type == boolean.class) { if (type == boolean.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Boolean tmp = rs.getBoolean(count); final Boolean tmp = rs.getBoolean(count);
if (rs.wasNull()) { if (rs.wasNull()) {
// field.set(obj, null); // field.set(obj, null);
@ -550,7 +550,7 @@ public class DataAccess {
}; };
} }
if (type == Timestamp.class) { if (type == Timestamp.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final Timestamp tmp = rs.getTimestamp(count); final Timestamp tmp = rs.getTimestamp(count);
if (rs.wasNull()) { if (rs.wasNull()) {
field.set(obj, null); field.set(obj, null);
@ -560,7 +560,7 @@ public class DataAccess {
}; };
} }
if (type == Date.class) { if (type == Date.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
try { try {
final Timestamp tmp = rs.getTimestamp(count); final Timestamp tmp = rs.getTimestamp(count);
if (rs.wasNull()) { if (rs.wasNull()) {
@ -582,7 +582,7 @@ public class DataAccess {
}; };
} }
if (type == LocalDate.class) { if (type == LocalDate.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final java.sql.Date tmp = rs.getDate(count); final java.sql.Date tmp = rs.getDate(count);
if (rs.wasNull()) { if (rs.wasNull()) {
field.set(obj, null); field.set(obj, null);
@ -592,7 +592,7 @@ public class DataAccess {
}; };
} }
if (type == LocalTime.class) { if (type == LocalTime.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final java.sql.Time tmp = rs.getTime(count); final java.sql.Time tmp = rs.getTime(count);
if (rs.wasNull()) { if (rs.wasNull()) {
field.set(obj, null); field.set(obj, null);
@ -602,7 +602,7 @@ public class DataAccess {
}; };
} }
if (type == String.class) { if (type == String.class) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final String tmp = rs.getString(count); final String tmp = rs.getString(count);
if (rs.wasNull()) { if (rs.wasNull()) {
field.set(obj, null); field.set(obj, null);
@ -612,7 +612,7 @@ public class DataAccess {
}; };
} }
if (type.isEnum()) { if (type.isEnum()) {
return (final ResultSet rs, Object obj) -> { return (final ResultSet rs, final Object obj) -> {
final String tmp = rs.getString(count); final String tmp = rs.getString(count);
if (rs.wasNull()) { if (rs.wasNull()) {
field.set(obj, null); field.set(obj, null);
@ -1508,42 +1508,51 @@ public class DataAccess {
} }
} }
/* /* - useful code to manage external query: List<T> query<T>(class<T> clazz, ); ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2"); */
- useful code to manage external query: List<T> query<T>(class<T> clazz, ); public static <TYPE> List<TYPE> query(final Class<TYPE> clazz, final String query, final List<Object> parameters, final QueryOption... option) throws Exception {
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
*/
public static <TYPE> List<TYPE> query(final Class<TYPE> clazz, String query, List<Object> parameters, final QueryOption... option) throws Exception {
final QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
return query(clazz, query, parameters, options);
}
public static <TYPE> List<TYPE> query(final Class<TYPE> clazz, final String queryBase, final List<Object> parameters, final QueryOptions options) throws Exception {
final List<LazyGetter> lazyCall = new ArrayList<>(); final List<LazyGetter> lazyCall = new ArrayList<>();
// TODO ... final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); // TODO ... final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
final DBEntry entry = DBInterfaceOption.getAutoEntry(options); final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
Condition condition = options.get(Condition.class);
if (condition == null) {
condition = new Condition();
}
final StringBuilder query = new StringBuilder(queryBase);
final List<TYPE> outs = new ArrayList<>(); final List<TYPE> outs = new ArrayList<>();
// real add in the BDD: // real add in the BDD:
try { try {
final CountInOut count = new CountInOut(); final CountInOut count = new CountInOut();
condition.whereAppendQuery(query, null, options, null);
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, 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);
if (parameters != null) { if (parameters != null) {
for (Object elem : parameters) { for (final Object elem : parameters) {
DataAccess.addElement(ps, elem, iii); DataAccess.addElement(ps, elem, iii);
} }
iii.inc(); iii.inc();
} }
condition.injectQuerry(ps, iii);
// execute the request // execute the request
final ResultSet rs = ps.executeQuery(); final ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData(); final ResultSetMetaData rsmd = rs.getMetaData();
List<RetreiveFromDB> actionToRetreive = new ArrayList<>(); final List<RetreiveFromDB> actionToRetreive = new ArrayList<>();
for (int jjj = 0; jjj < rsmd.getColumnCount(); jjj++) { for (int jjj = 0; jjj < rsmd.getColumnCount(); jjj++) {
String name = rsmd.getColumnName(jjj + 1); final String name = rsmd.getColumnName(jjj + 1);
// find field name ... // find field name ...
Field field = AnnotationTools.getFieldNamed(clazz, name); final Field field = AnnotationTools.getFieldNamed(clazz, name);
if (field == null) { if (field == null) {
throw new DataAccessException("Querry with unknown field: '" + name + "'"); throw new DataAccessException("Querry with unknown field: '" + name + "'");
} }
// create the callback... // create the callback...
RetreiveFromDB element = createSetValueFromDbCallback(jjj + 1, field); final RetreiveFromDB element = createSetValueFromDbCallback(jjj + 1, field);
actionToRetreive.add(element); actionToRetreive.add(element);
} }
@ -1559,7 +1568,7 @@ public class DataAccess {
if (data == null) { if (data == null) {
// TODO... // TODO...
} else { } else {
for (RetreiveFromDB action : actionToRetreive) { for (final RetreiveFromDB action : actionToRetreive) {
action.doRequest(rs, data); action.doRequest(rs, data);
} }
} }

View File

@ -17,6 +17,10 @@ public class QueryAnd implements QueryItem {
Collections.addAll(this.childs, items); Collections.addAll(this.childs, items);
} }
public void add(final QueryItem... items) {
Collections.addAll(this.childs, items);
}
@Override @Override
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuerry(final StringBuilder query, final String tableName) {
if (this.childs.size() >= 1) { if (this.childs.size() >= 1) {
@ -43,4 +47,8 @@ public class QueryAnd implements QueryItem {
elem.injectQuerry(ps, iii); elem.injectQuerry(ps, iii);
} }
} }
public int size() {
return this.childs.size();
}
} }

View File

@ -15,8 +15,10 @@ public class QueryCondition implements QueryItem {
@Override @Override
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuerry(final StringBuilder query, final String tableName) {
query.append(tableName); if (tableName != null) {
query.append("."); query.append(tableName);
query.append(".");
}
query.append(this.key); query.append(this.key);
query.append(" "); query.append(" ");
query.append(this.comparator); query.append(this.comparator);

View File

@ -24,8 +24,10 @@ public class QueryInList<T> implements QueryItem {
@Override @Override
public void generateQuerry(final StringBuilder query, final String tableName) { public void generateQuerry(final StringBuilder query, final String tableName) {
query.append(tableName); if (tableName != null) {
query.append("."); query.append(tableName);
query.append(".");
}
query.append(this.key); query.append(this.key);
query.append(" "); query.append(" ");
query.append(this.comparator); query.append(this.comparator);

View File

@ -0,0 +1,24 @@
package org.kar.archidata.dataAccess;
import java.sql.PreparedStatement;
public class QueryNotNull implements QueryItem {
private final String key;
public QueryNotNull(final String key) {
this.key = key;
}
@Override
public void generateQuerry(final StringBuilder query, final String tableName) {
if (tableName != null) {
query.append(tableName);
query.append(".");
}
query.append(this.key);
query.append(" IS NOT NULL");
}
@Override
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {}
}

View File

@ -0,0 +1,24 @@
package org.kar.archidata.dataAccess;
import java.sql.PreparedStatement;
public class QueryNull implements QueryItem {
private final String key;
public QueryNull(final String key) {
this.key = key;
}
@Override
public void generateQuerry(final StringBuilder query, final String tableName) {
if (tableName != null) {
query.append(tableName);
query.append(".");
}
query.append(this.key);
query.append(" IS NULL");
}
@Override
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {}
}

View File

@ -0,0 +1,49 @@
package org.kar.archidata.serializer;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.Provider;
/** Body writer use in jersey with : In your main: ```java rc.register(new CSVMessageBodyWritter()); ```
*
* and in the produce element: ```java
* @GET
* @Produces(CSVMessageBodyWritter.CSV_TYPE) public List<Data> getData() {} ``` */
@Provider
@Produces("text/csv")
public class CSVMessageBodyWriter implements MessageBodyWriter<List<Object>> {
public static final String CSV_TYPE = "text/csv";
@Override
public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
return List.class.isAssignableFrom(type);
}
@Override
public long getSize(final List<Object> data, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
return 0;
}
@Override
public void writeTo(final List<Object> data, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType,
final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException {
if (data != null && data.size() > 0) {
final CsvMapper mapper = new CsvMapper();
final Object o = data.get(0);
final CsvSchema schema = mapper.schemaFor(o.getClass()).withHeader();
mapper.writer(schema).writeValue(entityStream, data);
}
}
}

View File

@ -1,57 +0,0 @@
package org.kar.archidata.serializer;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.Provider;
/**
* Body writter use in jersey with :
* In your main:
* ```java
* rc.register(new CSVMessageBodyWritter());
* ```
*
* and in the produce element:
* ```java
* @GET
* @Produces("text/csv")
* public List<Data> getData() {}
* ```
*/
@Provider
@Produces("text/csv")
public class CSVMessageBodyWritter implements MessageBodyWriter<List<?>> {
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return List.class.isAssignableFrom(type);
}
@Override
public long getSize(List<?> data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return 0;
}
@Override
public void writeTo(List<?> data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
if (data != null && data.size() > 0) {
CsvMapper mapper = new CsvMapper();
Object o = data.get(0);
CsvSchema schema = mapper.schemaFor(o.getClass()).withHeader();
mapper.writer(schema).writeValue(entityStream, data);
}
}
}