You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by Bill Schneider <bs...@vecna.com> on 2002/02/07 20:13:59 UTC
[PATCH] BasePeer.java
this patch factors out common code from BasePeer.createQuery and
createPreparedStatement 'cause they're almost identical.
hope you find this useful.
-- Bill
Index: src/java/org/apache/torque/util/BasePeer.java
===================================================================
RCS file:
/home/cvspublic/jakarta-turbine-torque/src/java/org/apache/torque/util/BasePeer.java,v
retrieving revision 1.22
diff -u -r1.22 BasePeer.java
--- src/java/org/apache/torque/util/BasePeer.java 26 Jan 2002 02:32:29 -0000 1.22
+++ src/java/org/apache/torque/util/BasePeer.java 7 Feb 2002 19:11:16 -0000
@@ -943,8 +943,21 @@
*
* @param criteria A Criteria.
* @exception Exception Trouble creating the query string.
+ * @return a SQL query based on input Criteria
*/
- public static String createQueryString( Criteria criteria )
+ public static String createQueryString( Criteria criteria )
+ throws Exception {
+ return createQueryString(criteria, null, null);
+ }
+
+ /**
+ * Internal helper function used by createQueryString and
+ * createPreparedStatement.
+ */
+ private static String createQueryString( Criteria criteria,
+
StringBuffer queryString,
+
List params )
+
throws Exception
{
Query query = new Query();
@@ -1049,8 +1062,16 @@
}
criterion.setDB(db);
- whereClause.add( criterion.toString() );
-
+
if (params == null) {
+
// just get the criterion as a string with values inline
+
whereClause.add( criterion.toString() );
+
} else {
+
// prepared statement: use bind vars ('?') in query string
+
// and put values in the params list
+
StringBuffer sb = new StringBuffer();
+
criterion.appendPsTo (sb,params);
+
whereClause.add( sb.toString() );
+
}
}
List join = criteria.getJoinL();
@@ -1176,6 +1197,9 @@
String sql = query.toString();
category.debug(sql);
+
if (queryString != null) {
+
queryString.append(sql);
+
}
return sql;
}
@@ -2060,244 +2084,17 @@
/**
* Create a new PreparedStatement. It builds a string representation
* of a query and a list of PreparedStatement parameters.
+ * @param criteria the criteria for the prepared statement
+ * @param queryString the StringBuffer that the query string will be
+ * appended to, with bind variable markers ('?') in place of values
+ * @param params a List that will contain the bind variable
+ * values from the criteria, corresponding to the bind variables
+ * in the query string.
*/
public static void createPreparedStatement(Criteria criteria,
StringBuffer queryString,
- List params)
- throws Exception
- {
- DB db = Torque.getDB( criteria.getDbName() );
- DatabaseMap dbMap = Torque.getDatabaseMap( criteria.getDbName() );
-
- Query query = new Query();
-
- StringStack selectModifiers = query.getSelectModifiers();
- StringStack selectClause = query.getSelectClause();
- StringStack fromClause = query.getFromClause();
- StringStack whereClause = query.getWhereClause();
- StringStack orderByClause = query.getOrderByClause();
-
- StringStack orderBy = criteria.getOrderByColumns();
- boolean ignoreCase = criteria.isIgnoreCase();
- StringStack select = criteria.getSelectColumns();
- Hashtable aliases = criteria.getAsColumns();
- StringStack modifiers = criteria.getSelectModifiers();
-
- for (int i=0; i<modifiers.size(); i++)
- {
- selectModifiers.add( modifiers.get(i) );
- }
-
- for (int i=0; i<modifiers.size(); i++)
- {
- selectModifiers.add( modifiers.get(i) );
- }
-
- for (int i=0; i<select.size(); i++)
- {
- String columnName = select.get(i);
- String tableName = null;
- selectClause.add(columnName);
- int parenPos = columnName.indexOf('(');
- if (parenPos == -1)
- {
- tableName = columnName.substring(0,
- columnName.indexOf('.') );
- }
- else
- {
- tableName = columnName.substring(parenPos + 1,
- columnName.indexOf('.') );
- }
- String tableName2 = criteria.getTableForAlias(tableName);
- if ( tableName2 != null )
- {
- fromClause.add(
- new StringBuffer(tableName.length() +
- tableName2.length() + 1)
- .append(tableName2).append(' ').append(tableName)
- .toString() );
- }
- else
- {
- fromClause.add(tableName);
- }
- }
-
-
- Iterator it = aliases.keySet().iterator();
- while(it.hasNext())
- {
- String key = (String)it.next();
- selectClause.add((String)aliases.get(key) + " AS " + key);
- }
-
- Enumeration e = criteria.keys();
- while (e.hasMoreElements())
- {
- String key = (String)e.nextElement();
- Criteria.Criterion criterion =
- (Criteria.Criterion)criteria.getCriterion(key);
- Criteria.Criterion[] someCriteria =
- criterion.getAttachedCriterion();
-
- String table = null;
- for (int i=0; i<someCriteria.length; i++)
- {
- String tableName = someCriteria[i].getTable();
- table = criteria.getTableForAlias(tableName);
- if ( table != null )
- {
- fromClause.add(
- new StringBuffer(tableName.length() +
- table.length() + 1)
- .append(table).append(' ').append(tableName)
- .toString() );
- }
- else
- {
- fromClause.add(tableName);
- table = tableName;
- }
-
- boolean ignorCase = ((criteria.isIgnoreCase() ||
- someCriteria[i].isIgnoreCase()) &&
- (dbMap.getTable(table).getColumn(
- someCriteria[i].getColumn()).getType() instanceof
String));
-
- someCriteria[i].setIgnoreCase(ignorCase);
- }
-
- criterion.setDB(db);
- StringBuffer sb = new StringBuffer();
- criterion.appendPsTo (sb,params);
- whereClause.add( sb.toString() );
-
- }
-
- List join = criteria.getJoinL();
- if ( join != null)
- {
- for ( int i=0; i<join.size(); i++ )
- {
- String join1 = (String)join.get(i);
- String join2 = (String)criteria.getJoinR().get(i);
-
- String tableName = join1.substring(0, join1.indexOf('.'));
- String table = criteria.getTableForAlias(tableName);
- if ( table != null )
- {
- fromClause.add(
- new StringBuffer(tableName.length() +
- table.length() + 1)
- .append(table).append(' ').append(tableName)
- .toString() );
- }
- else
- {
- fromClause.add(tableName);
- }
-
- int dot = join2.indexOf('.');
- tableName = join2.substring(0, dot);
- table = criteria.getTableForAlias(tableName);
- if ( table != null )
- {
- fromClause.add(
- new StringBuffer(tableName.length() +
- table.length() + 1)
- .append(table).append(' ').append(tableName)
- .toString() );
- }
- else
- {
- fromClause.add(tableName);
- table = tableName;
- }
-
- boolean ignorCase = (criteria.isIgnoreCase() &&
- (dbMap.getTable(table).getColumn(
- join2.substring(dot+1, join2.length()) )
- .getType() instanceof String));
-
- whereClause.add(
- SqlExpression.buildInnerJoin(join1, join2,
- ignorCase, db) );
- }
- }
-
- if ( orderBy != null && orderBy.size() > 0)
- {
- // Check for each String/Character column and apply
- // toUpperCase().
- for (int i=0; i<orderBy.size(); i++)
- {
- String orderByColumn = orderBy.get(i);
- String table =
orderByColumn.substring(0,orderByColumn.indexOf('.') );
- // See if there's a space (between the column list and sort
- // order in ORDER BY table.column DESC).
- int spacePos = orderByColumn.indexOf(' ');
- String columnName;
- if (spacePos == -1)
- columnName =
orderByColumn.substring(orderByColumn.indexOf('.') + 1);
- else
- columnName =
orderByColumn.substring(orderByColumn.indexOf('.') + 1, spacePos);
- ColumnMap column = dbMap.getTable(table).getColumn(
columnName );
- if ( column.getType() instanceof String )
- {
- if (spacePos == -1)
- orderByClause.add(
db.ignoreCaseInOrderBy(orderByColumn) );
- else
- orderByClause.add(
db.ignoreCaseInOrderBy(orderByColumn.substring(0, spacePos)) +
orderByColumn.substring(spacePos) );
- }
- else
- {
- orderByClause.add(orderByColumn);
- }
- }
- }
-
- // Limit the number of rows returned.
- int limit = criteria.getLimit();
- int offset = criteria.getOffset();
- String limitString = null;
- if ( offset > 0 && db.supportsNativeOffset() )
- {
- switch(db.getLimitStyle())
- {
- case DB.LIMIT_STYLE_MYSQL:
- limitString = new StringBuffer().append(offset)
- .append(", ")
- .append(limit)
- .toString();
- break;
- case DB.LIMIT_STYLE_POSTGRES:
- limitString = new StringBuffer().append(limit)
- .append(", ")
- .append(offset)
- .toString();
- break;
- }
-
- // Now set the criteria's limit and offset to return the
- // full resultset since the results are limited on the
- // server.
- criteria.setLimit(-1);
- criteria.setOffset(0);
- }
- else if (limit > 0 && db.supportsNativeLimit() )
- {
- limitString = String.valueOf(limit);
-
- // Now set the criteria's limit to return the full
- // resultset since the results are limited on the server.
- criteria.setLimit(-1);
- }
-
- if (limitString != null) query.setLimit(limitString);
-
- String sql = query.toString();
- category.debug(sql);
- queryString.append (sql);
+ List params)
+ throws Exception{
+ createQueryString(criteria, queryString, params);
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [PATCH] BasePeer.java
Posted by Jon Scott Stevens <jo...@latchkey.com>.
on 2/7/02 11:13 AM, "Bill Schneider" <bs...@vecna.com> wrote:
> this patch factors out common code from BasePeer.createQuery and
> createPreparedStatement 'cause they're almost identical.
>
> hope you find this useful.
>
> -- Bill
It is useful, but please follow the coding standards.
http://jakarta.apache.org/turbine/common/code-standards.html
-jon
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>