aboutsummaryrefslogtreecommitdiffstats
path: root/src/site/tools.mkd
blob: 1e38f4bf3d5b8896b3b8a53e6ecbf75a6879e5db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
## 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.

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

### Parameters
<table class="table">
<tr><th>-url</th><td>JDBC url for the database</td><td><em>REQUIRED</em></td></tr>
<tr><th>-username</th><td>username for JDBC connection</td><td><em>optional</em></td></tr>
<tr><th>-password</th><td>password for JDBC connection</td><td><em>optional</em></td></tr>
<tr><th>-schema</th><td>the target schema for model generation</td><td><em>default:</em> all schemas</td></tr>
<tr><th>-table</th><td>the target table for model generation</td><td><em>default:</em> all tables</td></tr>
<tr><th>-package</th><td>the destination package name for generated models</td><td><em>default:</em> default package</td></tr>
<tr><th>-folder</th><td>the output folder for .java files</td><td><em>default:</em> current folder</td></tr>
<tr><th>-annotateSchema</th><td>include the schema name in the class annotations</td><td><em>default:</em> true</td></tr>
<tr><th>-trimStrings</th><td>annotate trimStrings=true for any VARCHAR string mappings &nbsp; </td><td><em>default:</em> false</td></tr>
</table>

## 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:
---JAVA---
public static enum Level {
	CONSIDER, WARN, ERROR;
}
---JAVA---

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
---JAVA---
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);
	}
}
---JAVA---