Skip to content
James Moger edited this page Apr 9, 2016 · 1 revision

Model Generation

If you do not have or do not want to annotate your existing model classes, you can use the included model generation tool to create iciql model classes.

 win: java -cp iciql.jar;your_db_driver.jar <parameters>
*nix: java -cp iciql.jar:your_db_driver.jar <parameters>

Parameters

-url JDBC url for the database REQUIRED
-username username for JDBC connection optional
-password password for JDBC connection optional
-schema the target schema for model generation default: all schemas
-table the target table for model generation default: all tables
-package the destination package name for generated models default: default package
-folder the output folder for .java files default: current folder
-annotateSchema include the schema name in the class annotations default: true
-trimStrings annotate trimStrings=true for any VARCHAR string mappings   default: false

Model Validation

Iciql can validate your model classes against your database to ensure that your models are optimally defined and are consistent with the current table and index definitions.

Each com.iciql.ValidationRemark returned by the validation has an associated level from the following enum:

public static enum Level {
	CONSIDER, WARN, ERROR;
}

A typical validation may output recommendations for adjusting a model field annotation such as setting the maxLength of a string to match the length of its linked VARCHAR column.

Sample Model Validation using JUnit 4

import static org.junit.Assert.assertTrue;

import java.sql.SQLException;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

import com.iciql.Db;
import com.iciql.DbInspector;
import com.iciql.ValidationRemark;
import com.iciql.test.models.Product;
import com.iciql.test.models.ProductAnnotationOnly;
import com.iciql.test.models.ProductMixedAnnotation;

public class ValidateModels {

	/*
	 * The ErrorCollector Rule allows execution of a test to continue after the
	 * first problem is found and report them all at once
	 */
	@Rule
	public ErrorCollector errorCollector = new ErrorCollector();

	private Db db;

	@Before
	public void setUp() {
		db = Db.open("jdbc:h2:mem:", "sa", "sa");
	}

	@After
	public void tearDown() {
		db.close();
	}

	@Test
	public void testValidateModels() {
		DbInspector inspector = new DbInspector(db);
		validateModel(inspector, new Product());
		validateModel(inspector, new ProductAnnotationOnly());
		validateModel(inspector, new ProductMixedAnnotation());
	}

	private void validateModel(DbInspector inspector, Object o) {
		List<ValidationRemark> remarks = inspector.validateModel(o, false);
		assertTrue("Validation remarks are null for " + o.getClass().getName(), remarks != null);
		log("Validation remarks for " + o.getClass().getName());
		for (ValidationRemark remark : remarks) {
			log(remark.toString());
			if (remark.isError()) {
				errorCollector.addError(new SQLException(remark.toString()));
			}
		}
	}

	private void log(String message) {
		System.out.println(message);
	}
}