use of core.framework.db.Table in project core-ng-project by neowu.
the class EntitySchemaGenerator method schemeStatements.
private List<String> schemeStatements() {
List<String> statements = Lists.newArrayList();
CodeBuilder builder = new CodeBuilder().append("CREATE TABLE ");
Table table = entityClass.getDeclaredAnnotation(Table.class);
builder.append(table.name()).append(" (");
List<String> primaryKeys = Lists.newArrayList();
for (Field field : Classes.instanceFields(entityClass)) {
Column column = field.getDeclaredAnnotation(Column.class);
PrimaryKey primaryKey = field.getDeclaredAnnotation(PrimaryKey.class);
builder.append(column.name()).append(' ');
builder.append(columnType(field.getType(), field.getDeclaredAnnotation(Length.class)));
if (primaryKey != null) {
if (primaryKey.autoIncrement())
builder.append(" AUTO_INCREMENT");
if (!Strings.isEmpty(primaryKey.sequence())) {
statements.add("CREATE SEQUENCE IF NOT EXISTS " + primaryKey.sequence());
}
primaryKeys.add(column.name());
}
if (field.isAnnotationPresent(NotNull.class)) {
builder.append(" NOT NULL");
}
builder.append(", ");
}
builder.append("PRIMARY KEY(").appendCommaSeparatedValues(primaryKeys).append("))");
statements.add(builder.build());
return statements;
}
use of core.framework.db.Table in project core-ng-project by neowu.
the class DeleteQueryBuilder method build.
static String build(Class<?> entityClass) {
Table table = entityClass.getDeclaredAnnotation(Table.class);
StringBuilder builder = new StringBuilder("DELETE FROM ").append(table.name()).append(" WHERE ");
int index = 0;
for (Field field : Classes.instanceFields(entityClass)) {
if (field.isAnnotationPresent(PrimaryKey.class)) {
Column column = field.getDeclaredAnnotation(Column.class);
if (index > 0)
builder.append(" AND ");
builder.append(column.name()).append(" = ?");
index++;
}
}
return builder.toString();
}
Aggregations