[DEV] add sub-element with object and serialization as json
This commit is contained in:
parent
bc5c37e02a
commit
f69bc8097a
@ -47,25 +47,25 @@ public class AnnotationTools {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String getComment(final Field element) throws Exception {
|
public static String getComment(final Field element) throws Exception {
|
||||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(SQLComment.class);
|
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DataComment.class);
|
||||||
if (annotation.length == 0) {
|
if (annotation.length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (annotation.length > 1) {
|
if (annotation.length > 1) {
|
||||||
throw new Exception("Must not have more than 1 element @SQLComment on " + element.getClass().getCanonicalName());
|
throw new Exception("Must not have more than 1 element @SQLComment on " + element.getClass().getCanonicalName());
|
||||||
}
|
}
|
||||||
return ((SQLComment) annotation[0]).value();
|
return ((DataComment) annotation[0]).value();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getDefault(final Field element) throws Exception {
|
public static String getDefault(final Field element) throws Exception {
|
||||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(SQLDefault.class);
|
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DataDefault.class);
|
||||||
if (annotation.length == 0) {
|
if (annotation.length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (annotation.length > 1) {
|
if (annotation.length > 1) {
|
||||||
throw new Exception("Must not have more than 1 element @SQLDefault on " + element.getClass().getCanonicalName());
|
throw new Exception("Must not have more than 1 element @SQLDefault on " + element.getClass().getCanonicalName());
|
||||||
}
|
}
|
||||||
return ((SQLDefault) annotation[0]).value();
|
return ((DataDefault) annotation[0]).value();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Integer getLimitSize(final Field element) throws Exception {
|
public static Integer getLimitSize(final Field element) throws Exception {
|
||||||
@ -151,7 +151,7 @@ public class AnnotationTools {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isDeletedField(final Field element) throws Exception {
|
public static boolean isDeletedField(final Field element) throws Exception {
|
||||||
return element.getDeclaredAnnotationsByType(SQLDeleted.class).length != 0;
|
return element.getDeclaredAnnotationsByType(DataDeleted.class).length != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isCreatedAtField(final Field element) throws Exception {
|
public static boolean isCreatedAtField(final Field element) throws Exception {
|
||||||
@ -163,7 +163,7 @@ public class AnnotationTools {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isdefaultNotRead(final Field element) throws Exception {
|
public static boolean isdefaultNotRead(final Field element) throws Exception {
|
||||||
return element.getDeclaredAnnotationsByType(SQLNotRead.class).length != 0;
|
return element.getDeclaredAnnotationsByType(DataNotRead.class).length != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isIdField(final Field element) throws Exception {
|
public static boolean isIdField(final Field element) throws Exception {
|
||||||
|
@ -7,7 +7,7 @@ import java.lang.annotation.Target;
|
|||||||
|
|
||||||
@Target({ ElementType.TYPE, ElementType.FIELD })
|
@Target({ ElementType.TYPE, ElementType.FIELD })
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface SQLComment {
|
public @interface DataComment {
|
||||||
|
|
||||||
String value();
|
String value();
|
||||||
|
|
@ -7,7 +7,7 @@ import java.lang.annotation.Target;
|
|||||||
|
|
||||||
@Target({ ElementType.TYPE, ElementType.FIELD })
|
@Target({ ElementType.TYPE, ElementType.FIELD })
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface SQLDefault {
|
public @interface DataDefault {
|
||||||
|
|
||||||
String value();
|
String value();
|
||||||
|
|
@ -7,6 +7,6 @@ import java.lang.annotation.Target;
|
|||||||
|
|
||||||
@Target(ElementType.FIELD)
|
@Target(ElementType.FIELD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface SQLNotRead {
|
public @interface DataDeleted {
|
||||||
|
|
||||||
}
|
}
|
@ -7,6 +7,6 @@ import java.lang.annotation.Target;
|
|||||||
|
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface SQLIfNotExists {
|
public @interface DataIfNotExists {
|
||||||
|
|
||||||
}
|
}
|
@ -5,8 +5,6 @@ import java.lang.annotation.Retention;
|
|||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
@Target(ElementType.ANNOTATION_TYPE)
|
@Target({ ElementType.TYPE, ElementType.FIELD })
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface DataAddOn {
|
public @interface DataJson {}
|
||||||
|
|
||||||
}
|
|
@ -7,6 +7,6 @@ import java.lang.annotation.Target;
|
|||||||
|
|
||||||
@Target(ElementType.FIELD)
|
@Target(ElementType.FIELD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface SQLDeleted {
|
public @interface DataNotRead {
|
||||||
|
|
||||||
}
|
}
|
@ -1,15 +0,0 @@
|
|||||||
package org.kar.archidata.annotation.addOn;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
import org.kar.archidata.annotation.DataAddOn;
|
|
||||||
|
|
||||||
@Target(ElementType.FIELD)
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@DataAddOn
|
|
||||||
public @interface DataAddOnManyToManyOrdered {
|
|
||||||
|
|
||||||
}
|
|
@ -5,11 +5,8 @@ import java.lang.annotation.Retention;
|
|||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.DataAddOn;
|
|
||||||
|
|
||||||
@Target(ElementType.FIELD)
|
@Target(ElementType.FIELD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@DataAddOn
|
|
||||||
public @interface SQLTableExternalForeinKeyAsList {
|
public @interface SQLTableExternalForeinKeyAsList {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
17
src/org/kar/archidata/dataAccess/CountInOut.java
Normal file
17
src/org/kar/archidata/dataAccess/CountInOut.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package org.kar.archidata.dataAccess;
|
||||||
|
|
||||||
|
public class CountInOut {
|
||||||
|
public int value = 0;
|
||||||
|
|
||||||
|
public CountInOut() {
|
||||||
|
// TODO Auto-generated constructor stub
|
||||||
|
}
|
||||||
|
|
||||||
|
public CountInOut(final int i) {
|
||||||
|
this.value = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void inc() {
|
||||||
|
this.value++;
|
||||||
|
}
|
||||||
|
}
|
@ -19,9 +19,9 @@ import java.util.List;
|
|||||||
import org.kar.archidata.GlobalConfiguration;
|
import org.kar.archidata.GlobalConfiguration;
|
||||||
import org.kar.archidata.annotation.AnnotationTools;
|
import org.kar.archidata.annotation.AnnotationTools;
|
||||||
import org.kar.archidata.annotation.CreationTimestamp;
|
import org.kar.archidata.annotation.CreationTimestamp;
|
||||||
import org.kar.archidata.annotation.DataAddOn;
|
import org.kar.archidata.annotation.DataDefault;
|
||||||
import org.kar.archidata.annotation.SQLDefault;
|
|
||||||
import org.kar.archidata.annotation.UpdateTimestamp;
|
import org.kar.archidata.annotation.UpdateTimestamp;
|
||||||
|
import org.kar.archidata.dataAccess.addOn.AddOnDataJson;
|
||||||
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
|
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
|
||||||
import org.kar.archidata.dataAccess.addOn.AddOnManyToOne;
|
import org.kar.archidata.dataAccess.addOn.AddOnManyToOne;
|
||||||
import org.kar.archidata.dataAccess.addOn.AddOnSQLTableExternalForeinKeyAsList;
|
import org.kar.archidata.dataAccess.addOn.AddOnSQLTableExternalForeinKeyAsList;
|
||||||
@ -33,8 +33,6 @@ import org.slf4j.LoggerFactory;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import jakarta.persistence.ManyToMany;
|
|
||||||
import jakarta.persistence.ManyToOne;
|
|
||||||
import jakarta.ws.rs.InternalServerErrorException;
|
import jakarta.ws.rs.InternalServerErrorException;
|
||||||
|
|
||||||
public class DataAccess {
|
public class DataAccess {
|
||||||
@ -45,6 +43,7 @@ public class DataAccess {
|
|||||||
addOn.add(new AddOnManyToMany());
|
addOn.add(new AddOnManyToMany());
|
||||||
addOn.add(new AddOnManyToOne());
|
addOn.add(new AddOnManyToOne());
|
||||||
addOn.add(new AddOnSQLTableExternalForeinKeyAsList());
|
addOn.add(new AddOnSQLTableExternalForeinKeyAsList());
|
||||||
|
addOn.add(new AddOnDataJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addAddOn(final DataAccessAddOn addOn) {
|
public static void addAddOn(final DataAccessAddOn addOn) {
|
||||||
@ -183,223 +182,240 @@ public class DataAccess {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static <T> void setValuedb(final Class<?> type, final T data, int index, final Field field, final PreparedStatement ps) throws Exception {
|
protected static <T> void setValuedb(final Class<?> type, final T data, final CountInOut iii, final Field field, final PreparedStatement ps) throws Exception {
|
||||||
if (type == Long.class) {
|
if (type == Long.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.BIGINT);
|
ps.setNull(iii.value, Types.BIGINT);
|
||||||
} else {
|
} else {
|
||||||
ps.setLong(index++, (Long) tmp);
|
ps.setLong(iii.value, (Long) tmp);
|
||||||
}
|
}
|
||||||
} else if (type == long.class) {
|
} else if (type == long.class) {
|
||||||
ps.setLong(index++, field.getLong(data));
|
ps.setLong(iii.value, field.getLong(data));
|
||||||
} else if (type == Integer.class) {
|
} else if (type == Integer.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.INTEGER);
|
ps.setNull(iii.value, Types.INTEGER);
|
||||||
} else {
|
} else {
|
||||||
ps.setInt(index++, (Integer) tmp);
|
ps.setInt(iii.value, (Integer) tmp);
|
||||||
}
|
}
|
||||||
} else if (type == int.class) {
|
} else if (type == int.class) {
|
||||||
ps.setInt(index++, field.getInt(data));
|
ps.setInt(iii.value, field.getInt(data));
|
||||||
} else if (type == Float.class) {
|
} else if (type == Float.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.FLOAT);
|
ps.setNull(iii.value, Types.FLOAT);
|
||||||
} else {
|
} else {
|
||||||
ps.setFloat(index++, (Float) tmp);
|
ps.setFloat(iii.value, (Float) tmp);
|
||||||
}
|
}
|
||||||
} else if (type == float.class) {
|
} else if (type == float.class) {
|
||||||
ps.setFloat(index++, field.getFloat(data));
|
ps.setFloat(iii.value, field.getFloat(data));
|
||||||
} else if (type == Double.class) {
|
} else if (type == Double.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.DOUBLE);
|
ps.setNull(iii.value, Types.DOUBLE);
|
||||||
} else {
|
} else {
|
||||||
ps.setDouble(index++, (Double) tmp);
|
ps.setDouble(iii.value, (Double) tmp);
|
||||||
}
|
}
|
||||||
} else if (type == Double.class) {
|
} else if (type == Double.class) {
|
||||||
ps.setDouble(index++, field.getDouble(data));
|
ps.setDouble(iii.value, field.getDouble(data));
|
||||||
} else if (type == Boolean.class) {
|
} else if (type == Boolean.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.INTEGER);
|
ps.setNull(iii.value, Types.INTEGER);
|
||||||
} else {
|
} else {
|
||||||
ps.setBoolean(index++, (Boolean) tmp);
|
ps.setBoolean(iii.value, (Boolean) tmp);
|
||||||
}
|
}
|
||||||
} else if (type == boolean.class) {
|
} else if (type == boolean.class) {
|
||||||
ps.setBoolean(index++, field.getBoolean(data));
|
ps.setBoolean(iii.value, field.getBoolean(data));
|
||||||
} else if (type == Timestamp.class) {
|
} else if (type == Timestamp.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.INTEGER);
|
ps.setNull(iii.value, Types.INTEGER);
|
||||||
} else {
|
} else {
|
||||||
ps.setTimestamp(index++, (Timestamp) tmp);
|
ps.setTimestamp(iii.value, (Timestamp) tmp);
|
||||||
}
|
}
|
||||||
} else if (type == Date.class) {
|
} else if (type == Date.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.INTEGER);
|
ps.setNull(iii.value, Types.INTEGER);
|
||||||
} else {
|
} else {
|
||||||
final Timestamp sqlDate = java.sql.Timestamp.from(((Date) tmp).toInstant());
|
final Timestamp sqlDate = java.sql.Timestamp.from(((Date) tmp).toInstant());
|
||||||
ps.setTimestamp(index++, sqlDate);
|
ps.setTimestamp(iii.value, sqlDate);
|
||||||
}
|
}
|
||||||
} else if (type == LocalDate.class) {
|
} else if (type == LocalDate.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.INTEGER);
|
ps.setNull(iii.value, Types.INTEGER);
|
||||||
} else {
|
} else {
|
||||||
final java.sql.Date sqlDate = java.sql.Date.valueOf((LocalDate) tmp);
|
final java.sql.Date sqlDate = java.sql.Date.valueOf((LocalDate) tmp);
|
||||||
ps.setDate(index++, sqlDate);
|
ps.setDate(iii.value, sqlDate);
|
||||||
}
|
}
|
||||||
} else if (type == LocalTime.class) {
|
} else if (type == LocalTime.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.INTEGER);
|
ps.setNull(iii.value, Types.INTEGER);
|
||||||
} else {
|
} else {
|
||||||
final java.sql.Time sqlDate = java.sql.Time.valueOf((LocalTime) tmp);
|
final java.sql.Time sqlDate = java.sql.Time.valueOf((LocalTime) tmp);
|
||||||
ps.setTime(index++, sqlDate);
|
ps.setTime(iii.value, sqlDate);
|
||||||
}
|
}
|
||||||
} else if (type == String.class) {
|
} else if (type == String.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.VARCHAR);
|
ps.setNull(iii.value, Types.VARCHAR);
|
||||||
} else {
|
} else {
|
||||||
ps.setString(index++, (String) tmp);
|
ps.setString(iii.value, (String) tmp);
|
||||||
}
|
}
|
||||||
} else if (type.isEnum()) {
|
} else if (type.isEnum()) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(index++, Types.VARCHAR);
|
ps.setNull(iii.value, Types.VARCHAR);
|
||||||
} else {
|
} else {
|
||||||
ps.setString(index++, tmp.toString());
|
ps.setString(iii.value, tmp.toString());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("Unknown Field Type");
|
throw new Exception("Unknown Field Type");
|
||||||
}
|
}
|
||||||
|
iii.inc();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: maybe wrap this if the use of sqlite ==> maybe some problems came with sqlite ...
|
// TODO: maybe wrap this if the use of sqlite ==> maybe some problems came with sqlite ...
|
||||||
protected static <T> void setValueFromDb(final Class<?> type, final T data, final int index, final Field field, final ResultSet rs) throws Exception {
|
protected static <T> void setValueFromDb(final Class<?> type, final Object data, final CountInOut count, final Field field, final ResultSet rs, final CountInOut countNotNull) throws Exception {
|
||||||
if (type == Long.class) {
|
if (type == Long.class) {
|
||||||
final Long tmp = rs.getLong(index);
|
final Long tmp = rs.getLong(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
//logger.debug(" ==> {}", tmp);
|
//logger.debug(" ==> {}", tmp);
|
||||||
field.set(data, tmp);
|
field.set(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == long.class) {
|
} else if (type == long.class) {
|
||||||
final Long tmp = rs.getLong(index);
|
final Long tmp = rs.getLong(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
//field.set(data, null);
|
//field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.setLong(data, tmp);
|
field.setLong(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == Integer.class) {
|
} else if (type == Integer.class) {
|
||||||
final Integer tmp = rs.getInt(index);
|
final Integer tmp = rs.getInt(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, tmp);
|
field.set(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == int.class) {
|
} else if (type == int.class) {
|
||||||
final Integer tmp = rs.getInt(index);
|
final Integer tmp = rs.getInt(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
//field.set(data, null);
|
//field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.setInt(data, tmp);
|
field.setInt(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == Float.class) {
|
} else if (type == Float.class) {
|
||||||
final Float tmp = rs.getFloat(index);
|
final Float tmp = rs.getFloat(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, tmp);
|
field.set(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == float.class) {
|
} else if (type == float.class) {
|
||||||
final Float tmp = rs.getFloat(index);
|
final Float tmp = rs.getFloat(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
//field.set(data, null);
|
//field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.setFloat(data, tmp);
|
field.setFloat(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == Double.class) {
|
} else if (type == Double.class) {
|
||||||
final Double tmp = rs.getDouble(index);
|
final Double tmp = rs.getDouble(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, tmp);
|
field.set(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == double.class) {
|
} else if (type == double.class) {
|
||||||
final Double tmp = rs.getDouble(index);
|
final Double tmp = rs.getDouble(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
//field.set(data, null);
|
//field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.setDouble(data, tmp);
|
field.setDouble(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == Boolean.class) {
|
} else if (type == Boolean.class) {
|
||||||
final Boolean tmp = rs.getBoolean(index);
|
final Boolean tmp = rs.getBoolean(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, tmp);
|
field.set(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == boolean.class) {
|
} else if (type == boolean.class) {
|
||||||
final Boolean tmp = rs.getBoolean(index);
|
final Boolean tmp = rs.getBoolean(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
//field.set(data, null);
|
//field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.setBoolean(data, tmp);
|
field.setBoolean(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == Timestamp.class) {
|
} else if (type == Timestamp.class) {
|
||||||
final Timestamp tmp = rs.getTimestamp(index);
|
final Timestamp tmp = rs.getTimestamp(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, tmp);
|
field.set(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == Date.class) {
|
} else if (type == Date.class) {
|
||||||
try {
|
try {
|
||||||
final Timestamp tmp = rs.getTimestamp(index);
|
final Timestamp tmp = rs.getTimestamp(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, Date.from(tmp.toInstant()));
|
field.set(data, Date.from(tmp.toInstant()));
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} catch (final SQLException ex) {
|
} catch (final SQLException ex) {
|
||||||
final String tmp = rs.getString(index);
|
final String tmp = rs.getString(count.value);
|
||||||
LOGGER.error("Fail to parse the SQL time !!! {}", tmp);
|
LOGGER.error("Fail to parse the SQL time !!! {}", tmp);
|
||||||
LOGGER.error("Fail to parse the SQL time !!! {}", Date.parse(tmp));
|
LOGGER.error("Fail to parse the SQL time !!! {}", Date.parse(tmp));
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, Date.parse(tmp));
|
field.set(data, Date.parse(tmp));
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (type == LocalDate.class) {
|
} else if (type == LocalDate.class) {
|
||||||
final java.sql.Date tmp = rs.getDate(index);
|
final java.sql.Date tmp = rs.getDate(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, tmp.toLocalDate());
|
field.set(data, tmp.toLocalDate());
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == LocalTime.class) {
|
} else if (type == LocalTime.class) {
|
||||||
final java.sql.Time tmp = rs.getTime(index);
|
final java.sql.Time tmp = rs.getTime(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, tmp.toLocalTime());
|
field.set(data, tmp.toLocalTime());
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == String.class) {
|
} else if (type == String.class) {
|
||||||
final String tmp = rs.getString(index);
|
final String tmp = rs.getString(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, tmp);
|
field.set(data, tmp);
|
||||||
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type.isEnum()) {
|
} else if (type.isEnum()) {
|
||||||
final String tmp = rs.getString(index);
|
final String tmp = rs.getString(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
@ -407,6 +423,7 @@ public class DataAccess {
|
|||||||
for (final Object elem : arr) {
|
for (final Object elem : arr) {
|
||||||
if (elem.toString().equals(tmp)) {
|
if (elem.toString().equals(tmp)) {
|
||||||
field.set(data, elem);
|
field.set(data, elem);
|
||||||
|
countNotNull.inc();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -415,21 +432,11 @@ public class DataAccess {
|
|||||||
} else {
|
} else {
|
||||||
throw new Exception("Unknown Field Type");
|
throw new Exception("Unknown Field Type");
|
||||||
}
|
}
|
||||||
|
count.inc();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isAddOnField(final Field field) {
|
public static boolean isAddOnField(final Field field) {
|
||||||
final boolean ret = AnnotationTools.isAnnotationGroup(field, DataAddOn.class);
|
return findAddOnforField(field) != null;
|
||||||
if (ret) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// The specific element of the JPA manage fy generic add-on system:
|
|
||||||
if (field.getDeclaredAnnotationsByType(ManyToMany.class).length != 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (field.getDeclaredAnnotationsByType(ManyToOne.class).length != 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DataAccessAddOn findAddOnforField(final Field field) {
|
public static DataAccessAddOn findAddOnforField(final Field field) {
|
||||||
@ -453,10 +460,10 @@ public class DataAccess {
|
|||||||
try {
|
try {
|
||||||
final String tableName = AnnotationTools.getTableName(clazz, options);
|
final String tableName = AnnotationTools.getTableName(clazz, options);
|
||||||
//boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
|
//boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
|
||||||
final StringBuilder querry = new StringBuilder();
|
final StringBuilder query = new StringBuilder();
|
||||||
querry.append("INSERT INTO `");
|
query.append("INSERT INTO `");
|
||||||
querry.append(tableName);
|
query.append(tableName);
|
||||||
querry.append("` (");
|
query.append("` (");
|
||||||
|
|
||||||
boolean firstField = true;
|
boolean firstField = true;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@ -482,7 +489,7 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
if (!elem.getClass().isPrimitive()) {
|
if (!elem.getClass().isPrimitive()) {
|
||||||
final Object tmp = elem.get(data);
|
final Object tmp = elem.get(data);
|
||||||
if (tmp == null && elem.getDeclaredAnnotationsByType(SQLDefault.class).length != 0) {
|
if (tmp == null && elem.getDeclaredAnnotationsByType(DataDefault.class).length != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -491,28 +498,28 @@ public class DataAccess {
|
|||||||
if (firstField) {
|
if (firstField) {
|
||||||
firstField = false;
|
firstField = false;
|
||||||
} else {
|
} else {
|
||||||
querry.append(",");
|
query.append(",");
|
||||||
}
|
}
|
||||||
querry.append(" `");
|
query.append(" `");
|
||||||
querry.append(name);
|
query.append(name);
|
||||||
querry.append("`");
|
query.append("`");
|
||||||
}
|
}
|
||||||
firstField = true;
|
firstField = true;
|
||||||
querry.append(") VALUES (");
|
query.append(") VALUES (");
|
||||||
for (int iii = 0; iii < count; iii++) {
|
for (int iii = 0; iii < count; iii++) {
|
||||||
if (firstField) {
|
if (firstField) {
|
||||||
firstField = false;
|
firstField = false;
|
||||||
} else {
|
} else {
|
||||||
querry.append(",");
|
query.append(",");
|
||||||
}
|
}
|
||||||
querry.append("?");
|
query.append("?");
|
||||||
}
|
}
|
||||||
querry.append(")");
|
query.append(")");
|
||||||
LOGGER.warn("generate the querry: '{}'", querry.toString());
|
LOGGER.warn("generate the query: '{}'", query.toString());
|
||||||
// prepare the request:
|
// prepare the request:
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(querry.toString(), Statement.RETURN_GENERATED_KEYS);
|
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
|
||||||
Field primaryKeyField = null;
|
Field primaryKeyField = null;
|
||||||
int iii = 1;
|
final CountInOut iii = new CountInOut(1);
|
||||||
for (final Field elem : clazz.getFields()) {
|
for (final Field elem : clazz.getFields()) {
|
||||||
// static field is only for internal global declaration ==> remove it ..
|
// static field is only for internal global declaration ==> remove it ..
|
||||||
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
|
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
|
||||||
@ -536,17 +543,17 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
if (addOn != null) {
|
if (addOn != null) {
|
||||||
// Add-on specific insertion.
|
// Add-on specific insertion.
|
||||||
iii = addOn.insertData(ps, elem, data, iii);
|
addOn.insertData(ps, elem, data, iii);
|
||||||
} else {
|
} else {
|
||||||
// Generic class insertion...
|
// Generic class insertion...
|
||||||
final Class<?> type = elem.getType();
|
final Class<?> type = elem.getType();
|
||||||
if (!type.isPrimitive()) {
|
if (!type.isPrimitive()) {
|
||||||
final Object tmp = elem.get(data);
|
final Object tmp = elem.get(data);
|
||||||
if (tmp == null && elem.getDeclaredAnnotationsByType(SQLDefault.class).length != 0) {
|
if (tmp == null && elem.getDeclaredAnnotationsByType(DataDefault.class).length != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setValuedb(type, data, iii++, elem, ps);
|
setValuedb(type, data, iii, elem, ps);
|
||||||
}
|
}
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
@ -674,10 +681,10 @@ public class DataAccess {
|
|||||||
try {
|
try {
|
||||||
final String tableName = AnnotationTools.getTableName(clazz, options);
|
final String tableName = AnnotationTools.getTableName(clazz, options);
|
||||||
//boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
|
//boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
|
||||||
final StringBuilder querry = new StringBuilder();
|
final StringBuilder query = new StringBuilder();
|
||||||
querry.append("UPDATE `");
|
query.append("UPDATE `");
|
||||||
querry.append(tableName);
|
query.append(tableName);
|
||||||
querry.append("` SET ");
|
query.append("` SET ");
|
||||||
|
|
||||||
boolean firstField = true;
|
boolean firstField = true;
|
||||||
for (final Field field : clazz.getFields()) {
|
for (final Field field : clazz.getFields()) {
|
||||||
@ -699,27 +706,27 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
if (!field.getClass().isPrimitive()) {
|
if (!field.getClass().isPrimitive()) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null && field.getDeclaredAnnotationsByType(SQLDefault.class).length != 0) {
|
if (tmp == null && field.getDeclaredAnnotationsByType(DataDefault.class).length != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (firstField) {
|
if (firstField) {
|
||||||
firstField = false;
|
firstField = false;
|
||||||
} else {
|
} else {
|
||||||
querry.append(",");
|
query.append(",");
|
||||||
}
|
}
|
||||||
querry.append(" `");
|
query.append(" `");
|
||||||
querry.append(name);
|
query.append(name);
|
||||||
querry.append("` = ? ");
|
query.append("` = ? ");
|
||||||
}
|
}
|
||||||
querry.append(" ");
|
query.append(" ");
|
||||||
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||||
whereAppendQuery(querry, tableName, condition, null, deletedFieldName);
|
whereAppendQuery(query, tableName, condition, null, deletedFieldName);
|
||||||
firstField = true;
|
firstField = true;
|
||||||
LOGGER.debug("generate the querry: '{}'", querry.toString());
|
LOGGER.debug("generate the query: '{}'", query.toString());
|
||||||
// prepare the request:
|
// prepare the request:
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(querry.toString(), Statement.RETURN_GENERATED_KEYS);
|
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
|
||||||
int iii = 1;
|
final CountInOut iii = new CountInOut(1);
|
||||||
for (final Field field : clazz.getFields()) {
|
for (final Field field : clazz.getFields()) {
|
||||||
// static field is only for internal global declaration ==> remove it ..
|
// static field is only for internal global declaration ==> remove it ..
|
||||||
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
|
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
|
||||||
@ -741,16 +748,16 @@ public class DataAccess {
|
|||||||
final Class<?> type = field.getType();
|
final Class<?> type = field.getType();
|
||||||
if (!type.isPrimitive()) {
|
if (!type.isPrimitive()) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null && field.getDeclaredAnnotationsByType(SQLDefault.class).length != 0) {
|
if (tmp == null && field.getDeclaredAnnotationsByType(DataDefault.class).length != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setValuedb(type, data, iii++, field, ps);
|
setValuedb(type, data, iii, field, ps);
|
||||||
} else {
|
} else {
|
||||||
iii = addOn.insertData(ps, field, data, iii);
|
addOn.insertData(ps, field, data, iii);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
iii = whereInjectValue(ps, condition, iii);
|
whereInjectValue(ps, condition, iii);
|
||||||
|
|
||||||
return ps.executeUpdate();
|
return ps.executeUpdate();
|
||||||
} catch (final SQLException ex) {
|
} catch (final SQLException ex) {
|
||||||
@ -762,41 +769,41 @@ public class DataAccess {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void addElement(final PreparedStatement ps, final Object value, final int iii) throws Exception {
|
static void addElement(final PreparedStatement ps, final Object value, final CountInOut iii) throws Exception {
|
||||||
if (value instanceof final Long tmp) {
|
if (value instanceof final Long tmp) {
|
||||||
ps.setLong(iii, tmp);
|
ps.setLong(iii.value, tmp);
|
||||||
} else if (value instanceof final Integer tmp) {
|
} else if (value instanceof final Integer tmp) {
|
||||||
ps.setInt(iii, tmp);
|
ps.setInt(iii.value, tmp);
|
||||||
} else if (value instanceof final String tmp) {
|
} else if (value instanceof final String tmp) {
|
||||||
ps.setString(iii, tmp);
|
ps.setString(iii.value, tmp);
|
||||||
} else if (value instanceof final Short tmp) {
|
} else if (value instanceof final Short tmp) {
|
||||||
ps.setShort(iii, tmp);
|
ps.setShort(iii.value, tmp);
|
||||||
} else if (value instanceof final Byte tmp) {
|
} else if (value instanceof final Byte tmp) {
|
||||||
ps.setByte(iii, tmp);
|
ps.setByte(iii.value, tmp);
|
||||||
} else if (value instanceof final Float tmp) {
|
} else if (value instanceof final Float tmp) {
|
||||||
ps.setFloat(iii, tmp);
|
ps.setFloat(iii.value, tmp);
|
||||||
} else if (value instanceof final Double tmp) {
|
} else if (value instanceof final Double tmp) {
|
||||||
ps.setDouble(iii, tmp);
|
ps.setDouble(iii.value, tmp);
|
||||||
} else if (value instanceof final Boolean tmp) {
|
} else if (value instanceof final Boolean tmp) {
|
||||||
ps.setBoolean(iii, tmp);
|
ps.setBoolean(iii.value, tmp);
|
||||||
} else if (value instanceof final Boolean tmp) {
|
} else if (value instanceof final Boolean tmp) {
|
||||||
ps.setBoolean(iii, tmp);
|
ps.setBoolean(iii.value, tmp);
|
||||||
} else if (value instanceof final Timestamp tmp) {
|
} else if (value instanceof final Timestamp tmp) {
|
||||||
ps.setTimestamp(iii, tmp);
|
ps.setTimestamp(iii.value, tmp);
|
||||||
} else if (value instanceof final Date tmp) {
|
} else if (value instanceof final Date tmp) {
|
||||||
ps.setTimestamp(iii, java.sql.Timestamp.from((tmp).toInstant()));
|
ps.setTimestamp(iii.value, java.sql.Timestamp.from((tmp).toInstant()));
|
||||||
} else if (value instanceof final LocalDate tmp) {
|
} else if (value instanceof final LocalDate tmp) {
|
||||||
ps.setDate(iii, java.sql.Date.valueOf(tmp));
|
ps.setDate(iii.value, java.sql.Date.valueOf(tmp));
|
||||||
} else if (value instanceof final LocalTime tmp) {
|
} else if (value instanceof final LocalTime tmp) {
|
||||||
ps.setTime(iii, java.sql.Time.valueOf(tmp));
|
ps.setTime(iii.value, java.sql.Time.valueOf(tmp));
|
||||||
} else if (value.getClass().isEnum()) {
|
} else if (value.getClass().isEnum()) {
|
||||||
ps.setString(iii, value.toString());
|
ps.setString(iii.value, value.toString());
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("Not manage type ==> need to add it ...");
|
throw new Exception("Not manage type ==> need to add it ...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void whereAppendQuery(final StringBuilder querry, final String tableName, final QueryItem condition, final QueryOptions options, final String deletedFieldName)
|
public static void whereAppendQuery(final StringBuilder query, final String tableName, final QueryItem condition, final QueryOptions options, final String deletedFieldName)
|
||||||
throws ExceptionDBInterface {
|
throws ExceptionDBInterface {
|
||||||
boolean exclude_deleted = true;
|
boolean exclude_deleted = true;
|
||||||
if (options != null) {
|
if (options != null) {
|
||||||
@ -810,54 +817,52 @@ public class DataAccess {
|
|||||||
// Check if we have a condition to generate
|
// Check if we have a condition to generate
|
||||||
if (condition == null) {
|
if (condition == null) {
|
||||||
if (exclude_deleted && deletedFieldName != null) {
|
if (exclude_deleted && deletedFieldName != null) {
|
||||||
querry.append(" WHERE ");
|
query.append(" WHERE ");
|
||||||
querry.append(tableName);
|
query.append(tableName);
|
||||||
querry.append(".");
|
query.append(".");
|
||||||
querry.append(deletedFieldName);
|
query.append(deletedFieldName);
|
||||||
querry.append(" = false ");
|
query.append(" = false ");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
querry.append(" WHERE (");
|
query.append(" WHERE (");
|
||||||
condition.generateQuerry(querry, tableName);
|
condition.generateQuerry(query, tableName);
|
||||||
|
|
||||||
querry.append(") ");
|
query.append(") ");
|
||||||
if (exclude_deleted && deletedFieldName != null) {
|
if (exclude_deleted && deletedFieldName != null) {
|
||||||
querry.append("AND ");
|
query.append("AND ");
|
||||||
querry.append(tableName);
|
query.append(tableName);
|
||||||
querry.append(".");
|
query.append(".");
|
||||||
querry.append(deletedFieldName);
|
query.append(deletedFieldName);
|
||||||
querry.append(" = false ");
|
query.append(" = false ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int whereInjectValue(final PreparedStatement ps, final QueryItem condition, int iii) throws Exception {
|
public static void whereInjectValue(final PreparedStatement ps, final QueryItem condition, final CountInOut iii) throws Exception {
|
||||||
// Check if we have a condition to generate
|
// Check if we have a condition to generate
|
||||||
if (condition == null) {
|
if (condition != null) {
|
||||||
return iii;
|
condition.injectQuerry(ps, iii);
|
||||||
}
|
}
|
||||||
iii = condition.injectQuerry(ps, iii);
|
|
||||||
return iii;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int executeSimpleQuerry(final String querry, final boolean root) throws SQLException, IOException {
|
public static int executeSimpleQuerry(final String query, final boolean root) throws SQLException, IOException {
|
||||||
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, root);
|
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, root);
|
||||||
final Statement stmt = entry.connection.createStatement();
|
final Statement stmt = entry.connection.createStatement();
|
||||||
return stmt.executeUpdate(querry);
|
return stmt.executeUpdate(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int executeSimpleQuerry(final String querry) throws SQLException, IOException {
|
public static int executeSimpleQuerry(final String query) throws SQLException, IOException {
|
||||||
return executeSimpleQuerry(querry, false);
|
return executeSimpleQuerry(query, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean executeQuerry(final String querry, final boolean root) throws SQLException, IOException {
|
public static boolean executeQuerry(final String query, final boolean root) throws SQLException, IOException {
|
||||||
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, root);
|
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig, root);
|
||||||
final Statement stmt = entry.connection.createStatement();
|
final Statement stmt = entry.connection.createStatement();
|
||||||
return stmt.execute(querry);
|
return stmt.execute(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean executeQuerry(final String querry) throws SQLException, IOException {
|
public static boolean executeQuerry(final String query) throws SQLException, IOException {
|
||||||
return executeQuerry(querry, false);
|
return executeQuerry(query, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T getWhere(final Class<T> clazz, final QueryItem condition) throws Exception {
|
public static <T> T getWhere(final Class<T> clazz, final QueryItem condition) throws Exception {
|
||||||
@ -884,10 +889,11 @@ public class DataAccess {
|
|||||||
return getsWhere(clazz, condition, null, options, linit);
|
return getsWhere(clazz, condition, null, options, linit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int generateSelectField(final StringBuilder querry, final Class<?> clazz, final QueryOptions options, int 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.readAllFields(options);
|
final boolean readAllfields = QueryOptions.readAllFields(options);
|
||||||
final String tableName = AnnotationTools.getTableName(clazz, options);
|
final String tableName = AnnotationTools.getTableName(clazz, options);
|
||||||
boolean firstField = true;
|
boolean firstField = true;
|
||||||
|
|
||||||
for (final Field elem : clazz.getFields()) {
|
for (final Field elem : clazz.getFields()) {
|
||||||
// static field is only for internal global declaration ==> remove it ..
|
// static field is only for internal global declaration ==> remove it ..
|
||||||
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
|
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
|
||||||
@ -903,59 +909,87 @@ public class DataAccess {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
final String name = AnnotationTools.getFieldName(elem);
|
final String name = AnnotationTools.getFieldName(elem);
|
||||||
count++;
|
|
||||||
if (firstField) {
|
if (firstField) {
|
||||||
firstField = false;
|
firstField = false;
|
||||||
} else {
|
} else {
|
||||||
querry.append(",");
|
querySelect.append(",");
|
||||||
}
|
}
|
||||||
querry.append(" ");
|
querySelect.append(" ");
|
||||||
if (addOn != null) {
|
if (addOn != null) {
|
||||||
count = addOn.generateQuerry(tableName, elem, querry, name, count, options);
|
addOn.generateQuerry(tableName, elem, querySelect, query, name, count, options);
|
||||||
} else {
|
} else {
|
||||||
querry.append(tableName);
|
querySelect.append(tableName);
|
||||||
querry.append(".");
|
querySelect.append(".");
|
||||||
querry.append(name);
|
querySelect.append(name);
|
||||||
|
count.inc();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
querry.append(" FROM `");
|
|
||||||
querry.append(tableName);
|
|
||||||
querry.append("` ");
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: set limit as an query Option...
|
// TODO: set limit as an query Option...
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition, final String orderBy, final QueryOptions options, final Integer linit) throws Exception {
|
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition, final String orderBy, final QueryOptions options, final Integer linit) throws Exception {
|
||||||
final boolean readAllfields = QueryOptions.readAllFields(options);
|
final List<LazyGetter> lazyCall = new ArrayList<>();
|
||||||
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||||
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||||
final List<T> outs = new ArrayList<>();
|
final List<T> outs = new ArrayList<>();
|
||||||
// real add in the BDD:
|
// real add in the BDD:
|
||||||
try {
|
try {
|
||||||
int count = 0;
|
final CountInOut count = new CountInOut();
|
||||||
final StringBuilder querry = new StringBuilder();
|
final StringBuilder querySelect = new StringBuilder();
|
||||||
querry.append("SELECT ");
|
StringBuilder query = new StringBuilder();
|
||||||
count = generateSelectField(querry, clazz, options, count);
|
|
||||||
final String tableName = AnnotationTools.getTableName(clazz, options);
|
final String tableName = AnnotationTools.getTableName(clazz, options);
|
||||||
whereAppendQuery(querry, tableName, condition, options, deletedFieldName);
|
querySelect.append("SELECT ");
|
||||||
|
query.append(" FROM `");
|
||||||
|
query.append(tableName);
|
||||||
|
query.append("` ");
|
||||||
|
|
||||||
|
generateSelectField(querySelect, query, clazz, options, count);
|
||||||
|
querySelect.append(query.toString());
|
||||||
|
query = querySelect;
|
||||||
|
whereAppendQuery(query, tableName, condition, options, deletedFieldName);
|
||||||
if (orderBy != null && orderBy.length() >= 1) {
|
if (orderBy != null && orderBy.length() >= 1) {
|
||||||
querry.append(" ORDER BY ");
|
query.append(" ORDER BY ");
|
||||||
querry.append(orderBy);
|
query.append(orderBy);
|
||||||
}
|
}
|
||||||
if (linit != null && linit >= 1) {
|
if (linit != null && linit >= 1) {
|
||||||
querry.append(" LIMIT " + linit);
|
query.append(" LIMIT " + linit);
|
||||||
}
|
}
|
||||||
LOGGER.debug("generate the querry: '{}'", querry.toString());
|
LOGGER.debug("generate the query: '{}'", query.toString());
|
||||||
// prepare the request:
|
// prepare the request:
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(querry.toString(), Statement.RETURN_GENERATED_KEYS);
|
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
|
||||||
whereInjectValue(ps, condition, 1);
|
final CountInOut iii = new CountInOut(1);
|
||||||
|
whereInjectValue(ps, condition, iii);
|
||||||
// execute the request
|
// execute the request
|
||||||
final ResultSet rs = ps.executeQuery();
|
final ResultSet rs = ps.executeQuery();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
|
count.value = 1;
|
||||||
|
final CountInOut countNotNull = new CountInOut(0);
|
||||||
|
final Object data = createObjectFromSQLRequest(rs, clazz, count, countNotNull, options, lazyCall);
|
||||||
|
final T out = (T) data;
|
||||||
|
outs.add(out);
|
||||||
|
}
|
||||||
|
LOGGER.info("Async calls: {}", lazyCall.size());
|
||||||
|
for (final LazyGetter elem : lazyCall) {
|
||||||
|
elem.doRequest();
|
||||||
|
}
|
||||||
|
} catch (final SQLException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
throw ex;
|
||||||
|
} catch (final Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
entry.close();
|
||||||
|
entry = null;
|
||||||
|
}
|
||||||
|
return outs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Object createObjectFromSQLRequest(final ResultSet rs, final Class<?> clazz, final CountInOut count, final CountInOut countNotNull, final QueryOptions options,
|
||||||
|
final List<LazyGetter> lazyCall) throws Exception {
|
||||||
|
final boolean readAllfields = QueryOptions.readAllFields(options);
|
||||||
// TODO: manage class that is defined inside a class ==> Not manage for now...
|
// TODO: manage class that is defined inside a class ==> Not manage for now...
|
||||||
final Object data = clazz.getConstructors()[0].newInstance();
|
final Object data = clazz.getConstructors()[0].newInstance();
|
||||||
count = 1;
|
|
||||||
for (final Field elem : clazz.getFields()) {
|
for (final Field elem : clazz.getFields()) {
|
||||||
// static field is only for internal global declaration ==> remove it ..
|
// static field is only for internal global declaration ==> remove it ..
|
||||||
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
|
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
|
||||||
@ -971,27 +1005,12 @@ public class DataAccess {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (addOn != null) {
|
if (addOn != null) {
|
||||||
final int nbRowRead = addOn.fillFromQuerry(rs, elem, data, count, options);
|
addOn.fillFromQuerry(rs, elem, data, count, options, lazyCall);
|
||||||
count += nbRowRead;
|
|
||||||
} else {
|
} else {
|
||||||
setValueFromDb(elem.getType(), data, count, elem, rs);
|
setValueFromDb(elem.getType(), data, count, elem, rs, countNotNull);
|
||||||
count++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final T out = (T) data;
|
return data;
|
||||||
outs.add(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (final SQLException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
throw ex;
|
|
||||||
} catch (final Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
entry.close();
|
|
||||||
entry = null;
|
|
||||||
}
|
|
||||||
return outs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : detect the @Id
|
// TODO : detect the @Id
|
||||||
@ -1061,15 +1080,16 @@ public class DataAccess {
|
|||||||
// find the deleted field
|
// find the deleted field
|
||||||
|
|
||||||
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||||
final StringBuilder querry = new StringBuilder();
|
final StringBuilder query = new StringBuilder();
|
||||||
querry.append("DELETE FROM `");
|
query.append("DELETE FROM `");
|
||||||
querry.append(tableName);
|
query.append(tableName);
|
||||||
querry.append("` ");
|
query.append("` ");
|
||||||
whereAppendQuery(querry, tableName, condition, null, deletedFieldName);
|
whereAppendQuery(query, tableName, condition, null, deletedFieldName);
|
||||||
try {
|
try {
|
||||||
LOGGER.debug("APPLY: {}", querry.toString());
|
LOGGER.debug("APPLY: {}", query.toString());
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(querry.toString());
|
final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
|
||||||
whereInjectValue(ps, condition, 1);
|
final CountInOut iii = new CountInOut(1);
|
||||||
|
whereInjectValue(ps, condition, iii);
|
||||||
return ps.executeUpdate();
|
return ps.executeUpdate();
|
||||||
} finally {
|
} finally {
|
||||||
entry.close();
|
entry.close();
|
||||||
@ -1100,26 +1120,27 @@ public class DataAccess {
|
|||||||
// find the deleted field
|
// find the deleted field
|
||||||
|
|
||||||
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||||
final StringBuilder querry = new StringBuilder();
|
final StringBuilder query = new StringBuilder();
|
||||||
querry.append("UPDATE `");
|
query.append("UPDATE `");
|
||||||
querry.append(tableName);
|
query.append(tableName);
|
||||||
querry.append("` SET `");
|
query.append("` SET `");
|
||||||
querry.append(deletedFieldName);
|
query.append(deletedFieldName);
|
||||||
querry.append("`=true ");
|
query.append("`=true ");
|
||||||
/*
|
/*
|
||||||
* The trigger work well, but the timestamp is store @ seconds...
|
* The trigger work well, but the timestamp is store @ seconds...
|
||||||
if (updateFieldName != null) {
|
if (updateFieldName != null) {
|
||||||
// done only in SQLite (the trigger does not work...
|
// done only in SQLite (the trigger does not work...
|
||||||
querry.append(", `");
|
query.append(", `");
|
||||||
querry.append(updateFieldName);
|
query.append(updateFieldName);
|
||||||
querry.append("`=DATE()");
|
query.append("`=DATE()");
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
whereAppendQuery(querry, tableName, condition, null, deletedFieldName);
|
whereAppendQuery(query, tableName, condition, null, deletedFieldName);
|
||||||
try {
|
try {
|
||||||
LOGGER.debug("APPLY UPDATE: {}", querry.toString());
|
LOGGER.debug("APPLY UPDATE: {}", query.toString());
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(querry.toString());
|
final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
|
||||||
whereInjectValue(ps, condition, 1);
|
final CountInOut iii = new CountInOut(1);
|
||||||
|
whereInjectValue(ps, condition, iii);
|
||||||
return ps.executeUpdate();
|
return ps.executeUpdate();
|
||||||
} finally {
|
} finally {
|
||||||
entry.close();
|
entry.close();
|
||||||
@ -1142,24 +1163,25 @@ public class DataAccess {
|
|||||||
throw new Exception("The class " + clazz.getCanonicalName() + " has no deleted field");
|
throw new Exception("The class " + clazz.getCanonicalName() + " has no deleted field");
|
||||||
}
|
}
|
||||||
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||||
final StringBuilder querry = new StringBuilder();
|
final StringBuilder query = new StringBuilder();
|
||||||
querry.append("UPDATE `");
|
query.append("UPDATE `");
|
||||||
querry.append(tableName);
|
query.append(tableName);
|
||||||
querry.append("` SET `");
|
query.append("` SET `");
|
||||||
querry.append(deletedFieldName);
|
query.append(deletedFieldName);
|
||||||
querry.append("`=false ");
|
query.append("`=false ");
|
||||||
/*
|
/*
|
||||||
* is is needed only for SQLite ???
|
* is is needed only for SQLite ???
|
||||||
querry.append("`modify_date`=");
|
query.append("`modify_date`=");
|
||||||
querry.append(getDBNow());
|
query.append(getDBNow());
|
||||||
querry.append(", ");
|
query.append(", ");
|
||||||
*/
|
*/
|
||||||
// need to disable the deleted false because the model must be unselected to be updated.
|
// need to disable the deleted false because the model must be unselected to be updated.
|
||||||
options.put(QueryOptions.SQL_DELETED_DISABLE, true);
|
options.put(QueryOptions.SQL_DELETED_DISABLE, true);
|
||||||
whereAppendQuery(querry, tableName, condition, options, deletedFieldName);
|
whereAppendQuery(query, tableName, condition, options, deletedFieldName);
|
||||||
try {
|
try {
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(querry.toString());
|
final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
|
||||||
whereInjectValue(ps, condition, 1);
|
final CountInOut iii = new CountInOut(1);
|
||||||
|
whereInjectValue(ps, condition, iii);
|
||||||
return ps.executeUpdate();
|
return ps.executeUpdate();
|
||||||
} finally {
|
} finally {
|
||||||
entry.close();
|
entry.close();
|
||||||
|
@ -37,7 +37,7 @@ public interface DataAccessAddOn {
|
|||||||
* @return the new index of injection in case of multiple value management
|
* @return the new index of injection in case of multiple value management
|
||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
*/
|
*/
|
||||||
int insertData(PreparedStatement ps, final Field field, Object data, int iii) throws Exception, SQLException, IllegalArgumentException, IllegalAccessException;
|
void insertData(PreparedStatement ps, final Field field, Object data, CountInOut iii) throws Exception, SQLException, IllegalArgumentException, IllegalAccessException;
|
||||||
|
|
||||||
// Element can insert in the single request
|
// Element can insert in the single request
|
||||||
boolean canInsert(final Field field);
|
boolean canInsert(final Field field);
|
||||||
@ -45,10 +45,12 @@ public interface DataAccessAddOn {
|
|||||||
// Element can be retrieve with the specific mode
|
// Element can be retrieve with the specific mode
|
||||||
boolean canRetrieve(final Field field);
|
boolean canRetrieve(final Field field);
|
||||||
|
|
||||||
int generateQuerry(@NotNull String tableName, @NotNull Field field, @NotNull StringBuilder querry, @NotNull String name, @NotNull int elemCount, QueryOptions options) throws Exception;
|
void generateQuerry(@NotNull String tableName, @NotNull Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull String name,
|
||||||
|
@NotNull CountInOut count, QueryOptions options) throws Exception;
|
||||||
|
|
||||||
// Return the number of colomn read
|
// Return the number of colomn read
|
||||||
int fillFromQuerry(ResultSet rs, Field field, Object data, int count, QueryOptions options) throws SQLException, IllegalArgumentException, IllegalAccessException;
|
void fillFromQuerry(ResultSet rs, Field field, Object data, CountInOut count, QueryOptions options, final List<LazyGetter> lazyCall)
|
||||||
|
throws Exception, SQLException, IllegalArgumentException, IllegalAccessException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create associated table of the specific element.
|
* Create associated table of the specific element.
|
||||||
|
@ -10,12 +10,14 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.kar.archidata.annotation.AnnotationTools;
|
import org.kar.archidata.annotation.AnnotationTools;
|
||||||
import org.kar.archidata.annotation.CreationTimestamp;
|
import org.kar.archidata.annotation.CreationTimestamp;
|
||||||
import org.kar.archidata.annotation.SQLIfNotExists;
|
import org.kar.archidata.annotation.DataIfNotExists;
|
||||||
import org.kar.archidata.annotation.UpdateTimestamp;
|
import org.kar.archidata.annotation.UpdateTimestamp;
|
||||||
import org.kar.archidata.util.ConfigBaseVariable;
|
import org.kar.archidata.util.ConfigBaseVariable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
|
||||||
import jakarta.persistence.GenerationType;
|
import jakarta.persistence.GenerationType;
|
||||||
|
|
||||||
public class DataFactory {
|
public class DataFactory {
|
||||||
@ -50,6 +52,9 @@ public class DataFactory {
|
|||||||
if (type == String.class) {
|
if (type == String.class) {
|
||||||
return "text";
|
return "text";
|
||||||
}
|
}
|
||||||
|
if (type == JsonValue.class) {
|
||||||
|
return "json";
|
||||||
|
}
|
||||||
if (type.isEnum()) {
|
if (type.isEnum()) {
|
||||||
final Object[] arr = type.getEnumConstants();
|
final Object[] arr = type.getEnumConstants();
|
||||||
final StringBuilder out = new StringBuilder();
|
final StringBuilder out = new StringBuilder();
|
||||||
@ -95,6 +100,9 @@ public class DataFactory {
|
|||||||
if (type == String.class) {
|
if (type == String.class) {
|
||||||
return "text";
|
return "text";
|
||||||
}
|
}
|
||||||
|
if (type == JsonValue.class) {
|
||||||
|
return "text";
|
||||||
|
}
|
||||||
if (type.isEnum()) {
|
if (type.isEnum()) {
|
||||||
final Object[] arr = type.getEnumConstants();
|
final Object[] arr = type.getEnumConstants();
|
||||||
final StringBuilder out = new StringBuilder();
|
final StringBuilder out = new StringBuilder();
|
||||||
@ -289,7 +297,7 @@ public class DataFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
|
final boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(DataIfNotExists.class).length != 0;
|
||||||
final List<String> preActionList = new ArrayList<>();
|
final List<String> preActionList = new ArrayList<>();
|
||||||
final List<String> postActionList = new ArrayList<>();
|
final List<String> postActionList = new ArrayList<>();
|
||||||
final StringBuilder out = new StringBuilder();
|
final StringBuilder out = new StringBuilder();
|
||||||
|
5
src/org/kar/archidata/dataAccess/LazyGetter.java
Normal file
5
src/org/kar/archidata/dataAccess/LazyGetter.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package org.kar.archidata.dataAccess;
|
||||||
|
|
||||||
|
public interface LazyGetter {
|
||||||
|
void doRequest() throws Exception;
|
||||||
|
}
|
@ -2,28 +2,28 @@ package org.kar.archidata.dataAccess;
|
|||||||
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class QueryAnd implements QueryItem {
|
public class QueryAnd implements QueryItem {
|
||||||
protected final List<QueryItem> childs;
|
protected final List<QueryItem> childs;
|
||||||
|
|
||||||
public QueryAnd(List<QueryItem> childs) {
|
public QueryAnd(final List<QueryItem> childs) {
|
||||||
this.childs = childs;
|
this.childs = childs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryAnd(QueryItem... items) {
|
public QueryAnd(final QueryItem... items) {
|
||||||
this.childs = new ArrayList<>();
|
this.childs = new ArrayList<>();
|
||||||
for (int iii = 0; iii < items.length; iii++) {
|
Collections.addAll(this.childs, items);
|
||||||
this.childs.add(items[iii]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateQuerry(StringBuilder querry, String tableName) {
|
@Override
|
||||||
|
public void generateQuerry(final StringBuilder querry, final String tableName) {
|
||||||
if (this.childs.size() >= 1) {
|
if (this.childs.size() >= 1) {
|
||||||
querry.append(" (");
|
querry.append(" (");
|
||||||
}
|
}
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
for (QueryItem elem : this.childs) {
|
for (final QueryItem elem : this.childs) {
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
@ -37,11 +37,10 @@ public class QueryAnd implements QueryItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int injectQuerry(PreparedStatement ps, int iii) throws Exception {
|
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||||
|
|
||||||
for (QueryItem elem : this.childs) {
|
for (final QueryItem elem : this.childs) {
|
||||||
iii = elem.injectQuerry(ps, iii);
|
elem.injectQuerry(ps, iii);
|
||||||
}
|
}
|
||||||
return iii;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,14 @@ public class QueryCondition implements QueryItem {
|
|||||||
private final String comparator;
|
private final String comparator;
|
||||||
private final Object value;
|
private final Object value;
|
||||||
|
|
||||||
public QueryCondition(String key, String comparator, Object value) {
|
public QueryCondition(final String key, final String comparator, final Object value) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.comparator = comparator;
|
this.comparator = comparator;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateQuerry(StringBuilder querry, String tableName) {
|
@Override
|
||||||
|
public void generateQuerry(final StringBuilder querry, final String tableName) {
|
||||||
querry.append(tableName);
|
querry.append(tableName);
|
||||||
querry.append(".");
|
querry.append(".");
|
||||||
querry.append(this.key);
|
querry.append(this.key);
|
||||||
@ -24,8 +25,8 @@ public class QueryCondition implements QueryItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int injectQuerry(PreparedStatement ps, int iii) throws Exception {
|
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||||
DataAccess.addElement(ps, this.value, iii++);
|
DataAccess.addElement(ps, this.value, iii);
|
||||||
return iii;
|
iii.inc();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,5 +5,5 @@ import java.sql.PreparedStatement;
|
|||||||
public interface QueryItem {
|
public interface QueryItem {
|
||||||
void generateQuerry(StringBuilder querry, String tableName);
|
void generateQuerry(StringBuilder querry, String tableName);
|
||||||
|
|
||||||
int injectQuerry(PreparedStatement ps, int iii) throws Exception;
|
void injectQuerry(PreparedStatement ps, CountInOut iii) throws Exception;
|
||||||
}
|
}
|
||||||
|
@ -6,16 +6,17 @@ import java.util.List;
|
|||||||
public class QueryOr implements QueryItem {
|
public class QueryOr implements QueryItem {
|
||||||
protected final List<QueryItem> childs;
|
protected final List<QueryItem> childs;
|
||||||
|
|
||||||
public QueryOr(List<QueryItem> childs) {
|
public QueryOr(final List<QueryItem> childs) {
|
||||||
this.childs = childs;
|
this.childs = childs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateQuerry(StringBuilder querry, String tableName) {
|
@Override
|
||||||
|
public void generateQuerry(final StringBuilder querry, final String tableName) {
|
||||||
if (this.childs.size() >= 1) {
|
if (this.childs.size() >= 1) {
|
||||||
querry.append(" (");
|
querry.append(" (");
|
||||||
}
|
}
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
for (QueryItem elem : this.childs) {
|
for (final QueryItem elem : this.childs) {
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
@ -29,11 +30,9 @@ public class QueryOr implements QueryItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int injectQuerry(PreparedStatement ps, int iii) throws Exception {
|
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||||
|
for (final QueryItem elem : this.childs) {
|
||||||
for (QueryItem elem : this.childs) {
|
elem.injectQuerry(ps, iii);
|
||||||
iii = elem.injectQuerry(ps, iii);
|
}
|
||||||
}
|
|
||||||
return iii;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
93
src/org/kar/archidata/dataAccess/addOn/AddOnDataJson.java
Normal file
93
src/org/kar/archidata/dataAccess/addOn/AddOnDataJson.java
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
package org.kar.archidata.dataAccess.addOn;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.Types;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.kar.archidata.annotation.AnnotationTools;
|
||||||
|
import org.kar.archidata.annotation.DataJson;
|
||||||
|
import org.kar.archidata.dataAccess.CountInOut;
|
||||||
|
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
||||||
|
import org.kar.archidata.dataAccess.DataFactory;
|
||||||
|
import org.kar.archidata.dataAccess.LazyGetter;
|
||||||
|
import org.kar.archidata.dataAccess.QueryOptions;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public class AddOnDataJson implements DataAccessAddOn {
|
||||||
|
static final Logger LOGGER = LoggerFactory.getLogger(AddOnDataJson.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getAnnotationClass() {
|
||||||
|
return DataJson.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSQLFieldType(final Field elem) throws Exception {
|
||||||
|
final String fieldName = AnnotationTools.getFieldName(elem);
|
||||||
|
return DataFactory.convertTypeInSQL(String.class, fieldName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCompatibleField(final Field elem) {
|
||||||
|
final DataJson decorators = elem.getDeclaredAnnotation(DataJson.class);
|
||||||
|
return decorators != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii) throws Exception {
|
||||||
|
final Object data = field.get(rootObject);
|
||||||
|
if (data == null) {
|
||||||
|
ps.setNull(iii.value, Types.VARCHAR);
|
||||||
|
}
|
||||||
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
final String dataString = objectMapper.writeValueAsString(data);
|
||||||
|
ps.setString(iii.value, dataString);
|
||||||
|
iii.inc();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canInsert(final Field field) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canRetrieve(final Field field) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name,
|
||||||
|
@NotNull final CountInOut elemCount, final QueryOptions options) throws Exception {
|
||||||
|
querrySelect.append(" ");
|
||||||
|
querrySelect.append(tableName);
|
||||||
|
querrySelect.append(".");
|
||||||
|
querrySelect.append(name);
|
||||||
|
elemCount.inc();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 {
|
||||||
|
final String jsonData = rs.getString(count.value);
|
||||||
|
count.inc();
|
||||||
|
if (!rs.wasNull()) {
|
||||||
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
final Object dataParsed = objectMapper.readValue(jsonData, field.getType());
|
||||||
|
field.set(data, dataParsed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createTables(final String tableName, final Field field, final StringBuilder mainTableBuilder, final List<String> preActionList, final List<String> postActionList,
|
||||||
|
final boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception {
|
||||||
|
DataFactory.createTablesSpecificType(tableName, field, mainTableBuilder, preActionList, postActionList, createIfNotExist, createDrop, fieldId, JsonValue.class);
|
||||||
|
}
|
||||||
|
}
|
@ -1,23 +1,30 @@
|
|||||||
package org.kar.archidata.dataAccess.addOn;
|
package org.kar.archidata.dataAccess.addOn;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.AnnotationTools;
|
import org.kar.archidata.annotation.AnnotationTools;
|
||||||
|
import org.kar.archidata.dataAccess.CountInOut;
|
||||||
import org.kar.archidata.dataAccess.DataAccess;
|
import org.kar.archidata.dataAccess.DataAccess;
|
||||||
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
||||||
import org.kar.archidata.dataAccess.DataFactory;
|
import org.kar.archidata.dataAccess.DataFactory;
|
||||||
|
import org.kar.archidata.dataAccess.LazyGetter;
|
||||||
import org.kar.archidata.dataAccess.QueryAnd;
|
import org.kar.archidata.dataAccess.QueryAnd;
|
||||||
import org.kar.archidata.dataAccess.QueryCondition;
|
import org.kar.archidata.dataAccess.QueryCondition;
|
||||||
|
import org.kar.archidata.dataAccess.QueryItem;
|
||||||
import org.kar.archidata.dataAccess.QueryOptions;
|
import org.kar.archidata.dataAccess.QueryOptions;
|
||||||
|
import org.kar.archidata.dataAccess.QueryOr;
|
||||||
import org.kar.archidata.dataAccess.addOn.model.LinkTable;
|
import org.kar.archidata.dataAccess.addOn.model.LinkTable;
|
||||||
import org.kar.archidata.util.ConfigBaseVariable;
|
import org.kar.archidata.util.ConfigBaseVariable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import jakarta.persistence.FetchType;
|
||||||
import jakarta.persistence.ManyToMany;
|
import jakarta.persistence.ManyToMany;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
@ -42,8 +49,8 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int insertData(final PreparedStatement ps, final Field field, final Object rootObject, final int iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
||||||
return iii;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -69,42 +76,41 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
|||||||
return tableName + "_link_" + localName;
|
return tableName + "_link_" + localName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void generateConcatQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry,
|
||||||
public int generateQuerry(@NotNull final String tableName, @NotNull final Field elem, @NotNull final StringBuilder querry, @NotNull final String name, @NotNull final int elemCount,
|
@NotNull final String name, @NotNull final CountInOut elemCount, final QueryOptions options) {
|
||||||
final QueryOptions options) {
|
|
||||||
final String linkTableName = generateLinkTableName(tableName, name);
|
|
||||||
|
|
||||||
final String tmpVariable = "tmp_" + Integer.toString(elemCount);
|
final String linkTableName = generateLinkTableName(tableName, name);
|
||||||
querry.append(" (SELECT GROUP_CONCAT(");
|
final String tmpVariable = "tmp_" + Integer.toString(elemCount.value);
|
||||||
querry.append(tmpVariable);
|
querrySelect.append(" (SELECT GROUP_CONCAT(");
|
||||||
querry.append(".object2Id");
|
querrySelect.append(tmpVariable);
|
||||||
|
querrySelect.append(".object2Id");
|
||||||
if (ConfigBaseVariable.getDBType().equals("sqlite")) {
|
if (ConfigBaseVariable.getDBType().equals("sqlite")) {
|
||||||
querry.append(", ");
|
querrySelect.append(", ");
|
||||||
} else {
|
} else {
|
||||||
querry.append("SEPARATOR ");
|
querrySelect.append("SEPARATOR ");
|
||||||
}
|
}
|
||||||
querry.append("'");
|
querrySelect.append("'");
|
||||||
querry.append(SEPARATOR);
|
querrySelect.append(SEPARATOR);
|
||||||
querry.append("') FROM ");
|
querrySelect.append("') FROM ");
|
||||||
querry.append(linkTableName);
|
querrySelect.append(linkTableName);
|
||||||
querry.append(" ");
|
querrySelect.append(" ");
|
||||||
querry.append(tmpVariable);
|
querrySelect.append(tmpVariable);
|
||||||
querry.append(" WHERE ");
|
querrySelect.append(" WHERE ");
|
||||||
querry.append(tmpVariable);
|
querrySelect.append(tmpVariable);
|
||||||
querry.append(".deleted = false AND ");
|
querrySelect.append(".deleted = false AND ");
|
||||||
querry.append(tableName);
|
querrySelect.append(tableName);
|
||||||
querry.append(".id = ");
|
querrySelect.append(".id = ");
|
||||||
querry.append(tmpVariable);
|
querrySelect.append(tmpVariable);
|
||||||
querry.append(".");
|
querrySelect.append(".");
|
||||||
querry.append("object1Id ");
|
querrySelect.append("object1Id ");
|
||||||
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
|
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
|
||||||
querry.append(" GROUP BY ");
|
querrySelect.append(" GROUP BY ");
|
||||||
querry.append(tmpVariable);
|
querrySelect.append(tmpVariable);
|
||||||
querry.append(".object2Id");
|
querrySelect.append(".object2Id");
|
||||||
}
|
}
|
||||||
querry.append(") AS ");
|
querrySelect.append(") AS ");
|
||||||
querry.append(name);
|
querrySelect.append(name);
|
||||||
querry.append(" ");
|
querrySelect.append(" ");
|
||||||
/*
|
/*
|
||||||
" (SELECT GROUP_CONCAT(tmp.data_id SEPARATOR '-')" +
|
" (SELECT GROUP_CONCAT(tmp.data_id SEPARATOR '-')" +
|
||||||
" FROM cover_link_node tmp" +
|
" FROM cover_link_node tmp" +
|
||||||
@ -112,15 +118,73 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
|||||||
" AND node.id = tmp.node_id" +
|
" AND node.id = tmp.node_id" +
|
||||||
" GROUP BY tmp.node_id) AS covers" +
|
" GROUP BY tmp.node_id) AS covers" +
|
||||||
*/
|
*/
|
||||||
return 1;
|
elemCount.inc();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int fillFromQuerry(final ResultSet rs, final Field elem, final Object data, final int count, final QueryOptions options)
|
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name,
|
||||||
throws SQLException, IllegalArgumentException, IllegalAccessException {
|
@NotNull final CountInOut elemCount, final QueryOptions options) throws Exception {
|
||||||
final List<Long> idList = DataAccess.getListOfIds(rs, count, SEPARATOR);
|
if (field.getType() != List.class) {
|
||||||
elem.set(data, idList);
|
return;
|
||||||
return 1;
|
}
|
||||||
|
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
|
||||||
|
if (objectClass == Long.class) {
|
||||||
|
generateConcatQuerry(tableName, field, querrySelect, querry, name, elemCount, options);
|
||||||
|
}
|
||||||
|
final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class);
|
||||||
|
if (decorators == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (objectClass == decorators.targetEntity()) {
|
||||||
|
if (decorators.fetch() == FetchType.EAGER) {
|
||||||
|
throw new Exception("EAGER is not supported for list of element...");
|
||||||
|
} else {
|
||||||
|
generateConcatQuerry(tableName, field, querrySelect, querry, name, elemCount, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 {
|
||||||
|
if (field.getType() != List.class) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
|
||||||
|
if (objectClass == Long.class) {
|
||||||
|
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR);
|
||||||
|
field.set(data, idList);
|
||||||
|
count.inc();
|
||||||
|
}
|
||||||
|
final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class);
|
||||||
|
if (decorators == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (objectClass == decorators.targetEntity()) {
|
||||||
|
if (decorators.fetch() == FetchType.EAGER) {
|
||||||
|
throw new Exception("EAGER is not supported for list of element...");
|
||||||
|
} else {
|
||||||
|
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR);
|
||||||
|
//field.set(data, idList);
|
||||||
|
count.inc();
|
||||||
|
if (idList != null && idList.size() > 0) {
|
||||||
|
final String idField = AnnotationTools.getFieldName(AnnotationTools.getIdField(objectClass));
|
||||||
|
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
|
||||||
|
final LazyGetter lambda = () -> {
|
||||||
|
final List<QueryItem> childs = new ArrayList<>();
|
||||||
|
for (final Long elem : idList) {
|
||||||
|
childs.add(new QueryCondition(idField, "=", elem));
|
||||||
|
}
|
||||||
|
// TODO: update to have get with abstract types ....
|
||||||
|
final Object foreignData = DataAccess.getsWhere(decorators.targetEntity(), new QueryOr(childs), options);
|
||||||
|
if (foreignData == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
field.set(data, foreignData);
|
||||||
|
};
|
||||||
|
lazyCall.add(lambda);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addLink(final Class<?> clazz, final long localKey, final String column, final long remoteKey) throws Exception {
|
public static void addLink(final Class<?> clazz, final long localKey, final String column, final long remoteKey) throws Exception {
|
||||||
|
@ -1,236 +0,0 @@
|
|||||||
package org.kar.archidata.dataAccess.addOn;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.kar.archidata.GlobalConfiguration;
|
|
||||||
import org.kar.archidata.annotation.AnnotationTools;
|
|
||||||
import org.kar.archidata.annotation.addOn.DataAddOnManyToManyOrdered;
|
|
||||||
import org.kar.archidata.dataAccess.DataAccess;
|
|
||||||
import org.kar.archidata.dataAccess.DataAccess.ExceptionDBInterface;
|
|
||||||
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
|
||||||
import org.kar.archidata.dataAccess.QueryOptions;
|
|
||||||
import org.kar.archidata.db.DBEntry;
|
|
||||||
import org.kar.archidata.util.ConfigBaseVariable;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Manage the decorator element @DataAddOnManyToManyOrdered to be injected in the DB.
|
|
||||||
* The objective of this table is to manage a link between 2 table that have a specific order (Only work in 1 direction)
|
|
||||||
*/
|
|
||||||
public class AddOnManyToManyOrdered implements DataAccessAddOn {
|
|
||||||
static final Logger LOGGER = LoggerFactory.getLogger(AddOnManyToManyOrdered.class);
|
|
||||||
static final String SEPARATOR = "-";
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<?> getAnnotationClass() {
|
|
||||||
return DataAddOnManyToManyOrdered.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getSQLFieldType(final Field elem) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isCompatibleField(final Field elem) {
|
|
||||||
final DataAddOnManyToManyOrdered decorators = elem.getDeclaredAnnotation(DataAddOnManyToManyOrdered.class);
|
|
||||||
return decorators != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int insertData(final PreparedStatement ps, final Field field, final Object rootObject, final int iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
|
||||||
return iii;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canInsert(final Field field) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canRetrieve(final Field field) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int generateQuerry(@NotNull final String tableName, @NotNull final Field elem, @NotNull final StringBuilder querry, @NotNull final String name, @NotNull final int elemCount,
|
|
||||||
final QueryOptions options) {
|
|
||||||
String localName = name;
|
|
||||||
if (name.endsWith("s")) {
|
|
||||||
localName = name.substring(0, name.length() - 1);
|
|
||||||
}
|
|
||||||
final String tmpVariable = "tmp_" + Integer.toString(elemCount);
|
|
||||||
querry.append(" (SELECT GROUP_CONCAT(");
|
|
||||||
querry.append(tmpVariable);
|
|
||||||
querry.append(".");
|
|
||||||
querry.append(localName);
|
|
||||||
querry.append("_id ");
|
|
||||||
if (ConfigBaseVariable.getDBType().equals("sqlite")) {
|
|
||||||
querry.append(", ");
|
|
||||||
} else {
|
|
||||||
querry.append("SEPARATOR ");
|
|
||||||
}
|
|
||||||
querry.append("'");
|
|
||||||
querry.append(SEPARATOR);
|
|
||||||
querry.append("') FROM ");
|
|
||||||
querry.append(tableName);
|
|
||||||
querry.append("_link_");
|
|
||||||
querry.append(localName);
|
|
||||||
querry.append(" ");
|
|
||||||
querry.append(tmpVariable);
|
|
||||||
querry.append(" WHERE ");
|
|
||||||
querry.append(tmpVariable);
|
|
||||||
querry.append(".deleted = false AND ");
|
|
||||||
querry.append(tableName);
|
|
||||||
querry.append(".id = ");
|
|
||||||
querry.append(tmpVariable);
|
|
||||||
querry.append(".");
|
|
||||||
querry.append(tableName);
|
|
||||||
querry.append("_id ORDER BY ");
|
|
||||||
querry.append(tmpVariable);
|
|
||||||
querry.append(".order ASC");
|
|
||||||
querry.append(" GROUP BY ");
|
|
||||||
querry.append(tmpVariable);
|
|
||||||
querry.append(".");
|
|
||||||
querry.append(tableName);
|
|
||||||
querry.append("_id ) AS ");
|
|
||||||
querry.append(name);
|
|
||||||
querry.append(" ");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int fillFromQuerry(final ResultSet rs, final Field elem, final Object data, final int count, final QueryOptions options)
|
|
||||||
throws SQLException, IllegalArgumentException, IllegalAccessException {
|
|
||||||
//throw new IllegalAccessException("This Add-on has not the capability to insert data directly in DB");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void addLink(final Class<?> clazz, final long localKey, final String table, final long remoteKey) throws Exception {
|
|
||||||
final String tableName = AnnotationTools.getTableName(clazz);
|
|
||||||
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
|
||||||
long uniqueSQLID = -1;
|
|
||||||
// real add in the BDD:
|
|
||||||
try {
|
|
||||||
// prepare the request:
|
|
||||||
final String querry = "INSERT INTO " + tableName + "_link_" + table + " (create_date, modify_date, " + tableName + "_id, " + table + "_id)" + " VALUES (" + DataAccess.getDBNow() + ", "
|
|
||||||
+ DataAccess.getDBNow() + ", ?, ?)";
|
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(querry, Statement.RETURN_GENERATED_KEYS);
|
|
||||||
int iii = 1;
|
|
||||||
ps.setLong(iii++, localKey);
|
|
||||||
ps.setLong(iii++, remoteKey);
|
|
||||||
// execute the request
|
|
||||||
final int affectedRows = ps.executeUpdate();
|
|
||||||
if (affectedRows == 0) {
|
|
||||||
throw new SQLException("Creating data failed, no rows affected.");
|
|
||||||
}
|
|
||||||
// retrieve uid inserted
|
|
||||||
try (ResultSet generatedKeys = ps.getGeneratedKeys()) {
|
|
||||||
if (generatedKeys.next()) {
|
|
||||||
uniqueSQLID = generatedKeys.getLong(1);
|
|
||||||
} else {
|
|
||||||
throw new SQLException("Creating user failed, no ID obtained (1).");
|
|
||||||
}
|
|
||||||
} catch (final Exception ex) {
|
|
||||||
LOGGER.debug("Can not get the UID key inserted ... ");
|
|
||||||
ex.printStackTrace();
|
|
||||||
throw new SQLException("Creating user failed, no ID obtained (2).");
|
|
||||||
}
|
|
||||||
} catch (final SQLException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
throw new ExceptionDBInterface(500, "SQL error: " + ex.getMessage());
|
|
||||||
} finally {
|
|
||||||
entry.close();
|
|
||||||
entry = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void removeLink(final Class<?> clazz, final long localKey, final String table, final long remoteKey) throws Exception {
|
|
||||||
final String tableName = AnnotationTools.getTableName(clazz);
|
|
||||||
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
|
||||||
final String querry = "UPDATE `" + tableName + "_link_" + table + "` SET `modify_date`=" + DataAccess.getDBNow() + ", `deleted`=true WHERE `" + tableName + "_id` = ? AND `" + table
|
|
||||||
+ "_id` = ?";
|
|
||||||
try {
|
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(querry);
|
|
||||||
int iii = 1;
|
|
||||||
ps.setLong(iii++, localKey);
|
|
||||||
ps.setLong(iii++, remoteKey);
|
|
||||||
ps.executeUpdate();
|
|
||||||
} catch (final SQLException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
throw new ExceptionDBInterface(500, "SQL error: " + ex.getMessage());
|
|
||||||
} finally {
|
|
||||||
entry.close();
|
|
||||||
entry = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO : refacto this table to manage a generic table with dynamic name to be serializable with the default system
|
|
||||||
@Override
|
|
||||||
public void createTables(final String tableName, final Field field, final StringBuilder mainTableBuilder, final List<String> preActionList, final List<String> postActionList,
|
|
||||||
final boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception {
|
|
||||||
final String name = AnnotationTools.getFieldName(field);
|
|
||||||
String localName = name;
|
|
||||||
if (name.endsWith("s")) {
|
|
||||||
localName = name.substring(0, name.length() - 1);
|
|
||||||
}
|
|
||||||
if (createIfNotExist && createDrop) {
|
|
||||||
final StringBuilder tableTmp = new StringBuilder();
|
|
||||||
tableTmp.append("DROP TABLE IF EXISTS `");
|
|
||||||
tableTmp.append(tableName);
|
|
||||||
tableTmp.append("_link_");
|
|
||||||
tableTmp.append(localName);
|
|
||||||
tableTmp.append("`;");
|
|
||||||
postActionList.add(tableTmp.toString());
|
|
||||||
}
|
|
||||||
final StringBuilder otherTable = new StringBuilder();
|
|
||||||
otherTable.append("CREATE TABLE `");
|
|
||||||
otherTable.append(tableName);
|
|
||||||
otherTable.append("_link_");
|
|
||||||
otherTable.append(localName);
|
|
||||||
otherTable.append("`(\n");
|
|
||||||
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
|
|
||||||
otherTable.append("\t\t`id` bigint NOT NULL AUTO_INCREMENT,\n");
|
|
||||||
otherTable.append("\t\t`deleted` tinyint(1) NOT NULL DEFAULT '0',\n");
|
|
||||||
otherTable.append("\t\t`createdAt` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),\n");
|
|
||||||
otherTable.append("\t\t`updatedAt` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),\n");
|
|
||||||
} else {
|
|
||||||
otherTable.append("\t\t`id` INTEGER PRIMARY KEY AUTOINCREMENT,\n");
|
|
||||||
otherTable.append("\t\t`deleted` INTEGER NOT NULL DEFAULT '0',\n");
|
|
||||||
otherTable.append("\t\t`createdAt` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP,\n");
|
|
||||||
otherTable.append("\t\t`updatedAt` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP,\n");
|
|
||||||
}
|
|
||||||
otherTable.append("\t\t`");
|
|
||||||
otherTable.append(tableName);
|
|
||||||
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
|
|
||||||
otherTable.append("_id` bigint NOT NULL,\n");
|
|
||||||
} else {
|
|
||||||
otherTable.append("_id` INTEGER NOT NULL,\n");
|
|
||||||
}
|
|
||||||
otherTable.append("\t\t`order` INTEGER NOT NULL DEFAULT 0,\n");
|
|
||||||
otherTable.append("\t\t`");
|
|
||||||
otherTable.append(localName);
|
|
||||||
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
|
|
||||||
otherTable.append("_id` bigint NOT NULL\n");
|
|
||||||
} else {
|
|
||||||
otherTable.append("_id` INTEGER NOT NULL\n");
|
|
||||||
}
|
|
||||||
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
|
|
||||||
otherTable.append("\t, PRIMARY KEY (`id`)\n");
|
|
||||||
}
|
|
||||||
otherTable.append("\t)");
|
|
||||||
if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
|
|
||||||
otherTable.append(" ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;\n\n");
|
|
||||||
}
|
|
||||||
otherTable.append(";");
|
|
||||||
postActionList.add(otherTable.toString());
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,18 +3,20 @@ package org.kar.archidata.dataAccess.addOn;
|
|||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Types;
|
import java.sql.Types;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.AnnotationTools;
|
import org.kar.archidata.annotation.AnnotationTools;
|
||||||
|
import org.kar.archidata.dataAccess.CountInOut;
|
||||||
import org.kar.archidata.dataAccess.DataAccess;
|
import org.kar.archidata.dataAccess.DataAccess;
|
||||||
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
||||||
import org.kar.archidata.dataAccess.DataFactory;
|
import org.kar.archidata.dataAccess.DataFactory;
|
||||||
|
import org.kar.archidata.dataAccess.LazyGetter;
|
||||||
import org.kar.archidata.dataAccess.QueryOptions;
|
import org.kar.archidata.dataAccess.QueryOptions;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import jakarta.persistence.FetchType;
|
||||||
import jakarta.persistence.ManyToOne;
|
import jakarta.persistence.ManyToOne;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
@ -45,25 +47,25 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int insertData(final PreparedStatement ps, final Field field, final Object rootObject, int iii) throws Exception {
|
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii) throws Exception {
|
||||||
final Object data = field.get(rootObject);
|
final Object data = field.get(rootObject);
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
ps.setNull(iii++, Types.BIGINT);
|
ps.setNull(iii.value, Types.BIGINT);
|
||||||
} else if (field.getType() == Long.class) {
|
} else if (field.getType() == Long.class) {
|
||||||
final Long dataLong = (Long) data;
|
final Long dataLong = (Long) data;
|
||||||
ps.setLong(iii++, dataLong);
|
ps.setLong(iii.value, dataLong);
|
||||||
} else {
|
} else {
|
||||||
final Field idField = AnnotationTools.getFieldOfId(field.getType());
|
final Field idField = AnnotationTools.getFieldOfId(field.getType());
|
||||||
final Object uid = idField.get(data);
|
final Object uid = idField.get(data);
|
||||||
if (uid == null) {
|
if (uid == null) {
|
||||||
ps.setNull(iii++, Types.BIGINT);
|
ps.setNull(iii.value, Types.BIGINT);
|
||||||
throw new Exception("Not implemented adding subClasses ==> add it manualy before...");
|
throw new Exception("Not implemented adding subClasses ==> add it manualy before...");
|
||||||
} else {
|
} else {
|
||||||
final Long dataLong = (Long) uid;
|
final Long dataLong = (Long) uid;
|
||||||
ps.setLong(iii++, dataLong);
|
ps.setLong(iii.value, dataLong);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return iii++;
|
iii.inc();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -91,34 +93,94 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querry, @NotNull final String name, @NotNull final int elemCount,
|
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name,
|
||||||
final QueryOptions options) throws Exception {
|
@NotNull final CountInOut elemCount, final QueryOptions options) throws Exception {
|
||||||
if (field.getType() == Long.class) {
|
if (field.getType() == Long.class) {
|
||||||
querry.append(" ");
|
querrySelect.append(" ");
|
||||||
querry.append(tableName);
|
querrySelect.append(tableName);
|
||||||
querry.append(".");
|
querrySelect.append(".");
|
||||||
querry.append(name);
|
querrySelect.append(name);
|
||||||
return elemCount + 1;
|
elemCount.inc();
|
||||||
|
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()) {
|
||||||
return DataAccess.generateSelectField(querry, field.getType(), options, elemCount);
|
if (decorators.fetch() == FetchType.EAGER) {
|
||||||
|
// TODO: rework this to have a lazy mode ...
|
||||||
|
DataAccess.generateSelectField(querrySelect, querry, field.getType(), options, elemCount);
|
||||||
|
final Class<?> subType = field.getType();
|
||||||
|
final String subTableName = AnnotationTools.getTableName(subType);
|
||||||
|
final Field idField = AnnotationTools.getFieldOfId(subType);
|
||||||
|
querry.append("LEFT OUTER JOIN `");
|
||||||
|
querry.append(subTableName);
|
||||||
|
querry.append("` ON ");
|
||||||
|
querry.append(subTableName);
|
||||||
|
querry.append(".");
|
||||||
|
querry.append(AnnotationTools.getFieldName(idField));
|
||||||
|
querry.append(" = ");
|
||||||
|
querry.append(tableName);
|
||||||
|
querry.append(".");
|
||||||
|
querry.append(AnnotationTools.getFieldName(field));
|
||||||
|
} else {
|
||||||
|
querrySelect.append(" ");
|
||||||
|
querrySelect.append(tableName);
|
||||||
|
querrySelect.append(".");
|
||||||
|
querrySelect.append(name);
|
||||||
|
elemCount.inc();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return elemCount;
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
SELECT k.id, r.id
|
||||||
|
FROM `right` k
|
||||||
|
LEFT OUTER JOIN `rightDescription` r ON k.rightDescriptionId=r.id
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int fillFromQuerry(final ResultSet rs, final Field field, final Object data, final int count, final QueryOptions options)
|
public void fillFromQuerry(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) throws Exception {
|
||||||
throws SQLException, IllegalArgumentException, IllegalAccessException {
|
if (field.getType() == Long.class) {
|
||||||
final Long foreignKey = rs.getLong(count);
|
final Long foreignKey = rs.getLong(count.value);
|
||||||
if (rs.wasNull()) {
|
count.inc();
|
||||||
return 0;
|
if (!rs.wasNull()) {
|
||||||
}
|
|
||||||
field.set(data, foreignKey);
|
field.set(data, foreignKey);
|
||||||
return 1;
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final Class<?> objectClass = field.getType();
|
||||||
|
final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class);
|
||||||
|
if (decorators == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (objectClass == decorators.targetEntity()) {
|
||||||
|
if (decorators.fetch() == FetchType.EAGER) {
|
||||||
|
final CountInOut countNotNull = new CountInOut(0);
|
||||||
|
final Object dataNew = DataAccess.createObjectFromSQLRequest(rs, objectClass, count, countNotNull, options, lazyCall);
|
||||||
|
if (dataNew != null && countNotNull.value != 0) {
|
||||||
|
field.set(data, dataNew);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// here we have the field, the data and the the remote value ==> can create callback that generate the update of the value ...
|
||||||
|
final Long foreignKey = rs.getLong(count.value);
|
||||||
|
count.inc();
|
||||||
|
if (!rs.wasNull()) {
|
||||||
|
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
|
||||||
|
final LazyGetter lambda = () -> {
|
||||||
|
// TODO: update to have get with abstract types ....
|
||||||
|
final Object foreignData = DataAccess.get(decorators.targetEntity(), foreignKey);
|
||||||
|
if (foreignData == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
field.set(data, foreignData);
|
||||||
|
};
|
||||||
|
lazyCall.add(lambda);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : refacto this table to manage a generic table with dynamic name to be serializable with the default system
|
// TODO : refacto this table to manage a generic table with dynamic name to be serialisable with the default system
|
||||||
@Override
|
@Override
|
||||||
public void createTables(final String tableName, final Field field, final StringBuilder mainTableBuilder, final List<String> preActionList, final List<String> postActionList,
|
public void createTables(final String tableName, final Field field, final StringBuilder mainTableBuilder, final List<String> preActionList, final List<String> postActionList,
|
||||||
final boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception {
|
final boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception {
|
||||||
|
@ -10,8 +10,10 @@ import java.util.List;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.AnnotationTools;
|
import org.kar.archidata.annotation.AnnotationTools;
|
||||||
|
import org.kar.archidata.dataAccess.CountInOut;
|
||||||
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
||||||
import org.kar.archidata.dataAccess.DataFactory;
|
import org.kar.archidata.dataAccess.DataFactory;
|
||||||
|
import org.kar.archidata.dataAccess.LazyGetter;
|
||||||
import org.kar.archidata.dataAccess.QueryOptions;
|
import org.kar.archidata.dataAccess.QueryOptions;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -77,16 +79,16 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int insertData(final PreparedStatement ps, final Field field, final Object rootObject, int iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
||||||
final Object data = field.get(rootObject);
|
final Object data = field.get(rootObject);
|
||||||
|
iii.inc();
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
ps.setNull(iii++, Types.BIGINT);
|
ps.setNull(iii.value, Types.BIGINT);
|
||||||
} else {
|
} else {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
final String dataTmp = getStringOfIds((List<Long>) data);
|
final String dataTmp = getStringOfIds((List<Long>) data);
|
||||||
ps.setString(iii++, dataTmp);
|
ps.setString(iii.value, dataTmp);
|
||||||
}
|
}
|
||||||
return iii++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -100,24 +102,24 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querry, @NotNull final String name, @NotNull final int elemCount,
|
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name,
|
||||||
final QueryOptions options) {
|
@NotNull final CountInOut elemCount, final QueryOptions options) {
|
||||||
querry.append(" ");
|
querrySelect.append(" ");
|
||||||
querry.append(tableName);
|
querrySelect.append(tableName);
|
||||||
querry.append(".");
|
querrySelect.append(".");
|
||||||
querry.append(name);
|
querrySelect.append(name);
|
||||||
return 1;
|
elemCount.inc();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int fillFromQuerry(final ResultSet rs, final Field field, final Object data, final int count, final QueryOptions options)
|
public void fillFromQuerry(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);
|
final Long foreignKey = rs.getLong(count.value);
|
||||||
if (rs.wasNull()) {
|
count.inc();
|
||||||
return 0;
|
if (!rs.wasNull()) {
|
||||||
}
|
|
||||||
field.set(data, foreignKey);
|
field.set(data, foreignKey);
|
||||||
return 1;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : refacto this table to manage a generic table with dynamic name to be serializable with the default system
|
// TODO : refacto this table to manage a generic table with dynamic name to be serializable with the default system
|
||||||
|
@ -11,9 +11,11 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import org.kar.archidata.annotation.AnnotationTools;
|
import org.kar.archidata.annotation.AnnotationTools;
|
||||||
import org.kar.archidata.annotation.addOn.SQLTableExternalForeinKeyAsList;
|
import org.kar.archidata.annotation.addOn.SQLTableExternalForeinKeyAsList;
|
||||||
|
import org.kar.archidata.dataAccess.CountInOut;
|
||||||
import org.kar.archidata.dataAccess.DataAccess;
|
import org.kar.archidata.dataAccess.DataAccess;
|
||||||
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
import org.kar.archidata.dataAccess.DataAccessAddOn;
|
||||||
import org.kar.archidata.dataAccess.DataFactory;
|
import org.kar.archidata.dataAccess.DataFactory;
|
||||||
|
import org.kar.archidata.dataAccess.LazyGetter;
|
||||||
import org.kar.archidata.dataAccess.QueryOptions;
|
import org.kar.archidata.dataAccess.QueryOptions;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -58,16 +60,16 @@ public class AddOnSQLTableExternalForeinKeyAsList implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int insertData(final PreparedStatement ps, final Field field, final Object rootObject, int iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
||||||
final Object data = field.get(rootObject);
|
final Object data = field.get(rootObject);
|
||||||
|
iii.inc();
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
ps.setNull(iii++, Types.BIGINT);
|
ps.setNull(iii.value, Types.BIGINT);
|
||||||
} else {
|
} else {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
final String dataTmp = getStringOfIds((List<Long>) data);
|
final String dataTmp = getStringOfIds((List<Long>) data);
|
||||||
ps.setString(iii++, dataTmp);
|
ps.setString(iii.value, dataTmp);
|
||||||
}
|
}
|
||||||
return iii++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -81,21 +83,21 @@ public class AddOnSQLTableExternalForeinKeyAsList implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querry, @NotNull final String name, @NotNull final int elemCount,
|
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name,
|
||||||
final QueryOptions options) {
|
@NotNull final CountInOut elemCount, final QueryOptions options) {
|
||||||
querry.append(" ");
|
elemCount.inc();
|
||||||
querry.append(tableName);
|
querrySelect.append(" ");
|
||||||
querry.append(".");
|
querrySelect.append(tableName);
|
||||||
querry.append(name);
|
querrySelect.append(".");
|
||||||
return 1;
|
querrySelect.append(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int fillFromQuerry(final ResultSet rs, final Field field, final Object data, final int count, final QueryOptions options)
|
public void fillFromQuerry(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, SEPARATOR);
|
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR);
|
||||||
field.set(data, idList);
|
field.set(data, idList);
|
||||||
return 1;
|
count.inc();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package org.kar.archidata.dataAccess.addOn.model;
|
package org.kar.archidata.dataAccess.addOn.model;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.SQLComment;
|
import org.kar.archidata.annotation.DataComment;
|
||||||
import org.kar.archidata.model.GenericDataSoftDelete;
|
import org.kar.archidata.model.GenericDataSoftDelete;
|
||||||
|
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
@ -15,10 +15,10 @@ public class LinkTable extends GenericDataSoftDelete {
|
|||||||
this.object2Id = object2Id;
|
this.object2Id = object2Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SQLComment("Object reference 1")
|
@DataComment("Object reference 1")
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
public Long object1Id;
|
public Long object1Id;
|
||||||
@SQLComment("Object reference 2")
|
@DataComment("Object reference 2")
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
public Long object2Id;
|
public Long object2Id;
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package org.kar.archidata.migration.model;
|
package org.kar.archidata.migration.model;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.SQLComment;
|
import org.kar.archidata.annotation.DataComment;
|
||||||
import org.kar.archidata.annotation.SQLDefault;
|
import org.kar.archidata.annotation.DataDefault;
|
||||||
import org.kar.archidata.annotation.SQLIfNotExists;
|
import org.kar.archidata.annotation.DataIfNotExists;
|
||||||
import org.kar.archidata.model.GenericDataSoftDelete;
|
import org.kar.archidata.model.GenericDataSoftDelete;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
@ -14,20 +14,20 @@ import jakarta.persistence.Table;
|
|||||||
//public static final String TABLE_NAME = "KAR_migration";
|
//public static final String TABLE_NAME = "KAR_migration";
|
||||||
|
|
||||||
@Table(name = "KAR_migration")
|
@Table(name = "KAR_migration")
|
||||||
@SQLIfNotExists
|
@DataIfNotExists
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public class Migration extends GenericDataSoftDelete {
|
public class Migration extends GenericDataSoftDelete {
|
||||||
@SQLComment("Name of the migration")
|
@DataComment("Name of the migration")
|
||||||
@Column(length = 256)
|
@Column(length = 256)
|
||||||
public String name;
|
public String name;
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@SQLDefault("'0'")
|
@DataDefault("'0'")
|
||||||
@SQLComment("if the migration is well terminated or not")
|
@DataComment("if the migration is well terminated or not")
|
||||||
public Boolean terminated = false;
|
public Boolean terminated = false;
|
||||||
@SQLComment("index in the migration progression")
|
@DataComment("index in the migration progression")
|
||||||
public Integer stepId = 0;
|
public Integer stepId = 0;
|
||||||
@SQLComment("number of element in the migration")
|
@DataComment("number of element in the migration")
|
||||||
public Integer count;
|
public Integer count;
|
||||||
@SQLComment("Log generate by the migration")
|
@DataComment("Log generate by the migration")
|
||||||
public String log = "";
|
public String log = "";
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package org.kar.archidata.model;
|
package org.kar.archidata.model;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.SQLComment;
|
import org.kar.archidata.annotation.DataComment;
|
||||||
import org.kar.archidata.annotation.SQLIfNotExists;
|
import org.kar.archidata.annotation.DataIfNotExists;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
|
||||||
@ -9,17 +9,17 @@ import jakarta.persistence.Column;
|
|||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
@Table(name = "data")
|
@Table(name = "data")
|
||||||
@SQLIfNotExists
|
@DataIfNotExists
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public class Data extends GenericDataSoftDelete {
|
public class Data extends GenericDataSoftDelete {
|
||||||
|
|
||||||
@Column(length = 128, nullable = false)
|
@Column(length = 128, nullable = false)
|
||||||
@SQLComment("Sha512 of the data")
|
@DataComment("Sha512 of the data")
|
||||||
public String sha512;
|
public String sha512;
|
||||||
@Column(length = 128, nullable = false)
|
@Column(length = 128, nullable = false)
|
||||||
@SQLComment("Mime -type of the media")
|
@DataComment("Mime -type of the media")
|
||||||
public String mimeType;
|
public String mimeType;
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@SQLComment("Size in Byte of the data")
|
@DataComment("Size in Byte of the data")
|
||||||
public Long size;
|
public Long size;
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,8 @@ package org.kar.archidata.model;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.CreationTimestamp;
|
import org.kar.archidata.annotation.CreationTimestamp;
|
||||||
import org.kar.archidata.annotation.SQLComment;
|
import org.kar.archidata.annotation.DataComment;
|
||||||
import org.kar.archidata.annotation.SQLNotRead;
|
import org.kar.archidata.annotation.DataNotRead;
|
||||||
import org.kar.archidata.annotation.UpdateTimestamp;
|
import org.kar.archidata.annotation.UpdateTimestamp;
|
||||||
|
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
@ -18,18 +18,18 @@ public class GenericData {
|
|||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@Column(nullable = false, unique = true)
|
@Column(nullable = false, unique = true)
|
||||||
@SQLComment("Primary key of the base")
|
@DataComment("Primary key of the base")
|
||||||
public Long id = null;
|
public Long id = null;
|
||||||
@SQLNotRead
|
@DataNotRead
|
||||||
@CreationTimestamp
|
@CreationTimestamp
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
@SQLComment("Create time of the object")
|
@DataComment("Create time of the object")
|
||||||
public Date createdAt = null;
|
public Date createdAt = null;
|
||||||
@SQLNotRead
|
@DataNotRead
|
||||||
@UpdateTimestamp
|
@UpdateTimestamp
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
@SQLComment("When update the object")
|
@DataComment("When update the object")
|
||||||
public Date updatedAt = null;
|
public Date updatedAt = null;
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
package org.kar.archidata.model;
|
package org.kar.archidata.model;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.SQLComment;
|
import org.kar.archidata.annotation.DataComment;
|
||||||
import org.kar.archidata.annotation.SQLDefault;
|
import org.kar.archidata.annotation.DataDefault;
|
||||||
import org.kar.archidata.annotation.SQLDeleted;
|
import org.kar.archidata.annotation.DataDeleted;
|
||||||
import org.kar.archidata.annotation.SQLNotRead;
|
import org.kar.archidata.annotation.DataNotRead;
|
||||||
|
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
|
|
||||||
public class GenericDataSoftDelete extends GenericData {
|
public class GenericDataSoftDelete extends GenericData {
|
||||||
@SQLNotRead
|
@DataNotRead
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@SQLDefault("'0'")
|
@DataDefault("'0'")
|
||||||
@SQLDeleted
|
@DataDeleted
|
||||||
@SQLComment("When delete, they are not removed, they are just set in a deleted state")
|
@DataComment("When delete, they are not removed, they are just set in a deleted state")
|
||||||
public Boolean deleted = null;
|
public Boolean deleted = null;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package org.kar.archidata.model;
|
|||||||
|
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.SQLIfNotExists;
|
import org.kar.archidata.annotation.DataIfNotExists;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ import jakarta.persistence.Column;
|
|||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
@Table(name = "applicationToken")
|
@Table(name = "applicationToken")
|
||||||
@SQLIfNotExists
|
@DataIfNotExists
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public class GenericToken extends GenericDataSoftDelete {
|
public class GenericToken extends GenericDataSoftDelete {
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
@ -17,8 +17,8 @@ CREATE TABLE `user` (
|
|||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.SQLDefault;
|
import org.kar.archidata.annotation.DataDefault;
|
||||||
import org.kar.archidata.annotation.SQLIfNotExists;
|
import org.kar.archidata.annotation.DataIfNotExists;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
|
||||||
@ -28,20 +28,20 @@ import jakarta.persistence.ManyToMany;
|
|||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
@Table(name = "user")
|
@Table(name = "user")
|
||||||
@SQLIfNotExists
|
@DataIfNotExists
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public class User extends GenericDataSoftDelete {
|
public class User extends GenericDataSoftDelete {
|
||||||
@Column(length = 128)
|
@Column(length = 128)
|
||||||
public String login = null;
|
public String login = null;
|
||||||
|
|
||||||
public Timestamp lastConnection = null;
|
public Timestamp lastConnection = null;
|
||||||
@SQLDefault("'0'")
|
@DataDefault("'0'")
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
public boolean admin = false;
|
public boolean admin = false;
|
||||||
@SQLDefault("'0'")
|
@DataDefault("'0'")
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
public boolean blocked = false;
|
public boolean blocked = false;
|
||||||
@SQLDefault("'0'")
|
@DataDefault("'0'")
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
public boolean removed = false;
|
public boolean removed = false;
|
||||||
|
|
||||||
|
86
test/src/test/kar/archidata/TestJson.java
Normal file
86
test/src/test/kar/archidata/TestJson.java
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package test.kar.archidata;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.MethodOrderer;
|
||||||
|
import org.junit.jupiter.api.Order;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.kar.archidata.GlobalConfiguration;
|
||||||
|
import org.kar.archidata.dataAccess.DataAccess;
|
||||||
|
import org.kar.archidata.dataAccess.DataFactory;
|
||||||
|
import org.kar.archidata.db.DBEntry;
|
||||||
|
import org.kar.archidata.util.ConfigBaseVariable;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import test.kar.archidata.model.SerializeAsJson;
|
||||||
|
import test.kar.archidata.model.SimpleTable;
|
||||||
|
|
||||||
|
@ExtendWith(StepwiseExtension.class)
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
public class TestJson {
|
||||||
|
final static private Logger LOGGER = LoggerFactory.getLogger(TestJson.class);
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void configureWebServer() throws Exception {
|
||||||
|
ConfigBaseVariable.dbType = "sqlite";
|
||||||
|
ConfigBaseVariable.dbHost = "memory";
|
||||||
|
// for test we need to connect all time the DB
|
||||||
|
ConfigBaseVariable.dbKeepConnected = "true";
|
||||||
|
|
||||||
|
// Connect the dataBase...
|
||||||
|
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||||
|
entry.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void removeDataBase() throws IOException {
|
||||||
|
LOGGER.info("Remove the test db");
|
||||||
|
DBEntry.closeAllForceMode();
|
||||||
|
ConfigBaseVariable.clearAllValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(1)
|
||||||
|
@Test
|
||||||
|
public void testTableInsertAndRetrieve() throws Exception {
|
||||||
|
final List<String> sqlCommand = DataFactory.createTable(SerializeAsJson.class);
|
||||||
|
for (final String elem : sqlCommand) {
|
||||||
|
LOGGER.debug("request: '{}'", elem);
|
||||||
|
DataAccess.executeSimpleQuerry(elem, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(2)
|
||||||
|
@Test
|
||||||
|
public void testIO() throws Exception {
|
||||||
|
final SerializeAsJson test = new SerializeAsJson();
|
||||||
|
test.data = new SimpleTable();
|
||||||
|
test.data.data = "plopppopql";
|
||||||
|
|
||||||
|
final SerializeAsJson insertedData = DataAccess.insert(test);
|
||||||
|
|
||||||
|
Assertions.assertNotNull(insertedData);
|
||||||
|
Assertions.assertNotNull(insertedData.id);
|
||||||
|
Assertions.assertTrue(insertedData.id >= 0);
|
||||||
|
Assertions.assertNotNull(insertedData.data);
|
||||||
|
Assertions.assertNotNull(insertedData.data.data);
|
||||||
|
Assertions.assertEquals(test.data.data, insertedData.data.data);
|
||||||
|
|
||||||
|
// Try to retrieve all the data:
|
||||||
|
final SerializeAsJson retrieve = DataAccess.get(SerializeAsJson.class, insertedData.id);
|
||||||
|
|
||||||
|
Assertions.assertNotNull(retrieve);
|
||||||
|
Assertions.assertNotNull(retrieve.id);
|
||||||
|
Assertions.assertTrue(retrieve.id >= 0);
|
||||||
|
Assertions.assertNotNull(retrieve.data);
|
||||||
|
Assertions.assertNotNull(retrieve.data.data);
|
||||||
|
Assertions.assertEquals(test.data.data, retrieve.data.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import test.kar.archidata.model.TypeManyToManyRemote;
|
import test.kar.archidata.model.TypeManyToManyRemote;
|
||||||
import test.kar.archidata.model.TypeManyToManyRoot;
|
import test.kar.archidata.model.TypeManyToManyRoot;
|
||||||
|
import test.kar.archidata.model.TypeManyToManyRootExpand;
|
||||||
|
|
||||||
@ExtendWith(StepwiseExtension.class)
|
@ExtendWith(StepwiseExtension.class)
|
||||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
@ -85,7 +86,7 @@ public class TestManyToMany {
|
|||||||
|
|
||||||
@Order(3)
|
@Order(3)
|
||||||
@Test
|
@Test
|
||||||
public void testSimpleInsertAndRetieveZZZ() throws Exception {
|
public void testSimpleInsertAndRetieveSubValues() throws Exception {
|
||||||
|
|
||||||
TypeManyToManyRemote remote = new TypeManyToManyRemote();
|
TypeManyToManyRemote remote = new TypeManyToManyRemote();
|
||||||
remote.data = "remote1";
|
remote.data = "remote1";
|
||||||
@ -131,6 +132,18 @@ public class TestManyToMany {
|
|||||||
Assertions.assertEquals(retrieve.remote.get(0), insertedRemote1.id);
|
Assertions.assertEquals(retrieve.remote.get(0), insertedRemote1.id);
|
||||||
Assertions.assertEquals(retrieve.remote.get(1), insertedRemote2.id);
|
Assertions.assertEquals(retrieve.remote.get(1), insertedRemote2.id);
|
||||||
|
|
||||||
|
final TypeManyToManyRootExpand retrieveExpand = DataAccess.get(TypeManyToManyRootExpand.class, insertedData.id);
|
||||||
|
|
||||||
|
Assertions.assertNotNull(retrieveExpand);
|
||||||
|
Assertions.assertNotNull(retrieveExpand.id);
|
||||||
|
Assertions.assertEquals(insertedData.id, retrieveExpand.id);
|
||||||
|
Assertions.assertNotNull(retrieveExpand.otherData);
|
||||||
|
Assertions.assertEquals(insertedData.otherData, retrieveExpand.otherData);
|
||||||
|
Assertions.assertNotNull(retrieveExpand.remote);
|
||||||
|
Assertions.assertEquals(retrieveExpand.remote.size(), 2);
|
||||||
|
Assertions.assertEquals(retrieveExpand.remote.get(0).id, insertedRemote1.id);
|
||||||
|
Assertions.assertEquals(retrieveExpand.remote.get(1).id, insertedRemote2.id);
|
||||||
|
|
||||||
// Remove an element
|
// Remove an element
|
||||||
int count = AddOnManyToMany.removeLink(TypeManyToManyRoot.class, retrieve.id, "remote", insertedRemote1.id);
|
int count = AddOnManyToMany.removeLink(TypeManyToManyRoot.class, retrieve.id, "remote", insertedRemote1.id);
|
||||||
Assertions.assertEquals(1, count);
|
Assertions.assertEquals(1, count);
|
||||||
|
@ -21,6 +21,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import test.kar.archidata.model.TypeManyToOneRemote;
|
import test.kar.archidata.model.TypeManyToOneRemote;
|
||||||
import test.kar.archidata.model.TypeManyToOneRoot;
|
import test.kar.archidata.model.TypeManyToOneRoot;
|
||||||
|
import test.kar.archidata.model.TypeManyToOneRootExpand;
|
||||||
|
|
||||||
@ExtendWith(StepwiseExtension.class)
|
@ExtendWith(StepwiseExtension.class)
|
||||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
@ -79,7 +80,42 @@ public class TestManyToOne {
|
|||||||
Assertions.assertNotNull(insertedData.id);
|
Assertions.assertNotNull(insertedData.id);
|
||||||
Assertions.assertTrue(insertedData.id >= 0);
|
Assertions.assertTrue(insertedData.id >= 0);
|
||||||
Assertions.assertEquals(test.otherData, insertedData.otherData);
|
Assertions.assertEquals(test.otherData, insertedData.otherData);
|
||||||
Assertions.assertEquals(insertedData.remoteId, insertedRemote2.id);
|
Assertions.assertEquals(insertedRemote2.id, insertedData.remoteId);
|
||||||
|
|
||||||
|
TypeManyToOneRoot retrieve = DataAccess.get(TypeManyToOneRoot.class, insertedData.id);
|
||||||
|
Assertions.assertNotNull(retrieve);
|
||||||
|
Assertions.assertNotNull(retrieve.id);
|
||||||
|
Assertions.assertEquals(insertedData.id, retrieve.id);
|
||||||
|
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
|
||||||
|
Assertions.assertEquals(insertedRemote2.id, retrieve.remoteId);
|
||||||
|
|
||||||
|
TypeManyToOneRootExpand retrieve2 = DataAccess.get(TypeManyToOneRootExpand.class, insertedData.id);
|
||||||
|
Assertions.assertNotNull(retrieve2);
|
||||||
|
Assertions.assertNotNull(retrieve2.id);
|
||||||
|
Assertions.assertEquals(insertedData.id, retrieve2.id);
|
||||||
|
Assertions.assertEquals(insertedData.otherData, retrieve2.otherData);
|
||||||
|
Assertions.assertNotNull(retrieve2.remote);
|
||||||
|
Assertions.assertEquals(insertedRemote2.id, retrieve2.remote.id);
|
||||||
|
Assertions.assertEquals(insertedRemote2.data, retrieve2.remote.data);
|
||||||
|
|
||||||
|
// remove values:
|
||||||
|
final int count = DataAccess.delete(TypeManyToOneRemote.class, remote.id);
|
||||||
|
Assertions.assertEquals(1, count);
|
||||||
|
|
||||||
|
// check fail:
|
||||||
|
|
||||||
|
retrieve = DataAccess.get(TypeManyToOneRoot.class, insertedData.id);
|
||||||
|
Assertions.assertNotNull(retrieve);
|
||||||
|
Assertions.assertNotNull(retrieve.id);
|
||||||
|
Assertions.assertEquals(insertedData.id, retrieve.id);
|
||||||
|
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
|
||||||
|
Assertions.assertEquals(insertedRemote2.id, retrieve.remoteId);
|
||||||
|
|
||||||
|
retrieve2 = DataAccess.get(TypeManyToOneRootExpand.class, insertedData.id);
|
||||||
|
Assertions.assertNotNull(retrieve2);
|
||||||
|
Assertions.assertNotNull(retrieve2.id);
|
||||||
|
Assertions.assertEquals(insertedData.id, retrieve2.id);
|
||||||
|
Assertions.assertEquals(insertedData.otherData, retrieve2.otherData);
|
||||||
|
Assertions.assertNull(retrieve2.remote);
|
||||||
}
|
}
|
||||||
}
|
}
|
11
test/src/test/kar/archidata/model/SerializeAsJson.java
Normal file
11
test/src/test/kar/archidata/model/SerializeAsJson.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package test.kar.archidata.model;
|
||||||
|
|
||||||
|
import org.kar.archidata.annotation.DataJson;
|
||||||
|
import org.kar.archidata.model.GenericData;
|
||||||
|
|
||||||
|
public class SerializeAsJson extends GenericData {
|
||||||
|
|
||||||
|
@DataJson
|
||||||
|
public SimpleTable data;
|
||||||
|
|
||||||
|
}
|
@ -8,13 +8,17 @@ import jakarta.persistence.GeneratedValue;
|
|||||||
import jakarta.persistence.GenerationType;
|
import jakarta.persistence.GenerationType;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import jakarta.persistence.ManyToMany;
|
import jakarta.persistence.ManyToMany;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Table(name = "TypeManyToManyRoot")
|
||||||
public class TypeManyToManyRootExpand {
|
public class TypeManyToManyRootExpand {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@Column(nullable = false, unique = true)
|
@Column(nullable = false, unique = true)
|
||||||
public Long id = null;
|
public Long id = null;
|
||||||
|
|
||||||
|
public String otherData;
|
||||||
|
|
||||||
@ManyToMany(fetch = FetchType.LAZY, targetEntity = TypeManyToManyRemote.class)
|
@ManyToMany(fetch = FetchType.LAZY, targetEntity = TypeManyToManyRemote.class)
|
||||||
public List<TypeManyToManyRemote> remote;
|
public List<TypeManyToManyRemote> remote;
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,9 @@ import jakarta.persistence.GeneratedValue;
|
|||||||
import jakarta.persistence.GenerationType;
|
import jakarta.persistence.GenerationType;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import jakarta.persistence.ManyToOne;
|
import jakarta.persistence.ManyToOne;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Table(name = "TypeManyToOneRoot")
|
||||||
public class TypeManyToOneRootExpand {
|
public class TypeManyToOneRootExpand {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user