User: dsundstrom Date: 02/01/15 13:29:50 Modified: src/main/org/jboss/ejb/plugins/cmp/jdbc SQLUtil.java Log: Converted field arrays to Lists for easier handling. Revision Changes Path 1.5 +184 -90 jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/SQLUtil.java Index: SQLUtil.java =================================================================== RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/SQLUtil.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- SQLUtil.java 2001/09/01 22:03:15 1.4 +++ SQLUtil.java 2002/01/15 21:29:50 1.5 @@ -7,44 +7,54 @@ package org.jboss.ejb.plugins.cmp.jdbc; -import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge; + /** * SQLUtil helps with building sql statements. * * @author <a href="mailto:[EMAIL PROTECTED]">Dain Sundstrom</a> - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ */ public class SQLUtil { // ======================================================================= // Create Table Columns Clause - // columnName0 sqlType0 [, columnName1 sqlType0 [, columnName2 sqlType0 [...]]] + // columnName0 sqlType0 + // [, columnName1 sqlType0 + // [, columnName2 sqlType0 [...]]] // ======================================================================= - - /** - * Returns columnName0 [, columnName1 [AND columnName2 [...]]] - */ - public static String getCreateTableColumnsClause(JDBCCMPFieldBridge[] fields) { + public static String getCreateTableColumnsClause(List fields) { StringBuffer buf = new StringBuffer(); - for(int i=0; i<fields.length; i++) { - if(i!=0) { + + List types = getJDBCTypes(fields); + for(Iterator iter = types.iterator(); iter.hasNext();) { + JDBCType type = (JDBCType)iter.next(); + buf.append(getCreateTableColumnsClause(type)); + if(iter.hasNext()) { buf.append(", "); } - buf.append(getCreateTableColumnsClause(fields[i].getJDBCType())); } return buf.toString(); } - + /** - * Returns columnName0 sqlType0 [, columnName1 sqlType0 [, columnName2 sqlType0 [...]]] - */ - public static String getCreateTableColumnsClause(JDBCCMPFieldBridge field) { + * Returns columnName0 sqlType0 + * [, columnName1 sqlType0 + * [, columnName2 sqlType0 [...]]] + */ + public static String getCreateTableColumnsClause(JDBCFieldBridge field) { return getCreateTableColumnsClause(field.getJDBCType()); } /** - * Returns columnName0 sqlType0 [, columnName1 sqlType0 [, columnName2 sqlType0 [...]]] - */ + * Returns columnName0 sqlType0 + * [, columnName1 sqlType0 + * [, columnName2 sqlType0 [...]]] + */ public static String getCreateTableColumnsClause(JDBCType type) { String[] columnNames = type.getColumnNames(); String[] sqlTypes = type.getSQLTypes(); @@ -65,43 +75,52 @@ // ======================================================================= /** - * Returns columnName0 [, columnName1 [AND columnName2 [...]]] - */ - public static String getColumnNamesClause(JDBCCMPFieldBridge[] fields) { + * Returns columnName0 [, columnName1 [AND columnName2 [...]]] + */ + public static String getColumnNamesClause(List fields) { return getColumnNamesClause(fields, ""); } /** - * Returns columnName0 [, columnName1 [AND columnName2 [...]]] - */ - public static String getColumnNamesClause(JDBCCMPFieldBridge[] fields, String identifier) { + * Returns columnName0 [, columnName1 [AND columnName2 [...]]] + */ + public static String getColumnNamesClause(List fields, String identifier) { StringBuffer buf = new StringBuffer(); - for(int i=0; i<fields.length; i++) { - if(i!=0) { + + List types = getJDBCTypes(fields); + for(Iterator iter = types.iterator(); iter.hasNext();) { + JDBCType type = (JDBCType)iter.next(); + buf.append(getColumnNamesClause(type, identifier)); + if(iter.hasNext()) { buf.append(", "); } - buf.append(getColumnNamesClause(fields[i].getJDBCType(), identifier)); } return buf.toString(); } - + /** - * Returns columnName0 [, columnName1 [, columnName2 [...]]] - */ - public static String getColumnNamesClause(JDBCCMPFieldBridge field) { + * Returns columnName0 [, columnName1 [, columnName2 [...]]] + */ + public static String getColumnNamesClause(JDBCFieldBridge field) { return getColumnNamesClause(field, ""); } /** - * Returns identifier.columnName0 [, identifier.columnName1 [, identifier.columnName2 [...]]] - */ - public static String getColumnNamesClause(JDBCCMPFieldBridge field, String identifier) { + * Returns identifier.columnName0 + * [, identifier.columnName1 + * [, identifier.columnName2 [...]]] + */ + public static String getColumnNamesClause( + JDBCFieldBridge field, String identifier) { + return getColumnNamesClause(field.getJDBCType(), identifier); } /** - * Returns identifier.columnName0 [, identifier.columnName1 [, identifier.columnName2 [...]]] - */ + * Returns identifier.columnName0 + * [, identifier.columnName1 + * [, identifier.columnName2 [...]]] + */ public static String getColumnNamesClause(JDBCType type, String identifier) { if(identifier.length() > 0) { identifier += "."; @@ -125,29 +144,32 @@ // ======================================================================= /** - * Returns columnName0=? [, columnName1=? [, columnName2=? [...]]] - */ - public static String getSetClause(JDBCCMPFieldBridge[] fields) { + * Returns columnName0=? [, columnName1=? [, columnName2=? [...]]] + */ + public static String getSetClause(List fields) { StringBuffer buf = new StringBuffer(); - for(int i=0; i<fields.length; i++) { - if(i!=0) { + + List types = getJDBCTypes(fields); + for(Iterator iter = types.iterator(); iter.hasNext();) { + JDBCType type = (JDBCType)iter.next(); + buf.append(getSetClause(type)); + if(iter.hasNext()) { buf.append(", "); } - buf.append(getSetClause(fields[i].getJDBCType())); } return buf.toString(); } /** - * Returns columnName0=? [, columnName1=? [, columnName2=? [...]]] - */ - public static String getSetClause(JDBCCMPFieldBridge field) { + * Returns columnName0=? [, columnName1=? [, columnName2=? [...]]] + */ + public static String getSetClause(JDBCFieldBridge field) { return getSetClause(field.getJDBCType()); } /** - * Returns columnName0=? [, columnName1=? [, columnName2=? [...]]] - */ + * Returns columnName0=? [, columnName1=? [, columnName2=? [...]]] + */ public static String getSetClause(JDBCType type) { String[] columnNames = type.getColumnNames(); @@ -167,29 +189,32 @@ // ======================================================================= /** - * Returns ? [, ? [, ? [...]]] - */ - public static String getValuesClause(JDBCCMPFieldBridge[] fields) { + * Returns ? [, ? [, ? [...]]] + */ + public static String getValuesClause(List fields) { StringBuffer buf = new StringBuffer(); - for(int i=0; i<fields.length; i++) { - if(i!=0) { + + List types = getJDBCTypes(fields); + for(Iterator iter = types.iterator(); iter.hasNext();) { + JDBCType type = (JDBCType)iter.next(); + buf.append(getValuesClause(type)); + if(iter.hasNext()) { buf.append(", "); } - buf.append(getValuesClause(fields[i].getJDBCType())); } return buf.toString(); } - + /** - * Returns ? [, ? [, ? [...]]] - */ - public static String getValuesClause(JDBCCMPFieldBridge field) { + * Returns ? [, ? [, ? [...]]] + */ + public static String getValuesClause(JDBCFieldBridge field) { return getValuesClause(field.getJDBCType()); } /** - * Returns ? [, ? [, ? [...]]] - */ + * Returns ? [, ? [, ? [...]]] + */ public static String getValuesClause(JDBCType type) { int columnCount = type.getColumnNames().length; @@ -209,43 +234,54 @@ // ======================================================================= /** - * Returns columnName0=? [AND columnName1=? [AND columnName2=? [...]]] - */ - public static String getWhereClause(JDBCCMPFieldBridge[] fields) { + * Returns columnName0=? [AND columnName1=? [AND columnName2=? [...]]] + */ + public static String getWhereClause(List fields) { return getWhereClause(fields, ""); } /** - * Returns identifier.columnName0=? [AND identifier.columnName1=? [AND identifier.columnName2=? [...]]] - */ - public static String getWhereClause(JDBCCMPFieldBridge[] fields, String identifier) { + * Returns identifier.columnName0=? + * [AND identifier.columnName1=? + * [AND identifier.columnName2=? [...]]] + */ + public static String getWhereClause(List fields, String identifier) { StringBuffer buf = new StringBuffer(); - for(int i=0; i<fields.length; i++) { - if(i!=0) { + + List types = getJDBCTypes(fields); + for(Iterator iter = types.iterator(); iter.hasNext();) { + JDBCType type = (JDBCType)iter.next(); + buf.append(getWhereClause(type, identifier)); + if(iter.hasNext()) { buf.append(" AND "); } - buf.append(getWhereClause(fields[i].getJDBCType(), identifier)); } return buf.toString(); } - + /** - * Returns columnName0=? [AND columnName1=? [AND columnName2=? [...]]] - */ - public static String getWhereClause(JDBCCMPFieldBridge field) { + * Returns columnName0=? [AND columnName1=? [AND columnName2=? [...]]] + */ + public static String getWhereClause(JDBCFieldBridge field) { return getWhereClause(field, ""); } /** - * Returns identifier.columnName0=? [AND identifier.columnName1=? [AND identifier.columnName2=? [...]]] - */ - public static String getWhereClause(JDBCCMPFieldBridge field, String identifier) { + * Returns identifier.columnName0=? + * [AND identifier.columnName1=? + * [AND identifier.columnName2=? [...]]] + */ + public static String getWhereClause( + JDBCFieldBridge field, String identifier) { + return getWhereClause(field.getJDBCType(), identifier); } /** - * Returns identifier.columnName0=? [AND identifier.columnName1=? [AND identifier.columnName2=? [...]]] - */ + * Returns identifier.columnName0=? + * [AND identifier.columnName1=? + * [AND identifier.columnName2=? [...]]] + */ public static String getWhereClause(JDBCType type, String identifier) { if(identifier.length() > 0) { identifier += "."; @@ -264,16 +300,53 @@ } /** - * Returns parent.pkColumnName0=child.fkColumnName0 [AND parent.pkColumnName1=child.fkColumnName1 [AND parent.pkColumnName2=child.fkColumnName2 [...]]] - */ - public static String getWhereClause(JDBCCMPFieldBridge pkField, String parent, JDBCCMPFieldBridge fkField, String child) { - return getWhereClause(pkField.getJDBCType(), parent, fkField.getJDBCType(), child); + * Returns parent.pkColumnName0=child.fkColumnName0 + * [AND parent.pkColumnName1=child.fkColumnName1 + * [AND parent.pkColumnName2=child.fkColumnName2 [...]]] + */ + public static String getJoinClause( + JDBCFieldBridge pkField, String parent, + JDBCFieldBridge fkField, String child) { + + return getJoinClause( + pkField.getJDBCType(), parent, + fkField.getJDBCType(), child); } + public static String getJoinClause( + List pkFields, String parent, List fkFields, String child) { + + if(pkFields.size() != fkFields.size()) { + throw new IllegalArgumentException( + "Error createing theta join clause:" + + " pkField.size()="+pkFields.size() + + " fkField.size()="+fkFields.size()); + } + + StringBuffer buf = new StringBuffer(); + + List pkTypes = getJDBCTypes(pkFields); + List fkTypes = getJDBCTypes(fkFields); + Iterator fkIter = fkTypes.iterator(); + for(Iterator pkIter = pkTypes.iterator(); pkIter.hasNext();) { + JDBCType pkType = (JDBCType)pkIter.next(); + JDBCType fkType = (JDBCType)fkIter.next(); + + buf.append(getJoinClause(pkType, parent, fkType, child)); + if(pkIter.hasNext()) { + buf.append(" AND "); + } + } + return buf.toString(); + } + /** - * Returns parent.pkColumnName0=child.fkColumnName0 [AND parent.pkColumnName1=child.fkColumnName1 [AND parent.pkColumnName2=child.fkColumnName2 [...]]] - */ - public static String getWhereClause(JDBCType pkType, String parent, JDBCType fkType, String child) { + * Returns parent.pkColumnName0=child.fkColumnName0 + * [AND parent.pkColumnName1=child.fkColumnName1 + * [AND parent.pkColumnName2=child.fkColumnName2 [...]]] + */ + public static String getJoinClause( + JDBCType pkType, String parent, JDBCType fkType, String child) { if(parent.length() > 0) { parent += "."; } @@ -284,7 +357,8 @@ String[] pkColumnNames = pkType.getColumnNames(); String[] fkColumnNames = fkType.getColumnNames(); if(pkColumnNames.length != fkColumnNames.length) { - throw new IllegalArgumentException("PK and FK have different number of columns"); + throw new IllegalArgumentException( + "PK and FK have different number of columns"); } StringBuffer buf = new StringBuffer(); @@ -299,19 +373,26 @@ return buf.toString(); } - public static String getSelfCompareWhereClause(JDBCCMPFieldBridge[] fields, String fromIdentifier, String toIdentifier) { + public static String getSelfCompareWhereClause( + List fields, String fromIdentifier, String toIdentifier) { StringBuffer buf = new StringBuffer(); - for(int i=0; i<fields.length; i++) { - if(i!=0) { + + List types = getJDBCTypes(fields); + for(Iterator iter = types.iterator(); iter.hasNext();) { + JDBCType type = (JDBCType)iter.next(); + buf.append(getSelfCompareWhereClause( + type, fromIdentifier, toIdentifier)); + if(iter.hasNext()) { buf.append(" AND "); } - buf.append(getSelfCompareWhereClause(fields[i].getJDBCType(), fromIdentifier, toIdentifier)); } return buf.toString(); } - - public static String getSelfCompareWhereClause(JDBCType type, String fromIdentifier, String toIdentifier) { + + public static String getSelfCompareWhereClause( + JDBCType type, String fromIdentifier, String toIdentifier) { + if(fromIdentifier.length() > 0) { fromIdentifier += "."; } @@ -332,4 +413,17 @@ } return buf.toString(); } + + private static List getJDBCTypes(List fields) { + ArrayList types = new ArrayList(fields.size()); + + for(Iterator iter = fields.iterator(); iter.hasNext();) { + JDBCFieldBridge field = (JDBCFieldBridge)iter.next(); + JDBCType type = field.getJDBCType(); + if(type != null && type.getColumnNames().length > 0) { + types.add(type); + } + } + return Collections.unmodifiableList(types); + } }
_______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development