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; }
/** * Generates a model (class definition) from this table. The model includes indexes, primary keys, * default values, lengths, and nullables. information. * * <p>The caller may optionally set a destination package name, whether or not to include the * schema name (setting schema can be a problem when using the model between databases), and if to * automatically trim strings for those that have a maximum length. * * <p> * * @param packageName * @param annotateSchema * @param trimStrings * @return a complete model (class definition) for this table as a string */ String generateModel(String packageName, boolean annotateSchema, boolean trimStrings) { // import statements Set<String> imports = Utils.newHashSet(); imports.add(Serializable.class.getCanonicalName()); imports.add(IQSchema.class.getCanonicalName()); imports.add(IQTable.class.getCanonicalName()); imports.add(IQIndexes.class.getCanonicalName()); imports.add(IQIndex.class.getCanonicalName()); imports.add(IQColumn.class.getCanonicalName()); imports.add(IndexType.class.getCanonicalName()); // fields StringBuilder fields = new StringBuilder(); List<ColumnInspector> sortedColumns = Utils.newArrayList(columns.values()); Collections.sort(sortedColumns); for (ColumnInspector col : sortedColumns) { fields.append(generateColumn(imports, col, trimStrings)); } // build complete class definition StringBuilder model = new StringBuilder(); if (!isNullOrEmpty(packageName)) { // package model.append("package " + packageName + ";"); model.append(eol).append(eol); } // imports List<String> sortedImports = new ArrayList<String>(imports); Collections.sort(sortedImports); for (String imp : sortedImports) { model.append("import ").append(imp).append(';').append(eol); } model.append(eol); // @IQSchema if (annotateSchema && !isNullOrEmpty(schema)) { model.append('@').append(IQSchema.class.getSimpleName()); model.append('('); AnnotationBuilder ap = new AnnotationBuilder(); ap.addParameter(null, schema); model.append(ap); model.append(')').append(eol); } // @IQTable model.append('@').append(IQTable.class.getSimpleName()); model.append('('); // IQTable annotation parameters AnnotationBuilder ap = new AnnotationBuilder(); ap.addParameter("name", table); if (primaryKeys.size() > 1) { ap.addParameter("primaryKey", primaryKeys); } // finish @IQTable annotation model.append(ap); model.append(')').append(eol); // @IQIndexes // @IQIndex String indexAnnotations = generateIndexAnnotations(); if (!StringUtils.isNullOrEmpty(indexAnnotations)) { model.append(indexAnnotations); } // class declaration String clazzName = ModelUtils.convertTableToClassName(table); model.append(format("public class {0} implements Serializable '{'", clazzName)).append(eol); model.append(eol); model.append("\tprivate static final long serialVersionUID = 1L;").append(eol); model.append(eol); // field declarations model.append(fields); // default constructor model.append("\t" + "public ").append(clazzName).append("() {").append(eol); model.append("\t}").append(eol); // end of class body model.append('}'); model.trimToSize(); return model.toString(); }