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