diff options
author | James Moger <james.moger@gmail.com> | 2011-08-16 15:45:29 -0400 |
---|---|---|
committer | James Moger <james.moger@gmail.com> | 2011-08-16 15:45:29 -0400 |
commit | 265bb2eebd995af0445ce7bc37f066e43d7da287 (patch) | |
tree | 055d8d5111caca32b474a09cc08161831fe6ae89 /src | |
parent | d95e16170d063c0b74d04e6636e8fddbfa120689 (diff) | |
download | iciql-265bb2eebd995af0445ce7bc37f066e43d7da287.tar.gz iciql-265bb2eebd995af0445ce7bc37f066e43d7da287.zip |
Refactoring.
Diffstat (limited to 'src')
-rw-r--r-- | src/com/iciql/Db.java | 12 | ||||
-rw-r--r-- | src/com/iciql/Define.java | 18 | ||||
-rw-r--r-- | 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 <T> 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 <T> 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 <T> 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 <T> 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 <T> 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 <T extends Object> Query<T> 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<T> { }
}
- 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<T> { }
}
- 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<T> { List<String> primaryKeyColumnNames;
boolean memoryTable;
- private boolean createTableIfRequired = true;
+ private boolean createIfRequired = true;
private Class<T> clazz;
private IdentityHashMap<Object, FieldDefinition> fieldMap = Utils.newIdentityHashMap();
private ArrayList<IndexDefinition> indexes = Utils.newArrayList();
@@ -149,20 +149,11 @@ public class TableDefinition<T> { 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<T> { * @param modelFields
* the ordered list of model fields
*/
- void setPrimaryKey(Object[] modelFields) {
+ void definePrimaryKey(Object[] modelFields) {
List<String> columnNames = mapColumnNames(modelFields);
setPrimaryKey(columnNames);
}
@@ -183,7 +174,7 @@ public class TableDefinition<T> { * @param columnNames
* the ordered list of column names
*/
- void setPrimaryKey(List<String> columnNames) {
+ private void setPrimaryKey(List<String> columnNames) {
primaryKeyColumnNames = Utils.newArrayList(columnNames);
// set isPrimaryKey flag for all field definitions
for (FieldDefinition fieldDefinition : fieldMap.values()) {
@@ -191,7 +182,7 @@ public class TableDefinition<T> { }
}
- <A> String getColumnName(A fieldObject) {
+ private <A> String getColumnName(A fieldObject) {
FieldDefinition def = fieldMap.get(fieldObject);
return def == null ? null : def.columnName;
}
@@ -212,7 +203,7 @@ public class TableDefinition<T> { * @param modelFields
* the ordered list of model fields
*/
- void addIndex(IndexType type, Object[] modelFields) {
+ void defineIndex(IndexType type, Object[] modelFields) {
List<String> columnNames = mapColumnNames(modelFields);
addIndex(null, type, columnNames);
}
@@ -225,7 +216,7 @@ public class TableDefinition<T> { * @param columnNames
* the ordered list of column names
*/
- void addIndex(String name, IndexType type, List<String> columnNames) {
+ private void addIndex(String name, IndexType type, List<String> columnNames) {
IndexDefinition index = new IndexDefinition();
if (StringUtils.isNullOrEmpty(name)) {
index.indexName = tableName + "_" + indexes.size();
@@ -237,21 +228,21 @@ public class TableDefinition<T> { 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<T> { }
return value;
}
- // standard behavior
+
+ // return the value unchanged
return value;
}
@@ -547,8 +539,8 @@ public class TableDefinition<T> { return stat.executeUpdate();
}
- TableDefinition<T> createTableIfRequired(Db db) {
- if (!createTableIfRequired) {
+ TableDefinition<T> 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<T> { }
// allow control over createTableIfRequired()
- createTableIfRequired = tableAnnotation.create();
+ createIfRequired = tableAnnotation.create();
// model version
if (clazz.isAnnotationPresent(IQVersion.class)) {
@@ -640,7 +632,7 @@ public class TableDefinition<T> { }
}
- void addIndex(IQIndex index) {
+ private void addIndex(IQIndex index) {
List<String> columns = Arrays.asList(index.value());
addIndex(index.name(), index.type(), columns);
}
@@ -649,7 +641,7 @@ public class TableDefinition<T> { return indexes;
}
- void initObject(Object obj, Map<Object, FieldDefinition> map) {
+ private void initObject(Object obj, Map<Object, FieldDefinition> map) {
for (FieldDefinition def : fields) {
Object newValue = def.initWithNewObject(obj);
map.put(newValue, def);
|