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 DSDataSourceSybase extends DSDataSourceJDBC {
026:
027: /**
028: * DSSybaseDataSource constructor comment.
029: */
030: public DSDataSourceSybase() {
031: super ();
032: }
033:
034: /**
035: * This method was created in VisualAge.
036: * @return java.lang.String
037: * @param ds com.salmonllc.sql.DataStore
038: */
039: public String generateSelect(DataStore ds, String criteria,
040: boolean countOnly) throws DataStoreException {
041: //build the column list
042: StringBuffer colList = new StringBuffer();
043: colList.append("SELECT ");
044: if (ds.getDistinct())
045: colList.append("DISTINCT ");
046:
047: if (countOnly)
048: colList.append("count(*)");
049: else {
050: for (int i = 0; i < ds.getColumnCount(); i++) {
051: String dbName = ds.getColumnDatabaseName(i);
052: if (dbName != null) {
053: colList.append(dbName);
054: colList.append(",");
055: }
056: }
057: colList.setCharAt(colList.length() - 1, ' ');
058: }
059:
060: //build the from clause
061: StringBuffer fromClause = new StringBuffer();
062: fromClause.append(" FROM ");
063: if (ds.getDefaultTable() != null)
064: fromClause.append(ds.getDefaultTable() + ",");
065: for (int i = 0; i < ds.getAliasCount(); i++) {
066: fromClause.append(ds.getTable(i));
067: if (ds.getAlias(i) != null)
068: fromClause.append(" " + ds.getAlias(i));
069: fromClause.append(",");
070: }
071: fromClause.setCharAt(fromClause.length() - 1, ' ');
072:
073: //build the where clause
074: StringBuffer whereClause = new StringBuffer();
075: // fc: 07/18/02 Added check for empty criteria to stop a bad sql statement from being generated.
076: if (criteria != null && !criteria.trim().equals("")
077: && ds.getCriteria() != null
078: && !ds.getCriteria().trim().equals(""))
079: criteria = "(" + criteria + ") AND (" + ds.getCriteria()
080: + ")";
081: else if (criteria == null)
082: criteria = ds.getCriteria();
083:
084: if (criteria != null)
085: if (criteria.trim().equals(""))
086: criteria = "";
087:
088: if (criteria != null || ds.getJoinCount() > 0) {
089: whereClause.append(" WHERE ");
090: if (ds.getJoinCount() > 0) {
091: whereClause.append("(");
092: for (int i = 0; i < ds.getJoinCount(); i++) {
093: for (int j = 0; j < ds.getJoinColumnCount(i); j++) {
094: whereClause.append(ds.getJoinLeftColumn(i, j));
095: if (ds.getJoinOuter(i))
096: whereClause.append('*');
097: whereClause.append('=');
098: whereClause.append(ds.getJoinRightColumn(i, j));
099: if (j < (ds.getJoinColumnCount(i) - 1))
100: whereClause.append(" AND ");
101: }
102: if (i < (ds.getJoinCount() - 1))
103: whereClause.append(" AND ");
104: }
105: whereClause.append(")");
106: if (criteria != null)
107: whereClause.append(" AND (" + criteria + ")");
108: } else if (criteria != null)
109: whereClause.append(criteria);
110:
111: }
112:
113: //finish it up and return
114: String retVal = colList.toString() + fromClause.toString()
115: + whereClause.toString();
116:
117: if (ds.getGroupBy() != null)
118: retVal += " GROUP BY " + ds.getGroupBy();
119:
120: if (ds.getHaving() != null)
121: retVal += " HAVING " + ds.getHaving();
122:
123: if (ds.getOrderBy() != null && !countOnly)
124: retVal += " ORDER BY " + ds.getOrderBy();
125:
126: return retVal;
127:
128: }
129: }
|