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());
}
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);
}
}
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());
}
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());
}
}
Aggregations