From 265bb2eebd995af0445ce7bc37f066e43d7da287 Mon Sep 17 00:00:00 2001 From: James Moger Date: Tue, 16 Aug 2011 15:45:29 -0400 Subject: Refactoring. --- src/com/iciql/Db.java | 12 ++++----- src/com/iciql/Define.java | 18 +++++++------- src/com/iciql/TableDefinition.java | 50 ++++++++++++++++---------------------- 3 files changed, 36 insertions(+), 44 deletions(-) diff --git a/src/com/iciql/Db.java b/src/com/iciql/Db.java index 09c26a1..d369c2c 100644 --- a/src/com/iciql/Db.java +++ b/src/com/iciql/Db.java @@ -181,7 +181,7 @@ public class Db { public void insert(T t) { Class clazz = t.getClass(); - long rc = define(clazz).createTableIfRequired(this).insert(this, t, false); + long rc = define(clazz).createIfRequired(this).insert(this, t, false); if (rc == 0) { throw new IciqlException("Failed to insert {0}. Affected rowcount == 0.", t); } @@ -189,7 +189,7 @@ public class Db { public long insertAndGetKey(T t) { Class clazz = t.getClass(); - return define(clazz).createTableIfRequired(this).insert(this, t, true); + return define(clazz).createIfRequired(this).insert(this, t, true); } /** @@ -213,7 +213,7 @@ public class Db { */ public void merge(T t) { Class clazz = t.getClass(); - TableDefinition def = define(clazz).createTableIfRequired(this); + TableDefinition def = define(clazz).createIfRequired(this); int rc = def.merge(this, t); if (rc == 0) { rc = def.update(this, t); @@ -225,17 +225,17 @@ public class Db { public int update(T t) { Class clazz = t.getClass(); - return define(clazz).createTableIfRequired(this).update(this, t); + return define(clazz).createIfRequired(this).update(this, t); } public int delete(T t) { Class clazz = t.getClass(); - return define(clazz).createTableIfRequired(this).delete(this, t); + return define(clazz).createIfRequired(this).delete(this, t); } public Query from(T alias) { Class clazz = alias.getClass(); - define(clazz).createTableIfRequired(this); + define(clazz).createIfRequired(this); return Query.from(this, alias); } diff --git a/src/com/iciql/Define.java b/src/com/iciql/Define.java index aa05872..4216ea1 100644 --- a/src/com/iciql/Define.java +++ b/src/com/iciql/Define.java @@ -31,47 +31,47 @@ public class Define { public static void primaryKey(Object... columns) { checkInDefine(); - currentTableDefinition.setPrimaryKey(columns); + currentTableDefinition.definePrimaryKey(columns); } public static void index(Object... columns) { checkInDefine(); - currentTableDefinition.addIndex(IndexType.STANDARD, columns); + currentTableDefinition.defineIndex(IndexType.STANDARD, columns); } public static void uniqueIndex(Object... columns) { checkInDefine(); - currentTableDefinition.addIndex(IndexType.UNIQUE, columns); + currentTableDefinition.defineIndex(IndexType.UNIQUE, columns); } public static void hashIndex(Object column) { checkInDefine(); - currentTableDefinition.addIndex(IndexType.HASH, new Object[] { column }); + currentTableDefinition.defineIndex(IndexType.HASH, new Object[] { column }); } public static void uniqueHashIndex(Object column) { checkInDefine(); - currentTableDefinition.addIndex(IndexType.UNIQUE_HASH, new Object[] { column }); + currentTableDefinition.defineIndex(IndexType.UNIQUE_HASH, new Object[] { column }); } public static void columnName(Object column, String columnName) { checkInDefine(); - currentTableDefinition.setColumnName(column, columnName); + currentTableDefinition.defineColumnName(column, columnName); } public static void length(Object column, int length) { checkInDefine(); - currentTableDefinition.setLength(column, length); + currentTableDefinition.defineLength(column, length); } public static void scale(Object column, int scale) { checkInDefine(); - currentTableDefinition.setScale(column, scale); + currentTableDefinition.defineScale(column, scale); } public static void tableName(String tableName) { checkInDefine(); - currentTableDefinition.setTableName(tableName); + currentTableDefinition.defineTableName(tableName); } @SuppressWarnings("deprecation") diff --git a/src/com/iciql/TableDefinition.java b/src/com/iciql/TableDefinition.java index 251f098..b6084ca 100644 --- a/src/com/iciql/TableDefinition.java +++ b/src/com/iciql/TableDefinition.java @@ -89,13 +89,13 @@ public class TableDefinition { } } - Object initWithNewObject(Object obj) { + private Object initWithNewObject(Object obj) { Object o = Utils.newObject(field.getType()); setValue(obj, o); return o; } - void setValue(Object obj, Object o) { + private void setValue(Object obj, Object o) { try { if (!field.isAccessible()) { field.setAccessible(true); @@ -114,7 +114,7 @@ public class TableDefinition { } } - Object read(ResultSet rs, int columnIndex) { + private Object read(ResultSet rs, int columnIndex) { try { return rs.getObject(columnIndex); } catch (SQLException e) { @@ -130,7 +130,7 @@ public class TableDefinition { List primaryKeyColumnNames; boolean memoryTable; - private boolean createTableIfRequired = true; + private boolean createIfRequired = true; private Class clazz; private IdentityHashMap fieldMap = Utils.newIdentityHashMap(); private ArrayList indexes = Utils.newArrayList(); @@ -149,20 +149,11 @@ public class TableDefinition { return fields; } - FieldDefinition getField(String name) { - for (FieldDefinition field : fields) { - if (field.columnName.equalsIgnoreCase(name)) { - return field; - } - } - return null; - } - - void setSchemaName(String schemaName) { + void defineSchemaName(String schemaName) { this.schemaName = schemaName; } - void setTableName(String tableName) { + void defineTableName(String tableName) { this.tableName = tableName; } @@ -172,7 +163,7 @@ public class TableDefinition { * @param modelFields * the ordered list of model fields */ - void setPrimaryKey(Object[] modelFields) { + void definePrimaryKey(Object[] modelFields) { List columnNames = mapColumnNames(modelFields); setPrimaryKey(columnNames); } @@ -183,7 +174,7 @@ public class TableDefinition { * @param columnNames * the ordered list of column names */ - void setPrimaryKey(List columnNames) { + private void setPrimaryKey(List columnNames) { primaryKeyColumnNames = Utils.newArrayList(columnNames); // set isPrimaryKey flag for all field definitions for (FieldDefinition fieldDefinition : fieldMap.values()) { @@ -191,7 +182,7 @@ public class TableDefinition { } } - String getColumnName(A fieldObject) { + private String getColumnName(A fieldObject) { FieldDefinition def = fieldMap.get(fieldObject); return def == null ? null : def.columnName; } @@ -212,7 +203,7 @@ public class TableDefinition { * @param modelFields * the ordered list of model fields */ - void addIndex(IndexType type, Object[] modelFields) { + void defineIndex(IndexType type, Object[] modelFields) { List columnNames = mapColumnNames(modelFields); addIndex(null, type, columnNames); } @@ -225,7 +216,7 @@ public class TableDefinition { * @param columnNames * the ordered list of column names */ - void addIndex(String name, IndexType type, List columnNames) { + private void addIndex(String name, IndexType type, List columnNames) { IndexDefinition index = new IndexDefinition(); if (StringUtils.isNullOrEmpty(name)) { index.indexName = tableName + "_" + indexes.size(); @@ -237,21 +228,21 @@ public class TableDefinition { indexes.add(index); } - public void setColumnName(Object column, String columnName) { + void defineColumnName(Object column, String columnName) { FieldDefinition def = fieldMap.get(column); if (def != null) { def.columnName = columnName; } } - public void setLength(Object column, int length) { + void defineLength(Object column, int length) { FieldDefinition def = fieldMap.get(column); if (def != null) { def.length = length; } } - public void setScale(Object column, int scale) { + void defineScale(Object column, int scale) { FieldDefinition def = fieldMap.get(column); if (def != null) { def.scale = scale; @@ -411,7 +402,8 @@ public class TableDefinition { } return value; } - // standard behavior + + // return the value unchanged return value; } @@ -547,8 +539,8 @@ public class TableDefinition { return stat.executeUpdate(); } - TableDefinition createTableIfRequired(Db db) { - if (!createTableIfRequired) { + TableDefinition createIfRequired(Db db) { + if (!createIfRequired) { // skip table and index creation // but still check for upgrades db.upgradeTable(this); @@ -607,7 +599,7 @@ public class TableDefinition { } // allow control over createTableIfRequired() - createTableIfRequired = tableAnnotation.create(); + createIfRequired = tableAnnotation.create(); // model version if (clazz.isAnnotationPresent(IQVersion.class)) { @@ -640,7 +632,7 @@ public class TableDefinition { } } - void addIndex(IQIndex index) { + private void addIndex(IQIndex index) { List columns = Arrays.asList(index.value()); addIndex(index.name(), index.type(), columns); } @@ -649,7 +641,7 @@ public class TableDefinition { return indexes; } - void initObject(Object obj, Map map) { + private void initObject(Object obj, Map map) { for (FieldDefinition def : fields) { Object newValue = def.initWithNewObject(obj); map.put(newValue, def); -- cgit v1.2.3