private String generateIndexAnnotation(IndexInspector index) { AnnotationBuilder ap = new AnnotationBuilder(); ap.append('@').append(IQIndex.class.getSimpleName()); ap.append('('); ap.resetCount(); if (!StringUtils.isNullOrEmpty(index.name)) { ap.addParameter("name", index.name); } if (!index.type.equals(IndexType.STANDARD)) { ap.addEnum("type", index.type); } if (ap.getCount() > 0) { // multiple fields specified ap.addParameter("value", index.columns); } else { // default value ap.addParameter(null, index.columns); } ap.append(')'); return ap.toString(); }
/** * Generates the specified index annotation. * * @param ap */ String generateIndexAnnotations() { if (indexes == null || indexes.size() == 0) { // no matching indexes return null; } AnnotationBuilder ap = new AnnotationBuilder(); if (indexes.size() == 1) { // single index IndexInspector index = indexes.values().toArray(new IndexInspector[1])[0]; ap.append(generateIndexAnnotation(index)); ap.append(eol); } else { // multiple indexes ap.append('@').append(IQIndexes.class.getSimpleName()); ap.append("({"); ap.resetCount(); for (IndexInspector index : indexes.values()) { ap.appendExceptFirst(", "); ap.append(generateIndexAnnotation(index)); } ap.append("})").append(eol); } return ap.toString(); }
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 { // Imports // don't import primitives, java.lang classes, or byte [] if (clazz.getPackage() == null) { } else if (clazz.getPackage().getName().equals("java.lang")) { } else if (clazz.equals(byte[].class)) { } else { imports.add(clazz.getCanonicalName()); } // @IQColumn sb.append('@').append(IQColumn.class.getSimpleName()); // IQColumn annotation parameters AnnotationBuilder ap = new AnnotationBuilder(); // IQColumn.name if (!col.name.equalsIgnoreCase(column)) { ap.addParameter("name", col.name); } // IQColumn.primaryKey // composite primary keys are annotated on the table if (col.isPrimaryKey && primaryKeys.size() == 1) { ap.addParameter("primaryKey=true"); } // IQColumn.length if ((clazz == String.class) && (col.size > 0) && (col.size < Integer.MAX_VALUE)) { ap.addParameter("length", col.size); // IQColumn.trim if (trimStrings) { ap.addParameter("trim=true"); } } else { // IQColumn.AutoIncrement if (col.isAutoIncrement) { ap.addParameter("autoIncrement=true"); } } // IQColumn.nullable if (!col.nullable) { ap.addParameter("nullable=false"); } // IQColumn.defaultValue if (!isNullOrEmpty(col.defaultValue)) { ap.addParameter("defaultValue=\"" + col.defaultValue + "\""); } // add leading and trailing () if (ap.length() > 0) { ap.insert(0, '('); ap.append(')'); } sb.append(ap); } 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; }