Search in sources :

Example 1 with Table

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;
}
Also used : Field(java.lang.reflect.Field) Table(core.framework.db.Table) Column(core.framework.db.Column) PrimaryKey(core.framework.db.PrimaryKey) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 2 with Table

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();
}
Also used : Field(java.lang.reflect.Field) Table(core.framework.db.Table) Column(core.framework.db.Column)

Aggregations

Column (core.framework.db.Column)2 Table (core.framework.db.Table)2 Field (java.lang.reflect.Field)2 PrimaryKey (core.framework.db.PrimaryKey)1 CodeBuilder (core.framework.impl.asm.CodeBuilder)1