[cvs] expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj MultiDBObject.java

Larry Hamel lhamel at jcorp2.servlets.net
Tue May 4 15:15:56 PDT 2004


Update of /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj
In directory jcorp2.servlets.net:/tmp/cvs-serv28436/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj

Modified Files:
	MultiDBObject.java 
Log Message:
add ability to have aliases for joins


Index: MultiDBObject.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/MultiDBObject.java,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -d -r1.41 -r1.42
*** MultiDBObject.java	8 Mar 2004 20:39:42 -0000	1.41
--- MultiDBObject.java	4 May 2004 22:15:54 -0000	1.42
***************
*** 67,70 ****
--- 67,71 ----
  import com.jcorporate.expresso.core.controller.ControllerRequest;
  import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData;
+ import com.jcorporate.expresso.core.dataobjects.DataException;
  import com.jcorporate.expresso.core.dataobjects.jdbc.FieldRangeParser;
  import com.jcorporate.expresso.core.db.DBConnection;
***************
*** 167,170 ****
--- 168,176 ----
  
      /**
+      * Constant for shortName DBObject attribute
+      */
+     protected static final String SHORT_NAME = "shortName";
+ 
+     /**
       * 'FROM' clause formed by calls to setJoin().
       */
***************
*** 172,175 ****
--- 178,187 ----
  
      /**
+      * If set to true, the shortname for the DBObject will be used as an alias
+      * in the query; defaults to false for backward-compatiblity
+      */
+     private boolean shortNameAsAlias = false;
+ 
+     /**
       * Return an "attribute". Attributes are temporary (e.g. not stored in the DBMS)
       * values associated with this particular DB object instance.
***************
*** 351,355 ****
                      }
  
!                     fsb.append(oneObj.getJDBCMetaData().getTargetTable());
                      needComma = true;
                  }
--- 363,372 ----
                      }
  
!                     String targetTable = oneObj.getJDBCMetaData().getTargetTable();
!                     String tableName = getTableName(oneObj);
!                     fsb.append(targetTable);
!                     if (!targetTable.equals(tableName)) {
!                         fsb.append(" AS " + tableName);
!                     }
                      needComma = true;
                  }
***************
*** 371,375 ****
                      }
  
!                     fsb.append(oneObj.getJDBCMetaData().getTargetTable());
                      needComma = true;
                  }
--- 388,397 ----
                      }
  
!                     String targetTable = oneObj.getJDBCMetaData().getTargetTable();
!                     String tableName = getTableName(oneObj);
!                     fsb.append(targetTable);
!                     if (!targetTable.equals(tableName)) {
!                         fsb.append(" AS " + tableName);
!                     }
                      needComma = true;
                  }
***************
*** 389,392 ****
--- 411,415 ----
      // ----------------------------------------------------
  
+ 
      /**
       * Just like find, but only retrieves the count, not the records themselves.
***************
*** 395,399 ****
       * @throws DBException If the search could not be completed
       */
!     public synchronized int count()
              throws DBException {
  //        boolean needComma = false;
--- 418,422 ----
       * @throws DBException If the search could not be completed
       */
!     public synchronized int count(String expr)
              throws DBException {
  //        boolean needComma = false;
***************
*** 487,491 ****
                  myStatement.append(" DISTINCT ");
              }
!             myStatement.append("COUNT(*) ");
  
              // ----------------------------------------------------
--- 510,515 ----
                  myStatement.append(" DISTINCT ");
              }
!             if (StringUtil.isBlankOrNull(expr)) expr = "*";
!             myStatement.append("COUNT(" + expr + ") ");
  
              // ----------------------------------------------------
***************
*** 569,572 ****
--- 593,607 ----
      } /* count() */
  
