Search in sources :

Example 1 with DbInspector

use of org.h2.jaqu.DbInspector in project h2database by h2database.

the class ModelsTest method testModelGeneration.

private void testModelGeneration() {
    DbInspector inspector = new DbInspector(db);
    List<String> models = inspector.generateModel(null, "SupportedTypes", "org.h2.test.jaqu", true, true);
    assertEquals(1, models.size());
    // a poor test, but a start
    assertEquals(1364, models.get(0).length());
}
Also used : DbInspector(org.h2.jaqu.DbInspector)

Example 2 with DbInspector

use of org.h2.jaqu.DbInspector in project h2database by h2database.

the class GenerateModels method execute.

/**
 * Generates models from the database.
 *
 * @param url the database URL
 * @param user the user name
 * @param password the password
 * @param schema the schema to read from. null for all schemas.
 * @param table the table to model. null for all tables within schema.
 * @param packageName the package name of the model classes.
 * @param folder destination folder for model classes (package path not
 *            included)
 * @param annotateSchema includes the schema in the table model annotations
 * @param trimStrings automatically trim strings that exceed maxLength
 */
public static void execute(String url, String user, String password, String schema, String table, String packageName, String folder, boolean annotateSchema, boolean trimStrings) throws SQLException {
    Connection conn = null;
    try {
        org.h2.Driver.load();
        conn = DriverManager.getConnection(url, user, password);
        Db db = Db.open(url, user, password.toCharArray());
        DbInspector inspector = new DbInspector(db);
        List<String> models = inspector.generateModel(schema, table, packageName, annotateSchema, trimStrings);
        File parentFile;
        if (StringUtils.isNullOrEmpty(folder)) {
            parentFile = new File(System.getProperty("user.dir"));
        } else {
            parentFile = new File(folder);
        }
        parentFile.mkdirs();
        Pattern p = Pattern.compile("class ([a-zA-Z0-9]+)");
        for (String model : models) {
            Matcher m = p.matcher(model);
            if (m.find()) {
                String className = m.group().substring("class".length()).trim();
                File classFile = new File(parentFile, className + ".java");
                Writer o = new FileWriter(classFile, false);
                PrintWriter writer = new PrintWriter(new BufferedWriter(o));
                writer.write(model);
                writer.close();
                System.out.println("Generated " + classFile.getAbsolutePath());
            }
        }
    } catch (IOException io) {
        throw DbException.convertIOException(io, "could not generate model").getSQLException();
    } finally {
        JdbcUtils.closeSilently(conn);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileWriter(java.io.FileWriter) Connection(java.sql.Connection) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) DbInspector(org.h2.jaqu.DbInspector) File(java.io.File) Db(org.h2.jaqu.Db) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 3 with DbInspector

use of org.h2.jaqu.DbInspector in project h2database by h2database.

the class ModelsTest method testValidateModels.

private void testValidateModels() {
    DbInspector inspector = new DbInspector(db);
    validateModel(inspector, new Product());
    validateModel(inspector, new ProductAnnotationOnly());
    validateModel(inspector, new ProductMixedAnnotation());
}
Also used : DbInspector(org.h2.jaqu.DbInspector)

Example 4 with DbInspector

use of org.h2.jaqu.DbInspector in project h2database by h2database.

the class ModelsTest method validateModel.

private void validateModel(DbInspector inspector, Object o) {
    List<ValidationRemark> remarks = inspector.validateModel(o, false);
    if (config.traceTest && remarks.size() > 0) {
        trace("Validation remarks for " + o.getClass().getName());
        for (ValidationRemark remark : remarks) {
            trace(remark.toString());
        }
        trace("");
    }
    for (ValidationRemark remark : remarks) {
        assertFalse(remark.toString(), remark.isError());
    }
}
Also used : ValidationRemark(org.h2.jaqu.ValidationRemark)

Aggregations

DbInspector (org.h2.jaqu.DbInspector)3 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 Connection (java.sql.Connection)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Db (org.h2.jaqu.Db)1 ValidationRemark (org.h2.jaqu.ValidationRemark)1