[cvs] expresso commit by rauld: added anyFieldsToInput and inputFields.

JCorporate Ltd jcorp at jcorporate.com
Mon Jan 17 17:44:00 UTC 2005


Log Message:
-----------
added anyFieldsToInput and inputFields. Refactored createAndExecuteSearch() to make use of createSQLSelectStatement().
Contributed by Yves Henri Amaizo

Modified Files:
--------------
    expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dataobjects/jdbc:
        JDBCDataObject.java

Revision Data
-------------
Index: JDBCDataObject.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dataobjects/jdbc/JDBCDataObject.java,v
retrieving revision 1.45
retrieving revision 1.46
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/dataobjects/jdbc/JDBCDataObject.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/dataobjects/jdbc/JDBCDataObject.java -u -r1.45 -r1.46
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dataobjects/jdbc/JDBCDataObject.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dataobjects/jdbc/JDBCDataObject.java
@@ -169,6 +169,13 @@
     public HashMap retrieveFields = null;
 
     /**
+    * The actual fields inserted or updated
+    * Modify by Yves Henri AMAIZO <amy_amaizo at compuserve.com>
+    */
+    public HashMap inputFields = null;
+
+
+    /**
      * DBObjects themselves do not contain the "meta data" or the definition of
      * what fields they contain and other information. Instead, a DBObjectRef object
      * (one per TYPE of DBObject) contains this info and is looked up as needed.
@@ -188,6 +195,15 @@
 
 
     /**
+    *  If setFieldsToInput has been used to specify fields which will be
+    * insert or update by the next query.
+    * Modify by Yves Henri AMAIZO <amy_amaizo at compuserve.com>
+    */
+    protected boolean anyFieldsToInput = false;
+
+
+
+    /**
      * Local connection that we use if it's initialized, but
      * if it's null we generate our own connection
      */
