[DEV] add condition in row request and better name naming in condition
This commit is contained in:
parent
6f1dd991c5
commit
15125e2d6d
11
.classpath
11
.classpath
@ -30,5 +30,16 @@
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</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"/>
|
||||
</classpath>
|
||||
|
@ -448,9 +448,9 @@ public class DataAccess {
|
||||
|
||||
// TODO: this function will replace the previous one !!!
|
||||
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) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Long tmp = rs.getLong(count);
|
||||
if (rs.wasNull()) {
|
||||
field.set(obj, null);
|
||||
@ -460,7 +460,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == long.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Long tmp = rs.getLong(count);
|
||||
if (rs.wasNull()) {
|
||||
// field.set(data, null);
|
||||
@ -470,7 +470,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == Integer.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Integer tmp = rs.getInt(count);
|
||||
if (rs.wasNull()) {
|
||||
field.set(obj, null);
|
||||
@ -480,7 +480,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == int.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Integer tmp = rs.getInt(count);
|
||||
if (rs.wasNull()) {
|
||||
// field.set(obj, null);
|
||||
@ -490,7 +490,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == Float.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Float tmp = rs.getFloat(count);
|
||||
if (rs.wasNull()) {
|
||||
field.set(obj, null);
|
||||
@ -500,7 +500,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == float.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Float tmp = rs.getFloat(count);
|
||||
if (rs.wasNull()) {
|
||||
// field.set(obj, null);
|
||||
@ -510,7 +510,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == Double.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Double tmp = rs.getDouble(count);
|
||||
if (rs.wasNull()) {
|
||||
field.set(obj, null);
|
||||
@ -520,7 +520,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == double.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Double tmp = rs.getDouble(count);
|
||||
if (rs.wasNull()) {
|
||||
// field.set(obj, null);
|
||||
@ -530,7 +530,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == Boolean.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Boolean tmp = rs.getBoolean(count);
|
||||
if (rs.wasNull()) {
|
||||
field.set(obj, null);
|
||||
@ -540,7 +540,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == boolean.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Boolean tmp = rs.getBoolean(count);
|
||||
if (rs.wasNull()) {
|
||||
// field.set(obj, null);
|
||||
@ -550,7 +550,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == Timestamp.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final Timestamp tmp = rs.getTimestamp(count);
|
||||
if (rs.wasNull()) {
|
||||
field.set(obj, null);
|
||||
@ -560,7 +560,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == Date.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
try {
|
||||
final Timestamp tmp = rs.getTimestamp(count);
|
||||
if (rs.wasNull()) {
|
||||
@ -582,7 +582,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
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);
|
||||
if (rs.wasNull()) {
|
||||
field.set(obj, null);
|
||||
@ -592,7 +592,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
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);
|
||||
if (rs.wasNull()) {
|
||||
field.set(obj, null);
|
||||
@ -602,7 +602,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type == String.class) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final String tmp = rs.getString(count);
|
||||
if (rs.wasNull()) {
|
||||
field.set(obj, null);
|
||||
@ -612,7 +612,7 @@ public class DataAccess {
|
||||
};
|
||||
}
|
||||
if (type.isEnum()) {
|
||||
return (final ResultSet rs, Object obj) -> {
|
||||
return (final ResultSet rs, final Object obj) -> {
|
||||
final String tmp = rs.getString(count);
|
||||
if (rs.wasNull()) {
|
||||
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");
|
||||
*/
|
||||
public static <TYPE> List<TYPE> query(final Class<TYPE> clazz, String query, List<Object> parameters, final QueryOption... option) throws Exception {
|
||||
/* - useful code to manage external query: List<T> query<T>(class<T> clazz, ); ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2"); */
|
||||
public static <TYPE> List<TYPE> query(final Class<TYPE> clazz, final String query, final List<Object> parameters, final QueryOption... option) throws Exception {
|
||||
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<>();
|
||||
// TODO ... final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||
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<>();
|
||||
// real add in the BDD:
|
||||
try {
|
||||
final CountInOut count = new CountInOut();
|
||||
condition.whereAppendQuery(query, null, options, null);
|
||||
LOGGER.warn("generate the query: '{}'", query.toString());
|
||||
// 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);
|
||||
if (parameters != null) {
|
||||
for (Object elem : parameters) {
|
||||
for (final Object elem : parameters) {
|
||||
DataAccess.addElement(ps, elem, iii);
|
||||
}
|
||||
iii.inc();
|
||||
}
|
||||
condition.injectQuerry(ps, iii);
|
||||
// execute the request
|
||||
final ResultSet rs = ps.executeQuery();
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
List<RetreiveFromDB> actionToRetreive = new ArrayList<>();
|
||||
final ResultSetMetaData rsmd = rs.getMetaData();
|
||||
final List<RetreiveFromDB> actionToRetreive = new ArrayList<>();
|
||||
for (int jjj = 0; jjj < rsmd.getColumnCount(); jjj++) {
|
||||
String name = rsmd.getColumnName(jjj + 1);
|
||||
final String name = rsmd.getColumnName(jjj + 1);
|
||||
// find field name ...
|
||||
Field field = AnnotationTools.getFieldNamed(clazz, name);
|
||||
final Field field = AnnotationTools.getFieldNamed(clazz, name);
|
||||
if (field == null) {
|
||||
throw new DataAccessException("Querry with unknown field: '" + name + "'");
|
||||
}
|
||||
// create the callback...
|
||||
RetreiveFromDB element = createSetValueFromDbCallback(jjj + 1, field);
|
||||
final RetreiveFromDB element = createSetValueFromDbCallback(jjj + 1, field);
|
||||
actionToRetreive.add(element);
|
||||
}
|
||||
|
||||
@ -1559,7 +1568,7 @@ public class DataAccess {
|
||||
if (data == null) {
|
||||
// TODO...
|
||||
} else {
|
||||
for (RetreiveFromDB action : actionToRetreive) {
|
||||
for (final RetreiveFromDB action : actionToRetreive) {
|
||||
action.doRequest(rs, data);
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,10 @@ public class QueryAnd implements QueryItem {
|
||||
Collections.addAll(this.childs, items);
|
||||
}
|
||||
|
||||
public void add(final QueryItem... items) {
|
||||
Collections.addAll(this.childs, items);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateQuerry(final StringBuilder query, final String tableName) {
|
||||
if (this.childs.size() >= 1) {
|
||||
@ -43,4 +47,8 @@ public class QueryAnd implements QueryItem {
|
||||
elem.injectQuerry(ps, iii);
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.childs.size();
|
||||
}
|
||||
}
|
||||
|
@ -15,8 +15,10 @@ public class QueryCondition implements QueryItem {
|
||||
|
||||
@Override
|
||||
public void generateQuerry(final StringBuilder query, final String tableName) {
|
||||
query.append(tableName);
|
||||
query.append(".");
|
||||
if (tableName != null) {
|
||||
query.append(tableName);
|
||||
query.append(".");
|
||||
}
|
||||
query.append(this.key);
|
||||
query.append(" ");
|
||||
query.append(this.comparator);
|
||||
|
@ -24,8 +24,10 @@ public class QueryInList<T> implements QueryItem {
|
||||
|
||||
@Override
|
||||
public void generateQuerry(final StringBuilder query, final String tableName) {
|
||||
query.append(tableName);
|
||||
query.append(".");
|
||||
if (tableName != null) {
|
||||
query.append(tableName);
|
||||
query.append(".");
|
||||
}
|
||||
query.append(this.key);
|
||||
query.append(" ");
|
||||
query.append(this.comparator);
|
||||
|
24
src/org/kar/archidata/dataAccess/QueryNotNull.java
Normal file
24
src/org/kar/archidata/dataAccess/QueryNotNull.java
Normal 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 {}
|
||||
}
|
24
src/org/kar/archidata/dataAccess/QueryNull.java
Normal file
24
src/org/kar/archidata/dataAccess/QueryNull.java
Normal 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 {}
|
||||
}
|
49
src/org/kar/archidata/serializer/CSVMessageBodyWriter.java
Normal file
49
src/org/kar/archidata/serializer/CSVMessageBodyWriter.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user