]> source.dussan.org Git - iciql.git/commitdiff
Refactoring.
authorJames Moger <james.moger@gmail.com>
Tue, 16 Aug 2011 19:45:29 +0000 (15:45 -0400)
committerJames Moger <james.moger@gmail.com>
Tue, 16 Aug 2011 19:45:29 +0000 (15:45 -0400)
src/com/iciql/Db.java
src/com/iciql/Define.java
src/com/iciql/TableDefinition.java

index 09c26a1e10fc6c83354736cb2225968b7bdc9756..d369c2cdc6bb9962298d3de05ea32543f734aeab 100644 (file)
@@ -181,7 +181,7 @@ public class Db {
 \r
        public <T> void insert(T t) {\r
                Class<?> clazz = t.getClass();\r
-               long rc = define(clazz).createTableIfRequired(this).insert(this, t, false);\r
+               long rc = define(clazz).createIfRequired(this).insert(this, t, false);\r
                if (rc == 0) {\r
                        throw new IciqlException("Failed to insert {0}.  Affected rowcount == 0.", t);\r
                }\r
@@ -189,7 +189,7 @@ public class Db {
 \r
        public <T> long insertAndGetKey(T t) {\r
                Class<?> clazz = t.getClass();\r
-               return define(clazz).createTableIfRequired(this).insert(this, t, true);\r
+               return define(clazz).createIfRequired(this).insert(this, t, true);\r
        }\r
 \r
        /**\r
@@ -213,7 +213,7 @@ public class Db {
         */\r
        public <T> void merge(T t) {\r
                Class<?> clazz = t.getClass();\r
-               TableDefinition<?> def = define(clazz).createTableIfRequired(this);\r
+               TableDefinition<?> def = define(clazz).createIfRequired(this);\r
                int rc = def.merge(this, t);\r
                if (rc == 0) {\r
                        rc = def.update(this, t);\r
@@ -225,17 +225,17 @@ public class Db {
 \r
        public <T> int update(T t) {\r
                Class<?> clazz = t.getClass();\r
-               return define(clazz).createTableIfRequired(this).update(this, t);\r
+               return define(clazz).createIfRequired(this).update(this, t);\r
        }\r
 \r
        public <T> int delete(T t) {\r
                Class<?> clazz = t.getClass();\r
-               return define(clazz).createTableIfRequired(this).delete(this, t);\r
+               return define(clazz).createIfRequired(this).delete(this, t);\r
        }\r
 \r
        public <T extends Object> Query<T> from(T alias) {\r
                Class<?> clazz = alias.getClass();\r
-               define(clazz).createTableIfRequired(this);\r
+               define(clazz).createIfRequired(this);\r
                return Query.from(this, alias);\r
        }\r
 \r
index aa0587216d5898ff19370fbe9b8f061d3b95653e..4216ea17657a7fb5747904b123fafd9a8bead867 100644 (file)
@@ -31,47 +31,47 @@ public class Define {
 \r
        public static void primaryKey(Object... columns) {\r
                checkInDefine();\r
-               currentTableDefinition.setPrimaryKey(columns);\r
+               currentTableDefinition.definePrimaryKey(columns);\r
        }\r
 \r
        public static void index(Object... columns) {\r
                checkInDefine();\r
-               currentTableDefinition.addIndex(IndexType.STANDARD, columns);\r
+               currentTableDefinition.defineIndex(IndexType.STANDARD, columns);\r
        }\r
 \r
        public static void uniqueIndex(Object... columns) {\r
                checkInDefine();\r
-               currentTableDefinition.addIndex(IndexType.UNIQUE, columns);\r
+               currentTableDefinition.defineIndex(IndexType.UNIQUE, columns);\r
        }\r
 \r
        public static void hashIndex(Object column) {\r
                checkInDefine();\r
-               currentTableDefinition.addIndex(IndexType.HASH, new Object[] { column });\r
+               currentTableDefinition.defineIndex(IndexType.HASH, new Object[] { column });\r
        }\r
 \r
        public static void uniqueHashIndex(Object column) {\r
                checkInDefine();\r
-               currentTableDefinition.addIndex(IndexType.UNIQUE_HASH, new Object[] { column });\r
+               currentTableDefinition.defineIndex(IndexType.UNIQUE_HASH, new Object[] { column });\r
        }\r
 \r
        public static void columnName(Object column, String columnName) {\r
                checkInDefine();\r
-               currentTableDefinition.setColumnName(column, columnName);\r
+               currentTableDefinition.defineColumnName(column, columnName);\r
        }\r
 \r
        public static void length(Object column, int length) {\r
                checkInDefine();\r
-               currentTableDefinition.setLength(column, length);\r
+               currentTableDefinition.defineLength(column, length);\r
        }\r
 \r
        public static void scale(Object column, int scale) {\r
                checkInDefine();\r
-               currentTableDefinition.setScale(column, scale);\r
+               currentTableDefinition.defineScale(column, scale);\r
        }\r
 \r
        public static void tableName(String tableName) {\r
                checkInDefine();\r
-               currentTableDefinition.setTableName(tableName);\r
+               currentTableDefinition.defineTableName(tableName);\r
        }\r
 \r
        @SuppressWarnings("deprecation")\r
index 251f0989171d61e866d64f158abf98eede9f12e1..b6084ca34649c3d6a9d5f2975b4b34bab837e42f 100644 (file)
@@ -89,13 +89,13 @@ public class TableDefinition<T> {
                        }\r
                }\r
 \r
-               Object initWithNewObject(Object obj) {\r
+               private Object initWithNewObject(Object obj) {\r
                        Object o = Utils.newObject(field.getType());\r
                        setValue(obj, o);\r
                        return o;\r
                }\r
 \r
-               void setValue(Object obj, Object o) {\r
+               private void setValue(Object obj, Object o) {\r
                        try {\r
                                if (!field.isAccessible()) {\r
                                        field.setAccessible(true);\r
@@ -114,7 +114,7 @@ public class TableDefinition<T> {
                        }\r
                }\r
 \r
-               Object read(ResultSet rs, int columnIndex) {\r
+               private Object read(ResultSet rs, int columnIndex) {\r
                        try {\r
                                return rs.getObject(columnIndex);\r
                        } catch (SQLException e) {\r
@@ -130,7 +130,7 @@ public class TableDefinition<T> {
        List<String> primaryKeyColumnNames;\r
        boolean memoryTable;\r
 \r
-       private boolean createTableIfRequired = true;\r
+       private boolean createIfRequired = true;\r
        private Class<T> clazz;\r
        private IdentityHashMap<Object, FieldDefinition> fieldMap = Utils.newIdentityHashMap();\r
        private ArrayList<IndexDefinition> indexes = Utils.newArrayList();\r
@@ -149,20 +149,11 @@ public class TableDefinition<T> {
                return fields;\r
        }\r
 \r
-       FieldDefinition getField(String name) {\r
-               for (FieldDefinition field : fields) {\r
-                       if (field.columnName.equalsIgnoreCase(name)) {\r
-                               return field;\r
-                       }\r
-               }\r
-               return null;\r
-       }\r
-\r
-       void setSchemaName(String schemaName) {\r
+       void defineSchemaName(String schemaName) {\r
                this.schemaName = schemaName;\r
        }\r
 \r
-       void setTableName(String tableName) {\r
+       void defineTableName(String tableName) {\r
                this.tableName = tableName;\r
        }\r
 \r
@@ -172,7 +163,7 @@ public class TableDefinition<T> {
         * @param modelFields\r
         *            the ordered list of model fields\r
         */\r
-       void setPrimaryKey(Object[] modelFields) {\r
+       void definePrimaryKey(Object[] modelFields) {\r
                List<String> columnNames = mapColumnNames(modelFields);\r
                setPrimaryKey(columnNames);\r
        }\r
@@ -183,7 +174,7 @@ public class TableDefinition<T> {
         * @param columnNames\r
         *            the ordered list of column names\r
         */\r
-       void setPrimaryKey(List<String> columnNames) {\r
+       private void setPrimaryKey(List<String> columnNames) {\r
                primaryKeyColumnNames = Utils.newArrayList(columnNames);\r
                // set isPrimaryKey flag for all field definitions\r
                for (FieldDefinition fieldDefinition : fieldMap.values()) {\r
@@ -191,7 +182,7 @@ public class TableDefinition<T> {
                }\r
        }\r
 \r
-       <A> String getColumnName(A fieldObject) {\r
+       private <A> String getColumnName(A fieldObject) {\r
                FieldDefinition def = fieldMap.get(fieldObject);\r
                return def == null ? null : def.columnName;\r
        }\r
@@ -212,7 +203,7 @@ public class TableDefinition<T> {
         * @param modelFields\r
         *            the ordered list of model fields\r
         */\r
-       void addIndex(IndexType type, Object[] modelFields) {\r
+       void defineIndex(IndexType type, Object[] modelFields) {\r
                List<String> columnNames = mapColumnNames(modelFields);\r
                addIndex(null, type, columnNames);\r
        }\r
@@ -225,7 +216,7 @@ public class TableDefinition<T> {
         * @param columnNames\r
         *            the ordered list of column names\r
         */\r
-       void addIndex(String name, IndexType type, List<String> columnNames) {\r
+       private void addIndex(String name, IndexType type, List<String> columnNames) {\r
                IndexDefinition index = new IndexDefinition();\r
                if (StringUtils.isNullOrEmpty(name)) {\r
                        index.indexName = tableName + "_" + indexes.size();\r
@@ -237,21 +228,21 @@ public class TableDefinition<T> {
                indexes.add(index);\r
        }\r
 \r
-       public void setColumnName(Object column, String columnName) {\r
+       void defineColumnName(Object column, String columnName) {\r
                FieldDefinition def = fieldMap.get(column);\r
                if (def != null) {\r
                        def.columnName = columnName;\r
                }\r
        }\r
 \r
-       public void setLength(Object column, int length) {\r
+       void defineLength(Object column, int length) {\r
                FieldDefinition def = fieldMap.get(column);\r
                if (def != null) {\r
                        def.length = length;\r
                }\r
        }\r
 \r
-       public void setScale(Object column, int scale) {\r
+       void defineScale(Object column, int scale) {\r
                FieldDefinition def = fieldMap.get(column);\r
                if (def != null) {\r
                        def.scale = scale;\r
@@ -411,7 +402,8 @@ public class TableDefinition<T> {
                        }\r
                        return value;\r
                }\r
-               // standard behavior\r
+               \r
+               // return the value unchanged\r
                return value;\r
        }\r
 \r
@@ -547,8 +539,8 @@ public class TableDefinition<T> {
                return stat.executeUpdate();\r
        }\r
 \r
-       TableDefinition<T> createTableIfRequired(Db db) {\r
-               if (!createTableIfRequired) {\r
+       TableDefinition<T> createIfRequired(Db db) {\r
+               if (!createIfRequired) {\r
                        // skip table and index creation\r
                        // but still check for upgrades\r
                        db.upgradeTable(this);\r
@@ -607,7 +599,7 @@ public class TableDefinition<T> {
                        }\r
 \r
                        // allow control over createTableIfRequired()\r
-                       createTableIfRequired = tableAnnotation.create();\r
+                       createIfRequired = tableAnnotation.create();\r
 \r
                        // model version\r
                        if (clazz.isAnnotationPresent(IQVersion.class)) {\r
@@ -640,7 +632,7 @@ public class TableDefinition<T> {
                }\r
        }\r
 \r
-       void addIndex(IQIndex index) {\r
+       private void addIndex(IQIndex index) {\r
                List<String> columns = Arrays.asList(index.value());\r
                addIndex(index.name(), index.type(), columns);\r
        }\r
@@ -649,7 +641,7 @@ public class TableDefinition<T> {
                return indexes;\r
        }\r
 \r
-       void initObject(Object obj, Map<Object, FieldDefinition> map) {\r
+       private void initObject(Object obj, Map<Object, FieldDefinition> map) {\r
                for (FieldDefinition def : fields) {\r
                        Object newValue = def.initWithNewObject(obj);\r
                        map.put(newValue, def);\r