@@ -752,7 +768,6 @@
      */
     public DBConnection createAndExecuteSearch(java.util.ArrayList retrievedFieldList)
             throws DBException {
-        boolean needComma = false;
 
         if (recordSet == null) {
             recordSet = new ArrayList();
@@ -763,154 +778,239 @@
         myUpdates = null;
 
         DBConnection myConnection = null;
-        JDBCObjectMetaData myMetadata = this.getJDBCMetaData();
-        FastStringBuffer myStatement = FastStringBuffer.getInstance();
         try {
-            if (localConnection != null) {
-                myConnection = localConnection;
-            } else {
-                myConnection = this.getConnectionPool().getConnection(this.myClassName);
-            }
-
-            myStatement.append("SELECT ");
-
-            if (myConnection.getLimitationPosition() == DBConnection.LIMITATION_AFTER_SELECT &&
-                    (offsetRecord > 0 || maxRecords > 0)) {
-
-                // Insert limitation stub after table nomination
-                String limitStub = makeLimitationStub(myConnection);
+          if (localConnection != null) {
+            myConnection = localConnection;
+          }
+          else {
+            myConnection = this.getConnectionPool().getConnection(this.myClassName);
+          }
 
-                myStatement.append(" ");
-                myStatement.append(limitStub);
-                myStatement.append(" ");
+          myConnection.execute(createSQLSelectStatement(retrievedFieldList,myConnection));
+          return myConnection;
+        } catch (DBException ex) {
+            if (myConnection != null && localConnection == null) {
+                myConnection.release();
             }
+            log.error("Error building and executing search statement", ex);
+            throw ex;
+        }
+    }
 
-            if (anyFieldsDistinct) {
-                String oneFieldName = null;
-                myStatement.append(" ");
-                myStatement.append(getConnectionPool().getDistinctRowsetKeyword());
-                myStatement.append(" ");
-
-                for (Iterator i = getDistinctFieldArrayList().iterator();
-                     i.hasNext();) {
-                    oneFieldName = (String) i.next();
-                    retrievedFieldList.add(oneFieldName);
-
-                    if (needComma) {
-                        myStatement.append(", ");
-                    }
-
-                    myStatement.append(selectFieldString(oneFieldName));
-                    needComma = true;
-                }
-            } else if (anyFieldsToRetrieve) { /* for each distinct field */
-                String oneFieldName = null;
-
-                for (Iterator i = getFieldsToRetrieveIterator(); i.hasNext();) {
-                    oneFieldName = (String) i.next();
-
-                    if (needComma) {
-                        myStatement.append(", ");
-                    }
-
-                    retrievedFieldList.add(oneFieldName);
-                    myStatement.append(selectFieldString(oneFieldName));
-                    needComma = true;
-                }
-            } else { /* for each field */
-                for (Iterator i = myMetadata.getAllFieldsMap().values().iterator();
-                     i.hasNext();) {
-                    DBField oneField = (DBField) i.next();
-
-                    if (!oneField.isVirtual() && !oneField.isBinaryObjectType()) {
-                        if (needComma) {
-                            myStatement.append(", ");
-                        }
-
-                        retrievedFieldList.add(oneField.getName());
-                        myStatement.append(selectFieldString(oneField.getName()));
-                        needComma = true;
-                    } /* if field is not virtual */
-
-                } /* for each field */
-
-            } /* else this is a regular (non-distinct) search */
-
-            myStatement.append(" FROM ");
-            myStatement.append(myMetadata.getTargetSQLTable(this.getDataContext()));
-            if (myConnection.getLimitationPosition() == DBConnection.LIMITATION_AFTER_TABLE &&
-                    (offsetRecord > 0 || maxRecords > 0)) {
-
-                // Insert limitation stub after table nomination
-                String limitStub = makeLimitationStub(myConnection);
-                myStatement.append(" ");
-                myStatement.append(limitStub);
-                myStatement.append(" ");
-            }
+    /**
+    * Create the SQL select statement to be executed if the if searchAndRetrieveList is
+    * invoked
+    * The use of this method is when building complex custom WHERE clauses
+    * with nested queries one can call it to get the nested select built dynamically
+    *
+    * SIDE-EFFECT: custom 'where' clause is set to null.
+    *
+    * @param retrievedFieldList instantiate an ArrayList and the function will
+    * fill out the field order that the SQL statement will have it's fields in.
+    * @throws DBException upon database communication error
+    * @return String  that has contains the select statement prepared.
+    *
+    * Created by Yves Henri AMAIZO <amy_amaizo at compuserve.com>
+    * @since $DatabaseSchema  $Date$
+    */
+    public String createSQLSelectStatement(java.util.ArrayList retrievedFieldList)
+                throws DBException
+      {
+        DBConnection myConnection = null;
+        try{
+          if (localConnection != null) {
+            myConnection = localConnection;
+          }
+          else {
+            myConnection = this.getConnectionPool().getConnection(this.
+                myClassName);
+          }
+          return createSQLSelectStatement(retrievedFieldList, myConnection);
+        }
+        finally{
+          if (myConnection != null && localConnection == null) {
+            myConnection.release();
+          }
+        }
+      }
 
-            String whereClause;
 
-            if (customWhereClause != null) {
-                if (appendCustomWhere) {
-                    whereClause = buildWhereClause(true) + customWhereClause;
-                    appendCustomWhere = false;
-                } else {
-                    whereClause = customWhereClause;
-                }
-                myStatement.append(whereClause);
-                customWhereClause = null;
-            } else {
-                whereClause = buildWhereClause(true);
-                myStatement.append(whereClause);
-            }
-            if (myConnection.getLimitationPosition() == DBConnection.LIMITATION_AFTER_WHERE &&
-                    (offsetRecord > 0 || maxRecords > 0)) {
+    /**
+    * Create the SQL select statement to be executed if the if searchAndRetrieveList is
+    * invoked
+    * The use of this method is when building complex custom WHERE clauses
+    * with nested queries one can call it to get the nested select built dynamically
+    *
+    * SIDE-EFFECT: custom 'where' clause is set to null.
+    *
+    * @param retrievedFieldList instantiate an ArrayList and the function will
+    * fill out the field order that the SQL statement will have it's fields in.
+    * @throws DBException upon database communication error
+    * @return String  that has contains the select statement prepared.
+    *
+    * Created by Yves Henri AMAIZO <amy_amaizo at compuserve.com>
+    * @since $DatabaseSchema  $Date$
+    */
+    public String createSQLSelectStatement(java.util.ArrayList retrievedFieldList,DBConnection myConnection)
+                throws DBException
+      {
+        boolean needComma = false;
+        JDBCObjectMetaData myMetadata = this.getJDBCMetaData();
+        FastStringBuffer myStatement = FastStringBuffer.getInstance();
+        try {
 
-                // Insert limitation stub after table nomination
-                String limitStub = makeLimitationStub(myConnection);
+          myStatement.append("SELECT ");
 
-                if (whereClause.length() > 0) {
-                    myStatement.append(" AND");
+          if (myConnection.getLimitationPosition() ==
+              DBConnection.LIMITATION_AFTER_SELECT &&
+              (offsetRecord > 0 || maxRecords > 0)) {
+
+            // Insert limitation stub after table nomination
+            String limitStub = makeLimitationStub(myConnection);
+
+            myStatement.append(" ");
+            myStatement.append(limitStub);
+            myStatement.append(" ");
+          }
+
+          if (anyFieldsDistinct) {
+            String oneFieldName = null;
+            myStatement.append(" ");
+            myStatement.append(getConnectionPool().getDistinctRowsetKeyword());
+            myStatement.append(" ");
+
+            for (Iterator i = getDistinctFieldArrayList().iterator();
+                 i.hasNext(); ) {
+              oneFieldName = (String) i.next();
+              retrievedFieldList.add(oneFieldName);
+
+              if (needComma) {
+                myStatement.append(", ");
+              }
+
+              myStatement.append(selectFieldString(oneFieldName));
+              needComma = true;
+            }
+          }
+          else if (anyFieldsToRetrieve) { /* for each distinct field */
+            String oneFieldName = null;
+
+            for (Iterator i = getFieldsToRetrieveIterator(); i.hasNext(); ) {
+              oneFieldName = (String) i.next();
+
+              if (needComma) {
+                myStatement.append(", ");
+              }
+
+              retrievedFieldList.add(oneFieldName);
+              myStatement.append(selectFieldString(oneFieldName));
+              needComma = true;
+            }
+          }
+          else { /* for each field */
+            for (Iterator i = myMetadata.getAllFieldsMap().values().iterator();
+                 i.hasNext(); ) {
+              DBField oneField = (DBField) i.next();
+
+              if (!oneField.isVirtual() && !oneField.isBinaryObjectType()) {
+                if (needComma) {
+                  myStatement.append(", ");
                 }
 
-                myStatement.append(" ");
-                myStatement.append(limitStub);
-                myStatement.append(" ");
-            }
-            /* Add the ORDER BY clause if any sortKeys are specified */
-            if (sortKeys != null && sortKeys.size() > 0) {
-                myStatement.append(" ORDER BY ");
-
-                boolean needComma2 = false;
-
-                for (Iterator i = sortKeys.iterator(); i.hasNext();) {
-                    if (needComma2) {
-                        myStatement.append(", ");
-                    }
+                retrievedFieldList.add(oneField.getName());
+                myStatement.append(selectFieldString(oneField.getName()));
+                needComma = true;
+              }
+              /* if field is not virtual */
+
+            }
+            /* for each field */
+
+          }
+          /* else this is a regular (non-distinct) search */
+
+          myStatement.append(" FROM ");
+          myStatement.append(myMetadata.getTargetSQLTable(this.getDataContext()));
+          if (myConnection.getLimitationPosition() ==
+              DBConnection.LIMITATION_AFTER_TABLE &&
+              (offsetRecord > 0 || maxRecords > 0)) {
+
+            // Insert limitation stub after table nomination
+            String limitStub = makeLimitationStub(myConnection);
+            myStatement.append(" ");
+            myStatement.append(limitStub);
+            myStatement.append(" ");
+          }
+
+          String whereClause;
+
+          if (customWhereClause != null) {
+            if (appendCustomWhere) {
+              whereClause = buildWhereClause(true) + customWhereClause;
+              appendCustomWhere = false;
+            }
+            else {
+              whereClause = customWhereClause;
+            }
+            myStatement.append(whereClause);
+            customWhereClause = null;
+          }
+          else {
+            whereClause = buildWhereClause(true);
+            myStatement.append(whereClause);
+          }
+          if (myConnection.getLimitationPosition() ==
+              DBConnection.LIMITATION_AFTER_WHERE &&
+              (offsetRecord > 0 || maxRecords > 0)) {
+
+            // Insert limitation stub after table nomination
+            String limitStub = makeLimitationStub(myConnection);
+
+            if (whereClause.length() > 0) {
+              myStatement.append(" AND");
+            }
+
+            myStatement.append(" ");
+            myStatement.append(limitStub);
+            myStatement.append(" ");
+          }
+          /* Add the ORDER BY clause if any sortKeys are specified */
+          if (sortKeys != null && sortKeys.size() > 0) {
+            myStatement.append(" ORDER BY ");
+
+            boolean needComma2 = false;
+
+            for (Iterator i = sortKeys.iterator(); i.hasNext(); ) {
+              if (needComma2) {
+                myStatement.append(", ");
+              }
+
+              myStatement.append( (String) i.next());
+              needComma2 = true;
+            }
+          }
+
+          if (myConnection.getLimitationPosition() ==
+              DBConnection.LIMITATION_AFTER_ORDER_BY
+              && (offsetRecord > 0 || maxRecords > 0)) {
+            myStatement.append(" ");
+            myStatement.append(makeLimitationStub(myConnection));
+          }
 
-                    myStatement.append((String) i.next());
-                    needComma2 = true;
-                }
-            }
-
-            if (myConnection.getLimitationPosition() == DBConnection.LIMITATION_AFTER_ORDER_BY
-                    && (offsetRecord > 0 || maxRecords > 0)) {
-                myStatement.append(" ");
-                myStatement.append(makeLimitationStub(myConnection));
-            }
+          return myStatement.toString();
 
-            myConnection.execute(myStatement.toString());
-            return myConnection;
         } catch (DBException ex) {
-            if (myConnection != null && localConnection == null) {
-                myConnection.release();
-            }
-            log.error("Error building and executing search statement", ex);
+          if (myConnection != null && localConnection == null) {
+            myConnection.release();
+          }
+          log.error("Error building and executing search statement", ex);
             throw ex;
-        } finally {
+          } finally {
             myStatement.release();
+          }
+
         }
-    }
+
 
     /**
      * Fills the given constructed data object with data from the connection
@@ -1083,6 +1183,28 @@
 
         return retrieveFields.keySet().iterator();
     }
+
+    /**
+     * Get a special <code>Iterator</code> object list of all
+     * of the fields in this object that are set to <b>input</b>
+     * <p>Author Yves henri Amaizo <amy_amaizo at compuserve.com></p>
+     * @return    An Iterator of all the
+     * <b>retrieve</b> fieldNames in this object
+     *
+     * @throws DBException If the list cannot be input
+     *
+     */
+    public Iterator getFieldsToInputIterator()
+        throws DBException {
+
+      //Do a dummy so we don't throw null pointer exceptions
+      if (inputFields == null) {
+        return new HashMap().keySet().iterator();
+      }
+
+      return inputFields.keySet().iterator();
+    }
+
 
     /**
      * Build and return a string consisting of an SQL 'where' clause


More information about the cvs mailing list