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