From 69f2302c957a737564988aff2d9ae7b66287fe9d Mon Sep 17 00:00:00 2001 From: James Moger Date: Tue, 30 Aug 2011 19:26:27 -0400 Subject: Undeprecated interface configuration. Added more Define methods. I've revised my thinking about interface configuration. I think its a good option and should be supported. Field scope is now ignored across the board. If the developer does not want to map a field and is using the interface configuration approach, then the field should be annotated with IQIgnore. --- src/com/iciql/Constants.java | 6 +-- src/com/iciql/Define.java | 54 ++++++++++++++++------ src/com/iciql/Iciql.java | 13 ++++-- src/com/iciql/TableDefinition.java | 94 +++++++++++++++++++++++++++----------- 4 files changed, 120 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/com/iciql/Constants.java b/src/com/iciql/Constants.java index b8d38dd..53dfc56 100644 --- a/src/com/iciql/Constants.java +++ b/src/com/iciql/Constants.java @@ -25,14 +25,14 @@ public class Constants { // The build script extracts this exact line so be careful editing it // and only use A-Z a-z 0-9 .-_ in the string. - public static final String VERSION = "0.7.0"; + public static final String VERSION = "0.7.1-SNAPSHOT"; // The build script extracts this exact line so be careful editing it // and only use A-Z a-z 0-9 .-_ in the string. - public static final String VERSION_DATE = "2011-08-17"; + public static final String VERSION_DATE = "PENDING"; // The build script extracts this exact line so be careful editing it // and only use A-Z a-z 0-9 .-_ in the string. - public static final String API_CURRENT = "6"; + public static final String API_CURRENT = "7"; } diff --git a/src/com/iciql/Define.java b/src/com/iciql/Define.java index 4216ea1..3b58231 100644 --- a/src/com/iciql/Define.java +++ b/src/com/iciql/Define.java @@ -29,29 +29,39 @@ public class Define { private static TableDefinition currentTableDefinition; private static Iciql currentTable; - public static void primaryKey(Object... columns) { + public static void skipCreate() { checkInDefine(); - currentTableDefinition.definePrimaryKey(columns); + currentTableDefinition.defineSkipCreate(); } - public static void index(Object... columns) { + public static void index(IndexType type, Object... columns) { checkInDefine(); - currentTableDefinition.defineIndex(IndexType.STANDARD, columns); + currentTableDefinition.defineIndex(null, type, columns); } - public static void uniqueIndex(Object... columns) { + public static void index(String name, IndexType type, Object... columns) { checkInDefine(); - currentTableDefinition.defineIndex(IndexType.UNIQUE, columns); + currentTableDefinition.defineIndex(name, type, columns); } - public static void hashIndex(Object column) { + public static void primaryKey(Object... columns) { + checkInDefine(); + currentTableDefinition.definePrimaryKey(columns); + } + + public static void schemaName(String schemaName) { checkInDefine(); - currentTableDefinition.defineIndex(IndexType.HASH, new Object[] { column }); + currentTableDefinition.defineSchemaName(schemaName); + } + + public static void tableName(String tableName) { + checkInDefine(); + currentTableDefinition.defineTableName(tableName); } - public static void uniqueHashIndex(Object column) { + public static void memoryTable() { checkInDefine(); - currentTableDefinition.defineIndex(IndexType.UNIQUE_HASH, new Object[] { column }); + currentTableDefinition.defineMemoryTable(); } public static void columnName(Object column, String columnName) { @@ -59,6 +69,11 @@ public class Define { currentTableDefinition.defineColumnName(column, columnName); } + public static void autoIncrement(Object column) { + checkInDefine(); + currentTableDefinition.defineAutoIncrement(column); + } + public static void length(Object column, int length) { checkInDefine(); currentTableDefinition.defineLength(column, length); @@ -68,13 +83,22 @@ public class Define { checkInDefine(); currentTableDefinition.defineScale(column, scale); } - - public static void tableName(String tableName) { + + public static void trim(Object column) { checkInDefine(); - currentTableDefinition.defineTableName(tableName); + currentTableDefinition.defineTrim(column); } - - @SuppressWarnings("deprecation") + + public static void nullable(Object column, boolean isNullable) { + checkInDefine(); + currentTableDefinition.defineNullable(column, isNullable); + } + + public static void defaultValue(Object column, String defaultValue) { + checkInDefine(); + currentTableDefinition.defineDefaultValue(column, defaultValue); + } + static synchronized void define(TableDefinition tableDefinition, Iciql table) { currentTableDefinition = tableDefinition; currentTable = table; diff --git a/src/com/iciql/Iciql.java b/src/com/iciql/Iciql.java index 9ca8bf7..eaa256a 100644 --- a/src/com/iciql/Iciql.java +++ b/src/com/iciql/Iciql.java @@ -215,7 +215,7 @@ public interface Iciql { public @interface IQVersion { /** - * If set to a non-zero value, iciql maintains a "_iq_versions" table + * If set to a non-zero value, iciql maintains a "iq_versions" table * within your database. The version number is used to call to a * registered DbUpgrader implementation to perform relevant ALTER * statements. Default: 0. You must specify a DbUpgrader on your Db @@ -441,7 +441,7 @@ public interface Iciql { */ String defaultValue() default ""; - } + } /** * Interface for using the EnumType.ENUMID enumeration mapping strategy. @@ -499,10 +499,17 @@ public interface Iciql { EnumType value() default EnumType.NAME; } + /** + * Annotation to define an ignored field. + */ + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface IQIgnore{ + } + /** * This method is called to let the table define the primary key, indexes, * and the table name. */ - @Deprecated void defineIQ(); } diff --git a/src/com/iciql/TableDefinition.java b/src/com/iciql/TableDefinition.java index 9b3666c..05d312c 100644 --- a/src/com/iciql/TableDefinition.java +++ b/src/com/iciql/TableDefinition.java @@ -18,7 +18,6 @@ package com.iciql; import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -31,14 +30,15 @@ import com.iciql.Iciql.EnumId; import com.iciql.Iciql.EnumType; import com.iciql.Iciql.IQColumn; import com.iciql.Iciql.IQEnum; +import com.iciql.Iciql.IQIgnore; import com.iciql.Iciql.IQIndex; import com.iciql.Iciql.IQIndexes; import com.iciql.Iciql.IQSchema; import com.iciql.Iciql.IQTable; import com.iciql.Iciql.IQVersion; import com.iciql.Iciql.IndexType; -import com.iciql.util.StatementBuilder; import com.iciql.util.IciqlLogger; +import com.iciql.util.StatementBuilder; import com.iciql.util.StringUtils; import com.iciql.util.Utils; @@ -157,6 +157,14 @@ public class TableDefinition { this.tableName = tableName; } + void defineMemoryTable() { + this.memoryTable = true; + } + + void defineSkipCreate() { + this.createIfRequired = false; + } + /** * Define a primary key by the specified model fields. * @@ -198,14 +206,16 @@ public class TableDefinition { /** * Defines an index with the specified model fields. * + * @param name + * the index name (optional) * @param type * the index type (STANDARD, HASH, UNIQUE, UNIQUE_HASH) * @param modelFields * the ordered list of model fields */ - void defineIndex(IndexType type, Object[] modelFields) { + void defineIndex(String name, IndexType type, Object[] modelFields) { List columnNames = mapColumnNames(modelFields); - addIndex(null, type, columnNames); + addIndex(name, type, columnNames); } /** @@ -235,6 +245,13 @@ public class TableDefinition { } } + void defineAutoIncrement(Object column) { + FieldDefinition def = fieldMap.get(column); + if (def != null) { + def.isAutoIncrement = true; + } + } + void defineLength(Object column, int length) { FieldDefinition def = fieldMap.get(column); if (def != null) { @@ -249,6 +266,27 @@ public class TableDefinition { } } + void defineTrim(Object column) { + FieldDefinition def = fieldMap.get(column); + if (def != null) { + def.trim = true; + } + } + + void defineNullable(Object column, boolean isNullable) { + FieldDefinition def = fieldMap.get(column); + if (def != null) { + def.nullable = isNullable; + } + } + + void defineDefaultValue(Object column, String defaultValue) { + FieldDefinition def = fieldMap.get(column); + if (def != null) { + def.defaultValue = defaultValue; + } + } + void mapFields() { boolean byAnnotationsOnly = false; boolean inheritColumns = false; @@ -267,6 +305,11 @@ public class TableDefinition { T defaultObject = Db.instance(clazz); for (Field f : classFields) { + // check if we should skip this field + if (f.isAnnotationPresent(IQIgnore.class)) { + continue; + } + // default to field name String columnName = f.getName(); boolean isAutoIncrement = false; @@ -292,6 +335,25 @@ public class TableDefinition { } } + // try using default object + try { + f.setAccessible(true); + Object value = f.get(defaultObject); + if (value != null) { + if (value.getClass().isEnum()) { + // enum default, convert to target type + Enum anEnum = (Enum) value; + Object o = Utils.convertEnum(anEnum, enumType); + defaultValue = ModelUtils.formatDefaultValue(o); + } else { + // object default + defaultValue = ModelUtils.formatDefaultValue(value); + } + } + } catch (IllegalAccessException e) { + throw new IciqlException(e, "failed to get default object for {0}", columnName); + } + boolean hasAnnotation = f.isAnnotationPresent(IQColumn.class); if (hasAnnotation) { IQColumn col = f.getAnnotation(IQColumn.class); @@ -305,33 +367,13 @@ public class TableDefinition { trim = col.trim(); nullable = col.nullable(); - // try using default object - try { - f.setAccessible(true); - Object value = f.get(defaultObject); - if (value != null) { - if (value.getClass().isEnum()) { - // enum default, convert to target type - Enum anEnum = (Enum) value; - Object o = Utils.convertEnum(anEnum, enumType); - defaultValue = ModelUtils.formatDefaultValue(o); - } else { - // object default - defaultValue = ModelUtils.formatDefaultValue(value); - } - } - } catch (IllegalAccessException e) { - throw new IciqlException(e, "failed to get default object for {0}", columnName); - } - // annotation overrides if (!StringUtils.isNullOrEmpty(col.defaultValue())) { defaultValue = col.defaultValue(); } } - boolean isPublic = Modifier.isPublic(f.getModifiers()); - boolean reflectiveMatch = isPublic && !byAnnotationsOnly; + boolean reflectiveMatch = !byAnnotationsOnly; if (reflectiveMatch || hasAnnotation) { FieldDefinition fieldDef = new FieldDefinition(); fieldDef.isPrimitive = f.getType().isPrimitive(); @@ -402,7 +444,7 @@ public class TableDefinition { } return value; } - + // return the value unchanged return value; } -- cgit v1.2.3