001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.sql;
021:
022: /**
023: * This type was created in VisualAge.
024: */
025: class DSDataSourceOracle extends DSDataSourceJDBC {
026:
027: /**
028: * This method was created in VisualAge.
029: * @return java.lang.String
030: * @param ds com.salmonllc.sql.DataStore
031: */
032: public String generateSelect(DataStore ds, String criteria,
033: boolean countOnly) throws DataStoreException {
034: //build the column list
035: StringBuffer colList = new StringBuffer();
036: colList.append("SELECT ");
037: if (ds.getDistinct())
038: colList.append("DISTINCT ");
039:
040: if (countOnly)
041: colList.append("count(*)");
042: else {
043: for (int i = 0; i < ds.getColumnCount(); i++) {
044: String dbName = ds.getColumnDatabaseName(i);
045: if (dbName != null) {
046: colList.append(dbName);
047: colList.append(",");
048: }
049: }
050: colList.setCharAt(colList.length() - 1, ' ');
051: }
052:
053: //build the from clause
054: StringBuffer fromClause = new StringBuffer();
055: fromClause.append(" FROM ");
056: if (ds.getDefaultTable() != null)
057: fromClause.append(ds.getDefaultTable() + ",");
058: for (int i = 0; i < ds.getAliasCount(); i++) {
059: fromClause.append(ds.getTable(i));
060: if (ds.getAlias(i) != null)
061: fromClause.append(" " + ds.getAlias(i));
062: fromClause.append(",");
063: }
064: fromClause.setCharAt(fromClause.length() - 1, ' ');
065:
066: //build the where clause
067: StringBuffer whereClause = new StringBuffer();
068: // fc: 07/18/02 Added check for empty criteria to stop a bad sql statement from being generated.
069: if (criteria != null && !criteria.trim().equals("")
070: && ds.getCriteria() != null
071: && !ds.getCriteria().trim().equals(""))
072: criteria = "(" + criteria + ") AND (" + ds.getCriteria()
073: + ")";
074: else if (criteria == null)
075: criteria = ds.getCriteria();
076:
077: if (criteria != null)
078: if (criteria.trim().equals(""))
079: criteria = "";
080:
081: if (criteria != null || ds.getJoinCount() > 0) {
082: whereClause.append(" WHERE ");
083: if (ds.getJoinCount() > 0) {
084: whereClause.append("(");
085: for (int i = 0; i < ds.getJoinCount(); i++) {
086: for (int j = 0; j < ds.getJoinColumnCount(i); j++) {
087: whereClause.append(ds.getJoinLeftColumn(i, j));
088: whereClause.append('=');
089: whereClause.append(ds.getJoinRightColumn(i, j));
090: if (ds.getJoinOuter(i))
091: whereClause.append("(+)");
092: if (j < (ds.getJoinColumnCount(i) - 1))
093: whereClause.append(" AND ");
094: }
095: if (i < (ds.getJoinCount() - 1))
096: whereClause.append(" AND ");
097: }
098: whereClause.append(")");
099: if (criteria != null)
100: whereClause.append(" AND (" + criteria + ")");
101: } else if (criteria != null)
102: whereClause.append(criteria);
103:
104: }
105:
106: //finish it up and return
107: String retVal = colList.toString() + fromClause.toString()
108: + whereClause.toString();
109:
110: if (ds.getGroupBy() != null)
111: retVal += " GROUP BY " + ds.getGroupBy();
112:
113: if (ds.getHaving() != null)
114: retVal += " HAVING " + ds.getHaving();
115:
116: if (ds.getOrderBy() != null && !countOnly)
117: retVal += " ORDER BY " + ds.getOrderBy();
118:
119: return retVal;
120:
121: }
122: }
|