Search in sources :

Example 1 with JQColumn

use of org.h2.jaqu.Table.JQColumn in project h2database by h2database.

the class TableDefinition method mapFields.

void mapFields() {
    boolean byAnnotationsOnly = false;
    boolean inheritColumns = false;
    boolean strictTypeMapping = false;
    if (clazz.isAnnotationPresent(JQTable.class)) {
        JQTable tableAnnotation = clazz.getAnnotation(JQTable.class);
        byAnnotationsOnly = tableAnnotation.annotationsOnly();
        inheritColumns = tableAnnotation.inheritColumns();
        strictTypeMapping = tableAnnotation.strictTypeMapping();
    }
    List<Field> classFields = New.arrayList();
    classFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    if (inheritColumns) {
        Class<?> superClass = clazz.getSuperclass();
        classFields.addAll(Arrays.asList(superClass.getDeclaredFields()));
    }
    for (Field f : classFields) {
        // default to field name
        String columnName = f.getName();
        boolean isAutoIncrement = false;
        boolean isPrimaryKey = false;
        int maxLength = 0;
        boolean trimString = false;
        boolean allowNull = true;
        String defaultValue = "";
        boolean hasAnnotation = f.isAnnotationPresent(JQColumn.class);
        if (hasAnnotation) {
            JQColumn col = f.getAnnotation(JQColumn.class);
            if (!StringUtils.isNullOrEmpty(col.name())) {
                columnName = col.name();
            }
            isAutoIncrement = col.autoIncrement();
            isPrimaryKey = col.primaryKey();
            maxLength = col.maxLength();
            trimString = col.trimString();
            allowNull = col.allowNull();
            defaultValue = col.defaultValue();
        }
        boolean isPublic = Modifier.isPublic(f.getModifiers());
        boolean reflectiveMatch = isPublic && !byAnnotationsOnly;
        if (reflectiveMatch || hasAnnotation) {
            FieldDefinition fieldDef = new FieldDefinition();
            fieldDef.field = f;
            fieldDef.columnName = columnName;
            fieldDef.isAutoIncrement = isAutoIncrement;
            fieldDef.isPrimaryKey = isPrimaryKey;
            fieldDef.maxLength = maxLength;
            fieldDef.trimString = trimString;
            fieldDef.allowNull = allowNull;
            fieldDef.defaultValue = defaultValue;
            fieldDef.dataType = ModelUtils.getDataType(fieldDef, strictTypeMapping);
            fields.add(fieldDef);
        }
    }
    List<String> primaryKey = New.arrayList();
    for (FieldDefinition fieldDef : fields) {
        if (fieldDef.isPrimaryKey) {
            primaryKey.add(fieldDef.columnName);
        }
    }
    if (primaryKey.size() > 0) {
        setPrimaryKey(primaryKey);
    }
}
Also used : Field(java.lang.reflect.Field) JQTable(org.h2.jaqu.Table.JQTable) JQColumn(org.h2.jaqu.Table.JQColumn)

Example 2 with JQColumn

use of org.h2.jaqu.Table.JQColumn in project h2database by h2database.

the class TableInspector method generateColumn.

private StatementBuilder generateColumn(Set<String> imports, ColumnInspector col, boolean trimStrings) {
    StatementBuilder sb = new StatementBuilder();
    Class<?> clazz = col.clazz;
    String column = ModelUtils.convertColumnToFieldName(col.name.toLowerCase());
    sb.append('\t');
    if (clazz == null) {
        // unsupported type
        clazz = Object.class;
        sb.append("// unsupported type " + col.type);
    } else {
        // @JQColumn
        imports.add(clazz.getCanonicalName());
        sb.append('@').append(JQColumn.class.getSimpleName());
        // JQColumn annotation parameters
        AnnotationBuilder ap = new AnnotationBuilder();
        // JQColumn.name
        if (!col.name.equalsIgnoreCase(column)) {
            ap.addParameter("name", col.name);
        }
        // composite primary keys are annotated on the table
        if (col.isPrimaryKey && primaryKeys.size() == 1) {
            ap.addParameter("primaryKey=true");
        }
        // JQColumn.maxLength
        if ((clazz == String.class) && (col.size > 0) && (col.size < Integer.MAX_VALUE)) {
            ap.addParameter("maxLength", col.size);
            // JQColumn.trimStrings
            if (trimStrings) {
                ap.addParameter("trimString=true");
            }
        } else {
            // JQColumn.AutoIncrement
            if (col.isAutoIncrement) {
                ap.addParameter("autoIncrement=true");
            }
        }
        // JQColumn.allowNull
        if (!col.allowNull) {
            ap.addParameter("allowNull=false");
        }
        // JQColumn.defaultValue
        if (!StringUtils.isNullOrEmpty(col.defaultValue)) {
            ap.addParameter("defaultValue=\"" + col.defaultValue + "\"");
        }
        // add leading and trailing ()
        if (ap.length() > 0) {
            AnnotationBuilder b = new AnnotationBuilder();
            b.append('(').append(ap.toString()).append(')');
            ap = b;
        }
        sb.append(ap.toString());
    }
    sb.append(EOL);
    // variable declaration
    sb.append("\t" + "public ");
    sb.append(clazz.getSimpleName());
    sb.append(' ');
    sb.append(column);
    sb.append(';');
    sb.append(EOL).append(EOL);
    return sb;
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) JQColumn(org.h2.jaqu.Table.JQColumn)

Aggregations

JQColumn (org.h2.jaqu.Table.JQColumn)2 Field (java.lang.reflect.Field)1 JQTable (org.h2.jaqu.Table.JQTable)1 StatementBuilder (org.h2.util.StatementBuilder)1