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 com.salmonllc.util.MessageLog;
023:
024: import java.sql.*;
025:
026: /**
027: * This type was created in VisualAge.
028: */
029: class DSDataSourceFireBird extends DSDataSourceJDBC {
030:
031: /**
032: * This method was created in VisualAge.
033: * @return java.lang.String
034: * @param ds com.salmonllc.sql.DataStore
035: */
036: public String generateSelect(DataStore ds, String criteria,
037: boolean countOnly) throws DataStoreException {
038: //build the column list
039: StringBuffer colList = new StringBuffer();
040: colList.append("SELECT ");
041: if (ds.getDistinct())
042: colList.append("DISTINCT ");
043: if (countOnly)
044: colList.append("count(*)");
045: else {
046: for (int i = 0; i < ds.getColumnCount(); i++) {
047: String databaseName = ds.getColumnDatabaseName(i);
048: if (databaseName != null) {
049: colList.append(databaseName);
050: colList.append(",");
051: }
052: }
053: colList.setCharAt(colList.length() - 1, ' ');
054: }
055:
056: //build the join portion of the from clause
057: StringBuffer joinClause = new StringBuffer();
058: boolean aliasUsed[] = new boolean[ds.getAliasCount() + 1];
059: for (int i = 0; i < ds.getJoinCount(); i++) {
060: String join = " inner join ";
061: if (ds.getJoinOuter(i))
062: join = " left outer join ";
063: String leftTable = ds.getJoinLeftColumn(i, 0);
064: String rightTable = ds.getJoinRightColumn(i, 0);
065: int pos = leftTable.indexOf(".");
066: if (pos > -1)
067: leftTable = leftTable.substring(0, pos);
068: pos = rightTable.indexOf(".");
069: if (pos > -1)
070: rightTable = rightTable.substring(0, pos);
071: int index = findAlias(ds, leftTable);
072: if (index > -1) {
073: if (aliasUsed[index])
074: leftTable = "";
075: else {
076: if (ds.getAlias(index) != null)
077: leftTable = ds.getTable(index) + " "
078: + leftTable;
079:
080: }
081: aliasUsed[index] = true;
082: }
083: index = findAlias(ds, rightTable);
084: if (index > -1) {
085: aliasUsed[index] = true;
086: if (ds.getAlias(index) != null)
087: rightTable = ds.getTable(index) + " " + rightTable;
088:
089: }
090: joinClause.append(" " + leftTable + join + rightTable
091: + " ON ");
092: for (int j = 0; j < ds.getJoinColumnCount(i); j++) {
093: if (j > 0)
094: joinClause.append(" AND ");
095: joinClause.append(ds.getJoinLeftColumn(i, j) + " = "
096: + ds.getJoinRightColumn(i, j));
097: }
098: }
099: StringBuffer fromClause = new StringBuffer();
100: fromClause.append(" FROM ");
101: if ((ds.getDefaultTable() != null)
102: && (!aliasUsed[ds.getAliasCount()]))
103: fromClause.append(ds.getDefaultTable() + ",");
104: for (int i = 0; i < ds.getAliasCount(); i++) {
105: if (!aliasUsed[i]) {
106: fromClause.append(ds.getTable(i));
107: if (ds.getAlias(i) != null)
108: fromClause.append(" " + ds.getAlias(i));
109: fromClause.append(",");
110: }
111: }
112: if (fromClause.length() > 0 && joinClause.length() == 0)
113: fromClause.setCharAt(fromClause.length() - 1, ' ');
114: fromClause.append(joinClause);
115:
116: //build the where clause
117:
118: StringBuffer whereClause = new StringBuffer();
119: // fc: 07/18/02 Added check for empty criteria to stop a bad sql statement from being generated.
120: if (criteria != null && !criteria.trim().equals("")
121: && ds.getCriteria() != null
122: && !ds.getCriteria().trim().equals(""))
123: criteria = "(" + criteria + ") AND (" + ds.getCriteria()
124: + ")";
125: else if (criteria == null)
126: criteria = ds.getCriteria();
127: if (criteria != null)
128: if (!criteria.trim().equals(""))
129: whereClause.append(" WHERE " + criteria);
130:
131: //finish it up and return
132: String retVal = colList.toString() + fromClause.toString()
133: + whereClause.toString();
134: if (ds.getGroupBy() != null)
135: retVal += " GROUP BY " + ds.getGroupBy();
136: if (ds.getHaving() != null)
137: retVal += " HAVING " + ds.getHaving();
138: if (ds.getOrderBy() != null && !countOnly)
139: retVal += " ORDER BY " + ds.getOrderBy();
140: return retVal;
141:
142: }
143:
144: private int findAlias(DataStore ds, String table) {
145: try {
146: int count = ds.getAliasCount();
147:
148: for (int i = 0; i < count; i++) {
149: if (ds.getAlias(i) != null)
150: if (ds.getAlias(i).equalsIgnoreCase(table))
151: return i;
152:
153: if (ds.getTable(i).equalsIgnoreCase(table))
154: return i;
155: }
156:
157: if (ds.getDefaultTable() != null) {
158: if (ds.getDefaultTable().equalsIgnoreCase(table))
159: return count;
160: }
161: } catch (Exception e) {
162: }
163:
164: return -1;
165: }
166:
167: protected void populateAutoIncrementValue(DataStoreRow row,
168: DBConnection conn, int colNo) {
169: try {
170: Statement st = conn.createStatement();
171: ResultSet r = st.executeQuery("select LAST_INSERT_ID()");
172: if (r.next()) {
173: int colStat = row.getDSDataRow().getColumnStatus(colNo);
174: int rowStat = row.getDSDataRow().getRowStatus();
175: Object val = null;
176: if (row.getDataType(colNo) == DataStore.DATATYPE_LONG)
177: val = new Long(r.getLong(1));
178: else if (row.getDataType(colNo) == DataStore.DATATYPE_INT)
179: val = new Integer(r.getInt(1));
180: else if (row.getDataType(colNo) == DataStore.DATATYPE_SHORT)
181: val = new Short(r.getShort(1));
182:
183: row.setData(colNo, val);
184: row.getDSDataRow().setColumnStatus(colNo, colStat);
185: row.getDSDataRow().setRowStatus(rowStat);
186: }
187: r.close();
188: st.close();
189: } catch (Exception e) {
190: MessageLog.writeErrorMessage(
191: "Error getting last auto increment value", e, this);
192: }
193: }
194: }
|