+     /**
+      * Just like find, but only retrieves the count, not the records themselves.
+      *
+      * @return integer Count of the records matching the criteria
+      * @throws DBException If the search could not be completed
+      */
+     public synchronized int count()
+             throws DBException {
+         return count("");
+     } /* count() */
+ 
  
      /**
***************
*** 746,750 ****
              throws DBException {
          StringUtil.assertNotBlank(shortName, "Short name cannot be blank here");
!         oneDBObj.setAttribute("shortName", shortName);
          myDBObjects.put(shortName, oneDBObj);
  
--- 781,785 ----
              throws DBException {
          StringUtil.assertNotBlank(shortName, "Short name cannot be blank here");
!         oneDBObj.setAttribute(SHORT_NAME, shortName);
          myDBObjects.put(shortName, oneDBObj);
  
***************
*** 835,839 ****
                   eachObj.hasMoreElements();) {
                  oneObj = (DBObject) eachObj.nextElement();
!                 byTableName.put(oneObj.getJDBCMetaData().getTargetTable(), oneObj);
  
                  // ----------------------------------------------------
--- 870,874 ----
                   eachObj.hasMoreElements();) {
                  oneObj = (DBObject) eachObj.nextElement();
!                 byTableName.put(getTableName(oneObj), oneObj);
  
                  // ----------------------------------------------------
***************
*** 847,851 ****
                  //    In this case we ignore the current DBObject, if it is a
                  //    member of ignoreInWhereClause.
!                 String thisDbo = (String) oneObj.getAttribute("shortName");
  
                  if (dboAlias.trim().length() > 0) {
--- 882,886 ----
                  //    In this case we ignore the current DBObject, if it is a
                  //    member of ignoreInWhereClause.
!                 String thisDbo = (String) oneObj.getAttribute(SHORT_NAME);
  
                  if (dboAlias.trim().length() > 0) {
***************
*** 863,867 ****
  
                      if (!oneObj.isVirtual(fieldName)) {
!                         fields.addElement(oneObj.getJDBCMetaData().getTargetTable() + "." +
                                  fieldName);
                      }
--- 898,902 ----
  
                      if (!oneObj.isVirtual(fieldName)) {
!                         fields.addElement(getTableName(oneObj) + "." +
                                  fieldName);
                      }
***************
*** 883,887 ****
                  //    In this case we ignore the current DBObject, if it is a
                  //    member of ignoreInWhereClause.
!                 String thisDbo = (String) oneObj.getAttribute("shortName");
  
                  if (dboAlias.trim().length() > 0) {
--- 918,922 ----
                  //    In this case we ignore the current DBObject, if it is a
                  //    member of ignoreInWhereClause.
!                 String thisDbo = (String) oneObj.getAttribute(SHORT_NAME);
  
                  if (dboAlias.trim().length() > 0) {
***************
*** 900,904 ****
  
                      if (!oneObj.isVirtual(fieldName)) {
!                         fields.addElement(oneObj.getJDBCMetaData().getTargetTable() + "." +
                                  fieldName);
                      }
--- 935,939 ----
  
                      if (!oneObj.isVirtual(fieldName)) {
!                         fields.addElement(getTableName(oneObj) + "." +
                                  fieldName);
                      }
***************
*** 1014,1018 ****
                  if (oneObj.containsWildCards(oneFieldValue)) {
                      if (caseSensitiveQuery) {
!                         myStatement.append(oneObj.getJDBCMetaData().getTargetTable() + "." +
                                  oneFieldName);
                          myStatement.append(" LIKE ");
--- 1049,1053 ----
                  if (oneObj.containsWildCards(oneFieldValue)) {
                      if (caseSensitiveQuery) {
!                         myStatement.append(getTableName(oneObj) + "." +
                                  oneFieldName);
                          myStatement.append(" LIKE ");
***************
*** 1020,1024 ****
                      } else {
                          myStatement.append("UPPER(");
!                         myStatement.append(oneObj.getJDBCMetaData().getTargetTable() + "." +
                                  oneFieldName);
                          myStatement.append(") LIKE ");
--- 1055,1059 ----
                      } else {
                          myStatement.append("UPPER(");
!                         myStatement.append(getTableName(oneObj) + "." +
                                  oneFieldName);
                          myStatement.append(") LIKE ");
***************
*** 1026,1030 ****
                      }
                  } else if (rangeString != null) {
!                     myStatement.append(oneObj.getJDBCMetaData().getTargetTable() + "." +
                              oneFieldName);
                      myStatement.append(" " + rangeString + " ");
--- 1061,1065 ----
                      }
                  } else if (rangeString != null) {
!                     myStatement.append(getTableName(oneObj) + "." +
                              oneFieldName);
                      myStatement.append(" " + rangeString + " ");
***************
*** 1032,1036 ****
                  } else {
                      if (caseSensitiveQuery) {
!                         myStatement.append(oneObj.getJDBCMetaData().getTargetTable() + "." +
                                  oneFieldName);
                          myStatement.append(" = ");
--- 1067,1071 ----
                  } else {
                      if (caseSensitiveQuery) {
!                         myStatement.append(getTableName(oneObj) + "." +
                                  oneFieldName);
                          myStatement.append(" = ");
***************
*** 1038,1042 ****
                      } else {
                          myStatement.append("UPPER(");
!                         myStatement.append(oneObj.getJDBCMetaData().getTargetTable() + "." +
                                  oneFieldName);
                          myStatement.append(") = ");
--- 1073,1077 ----
                      } else {
                          myStatement.append("UPPER(");
!                         myStatement.append(getTableName(oneObj) + "." +
                                  oneFieldName);
                          myStatement.append(") = ");
***************
*** 1414,1421 ****
                  oneObj = (DBObject) eachObj.nextElement();
  
!                 ArrayList retrievedFieldList = (ArrayList) rtrvListByTable.get(oneObj.getJDBCMetaData().getTargetTable());
                  if (retrievedFieldList == null) {
                      retrievedFieldList = new ArrayList();
!                     rtrvListByTable.put(oneObj.getJDBCMetaData().getTargetTable(), retrievedFieldList);
                  }
  
--- 1449,1456 ----
                  oneObj = (DBObject) eachObj.nextElement();
  
!                 ArrayList retrievedFieldList = (ArrayList) rtrvListByTable.get(getTableName(oneObj));
                  if (retrievedFieldList == null) {
                      retrievedFieldList = new ArrayList();
!                     rtrvListByTable.put(getTableName(oneObj), retrievedFieldList);
                  }
  
***************
*** 1620,1624 ****
                      oneObj = (DBObject) eachObj.nextElement();
  
!                     ArrayList retrievedFieldList = (ArrayList) rtrvListByTable.get(oneObj.getJDBCMetaData().getTargetTable());
                      // The following should never happen .... but CYA
                      if (retrievedFieldList == null) {
--- 1655,1659 ----
                      oneObj = (DBObject) eachObj.nextElement();
  
!                     ArrayList retrievedFieldList = (ArrayList) rtrvListByTable.get(getTableName(oneObj));
                      // The following should never happen .... but CYA
                      if (retrievedFieldList == null) {
***************
*** 1637,1641 ****
                                  throw new DBException(myName +
                                          ":Error retrieving field '" +
!                                         oneObj.getJDBCMetaData().getTargetTable() +
                                          "." + fieldName + "'",
                                          de);
--- 1672,1676 ----
                                  throw new DBException(myName +
                                          ":Error retrieving field '" +
!                                         getTableName(oneObj) +
                                          "." + fieldName + "'",
                                          de);
***************
*** 1646,1654 ****
                              if (log.isDebugEnabled()) {
                                  log.debug("Setting " +
!                                         oneObj.getJDBCMetaData().getTargetTable() + "." +
                                          fieldName + " to " + oneFieldValue);
                              }
  
!                             myObj.setField((String) oneObj.getAttribute("shortName"),
                                      fieldName, oneFieldValue);
                          }
--- 1681,1689 ----
                              if (log.isDebugEnabled()) {
                                  log.debug("Setting " +
!                                         getTableName(oneObj) + "." +
                                          fieldName + " to " + oneFieldValue);
                              }
  
!                             myObj.setField((String) oneObj.getAttribute(SHORT_NAME),
                                      fieldName, oneFieldValue);
                          }
***************
*** 1912,1917 ****
          }
  
!         relations.addElement(foreignDBObj.getJDBCMetaData().getTargetTable() + "." + foreignKey +
!                 " = " + primaryDBObj.getJDBCMetaData().getTargetTable() + "." +
                  primaryKey);
          originalRelations.addElement(shortName + "|" + foreignKey + "|" +
--- 1947,1952 ----
          }
  
!         relations.addElement(getTableName(foreignDBObj) + "." + foreignKey +
!                 " = " + getTableName(primaryDBObj) + "." +
                  primaryKey);
          originalRelations.addElement(shortName + "|" + foreignKey + "|" +
***************
*** 2055,2059 ****
  
              if (fromClause == null) {
!                 s.append(leftDBObj.getJDBCMetaData().getTargetTable());
              } else {
                  s.append(fromClause);
--- 2090,2094 ----
  
              if (fromClause == null) {
!                 s.append(getTableName(leftDBObj));
              } else {
                  s.append(fromClause);
***************
*** 2084,2090 ****
              }
  
!             s.append(rightDBObj.getJDBCMetaData().getTargetTable() +
!                     " ON (" + leftDBObj.getJDBCMetaData().getTargetTable() + "." +
!                     leftColumn + " = " + rightDBObj.getJDBCMetaData().getTargetTable() +
                      "." + rightColumn + onClause + ")");
  
--- 2119,2125 ----
              }
  
!             s.append(getTableName(rightDBObj) +
!                     " ON (" + getTableName(leftDBObj) + "." +
!                     leftColumn + " = " + getTableName(rightDBObj) +
                      "." + rightColumn + onClause + ")");
  
***************
*** 2146,2150 ****
              throws DBException {
          return StringUtil.replaceStringOnce(oneObj.selectFieldString(fieldName),
!                 fieldName, oneObj.getJDBCMetaData().getTargetTable() + "." + fieldName);
  
      } /* selectFieldString(String) */
