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: import java.sql.*;
023:
024: /**
025: * This type was created in VisualAge.
026: */
027: class DSDataSourceDB2MVS extends DSDataSourceJDBC {
028:
029: /**
030: * DSSybaseDataSource constructor comment.
031: */
032: public DSDataSourceDB2MVS() {
033: super ();
034: _dateTimeFormat = new java.text.SimpleDateFormat(
035: "yyyy-MM-dd-HH.mm.ss");
036: }
037:
038: public String generateProc(DBConnection conn, DataStore ds)
039: throws Exception {
040: StringBuffer sql = new StringBuffer();
041: sql.append("{call ");
042: sql.append(_proc);
043: if (_parms != null) {
044: if (_parms.getParmCount() > 0) {
045: sql.append(" (");
046: for (int i = 0; i < _parms.getParmCount(); i++) {
047: sql.append("? ");
048: }
049: sql.setCharAt(sql.length() - 1, ' ');
050: sql.append(")");
051: }
052: }
053: sql.append('}');
054:
055: String sqlSt = sql.toString();
056: CallableStatement cst = null;
057:
058: cst = conn.prepareCall(sqlSt);
059: if (_parms != null) {
060: for (int i = 0; i < _parms.getParmCount(); i++)
061: prepareForType(ds, cst, _parms.getParm(i), _parms
062: .getDataType(i), i + 1);
063: }
064: _res = cst.executeQuery();
065: _st = cst;
066:
067: return sqlSt;
068: }
069:
070: /**
071: * This method was created in VisualAge.
072: * @return java.lang.String
073: * @param ds com.salmonllc.sql.DataStore
074: */
075: public String generateSelect(DataStore ds, String criteria,
076: boolean countOnly) throws DataStoreException {
077: //build the column list
078: StringBuffer colList = new StringBuffer();
079: colList.append("SELECT ");
080: if (ds.getDistinct())
081: colList.append("DISTINCT ");
082:
083: if (countOnly)
084: colList.append("count(*)");
085: else {
086: for (int i = 0; i < ds.getColumnCount(); i++) {
087: String dbName = ds.getColumnDatabaseName(i);
088: if (dbName != null) {
089: colList.append(dbName);
090: colList.append(",");
091: }
092: }
093: colList.setCharAt(colList.length() - 1, ' ');
094: }
095:
096: //build the from clause
097: StringBuffer fromClause = new StringBuffer();
098: fromClause.append(" FROM ");
099: if (ds.getDefaultTable() != null)
100: fromClause.append(ds.getDefaultTable() + ",");
101: for (int i = 0; i < ds.getAliasCount(); i++) {
102: fromClause.append(ds.getTable(i));
103: if (ds.getAlias(i) != null)
104: fromClause.append(" " + ds.getAlias(i));
105: fromClause.append(",");
106: }
107: fromClause.setCharAt(fromClause.length() - 1, ' ');
108:
109: //build the where clause
110: StringBuffer whereClause = new StringBuffer();
111: // fc: 07/18/02 Added check for empty criteria to stop a bad sql statement from being generated.
112: if (criteria != null && !criteria.trim().equals("")
113: && ds.getCriteria() != null
114: && !ds.getCriteria().trim().equals(""))
115: criteria = "(" + criteria + ") AND (" + ds.getCriteria()
116: + ")";
117: else if (criteria == null)
118: criteria = ds.getCriteria();
119:
120: if (criteria != null)
121: if (criteria.trim().equals(""))
122: criteria = "";
123:
124: if (criteria != null || ds.getJoinCount() > 0) {
125: whereClause.append(" WHERE ");
126: if (ds.getJoinCount() > 0) {
127: whereClause.append("(");
128: for (int i = 0; i < ds.getJoinCount(); i++) {
129: for (int j = 0; j < ds.getJoinColumnCount(i); j++) {
130: whereClause.append(ds.getJoinLeftColumn(i, j));
131: //if (ds.getJoinOuter(i))
132: // whereClause.append('*');
133: whereClause.append('=');
134: whereClause.append(ds.getJoinRightColumn(i, j));
135: if (j < (ds.getJoinColumnCount(i) - 1))
136: whereClause.append(" AND ");
137: }
138: if (i < (ds.getJoinCount() - 1))
139: whereClause.append(" AND ");
140: }
141: whereClause.append(")");
142: if (criteria != null)
143: whereClause.append(" AND (" + criteria + ")");
144: } else if (criteria != null)
145: whereClause.append(criteria);
146:
147: }
148:
149: //finish it up and return
150: String retVal = colList.toString() + fromClause.toString()
151: + whereClause.toString();
152:
153: if (ds.getGroupBy() != null)
154: retVal += " GROUP BY " + ds.getGroupBy();
155:
156: if (ds.getHaving() != null)
157: retVal += " HAVING " + ds.getHaving();
158:
159: if (ds.getOrderBy() != null && !countOnly)
160: retVal += " ORDER BY " + ds.getOrderBy();
161:
162: return retVal;
163:
164: }
165: }
|