Untabified. Made some string building use StringBuilder.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2734 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2010-02-10 00:39:37 +00:00
parent b8b9a0d70a
commit 0aeab762aa
+294 -294
View File
@@ -48,8 +48,8 @@ public class Table<T>
public Table (Class<T> clazz, String tableName, String key, public Table (Class<T> clazz, String tableName, String key,
boolean mixedCaseConvert) boolean mixedCaseConvert)
{ {
String[] keys = {key}; String[] keys = {key};
init(clazz, tableName, keys, mixedCaseConvert); init(clazz, tableName, keys, mixedCaseConvert);
} }
/** /**
@@ -62,8 +62,8 @@ public class Table<T>
* operations to locate record in the table. * operations to locate record in the table.
*/ */
public Table (Class<T> clazz, String tableName, String key) { public Table (Class<T> clazz, String tableName, String key) {
String[] keys = {key}; String[] keys = {key};
init(clazz, tableName, keys, false); init(clazz, tableName, keys, false);
} }
/** /**
@@ -77,7 +77,7 @@ public class Table<T>
*/ */
public Table (Class<T> clazz, String tableName, String[] keys) public Table (Class<T> clazz, String tableName, String[] keys)
{ {
init(clazz, tableName, keys, false); init(clazz, tableName, keys, false);
} }
/** /**
@@ -94,7 +94,7 @@ public class Table<T>
public Table (Class<T> clazz, String tableName, String[] keys, public Table (Class<T> clazz, String tableName, String[] keys,
boolean mixedCaseConvert) boolean mixedCaseConvert)
{ {
init(clazz, tableName, keys, mixedCaseConvert); init(clazz, tableName, keys, mixedCaseConvert);
} }
/** /**
@@ -113,8 +113,8 @@ public class Table<T>
*/ */
public final Cursor<T> select (Connection conn, String condition) public final Cursor<T> select (Connection conn, String condition)
{ {
String query = "select " + listOfFields + " from " + name + String query = "select " + listOfFields + " from " + name +
" " + condition; " " + condition;
return new Cursor<T>(this, conn, query); return new Cursor<T>(this, conn, query);
} }
@@ -130,8 +130,8 @@ public class Table<T>
public final Cursor<T> select (Connection conn, String tables, public final Cursor<T> select (Connection conn, String tables,
String condition) String condition)
{ {
String query = "select " + qualifiedListOfFields + String query = "select " + qualifiedListOfFields +
" from " + name + "," + tables + " " + condition; " from " + name + "," + tables + " " + condition;
return new Cursor<T>(this, conn, query); return new Cursor<T>(this, conn, query);
} }
@@ -161,8 +161,8 @@ public class Table<T>
public final Cursor<T> straightJoin (Connection conn, String table, public final Cursor<T> straightJoin (Connection conn, String table,
String condition) String condition)
{ {
String query = "select " + listOfFields + String query = "select " + listOfFields +
" from " + name + " straight_join " + table + " " + condition; " from " + name + " straight_join " + table + " " + condition;
return new Cursor<T>(this, conn, query); return new Cursor<T>(this, conn, query);
} }
@@ -219,15 +219,15 @@ public class Table<T>
* @param obj object specifing values of inserted record fields * @param obj object specifing values of inserted record fields
*/ */
public synchronized void insert (Connection conn, T obj) public synchronized void insert (Connection conn, T obj)
throws SQLException throws SQLException
{ {
String sql = "insert into " + name + StringBuilder sql = new StringBuilder(
" (" + listOfFields + ") values (?"; "insert into " + name + " (" + listOfFields + ") values (?");
for (int i = 1; i < nColumns; i++) { for (int i = 1; i < nColumns; i++) {
sql += ",?"; sql.append(",?");
} }
sql += ")"; sql.append(")");
PreparedStatement insertStmt = conn.prepareStatement(sql); PreparedStatement insertStmt = conn.prepareStatement(sql.toString());
bindUpdateVariables(insertStmt, obj, null); bindUpdateVariables(insertStmt, obj, null);
insertStmt.executeUpdate(); insertStmt.executeUpdate();
insertStmt.close(); insertStmt.close();
@@ -241,15 +241,15 @@ public class Table<T>
* fields * fields
*/ */
public synchronized void insert (Connection conn, T[] objects) public synchronized void insert (Connection conn, T[] objects)
throws SQLException throws SQLException
{ {
String sql = "insert into " + name + StringBuilder sql = new StringBuilder(
" (" + listOfFields + ") values (?"; "insert into " + name + " (" + listOfFields + ") values (?");
for (int i = 1; i < nColumns; i++) { for (int i = 1; i < nColumns; i++) {
sql += ",?"; sql.append(",?");
} }
sql += ")"; sql.append(")");
PreparedStatement insertStmt = conn.prepareStatement(sql); PreparedStatement insertStmt = conn.prepareStatement(sql.toString());
for (int i = 0; i < objects.length; i++) { for (int i = 0; i < objects.length; i++) {
bindUpdateVariables(insertStmt, objects[i], null); bindUpdateVariables(insertStmt, objects[i], null);
insertStmt.addBatch(); insertStmt.addBatch();
@@ -278,7 +278,7 @@ public class Table<T>
* @return number of objects actually updated * @return number of objects actually updated
*/ */
public int update (Connection conn, T obj) public int update (Connection conn, T obj)
throws SQLException throws SQLException
{ {
return update(conn, obj, null); return update(conn, obj, null);
} }
@@ -297,9 +297,9 @@ public class Table<T>
* @return number of objects actually updated * @return number of objects actually updated
*/ */
public synchronized int update (Connection conn, T obj, FieldMask mask) public synchronized int update (Connection conn, T obj, FieldMask mask)
throws SQLException throws SQLException
{ {
int nUpdated = 0; int nUpdated = 0;
String sql = "update " + name + " set " + String sql = "update " + name + " set " +
(mask != null ? buildListOfAssignments(mask) : listOfAssignments) + (mask != null ? buildListOfAssignments(mask) : listOfAssignments) +
buildUpdateWhere(); buildUpdateWhere();
@@ -311,7 +311,7 @@ public class Table<T>
} }
nUpdated = ustmt.executeUpdate(); nUpdated = ustmt.executeUpdate();
ustmt.close(); ustmt.close();
return nUpdated; return nUpdated;
} }
/** /**
@@ -325,14 +325,14 @@ public class Table<T>
* @return number of objects actually updated * @return number of objects actually updated
*/ */
public synchronized int update (Connection conn, T[] objects) public synchronized int update (Connection conn, T[] objects)
throws SQLException throws SQLException
{ {
if (primaryKeys == null) { if (primaryKeys == null) {
throw new IllegalStateException( throw new IllegalStateException(
"No primary key for table " + name + "."); "No primary key for table " + name + ".");
} }
int nUpdated = 0; int nUpdated = 0;
String sql = "update " + name + " set " + listOfAssignments + String sql = "update " + name + " set " + listOfAssignments +
buildUpdateWhere(); buildUpdateWhere();
PreparedStatement updateStmt = conn.prepareStatement(sql); PreparedStatement updateStmt = conn.prepareStatement(sql);
@@ -350,7 +350,7 @@ public class Table<T>
nUpdated += rc[k]; nUpdated += rc[k];
} }
updateStmt.close(); updateStmt.close();
return nUpdated; return nUpdated;
} }
/** /**
@@ -359,25 +359,25 @@ public class Table<T>
* @param obj object containing value of primary key. * @param obj object containing value of primary key.
*/ */
public synchronized int delete (Connection conn, T obj) public synchronized int delete (Connection conn, T obj)
throws SQLException throws SQLException
{ {
if (primaryKeys == null) { if (primaryKeys == null) {
throw new IllegalStateException( throw new IllegalStateException(
"No primary key for table " + name + "."); "No primary key for table " + name + ".");
}
int nDeleted = 0;
String sql = "delete from " + name +
" where " + primaryKeys[0] + " = ?";
for (int i = 1; i < primaryKeys.length; i++) {
sql += " and " + primaryKeys[i] + " = ?";
} }
PreparedStatement deleteStmt = conn.prepareStatement(sql); int nDeleted = 0;
StringBuilder sql = new StringBuilder(
"delete from " + name + " where " + primaryKeys[0] + " = ?");
for (int i = 1; i < primaryKeys.length; i++) {
sql.append(" and ").append(primaryKeys[i]).append(" = ?");
}
PreparedStatement deleteStmt = conn.prepareStatement(sql.toString());
for (int i = 0; i < primaryKeys.length; i++) { for (int i = 0; i < primaryKeys.length; i++) {
fields[primaryKeyIndices[i]].bindVariable(deleteStmt, obj,i+1); fields[primaryKeyIndices[i]].bindVariable(deleteStmt, obj,i+1);
} }
nDeleted = deleteStmt.executeUpdate(); nDeleted = deleteStmt.executeUpdate();
deleteStmt.close(); deleteStmt.close();
return nDeleted; return nDeleted;
} }
/** /**
@@ -388,19 +388,19 @@ public class Table<T>
* @return number of objects actually deleted * @return number of objects actually deleted
*/ */
public synchronized int delete (Connection conn, T[] objects) public synchronized int delete (Connection conn, T[] objects)
throws SQLException throws SQLException
{ {
if (primaryKeys == null) { if (primaryKeys == null) {
throw new IllegalStateException( throw new IllegalStateException(
"No primary key for table " + name + "."); "No primary key for table " + name + ".");
}
int nDeleted = 0;
String sql = "delete from " + name +
" where " + primaryKeys[0] + " = ?";
for (int i = 1; i < primaryKeys.length; i++) {
sql += " and " + primaryKeys[i] + " = ?";
} }
PreparedStatement deleteStmt = conn.prepareStatement(sql); int nDeleted = 0;
StringBuilder sql = new StringBuilder(
"delete from " + name + " where " + primaryKeys[0] + " = ?");
for (int i = 1; i < primaryKeys.length; i++) {
sql.append(" and ").append(primaryKeys[i]).append(" = ?");
}
PreparedStatement deleteStmt = conn.prepareStatement(sql.toString());
for (int i = 0; i < objects.length; i++) { for (int i = 0; i < objects.length; i++) {
for (int j = 0; j < primaryKeys.length; j++) { for (int j = 0; j < primaryKeys.length; j++) {
fields[primaryKeyIndices[j]].bindVariable( fields[primaryKeyIndices[j]].bindVariable(
@@ -413,7 +413,7 @@ public class Table<T>
nDeleted += rc[k]; nDeleted += rc[k];
} }
deleteStmt.close(); deleteStmt.close();
return nDeleted; return nDeleted;
} }
@Override @Override
@@ -437,41 +437,41 @@ public class Table<T>
name = tableName; name = tableName;
this.mixedCaseConvert = mixedCaseConvert; this.mixedCaseConvert = mixedCaseConvert;
_rowClass = clazz; _rowClass = clazz;
primaryKeys = keys; primaryKeys = keys;
listOfFields = ""; listOfFields = "";
qualifiedListOfFields = ""; qualifiedListOfFields = "";
listOfAssignments = ""; listOfAssignments = "";
ArrayList<FieldDescriptor> fieldsVector = ArrayList<FieldDescriptor> fieldsVector =
new ArrayList<FieldDescriptor>(); new ArrayList<FieldDescriptor>();
nFields = buildFieldsList(fieldsVector, _rowClass, ""); nFields = buildFieldsList(fieldsVector, _rowClass, "");
fields = fieldsVector.toArray(new FieldDescriptor[nFields]); fields = fieldsVector.toArray(new FieldDescriptor[nFields]);
fMask = new FieldMask(fields); fMask = new FieldMask(fields);
try { try {
constructor = _rowClass.getDeclaredConstructor(new Class[0]); constructor = _rowClass.getDeclaredConstructor(new Class[0]);
setBypass.invoke(constructor, bypassFlag); setBypass.invoke(constructor, bypassFlag);
} catch(Exception ex) {} } catch(Exception ex) {}
if (keys != null && keys.length > 0) { if (keys != null && keys.length > 0) {
primaryKeyIndices = new int[keys.length]; primaryKeyIndices = new int[keys.length];
for (int j = keys.length; --j >= 0;) { for (int j = keys.length; --j >= 0;) {
int i = nFields; int i = nFields;
while (--i >= 0) { while (--i >= 0) {
if (fields[i].name.equals(keys[j])) { if (fields[i].name.equals(keys[j])) {
if (!fields[i].isAtomic()) { if (!fields[i].isAtomic()) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Non-atomic primary key provided"); "Non-atomic primary key provided");
} }
primaryKeyIndices[j] = i; primaryKeyIndices[j] = i;
break; break;
} }
} }
if (i < 0) { if (i < 0) {
throw new NoSuchFieldError("No such field '" + keys[j] throw new NoSuchFieldError("No such field '" + keys[j]
+ "' in table " + name); + "' in table " + name);
} }
} }
} }
} }
protected final String convertName (String name) protected final String convertName (String name)
@@ -486,117 +486,117 @@ public class Table<T>
protected final int buildFieldsList (ArrayList<FieldDescriptor> buf, protected final int buildFieldsList (ArrayList<FieldDescriptor> buf,
Class<?> _rowClass, String prefix) Class<?> _rowClass, String prefix)
{ {
Field[] f = _rowClass.getDeclaredFields(); Field[] f = _rowClass.getDeclaredFields();
Class<?> superclass = _rowClass; Class<?> superclass = _rowClass;
while ((superclass = superclass.getSuperclass()) != null) { while ((superclass = superclass.getSuperclass()) != null) {
Field[] inheritedFields = superclass.getDeclaredFields(); Field[] inheritedFields = superclass.getDeclaredFields();
Field[] allFields = new Field[inheritedFields.length + f.length]; Field[] allFields = new Field[inheritedFields.length + f.length];
System.arraycopy(inheritedFields, 0, allFields, 0, System.arraycopy(inheritedFields, 0, allFields, 0,
inheritedFields.length); inheritedFields.length);
System.arraycopy(f,0, allFields, inheritedFields.length, f.length); System.arraycopy(f,0, allFields, inheritedFields.length, f.length);
f = allFields; f = allFields;
} }
try { try {
for (int i = f.length; --i>= 0;) { for (int i = f.length; --i>= 0;) {
setBypass.invoke(f[i], bypassFlag); setBypass.invoke(f[i], bypassFlag);
} }
} catch(Exception ex) { } catch(Exception ex) {
System.err.println("Failed to set bypass attribute"); System.err.println("Failed to set bypass attribute");
} }
int n = 0; int n = 0;
for (int i = 0; i < f.length; i++) { for (int i = 0; i < f.length; i++) {
if ((f[i].getModifiers()&(Modifier.TRANSIENT|Modifier.STATIC))==0) if ((f[i].getModifiers()&(Modifier.TRANSIENT|Modifier.STATIC))==0)
{ {
String name = f[i].getName(); String name = f[i].getName();
Class<?> fieldClass = f[i].getType(); Class<?> fieldClass = f[i].getType();
String fullName = prefix + convertName(name); String fullName = prefix + convertName(name);
FieldDescriptor fd = new FieldDescriptor(f[i], fullName); FieldDescriptor fd = new FieldDescriptor(f[i], fullName);
int type; int type;
buf.add(fd); buf.add(fd);
n += 1; n += 1;
String c = fieldClass.getName(); String c = fieldClass.getName();
if (c.equals("byte")) type = FieldDescriptor.t_byte; if (c.equals("byte")) type = FieldDescriptor.t_byte;
else if (c.equals("short")) type = FieldDescriptor.t_short; else if (c.equals("short")) type = FieldDescriptor.t_short;
else if (c.equals("int")) type = FieldDescriptor.t_int; else if (c.equals("int")) type = FieldDescriptor.t_int;
else if (c.equals("long")) type = FieldDescriptor.t_long; else if (c.equals("long")) type = FieldDescriptor.t_long;
else if (c.equals("float")) type = FieldDescriptor.t_float; else if (c.equals("float")) type = FieldDescriptor.t_float;
else if (c.equals("double")) type = FieldDescriptor.t_double; else if (c.equals("double")) type = FieldDescriptor.t_double;
else if (c.equals("boolean")) type = FieldDescriptor.t_boolean; else if (c.equals("boolean")) type = FieldDescriptor.t_boolean;
else if (c.equals("java.lang.Byte")) else if (c.equals("java.lang.Byte"))
type = FieldDescriptor.tByte; type = FieldDescriptor.tByte;
else if (c.equals("java.lang.Short")) else if (c.equals("java.lang.Short"))
type = FieldDescriptor.tShort; type = FieldDescriptor.tShort;
else if (c.equals("java.lang.Integer")) else if (c.equals("java.lang.Integer"))
type = FieldDescriptor.tInteger; type = FieldDescriptor.tInteger;
else if (c.equals("java.lang.Long")) else if (c.equals("java.lang.Long"))
type = FieldDescriptor.tLong; type = FieldDescriptor.tLong;
else if (c.equals("java.lang.Float")) else if (c.equals("java.lang.Float"))
type = FieldDescriptor.tFloat; type = FieldDescriptor.tFloat;
else if (c.equals("java.lang.Double")) else if (c.equals("java.lang.Double"))
type = FieldDescriptor.tDouble; type = FieldDescriptor.tDouble;
else if (c.equals("java.lang.Boolean")) else if (c.equals("java.lang.Boolean"))
type = FieldDescriptor.tBoolean; type = FieldDescriptor.tBoolean;
else if (c.equals("java.math.BigDecimal")) else if (c.equals("java.math.BigDecimal"))
type = FieldDescriptor.tDecimal; type = FieldDescriptor.tDecimal;
else if (c.equals("java.lang.String")) else if (c.equals("java.lang.String"))
type = FieldDescriptor.tString; type = FieldDescriptor.tString;
else if (fieldClass.equals(BYTE_PROTO.getClass())) else if (fieldClass.equals(BYTE_PROTO.getClass()))
type = FieldDescriptor.tBytes; type = FieldDescriptor.tBytes;
else if (c.equals("java.sql.Date")) else if (c.equals("java.sql.Date"))
type = FieldDescriptor.tDate; type = FieldDescriptor.tDate;
else if (c.equals("java.sql.Time")) else if (c.equals("java.sql.Time"))
type = FieldDescriptor.tTime; type = FieldDescriptor.tTime;
else if (c.equals("java.sql.Timestamp")) else if (c.equals("java.sql.Timestamp"))
type = FieldDescriptor.tTimestamp; type = FieldDescriptor.tTimestamp;
else if (c.equals("java.lang.InputStream")) else if (c.equals("java.lang.InputStream"))
type = FieldDescriptor.tStream; type = FieldDescriptor.tStream;
else if (c.equals("java.sql.BlobLocator")) else if (c.equals("java.sql.BlobLocator"))
type = FieldDescriptor.tBlob; type = FieldDescriptor.tBlob;
else if (c.equals("java.sql.ClobLocator")) else if (c.equals("java.sql.ClobLocator"))
type = FieldDescriptor.tClob; type = FieldDescriptor.tClob;
else if (serializableClass.isAssignableFrom(fieldClass)) else if (serializableClass.isAssignableFrom(fieldClass))
type = FieldDescriptor.tClosure; type = FieldDescriptor.tClosure;
else { else {
int nComponents = buildFieldsList(buf, fieldClass, int nComponents = buildFieldsList(buf, fieldClass,
fd.name+fieldSeparator); fd.name+fieldSeparator);
fd.inType = fd.outType = fd.inType = fd.outType =
FieldDescriptor.tCompound + nComponents; FieldDescriptor.tCompound + nComponents;
try { try {
fd.constructor = fd.constructor =
fieldClass.getDeclaredConstructor(new Class[0]); fieldClass.getDeclaredConstructor(new Class[0]);
setBypass.invoke(fd.constructor, bypassFlag); setBypass.invoke(fd.constructor, bypassFlag);
} catch(Exception ex) {} } catch(Exception ex) {}
n += nComponents; n += nComponents;
continue; continue;
} }
if (listOfFields.length() != 0) { if (listOfFields.length() != 0) {
listOfFields += ","; listOfFields += ",";
qualifiedListOfFields += ","; qualifiedListOfFields += ",";
listOfAssignments += ","; listOfAssignments += ",";
} }
listOfFields += fullName; listOfFields += fullName;
qualifiedListOfFields += this.name + "." + fullName; qualifiedListOfFields += this.name + "." + fullName;
listOfAssignments += fullName + "=?"; listOfAssignments += fullName + "=?";
fd.inType = fd.outType = type; fd.inType = fd.outType = type;
nColumns += 1; nColumns += 1;
} }
} }
return n; return n;
} }
protected final String buildListOfAssignments (FieldMask mask) protected final String buildListOfAssignments (FieldMask mask)
{ {
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
int fcount = fields.length; int fcount = fields.length;
for (int i = 0; i < fcount; i++) { for (int i = 0; i < fcount; i++) {
// skip non-modified fields // skip non-modified fields
if (!mask.isModified(i)) { if (!mask.isModified(i)) {
continue; continue;
@@ -613,66 +613,66 @@ public class Table<T>
protected final T load (ResultSet result) throws SQLException protected final T load (ResultSet result) throws SQLException
{ {
T obj; T obj;
try { try {
obj = constructor.newInstance(constructorArgs); obj = constructor.newInstance(constructorArgs);
} }
catch(IllegalAccessException ex) { throw new IllegalAccessError(); } catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
catch(InstantiationException ex) { throw new InstantiationError(); } catch(InstantiationException ex) { throw new InstantiationError(); }
catch(Exception ex) { catch(Exception ex) {
throw new InstantiationError("Exception was thrown by constructor"); throw new InstantiationError("Exception was thrown by constructor");
} }
load(obj, 0, nFields, 0, result); load(obj, 0, nFields, 0, result);
return obj; return obj;
} }
protected final int load ( protected final int load (
Object obj, int i, int end, int column, ResultSet result) Object obj, int i, int end, int column, ResultSet result)
throws SQLException throws SQLException
{ {
try { try {
while (i < end) { while (i < end) {
FieldDescriptor fd = fields[i++]; FieldDescriptor fd = fields[i++];
if (!fd.loadVariable(result, obj, ++column)) { if (!fd.loadVariable(result, obj, ++column)) {
Object component = Object component =
fd.constructor.newInstance(constructorArgs); fd.constructor.newInstance(constructorArgs);
fd.field.set(obj, component); fd.field.set(obj, component);
int nComponents = fd.inType - FieldDescriptor.tCompound; int nComponents = fd.inType - FieldDescriptor.tCompound;
column = load(component, i, i + nComponents, column = load(component, i, i + nComponents,
column-1, result); column-1, result);
i += nComponents; i += nComponents;
} }
} }
} }
catch(IllegalAccessException ex) { throw new IllegalAccessError(); } catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
catch(InstantiationException ex) { throw new InstantiationError(); } catch(InstantiationException ex) { throw new InstantiationError(); }
catch(InvocationTargetException ex) { catch(InvocationTargetException ex) {
throw new InstantiationError("Exception was thrown by constructor"); throw new InstantiationError("Exception was thrown by constructor");
} }
return column; return column;
} }
protected final int bindUpdateVariables(PreparedStatement pstmt, protected final int bindUpdateVariables(PreparedStatement pstmt,
T obj, T obj,
FieldMask mask) FieldMask mask)
throws SQLException throws SQLException
{ {
return bindUpdateVariables(pstmt, obj, 0, nFields, 0, mask); return bindUpdateVariables(pstmt, obj, 0, nFields, 0, mask);
} }
protected final void bindQueryVariables(PreparedStatement pstmt, protected final void bindQueryVariables(PreparedStatement pstmt,
T obj, T obj,
FieldMask mask) FieldMask mask)
throws SQLException throws SQLException
{ {
bindQueryVariables(pstmt, obj, 0, nFields, 0, mask); bindQueryVariables(pstmt, obj, 0, nFields, 0, mask);
} }
protected final void updateVariables(ResultSet result, T obj) protected final void updateVariables(ResultSet result, T obj)
throws SQLException throws SQLException
{ {
updateVariables(result, obj, 0, nFields, 0); updateVariables(result, obj, 0, nFields, 0);
result.updateRow(); result.updateRow();
} }
protected final String buildUpdateWhere() protected final String buildUpdateWhere()
@@ -689,10 +689,10 @@ public class Table<T>
{ {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
buildQueryList(buf, qbe, 0, nFields, mask, like); buildQueryList(buf, qbe, 0, nFields, mask, like);
if (buf.length() > 0) { if (buf.length() > 0) {
buf.insert(0, " where "); buf.insert(0, " where ");
} }
return "select " + listOfFields + " from " + name + buf; return "select " + listOfFields + " from " + name + buf;
} }
protected final int bindUpdateVariables ( protected final int bindUpdateVariables (
@@ -701,39 +701,39 @@ public class Table<T>
throws SQLException throws SQLException
{ {
try { try {
while (i < end) { while (i < end) {
FieldDescriptor fd = fields[i++]; FieldDescriptor fd = fields[i++];
Object comp = null; Object comp = null;
// skip non-modified fields // skip non-modified fields
if (mask != null && !mask.isModified(i-1)) { if (mask != null && !mask.isModified(i-1)) {
continue; continue;
} }
if (!fd.isBuiltin() && (comp = fd.field.get(obj)) == null) { if (!fd.isBuiltin() && (comp = fd.field.get(obj)) == null) {
if (fd.isCompound()) { if (fd.isCompound()) {
int nComponents = fd.outType-FieldDescriptor.tCompound; int nComponents = fd.outType-FieldDescriptor.tCompound;
while (--nComponents >= 0) { while (--nComponents >= 0) {
fd = fields[i++]; fd = fields[i++];
if (!fd.isCompound()) { if (!fd.isCompound()) {
pstmt.setNull(++column, pstmt.setNull(++column,
FieldDescriptor.sqlTypeMapping[fd.outType]); FieldDescriptor.sqlTypeMapping[fd.outType]);
} }
} }
} else { } else {
pstmt.setNull( pstmt.setNull(
++column, ++column,
FieldDescriptor.sqlTypeMapping[fd.outType]); FieldDescriptor.sqlTypeMapping[fd.outType]);
} }
} else { } else {
if (!fd.bindVariable(pstmt, obj, ++column)) { if (!fd.bindVariable(pstmt, obj, ++column)) {
int nComponents = fd.outType-FieldDescriptor.tCompound; int nComponents = fd.outType-FieldDescriptor.tCompound;
column = bindUpdateVariables( column = bindUpdateVariables(
pstmt, comp, i, i+nComponents,column-1, mask); pstmt, comp, i, i+nComponents,column-1, mask);
i += nComponents; i += nComponents;
} }
} }
} }
} catch(IllegalAccessException ex) { throw new IllegalAccessError(); } } catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
return column; return column;
} }
protected final int bindQueryVariables ( protected final int bindQueryVariables (
@@ -742,13 +742,13 @@ public class Table<T>
throws SQLException throws SQLException
{ {
try { try {
while (i < end) { while (i < end) {
Object comp; Object comp;
FieldDescriptor fd = fields[i++]; FieldDescriptor fd = fields[i++];
if (!fd.field.getDeclaringClass().isInstance(obj)) { if (!fd.field.getDeclaringClass().isInstance(obj)) {
return column; return column;
} }
int nComponents = fd.isCompound() ? int nComponents = fd.isCompound() ?
fd.outType-FieldDescriptor.tCompound : 0; fd.outType-FieldDescriptor.tCompound : 0;
try { try {
@@ -775,29 +775,29 @@ public class Table<T>
continue; continue;
} }
if (!fd.bindVariable(pstmt, obj, ++column)) { if (!fd.bindVariable(pstmt, obj, ++column)) {
column = bindQueryVariables( column = bindQueryVariables(
pstmt, comp, i, i+nComponents, column-1, mask); pstmt, comp, i, i+nComponents, column-1, mask);
} }
} finally { } finally {
i += nComponents; i += nComponents;
} }
} }
} catch(IllegalAccessException ex) { throw new IllegalAccessError(); } } catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
return column; return column;
} }
protected final void buildQueryList ( protected final void buildQueryList (
StringBuilder buf, Object qbe, int i, int end, FieldMask mask, StringBuilder buf, Object qbe, int i, int end, FieldMask mask,
boolean like) boolean like)
{ {
try { try {
while (i < end) { while (i < end) {
Object comp; Object comp;
FieldDescriptor fd = fields[i++]; FieldDescriptor fd = fields[i++];
int nComponents = int nComponents =
fd.isCompound() ? fd.outType-FieldDescriptor.tCompound : 0; fd.isCompound() ? fd.outType-FieldDescriptor.tCompound : 0;
try { try {
// skip closure fields (because querying by them makes // skip closure fields (because querying by them makes
@@ -840,8 +840,8 @@ public class Table<T>
} finally { } finally {
i += nComponents; i += nComponents;
} }
} }
} catch(IllegalAccessException ex) { throw new IllegalAccessError(); } } catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
} }
protected final int updateVariables ( protected final int updateVariables (
@@ -849,32 +849,32 @@ public class Table<T>
throws SQLException throws SQLException
{ {
try { try {
while (i < end) { while (i < end) {
FieldDescriptor fd = fields[i++]; FieldDescriptor fd = fields[i++];
Object comp = null; Object comp = null;
if (!fd.isBuiltin() && (comp = fd.field.get(obj)) == null) { if (!fd.isBuiltin() && (comp = fd.field.get(obj)) == null) {
if (fd.isCompound()) { if (fd.isCompound()) {
int nComponents = fd.outType-FieldDescriptor.tCompound; int nComponents = fd.outType-FieldDescriptor.tCompound;
while (--nComponents >= 0) { while (--nComponents >= 0) {
fd = fields[i++]; fd = fields[i++];
if (!fd.isCompound()) { if (!fd.isCompound()) {
result.updateNull(++column); result.updateNull(++column);
} }
} }
} else { } else {
result.updateNull(++column); result.updateNull(++column);
} }
} else { } else {
if (!fd.updateVariable(result, obj, ++column)) { if (!fd.updateVariable(result, obj, ++column)) {
int nComponents = fd.outType-FieldDescriptor.tCompound; int nComponents = fd.outType-FieldDescriptor.tCompound;
column = updateVariables(result, comp, column = updateVariables(result, comp,
i, i+nComponents, column-1); i, i+nComponents, column-1);
i += nComponents; i += nComponents;
} }
} }
} }
} catch(IllegalAccessException ex) { throw new IllegalAccessError(); } } catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
return column; return column;
} }
protected static Method getSetBypass () protected static Method getSetBypass ()