--- 2181,2185 ----
              throws DBException {
          return StringUtil.replaceStringOnce(oneObj.selectFieldString(fieldName),
!                 fieldName, getTableName(oneObj) + "." + fieldName);
  
      } /* selectFieldString(String) */
***************
*** 2245,2248 ****
--- 2280,2320 ----
          }
          return result;
+     }
+ 
+     /**
+      * Specify whether the shortName specified when a DBObject is added is also
+      * used as an alias for the table in the query.  Should be false by default.
+      *
+      * @param flag True if the shortName should be used as an alias
+      */
+     public void setShortNameAsAlias(boolean flag) {
+         shortNameAsAlias = flag;
+     }
+ 
+ 
+     /**
+      * Get whether the shortName specified when a DBObject is added is also
+      * used as an alias for the table in the query.
+      *
+      * @return True if short name is used as an alias for the table
+      */
+     public boolean getShortNameAsAlias() {
+         return shortNameAsAlias;
+     }
+ 
+ 
+     /**
+      * Get the name a table should be referenced by.  Should be the name of the
+      * table in the database unless using shortNames as alias.
+      *
+      * @param oneObj The DBObject to get the name of
+      * @return The name of the table
+      * @throws DataException
+      */
+     private String getTableName(DBObject oneObj)
+             throws DataException {
+         String name = (String) oneObj.getAttribute(SHORT_NAME);
+         if (name != null && shortNameAsAlias) return name;
+         return oneObj.getJDBCMetaData().getTargetTable();
      }
  } /* MultiDBObject */



More information about the cvs mailing list