[DEV] work on uuid and callbak migration
This commit is contained in:
parent
56609e4f59
commit
41fb181545
@ -22,6 +22,7 @@ import jakarta.validation.constraints.Min;
|
|||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import jakarta.validation.constraints.Pattern;
|
import jakarta.validation.constraints.Pattern;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
|
import jakarta.ws.rs.DefaultValue;
|
||||||
|
|
||||||
public class AnnotationTools {
|
public class AnnotationTools {
|
||||||
static final Logger LOGGER = LoggerFactory.getLogger(AnnotationTools.class);
|
static final Logger LOGGER = LoggerFactory.getLogger(AnnotationTools.class);
|
||||||
@ -75,14 +76,14 @@ public class AnnotationTools {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String getDefault(final Field element) throws Exception {
|
public static String getDefault(final Field element) throws Exception {
|
||||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DataDefault.class);
|
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DefaultValue.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 @DataDefault on " + element.getClass().getCanonicalName());
|
throw new Exception("Must not have more than 1 element @DataDefault on " + element.getClass().getCanonicalName());
|
||||||
}
|
}
|
||||||
return ((DataDefault) annotation[0]).value();
|
return ((DefaultValue) annotation[0]).value();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ManyToOne getManyToOne(final Field element) throws Exception {
|
public static ManyToOne getManyToOne(final Field element) throws Exception {
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
package org.kar.archidata.annotation;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
@Target({ ElementType.TYPE, ElementType.FIELD })
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
public @interface DataDefault {
|
|
||||||
|
|
||||||
String value();
|
|
||||||
|
|
||||||
}
|
|
@ -10,6 +10,7 @@ import java.sql.SQLException;
|
|||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.sql.Types;
|
import java.sql.Types;
|
||||||
|
import java.time.Instant;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -19,7 +20,6 @@ import java.util.UUID;
|
|||||||
|
|
||||||
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.DataDefault;
|
|
||||||
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.AddOnDataJson;
|
||||||
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
|
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
|
||||||
@ -35,12 +35,14 @@ import org.kar.archidata.db.DBEntry;
|
|||||||
import org.kar.archidata.exception.DataAccessException;
|
import org.kar.archidata.exception.DataAccessException;
|
||||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||||
import org.kar.archidata.tools.DateTools;
|
import org.kar.archidata.tools.DateTools;
|
||||||
|
import org.kar.archidata.tools.UuidUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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.ws.rs.DefaultValue;
|
||||||
import jakarta.ws.rs.InternalServerErrorException;
|
import jakarta.ws.rs.InternalServerErrorException;
|
||||||
|
|
||||||
/* TODO list:
|
/* TODO list:
|
||||||
@ -197,7 +199,8 @@ public class DataAccess {
|
|||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
ps.setNull(iii.value, Types.BINARY);
|
ps.setNull(iii.value, Types.BINARY);
|
||||||
} else {
|
} else {
|
||||||
ps.setObject(iii.value, tmp);
|
final byte[] dataByte = UuidUtils.asBytes((UUID) tmp);
|
||||||
|
ps.setBytes(iii.value, dataByte);
|
||||||
}
|
}
|
||||||
} else if (type == Long.class) {
|
} else if (type == Long.class) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
@ -259,6 +262,14 @@ public class DataAccess {
|
|||||||
final Timestamp sqlDate = java.sql.Timestamp.from(((Date) tmp).toInstant());
|
final Timestamp sqlDate = java.sql.Timestamp.from(((Date) tmp).toInstant());
|
||||||
ps.setTimestamp(iii.value, sqlDate);
|
ps.setTimestamp(iii.value, sqlDate);
|
||||||
}
|
}
|
||||||
|
} else if (type == Instant.class) {
|
||||||
|
final Object tmp = field.get(data);
|
||||||
|
if (tmp == null) {
|
||||||
|
ps.setNull(iii.value, Types.INTEGER);
|
||||||
|
} else {
|
||||||
|
final String sqlDate = ((Instant) tmp).toString();
|
||||||
|
ps.setString(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) {
|
||||||
@ -297,11 +308,14 @@ public class DataAccess {
|
|||||||
|
|
||||||
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 {
|
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 == UUID.class) {
|
if (type == UUID.class) {
|
||||||
final UUID tmp = rs.getObject(count.value, UUID.class);
|
final byte[] tmp = rs.getBytes(count.value);
|
||||||
|
//final UUID tmp = rs.getObject(count.value, UUID.class);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(data, null);
|
field.set(data, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(data, tmp);
|
//field.set(data, tmp);
|
||||||
|
final UUID uuid = UuidUtils.asUuid(tmp);
|
||||||
|
field.set(data, uuid);
|
||||||
countNotNull.inc();
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
} else if (type == Long.class) {
|
} else if (type == Long.class) {
|
||||||
@ -413,6 +427,14 @@ public class DataAccess {
|
|||||||
countNotNull.inc();
|
countNotNull.inc();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (type == Instant.class) {
|
||||||
|
final String tmp = rs.getString(count.value);
|
||||||
|
if (rs.wasNull()) {
|
||||||
|
field.set(data, null);
|
||||||
|
} else {
|
||||||
|
field.set(data, Instant.parse(tmp));
|
||||||
|
countNotNull.inc();
|
||||||
|
}
|
||||||
} else if (type == LocalDate.class) {
|
} else if (type == LocalDate.class) {
|
||||||
final java.sql.Date tmp = rs.getDate(count.value);
|
final java.sql.Date tmp = rs.getDate(count.value);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
@ -467,11 +489,15 @@ public class DataAccess {
|
|||||||
final Class<?> type = field.getType();
|
final Class<?> type = field.getType();
|
||||||
if (type == UUID.class) {
|
if (type == UUID.class) {
|
||||||
return (final ResultSet rs, final Object obj) -> {
|
return (final ResultSet rs, final Object obj) -> {
|
||||||
final UUID tmp = rs.getObject(count, UUID.class);
|
|
||||||
|
final byte[] tmp = rs.getBytes(count);
|
||||||
|
//final UUID tmp = rs.getObject(count, UUID.class);
|
||||||
if (rs.wasNull()) {
|
if (rs.wasNull()) {
|
||||||
field.set(obj, null);
|
field.set(obj, null);
|
||||||
} else {
|
} else {
|
||||||
field.set(obj, tmp);
|
//field.set(obj, tmp);
|
||||||
|
final UUID uuid = UuidUtils.asUuid(tmp);
|
||||||
|
field.set(obj, uuid);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -607,6 +633,16 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if (type == Instant.class) {
|
||||||
|
return (final ResultSet rs, final Object obj) -> {
|
||||||
|
final String tmp = rs.getString(count);
|
||||||
|
if (rs.wasNull()) {
|
||||||
|
field.set(obj, null);
|
||||||
|
} else {
|
||||||
|
field.set(obj, Instant.parse(tmp));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
if (type == LocalDate.class) {
|
if (type == LocalDate.class) {
|
||||||
return (final ResultSet rs, final Object obj) -> {
|
return (final ResultSet rs, final Object obj) -> {
|
||||||
final java.sql.Date tmp = rs.getDate(count);
|
final java.sql.Date tmp = rs.getDate(count);
|
||||||
@ -735,7 +771,7 @@ 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(DataDefault.class).length != 0) {
|
if (tmp == null && field.getDeclaredAnnotationsByType(DefaultValue.class).length != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -799,7 +835,7 @@ public class DataAccess {
|
|||||||
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(DataDefault.class).length != 0) {
|
if (tmp == null && elem.getDeclaredAnnotationsByType(DefaultValue.class).length != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -816,7 +852,9 @@ public class DataAccess {
|
|||||||
try (ResultSet generatedKeys = ps.getGeneratedKeys()) {
|
try (ResultSet generatedKeys = ps.getGeneratedKeys()) {
|
||||||
if (generatedKeys.next()) {
|
if (generatedKeys.next()) {
|
||||||
if (primaryKeyField.getType() == UUID.class) {
|
if (primaryKeyField.getType() == UUID.class) {
|
||||||
uniqueSQLUUID = generatedKeys.getObject(1, UUID.class);
|
//uniqueSQLUUID = generatedKeys.getObject(1, UUID.class);
|
||||||
|
final byte[] tmpid = generatedKeys.getBytes(1);
|
||||||
|
uniqueSQLUUID = UuidUtils.asUuid(tmpid);
|
||||||
} else {
|
} else {
|
||||||
uniqueSQLID = generatedKeys.getLong(1);
|
uniqueSQLID = generatedKeys.getLong(1);
|
||||||
}
|
}
|
||||||
@ -933,13 +971,21 @@ public class DataAccess {
|
|||||||
* @param filterValue
|
* @param filterValue
|
||||||
* @return the affected rows.
|
* @return the affected rows.
|
||||||
* @throws Exception */
|
* @throws Exception */
|
||||||
public static <T, ID_TYPE> int update(final T data, final ID_TYPE id, final List<String> updateColomn) throws Exception {
|
public static <T, ID_TYPE> int update(final T data, final ID_TYPE id, final List<String> updateColomn, final QueryOption... option) throws Exception {
|
||||||
return updateWhere(data, new Condition(getTableIdCondition(data.getClass(), id)), new FilterValue(updateColomn), new TransmitKey(id));
|
final QueryOptions options = new QueryOptions(option);
|
||||||
|
options.add(new Condition(getTableIdCondition(data.getClass(), id)));
|
||||||
|
options.add(new FilterValue(updateColomn));
|
||||||
|
options.add(new TransmitKey(id));
|
||||||
|
return updateWhere(data, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> int updateWhere(final T data, final QueryOption... option) throws Exception {
|
public static <T> int updateWhere(final T data, final QueryOption... option) throws Exception {
|
||||||
final Class<?> clazz = data.getClass();
|
|
||||||
final QueryOptions options = new QueryOptions(option);
|
final QueryOptions options = new QueryOptions(option);
|
||||||
|
return updateWhere(data, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> int updateWhere(final T data, final QueryOptions options) throws Exception {
|
||||||
|
final Class<?> clazz = data.getClass();
|
||||||
final Condition condition = options.get(Condition.class);
|
final Condition condition = options.get(Condition.class);
|
||||||
if (condition == null) {
|
if (condition == null) {
|
||||||
throw new DataAccessException("request a gets without any condition");
|
throw new DataAccessException("request a gets without any condition");
|
||||||
@ -991,7 +1037,7 @@ 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(DataDefault.class).length != 0) {
|
if (tmp == null && field.getDeclaredAnnotationsByType(DefaultValue.class).length != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1038,7 +1084,7 @@ 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(DataDefault.class).length != 0) {
|
if (tmp == null && field.getDeclaredAnnotationsByType(DefaultValue.class).length != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import java.sql.ResultSetMetaData;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
import java.time.Instant;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -165,6 +166,17 @@ public class DataExport {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if (type == Instant.class) {
|
||||||
|
return (final ResultSet rs, final Object obj) -> {
|
||||||
|
final String tmp = rs.getString(count);
|
||||||
|
if (!rs.wasNull()) {
|
||||||
|
final Instant date = Instant.parse(tmp);
|
||||||
|
LOGGER.error("Fail to parse the SQL time !!! {}", date);
|
||||||
|
final List<Object> data = (List<Object>) (obj);
|
||||||
|
data.set(id, date);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
if (type == LocalDate.class) {
|
if (type == LocalDate.class) {
|
||||||
return (final ResultSet rs, final Object obj) -> {
|
return (final ResultSet rs, final Object obj) -> {
|
||||||
final java.sql.Date tmp = rs.getDate(count);
|
final java.sql.Date tmp = rs.getDate(count);
|
||||||
|
@ -2,6 +2,7 @@ package org.kar.archidata.dataAccess;
|
|||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
import java.time.Instant;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -46,6 +47,9 @@ public class DataFactory {
|
|||||||
if (type == Double.class || type == double.class) {
|
if (type == Double.class || type == double.class) {
|
||||||
return "double";
|
return "double";
|
||||||
}
|
}
|
||||||
|
if (type == Instant.class) {
|
||||||
|
return "varchar(33)";
|
||||||
|
}
|
||||||
if (type == Date.class || type == Timestamp.class) {
|
if (type == Date.class || type == Timestamp.class) {
|
||||||
return "timestamp(3)";
|
return "timestamp(3)";
|
||||||
}
|
}
|
||||||
@ -97,6 +101,9 @@ public class DataFactory {
|
|||||||
if (type == Double.class || type == double.class) {
|
if (type == Double.class || type == double.class) {
|
||||||
return "REAL";
|
return "REAL";
|
||||||
}
|
}
|
||||||
|
if (type == Instant.class) {
|
||||||
|
return "text";
|
||||||
|
}
|
||||||
if (type == Date.class || type == Timestamp.class) {
|
if (type == Date.class || type == Timestamp.class) {
|
||||||
return "DATETIME";
|
return "DATETIME";
|
||||||
}
|
}
|
||||||
@ -179,16 +186,24 @@ public class DataFactory {
|
|||||||
}
|
}
|
||||||
if (defaultValue == null) {
|
if (defaultValue == null) {
|
||||||
if (updateTime || createTime) {
|
if (updateTime || createTime) {
|
||||||
mainTableBuilder.append("DEFAULT CURRENT_TIMESTAMP");
|
if ("varchar(33)".equals(typeValue)) {
|
||||||
if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
mainTableBuilder.append("DEFAULT DATE_FORMAT(now(6), '%Y-%m-%dT%H:%m:%s.%fZ')");
|
||||||
mainTableBuilder.append("(3)");
|
} else {
|
||||||
|
mainTableBuilder.append("DEFAULT CURRENT_TIMESTAMP");
|
||||||
|
if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
||||||
|
mainTableBuilder.append("(3)");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
mainTableBuilder.append(" ");
|
mainTableBuilder.append(" ");
|
||||||
}
|
}
|
||||||
if (updateTime) {
|
if (updateTime) {
|
||||||
if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
||||||
mainTableBuilder.append("ON UPDATE CURRENT_TIMESTAMP");
|
if ("varchar(33)".equals(typeValue)) {
|
||||||
mainTableBuilder.append("(3)");
|
mainTableBuilder.append("ON UPDATE DATE_FORMAT(now(6), '%Y-%m-%dT%H:%m:%s.%fZ')");
|
||||||
|
} else {
|
||||||
|
mainTableBuilder.append("ON UPDATE CURRENT_TIMESTAMP");
|
||||||
|
mainTableBuilder.append("(3)");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO: add trigger:
|
// TODO: add trigger:
|
||||||
/* CREATE TRIGGER your_table_trig AFTER UPDATE ON your_table BEGIN update your_table SET updated_on = datetime('now') WHERE user_id = NEW.user_id; END; */
|
/* CREATE TRIGGER your_table_trig AFTER UPDATE ON your_table BEGIN update your_table SET updated_on = datetime('now') WHERE user_id = NEW.user_id; END; */
|
||||||
@ -202,7 +217,11 @@ public class DataFactory {
|
|||||||
triggerBuilder.append(" SET ");
|
triggerBuilder.append(" SET ");
|
||||||
triggerBuilder.append(name);
|
triggerBuilder.append(name);
|
||||||
// triggerBuilder.append(" = datetime('now') WHERE id = NEW.id; \n");
|
// triggerBuilder.append(" = datetime('now') WHERE id = NEW.id; \n");
|
||||||
triggerBuilder.append(" = strftime('%Y-%m-%d %H:%M:%f', 'now') WHERE id = NEW.id; \n");
|
if ("varchar(33)".equals(typeValue)) {
|
||||||
|
triggerBuilder.append(" = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') WHERE id = NEW.id; \n");
|
||||||
|
} else {
|
||||||
|
triggerBuilder.append(" = strftime('%Y-%m-%d %H:%M:%f', 'now') WHERE id = NEW.id; \n");
|
||||||
|
}
|
||||||
triggerBuilder.append("END;");
|
triggerBuilder.append("END;");
|
||||||
|
|
||||||
postOtherTables.add(triggerBuilder.toString());
|
postOtherTables.add(triggerBuilder.toString());
|
||||||
@ -248,8 +267,11 @@ public class DataFactory {
|
|||||||
mainTableBuilder.append("PRIMARY KEY ");
|
mainTableBuilder.append("PRIMARY KEY ");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strategy == GenerationType.IDENTITY) {
|
if (strategy == GenerationType.IDENTITY) {
|
||||||
if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
if ("binary(16)".equals(typeValue)) {
|
||||||
|
|
||||||
|
} else if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
||||||
mainTableBuilder.append("AUTO_INCREMENT ");
|
mainTableBuilder.append("AUTO_INCREMENT ");
|
||||||
} else {
|
} else {
|
||||||
mainTableBuilder.append("AUTOINCREMENT ");
|
mainTableBuilder.append("AUTOINCREMENT ");
|
||||||
|
5
src/org/kar/archidata/migration/AsyncCall.java
Normal file
5
src/org/kar/archidata/migration/AsyncCall.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package org.kar.archidata.migration;
|
||||||
|
|
||||||
|
public interface AsyncCall {
|
||||||
|
void doRequest() throws Exception;
|
||||||
|
}
|
@ -13,13 +13,20 @@ import org.kar.archidata.tools.ConfigBaseVariable;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
record Action(String action, List<String> filterDB) {
|
record Action(String action, AsyncCall async, List<String> filterDB) {
|
||||||
public Action(final String action) {
|
public Action(final String action) {
|
||||||
this(action, List.of());
|
this(action, null, List.of());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Action(final String action, final String filterDB) {
|
public Action(final String action, final String filterDB) {
|
||||||
this(action, List.of(filterDB));
|
this(action, null, List.of(filterDB));
|
||||||
|
}
|
||||||
|
public Action(final AsyncCall async) {
|
||||||
|
this(null, async, List.of());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Action(final AsyncCall async, final String filterDB) {
|
||||||
|
this(null, async, List.of(filterDB));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +47,11 @@ public class MigrationSqlStep implements MigrationInterface {
|
|||||||
}
|
}
|
||||||
for (int iii = 0; iii < this.actions.size(); iii++) {
|
for (int iii = 0; iii < this.actions.size(); iii++) {
|
||||||
final Action action = this.actions.get(iii);
|
final Action action = this.actions.get(iii);
|
||||||
LOGGER.info(" >>>> SQL ACTION : {}/{} ==> filter='{}'\n{}", iii, this.actions.size(), action.filterDB(), action.action());
|
if (action.action() != null) {
|
||||||
|
LOGGER.info(" >>>> SQL ACTION : {}/{} ==> filter='{}'\n{}", iii, this.actions.size(), action.filterDB(), action.action());
|
||||||
|
} else {
|
||||||
|
LOGGER.info(" >>>> SQL ACTION : {}/{} ==> filter='{}'\nAsync lambda", iii, this.actions.size(), action.filterDB());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,8 +74,13 @@ public class MigrationSqlStep implements MigrationInterface {
|
|||||||
LOGGER.info(" >>>> SQL ACTION : {}/{}", iii + 1, this.actions.size());
|
LOGGER.info(" >>>> SQL ACTION : {}/{}", iii + 1, this.actions.size());
|
||||||
final Action action = this.actions.get(iii);
|
final Action action = this.actions.get(iii);
|
||||||
|
|
||||||
LOGGER.info("SQL request: ```{}``` on '{}' current={}", action.action(), action.filterDB(), ConfigBaseVariable.getDBType());
|
if (action.action() != null) {
|
||||||
log.append("SQL: " + action.action() + " on " + action.filterDB() + "\n");
|
LOGGER.info("SQL request: ```{}``` on '{}' current={}", action.action(), action.filterDB(), ConfigBaseVariable.getDBType());
|
||||||
|
log.append("SQL: " + action.action() + " on " + action.filterDB() + "\n");
|
||||||
|
} else {
|
||||||
|
LOGGER.info("SQL request: <Lambda> on '{}' current={}", action.filterDB(), ConfigBaseVariable.getDBType());
|
||||||
|
log.append("SQL: <Lambda> on " + action.filterDB() + "\n");
|
||||||
|
}
|
||||||
boolean isValid = true;
|
boolean isValid = true;
|
||||||
if (action.filterDB() != null && action.filterDB().size() > 0) {
|
if (action.filterDB() != null && action.filterDB().size() > 0) {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
@ -80,7 +96,11 @@ public class MigrationSqlStep implements MigrationInterface {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
DataAccess.executeQuerry(action.action());
|
if (action.action() != null) {
|
||||||
|
DataAccess.executeQuerry(action.action());
|
||||||
|
} else {
|
||||||
|
action.async().doRequest();
|
||||||
|
}
|
||||||
} catch (SQLException | IOException ex) {
|
} catch (SQLException | IOException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
LOGGER.info("SQL request ERROR: ", ex.getMessage());
|
LOGGER.info("SQL request ERROR: ", ex.getMessage());
|
||||||
@ -122,10 +142,16 @@ public class MigrationSqlStep implements MigrationInterface {
|
|||||||
public void addAction(final String action) {
|
public void addAction(final String action) {
|
||||||
this.actions.add(new Action(action));
|
this.actions.add(new Action(action));
|
||||||
}
|
}
|
||||||
|
public void addAction(final AsyncCall async) {
|
||||||
|
this.actions.add(new Action(async));
|
||||||
|
}
|
||||||
|
|
||||||
public void addAction(final String action, final String filterdBType) {
|
public void addAction(final String action, final String filterdBType) {
|
||||||
this.actions.add(new Action(action, filterdBType));
|
this.actions.add(new Action(action, filterdBType));
|
||||||
}
|
}
|
||||||
|
public void addAction(final AsyncCall async, final String filterdBType) {
|
||||||
|
this.actions.add(new Action(async, filterdBType));
|
||||||
|
}
|
||||||
|
|
||||||
public void addClass(final Class<?> clazz) throws Exception {
|
public void addClass(final Class<?> clazz) throws Exception {
|
||||||
final List<String> tmp = DataFactory.createTable(clazz);
|
final List<String> tmp = DataFactory.createTable(clazz);
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.kar.archidata.migration.model;
|
package org.kar.archidata.migration.model;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.DataDefault;
|
|
||||||
import org.kar.archidata.annotation.DataIfNotExists;
|
import org.kar.archidata.annotation.DataIfNotExists;
|
||||||
import org.kar.archidata.annotation.DataNotRead;
|
import org.kar.archidata.annotation.DataNotRead;
|
||||||
import org.kar.archidata.model.GenericDataSoftDelete;
|
import org.kar.archidata.model.GenericDataSoftDelete;
|
||||||
@ -10,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
|
|||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
|
import jakarta.ws.rs.DefaultValue;
|
||||||
|
|
||||||
// For logs only
|
// For logs only
|
||||||
//public static final String TABLE_NAME = "KAR_migration";
|
//public static final String TABLE_NAME = "KAR_migration";
|
||||||
@ -24,11 +24,11 @@ public class Migration extends GenericDataSoftDelete {
|
|||||||
@Column(length = 256)
|
@Column(length = 256)
|
||||||
public String name;
|
public String name;
|
||||||
@DataNotRead
|
@DataNotRead
|
||||||
@DataDefault("'2'")
|
@DefaultValue("'2'")
|
||||||
@Schema(description = "Version of the migration engine")
|
@Schema(description = "Version of the migration engine")
|
||||||
public Integer version;
|
public Integer version;
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@DataDefault("'0'")
|
@DefaultValue("'0'")
|
||||||
@Schema(description = "if the migration is well terminated or not")
|
@Schema(description = "if the migration is well terminated or not")
|
||||||
public Boolean terminated = false;
|
public Boolean terminated = false;
|
||||||
@Schema(description = "index in the migration progression")
|
@Schema(description = "index in the migration progression")
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.kar.archidata.migration.model;
|
package org.kar.archidata.migration.model;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.DataDefault;
|
|
||||||
import org.kar.archidata.annotation.DataIfNotExists;
|
import org.kar.archidata.annotation.DataIfNotExists;
|
||||||
import org.kar.archidata.model.GenericDataSoftDelete;
|
import org.kar.archidata.model.GenericDataSoftDelete;
|
||||||
|
|
||||||
@ -9,6 +8,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
|
|||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
|
import jakarta.ws.rs.DefaultValue;
|
||||||
|
|
||||||
// For logs only
|
// For logs only
|
||||||
//public static final String TABLE_NAME = "KAR_migration";
|
//public static final String TABLE_NAME = "KAR_migration";
|
||||||
@ -22,7 +22,7 @@ public class Migration1 extends GenericDataSoftDelete {
|
|||||||
@Column(length = 256)
|
@Column(length = 256)
|
||||||
public String name;
|
public String name;
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@DataDefault("'0'")
|
@DefaultValue("'0'")
|
||||||
@Schema(description = "if the migration is well terminated or not")
|
@Schema(description = "if the migration is well terminated or not")
|
||||||
public Boolean terminated = false;
|
public Boolean terminated = false;
|
||||||
@Schema(description = "index in the migration progression")
|
@Schema(description = "index in the migration progression")
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
package org.kar.archidata.model;
|
package org.kar.archidata.model;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.DataDefault;
|
|
||||||
import org.kar.archidata.annotation.DataDeleted;
|
import org.kar.archidata.annotation.DataDeleted;
|
||||||
import org.kar.archidata.annotation.DataNotRead;
|
import org.kar.archidata.annotation.DataNotRead;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import jakarta.ws.rs.DefaultValue;
|
||||||
|
|
||||||
public class GenericDataSoftDelete extends GenericData {
|
public class GenericDataSoftDelete extends GenericData {
|
||||||
@DataNotRead
|
@DataNotRead
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@DataDefault("'0'")
|
@DefaultValue("'0'")
|
||||||
@DataDeleted
|
@DataDeleted
|
||||||
@NotNull
|
@NotNull
|
||||||
@Schema(description = "Deleted state", hidden = true, required = false, readOnly = true)
|
@Schema(description = "Deleted state", hidden = true, required = false, readOnly = true)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.kar.archidata.model;
|
package org.kar.archidata.model;
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.CreationTimestamp;
|
import org.kar.archidata.annotation.CreationTimestamp;
|
||||||
@ -27,8 +26,10 @@ public class GenericTiming {
|
|||||||
@DataNotRead
|
@DataNotRead
|
||||||
@UpdateTimestamp
|
@UpdateTimestamp
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
@NotNull
|
@NotNull
|
||||||
@Schema(description = "When update the object", required = false, example = "2000-01-23T00:23:45.678Z", readOnly = true)
|
@Schema(description = "When update the object", required = false, example = "2000-01-23T00:23:45.678Z", readOnly = true)
|
||||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
|
||||||
public Instant updatedAt = null;
|
//public Instant updatedAt = null;
|
||||||
|
public Date updatedAt = null;
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import jakarta.ws.rs.DefaultValue;
|
|||||||
|
|
||||||
public class UUIDGenericData extends GenericTiming {
|
public class UUIDGenericData extends GenericTiming {
|
||||||
@Id
|
@Id
|
||||||
@DefaultValue("UUID_TO_BIN(UUID())")
|
@DefaultValue("(UUID_TO_BIN(UUID(), TRUE))")
|
||||||
@Column(nullable = false, unique = true)
|
@Column(nullable = false, unique = true)
|
||||||
@Schema(description = "Unique UUID of the object", required = false, readOnly = true, example = "e6b33c1c-d24d-11ee-b616-02420a030102")
|
@Schema(description = "Unique UUID of the object", required = false, readOnly = true, example = "e6b33c1c-d24d-11ee-b616-02420a030102")
|
||||||
public UUID id = null;
|
public UUID id = null;
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
package org.kar.archidata.model;
|
package org.kar.archidata.model;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.DataDefault;
|
|
||||||
import org.kar.archidata.annotation.DataDeleted;
|
import org.kar.archidata.annotation.DataDeleted;
|
||||||
import org.kar.archidata.annotation.DataNotRead;
|
import org.kar.archidata.annotation.DataNotRead;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import jakarta.ws.rs.DefaultValue;
|
||||||
|
|
||||||
public class UUIDGenericDataSoftDelete extends UUIDGenericData {
|
public class UUIDGenericDataSoftDelete extends UUIDGenericData {
|
||||||
@DataNotRead
|
@DataNotRead
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@DataDefault("'0'")
|
@DefaultValue("'0'")
|
||||||
@DataDeleted
|
@DataDeleted
|
||||||
@NotNull
|
@NotNull
|
||||||
@Schema(description = "Deleted state", hidden = true, required = false, readOnly = true)
|
@Schema(description = "Deleted state", hidden = true, required = false, readOnly = true)
|
||||||
|
@ -17,7 +17,6 @@ CREATE TABLE `user` (
|
|||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.DataDefault;
|
|
||||||
import org.kar.archidata.annotation.DataIfNotExists;
|
import org.kar.archidata.annotation.DataIfNotExists;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
@ -26,6 +25,7 @@ import jakarta.persistence.Column;
|
|||||||
import jakarta.persistence.FetchType;
|
import jakarta.persistence.FetchType;
|
||||||
import jakarta.persistence.ManyToMany;
|
import jakarta.persistence.ManyToMany;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
|
import jakarta.ws.rs.DefaultValue;
|
||||||
|
|
||||||
@Table(name = "user")
|
@Table(name = "user")
|
||||||
@DataIfNotExists
|
@DataIfNotExists
|
||||||
@ -35,13 +35,13 @@ public class User extends GenericDataSoftDelete {
|
|||||||
public String login = null;
|
public String login = null;
|
||||||
|
|
||||||
public Timestamp lastConnection = null;
|
public Timestamp lastConnection = null;
|
||||||
@DataDefault("'0'")
|
@DefaultValue("'0'")
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
public boolean admin = false;
|
public boolean admin = false;
|
||||||
@DataDefault("'0'")
|
@DefaultValue("'0'")
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
public boolean blocked = false;
|
public boolean blocked = false;
|
||||||
@DataDefault("'0'")
|
@DefaultValue("'0'")
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
public boolean removed = false;
|
public boolean removed = false;
|
||||||
|
|
||||||
|
21
src/org/kar/archidata/tools/UuidUtils.java
Normal file
21
src/org/kar/archidata/tools/UuidUtils.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package org.kar.archidata.tools;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class UuidUtils {
|
||||||
|
|
||||||
|
public static UUID asUuid(final byte[] bytes) {
|
||||||
|
final ByteBuffer bb = ByteBuffer.wrap(bytes);
|
||||||
|
final long firstLong = bb.getLong();
|
||||||
|
final long secondLong = bb.getLong();
|
||||||
|
return new UUID(firstLong, secondLong);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] asBytes(final UUID uuid) {
|
||||||
|
final ByteBuffer bb = ByteBuffer.allocate(16);
|
||||||
|
bb.putLong(uuid.getMostSignificantBits());
|
||||||
|
bb.putLong(uuid.getLeastSignificantBits());
|
||||||
|
return bb.array();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user