You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Iciql.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql;
  18. import java.lang.annotation.ElementType;
  19. import java.lang.annotation.Retention;
  20. import java.lang.annotation.RetentionPolicy;
  21. import java.lang.annotation.Target;
  22. /**
  23. * A class that implements this interface can be used as a database table.
  24. * <p>
  25. * You may implement the Table interface on your model object and optionally use
  26. * IQColumn annotations (which imposes a compile-time and runtime-dependency on
  27. * iciql), or may choose to use the IQTable and IQColumn annotations only (which
  28. * imposes a compile-time and runtime-dependency on this file only).
  29. * <p>
  30. * If a class is annotated with IQTable and at the same time implements Table,
  31. * the define() method is not called.
  32. * <p>
  33. * Supported data types:
  34. * <table>
  35. * <tr>
  36. * <td>java.lang.String</td>
  37. * <td>VARCHAR (maxLength > 0) or TEXT (maxLength == 0)</td>
  38. * </tr>
  39. * <tr>
  40. * <td>java.lang.Boolean</td>
  41. * <td>BIT</td>
  42. * </tr>
  43. * <tr>
  44. * <td>java.lang.Byte</td>
  45. * <td>TINYINT</td>
  46. * </tr>
  47. * <tr>
  48. * <td>java.lang.Short</td>
  49. * <td>SMALLINT</td>
  50. * </tr>
  51. * <tr>
  52. * <td>java.lang.Integer</td>
  53. * <td>INT</td>
  54. * </tr>
  55. * <tr>
  56. * <td>java.lang.Long</td>
  57. * <td>BIGINT</td>
  58. * </tr>
  59. * <tr>
  60. * <td>java.lang.Float</td>
  61. * <td>REAL</td>
  62. * </tr>
  63. * <tr>
  64. * <td>java.lang.Double</td>
  65. * <td>DOUBLE</td>
  66. * </tr>
  67. * <tr>
  68. * <td>java.math.BigDecimal</td>
  69. * <td>DECIMAL</td>
  70. * </tr>
  71. * <tr>
  72. * <td>java.sql.Date</td>
  73. * <td>DATE</td>
  74. * </tr>
  75. * <tr>
  76. * <td>java.sql.Time</td>
  77. * <td>TIME</td>
  78. * </tr>
  79. * <tr>
  80. * <td>java.sql.Timestamp</td>
  81. * <td>TIMESTAMP</td>
  82. * </tr>
  83. * <tr>
  84. * <td>java.util.Date</td>
  85. * <td>TIMESTAMP</td>
  86. * </tr>
  87. * </table>
  88. * <p>
  89. * Unsupported data types: binary types (BLOB, etc), and custom types.
  90. * <p>
  91. * Table and field mapping: by default, the mapped table name is the class name
  92. * and the public fields are reflectively mapped, by their name, to columns. As
  93. * an alternative, you may specify both the table and column definition by
  94. * annotations.
  95. * <p>
  96. * Table Interface: you may set additional parameters such as table name,
  97. * primary key, and indexes in the define() method.
  98. * <p>
  99. * Annotations: you may use the annotations with or without implementing the
  100. * Table interface. The annotations allow you to decouple your model completely
  101. * from iciql other than this file.
  102. * <p>
  103. * Automatic model generation: you may automatically generate model classes as
  104. * strings with the Db and DbInspector objects:
  105. *
  106. * <pre>
  107. * Db db = Db.open(&quot;jdbc:h2:mem:&quot;, &quot;sa&quot;, &quot;sa&quot;);
  108. * DbInspector inspector = new DbInspector(db);
  109. * List&lt;String&gt; models =
  110. * inspector.generateModel(schema, table, packageName,
  111. * annotateSchema, trimStrings)
  112. * </pre>
  113. *
  114. * Or you may use the GenerateModels tool to generate and save your classes to
  115. * the file system:
  116. *
  117. * <pre>
  118. * java -jar iciql.jar
  119. * -url &quot;jdbc:h2:mem:&quot;
  120. * -user sa -password sa -schema schemaName -table tableName
  121. * -package packageName -folder destination
  122. * -annotateSchema false -trimStrings true
  123. * </pre>
  124. *
  125. * Model validation: you may validate your model class with DbInspector object.
  126. * The DbInspector will report errors, warnings, and suggestions:
  127. *
  128. * <pre>
  129. * Db db = Db.open(&quot;jdbc:h2:mem:&quot;, &quot;sa&quot;, &quot;sa&quot;);
  130. * DbInspector inspector = new DbInspector(db);
  131. * List&lt;Validation&gt; remarks = inspector.validateModel(new MyModel(), throwOnError);
  132. * for (Validation remark : remarks) {
  133. * System.out.println(remark);
  134. * }
  135. * </pre>
  136. */
  137. public interface Iciql {
  138. /**
  139. * An annotation for a database.
  140. */
  141. @Retention(RetentionPolicy.RUNTIME)
  142. @Target(ElementType.TYPE)
  143. public @interface IQDatabase {
  144. /**
  145. * If set to a non-zero value, iciql maintains a "_iq_versions" table
  146. * within your database. The version number is used to call to a
  147. * registered DbUpgrader implementation to perform relevant ALTER
  148. * statements. Default: 0. You must specify a DbUpgrader on your Db
  149. * object to use this parameter.
  150. */
  151. int version() default 0;
  152. }
  153. /**
  154. * An annotation for a schema.
  155. */
  156. @Retention(RetentionPolicy.RUNTIME)
  157. @Target(ElementType.TYPE)
  158. public @interface IQSchema {
  159. /**
  160. * The schema may be optionally specified. Default: unspecified.
  161. */
  162. String name() default "";
  163. }
  164. /**
  165. * Enumeration defining the four index types.
  166. */
  167. public static enum IndexType {
  168. STANDARD, UNIQUE, HASH, UNIQUE_HASH;
  169. }
  170. /**
  171. * An index annotation.
  172. */
  173. @Retention(RetentionPolicy.RUNTIME)
  174. @Target(ElementType.TYPE)
  175. public @interface IQIndex {
  176. /**
  177. * Standard indexes may be optionally specified.
  178. * <ul>
  179. * <li>standard = "id, name"</li>
  180. * <li>standard = "id name"</li>
  181. * <li>standard = { "id name", "date" }</li>
  182. * </ul>
  183. * Standard indexes may still be added in the define() method if the
  184. * model class is not annotated with IQTable. Default: unspecified.
  185. */
  186. String[] standard() default {};
  187. /**
  188. * Unique indexes may be optionally specified.
  189. * <ul>
  190. * <li>unique = "id, name"</li>
  191. * <li>unique = "id name"</li>
  192. * <li>unique = { "id name", "date" }</li>
  193. * </ul>
  194. * Unique indexes may still be added in the define() method if the model
  195. * class is not annotated with IQTable. Default: unspecified.
  196. */
  197. String[] unique() default {};
  198. /**
  199. * Hash indexes may be optionally specified.
  200. * <ul>
  201. * <li>hash = "name"
  202. * <li>hash = { "name", "date" }
  203. * </ul>
  204. * Hash indexes may still be added in the define() method if the model
  205. * class is not annotated with IQTable. Default: unspecified.
  206. */
  207. String[] hash() default {};
  208. /**
  209. * Unique hash indexes may be optionally specified.
  210. * <ul>
  211. * <li>uniqueHash = "id"
  212. * <li>uniqueHash = "name"
  213. * <li>uniqueHash = { "id", "name" }
  214. * </ul>
  215. * Unique hash indexes may still be added in the define() method if the
  216. * model class is not annotated with IQTable. Default: unspecified.
  217. */
  218. String[] uniqueHash() default {};
  219. }
  220. /**
  221. * Annotation to define a table.
  222. */
  223. @Retention(RetentionPolicy.RUNTIME)
  224. @Target(ElementType.TYPE)
  225. public @interface IQTable {
  226. /**
  227. * The table name. If not specified the class name is used as the table
  228. * name.
  229. * <p>
  230. * The table name may still be overridden in the define() method if the
  231. * model class is not annotated with IQTable. Default: unspecified.
  232. */
  233. String name() default "";
  234. /**
  235. * The primary key may be optionally specified. If it is not specified,
  236. * then no primary key is set by the IQTable annotation. You may specify
  237. * a composite primary key.
  238. * <ul>
  239. * <li>primaryKey = "id, name"
  240. * <li>primaryKey = "id name"
  241. * </ul>
  242. * The primary key may still be overridden in the define() method if the
  243. * model class is not annotated with IQTable. Default: unspecified.
  244. */
  245. String primaryKey() default "";
  246. /**
  247. * The inherit columns allows this model class to inherit columns from
  248. * its super class. Any IQTable annotation present on the super class is
  249. * ignored. Default: false.
  250. */
  251. boolean inheritColumns() default false;
  252. /**
  253. * Whether or not iciql tries to create the table and indexes. Default:
  254. * true.
  255. */
  256. boolean createIfRequired() default true;
  257. /**
  258. * Whether only supported types are mapped. If true, unsupported mapped
  259. * types will throw an IciqlException. If false, unsupported mapped
  260. * types will default to VARCHAR. Default: true.
  261. */
  262. boolean strictTypeMapping() default true;
  263. /**
  264. * If true, only fields that are explicitly annotated as IQColumn are
  265. * mapped. Default: true.
  266. */
  267. boolean annotationsOnly() default true;
  268. /**
  269. * If true, this table is created as a memory table where data is
  270. * persistent, but index data is kept in main memory. Valid only for H2
  271. * databases. Default: false.
  272. */
  273. boolean memoryTable() default false;
  274. /**
  275. * If non-zero, iciql will maintain a "_iq_versions" table within your
  276. * database. The version number is used to call to a registered
  277. * DbUpgrader implementation to perform relevant ALTER statements.
  278. * Default: 0. You must specify a DbUpgrader on your Db object to use
  279. * this parameter.
  280. */
  281. int version() default 0;
  282. }
  283. /**
  284. * Annotation to define a column. Annotated fields may have any scope
  285. * (however, the JVM may raise a SecurityException if the SecurityManager
  286. * doesn't allow iciql to access the field.)
  287. */
  288. @Retention(RetentionPolicy.RUNTIME)
  289. @Target(ElementType.FIELD)
  290. public @interface IQColumn {
  291. /**
  292. * If not specified, the field name is used as the column name. Default:
  293. * the field name.
  294. */
  295. String name() default "";
  296. /**
  297. * This column is the primary key. Default: false.
  298. */
  299. boolean primaryKey() default false;
  300. /**
  301. * The column is created with a sequence as the default value. Default:
  302. * false.
  303. */
  304. boolean autoIncrement() default false;
  305. /**
  306. * If larger than zero, it is used during the CREATE TABLE phase. It may
  307. * also be used to prevent database exceptions on INSERT and UPDATE
  308. * statements (see trimString).
  309. * <p>
  310. * Any maxLength set in define() may override this annotation setting if
  311. * the model class is not annotated with IQTable. Default: 0.
  312. */
  313. int maxLength() default 0;
  314. /**
  315. * If true, iciql will automatically trim the string if it exceeds
  316. * maxLength (value.substring(0, maxLength)). Default: false.
  317. */
  318. boolean trimString() default false;
  319. /**
  320. * If false, iciql will set the column NOT NULL during the CREATE TABLE
  321. * phase. Default: false.
  322. */
  323. boolean allowNull() default false;
  324. /**
  325. * The default value assigned to the column during the CREATE TABLE
  326. * phase. This field could contain a literal single-quoted value, or a
  327. * function call. Empty strings are considered NULL. Examples:
  328. * <ul>
  329. * <li>defaultValue="" (null)
  330. * <li>defaultValue="CURRENT_TIMESTAMP"
  331. * <li>defaultValue="''" (empty string)
  332. * <li>defaultValue="'0'"
  333. * <li>defaultValue="'1970-01-01 00:00:01'"
  334. * </ul>
  335. * if the default value is specified, and auto increment is disabled,
  336. * and primary key is disabled, then this value is included in the
  337. * "DEFAULT ..." phrase of a column during the CREATE TABLE process.
  338. * Default: unspecified (null).
  339. */
  340. String defaultValue() default "";
  341. }
  342. /**
  343. * This method is called to let the table define the primary key, indexes,
  344. * and the table name.
  345. */
  346. @Deprecated
  347. void defineIQ();
  348. }