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: */
024: class DSDataSourceIngres extends DSDataSourceJDBC {
025:
026: public DSDataSourceIngres() {
027: super ();
028: }
029:
030: private int findAlias(DataStore ds, String table) {
031: try {
032: int count = ds.getAliasCount();
033:
034: for (int i = 0; i < count; i++) {
035: if (ds.getAlias(i) != null)
036: if (ds.getAlias(i).equalsIgnoreCase(table))
037: return i;
038:
039: if (ds.getTable(i).equalsIgnoreCase(table))
040: return i;
041: }
042:
043: if (ds.getDefaultTable() != null) {
044: if (ds.getDefaultTable().equalsIgnoreCase(table))
045: return count;
046: }
047: } catch (Exception e) {
048: }
049:
050: return -1;
051: }
052:
053: /**
054: * @return java.lang.String
055: * @param ds com.salmonllc.sql.DataStore
056: */
057: public String generateSelect(DataStore ds, String criteria,
058: boolean countOnly) throws DataStoreException {
059:
060: //build the column list
061: StringBuffer colList = new StringBuffer();
062: colList.append("SELECT ");
063: if (ds.getDistinct())
064: colList.append("DISTINCT ");
065: if (countOnly)
066: colList.append("count(*)");
067: else {
068: for (int i = 0; i < ds.getColumnCount(); i++) {
069: String databaseName = ds.getColumnDatabaseName(i);
070: if (databaseName != null) {
071: colList.append(databaseName);
072: colList.append(",");
073: }
074: }
075: colList.setCharAt(colList.length() - 1, ' ');
076: }
077:
078: //build the join portion of the from clause
079: StringBuffer joinClause = new StringBuffer();
080: boolean aliasUsed[] = new boolean[ds.getAliasCount() + 1];
081: for (int i = 0; i < ds.getJoinCount(); i++) {
082: String join = " inner join ";
083: if (ds.getJoinOuter(i))
084: join = " left outer join ";
085: String leftTable = ds.getJoinLeftColumn(i, 0);
086: String rightTable = ds.getJoinRightColumn(i, 0);
087: int pos = leftTable.indexOf(".");
088: if (pos > -1)
089: leftTable = leftTable.substring(0, pos);
090: pos = rightTable.indexOf(".");
091: if (pos > -1)
092: rightTable = rightTable.substring(0, pos);
093: int index = findAlias(ds, leftTable);
094: if (index > -1) {
095: if (aliasUsed[index])
096: leftTable = "";
097: aliasUsed[index] = true;
098: }
099: index = findAlias(ds, rightTable);
100: if (index > -1)
101: aliasUsed[index] = true;
102: joinClause.append(" " + leftTable + join + rightTable
103: + " ON ");
104: for (int j = 0; j < ds.getJoinColumnCount(i); j++) {
105: if (j > 0)
106: joinClause.append(" AND ");
107: joinClause.append(ds.getJoinLeftColumn(i, j) + " = "
108: + ds.getJoinRightColumn(i, j));
109: }
110: }
111: StringBuffer fromClause = new StringBuffer();
112: fromClause.append(" FROM ");
113: if ((ds.getDefaultTable() != null)
114: && (!aliasUsed[ds.getAliasCount() + 1]))
115: fromClause.append(ds.getDefaultTable() + ",");
116: for (int i = 0; i < ds.getAliasCount(); i++) {
117: if (!aliasUsed[i]) {
118: fromClause.append(ds.getTable(i));
119: if (ds.getAlias(i) != null)
120: fromClause.append(" " + ds.getAlias(i));
121: fromClause.append(",");
122: }
123: }
124: if (fromClause.length() > 0 && joinClause.length() == 0)
125: fromClause.setCharAt(fromClause.length() - 1, ' ');
126: fromClause.append(joinClause);
127:
128: //build the where clause
129:
130: StringBuffer whereClause = new StringBuffer();
131: // fc: 07/18/02 Added check for empty criteria to stop a bad sql statement from being generated.
132: if (criteria != null && !criteria.trim().equals("")
133: && ds.getCriteria() != null
134: && !ds.getCriteria().trim().equals(""))
135: criteria = "(" + criteria + ") AND (" + ds.getCriteria()
136: + ")";
137: else if (criteria == null)
138: criteria = ds.getCriteria();
139: if (criteria != null)
140: if (!criteria.trim().equals(""))
141: whereClause.append(" WHERE " + criteria);
142:
143: //finish it up and return
144: String retVal = colList.toString() + fromClause.toString()
145: + whereClause.toString();
146: if (ds.getGroupBy() != null)
147: retVal += " GROUP BY " + ds.getGroupBy();
148: if (ds.getHaving() != null)
149: retVal += " HAVING " + ds.getHaving();
150: if (ds.getOrderBy() != null && !countOnly)
151: retVal += " ORDER BY " + ds.getOrderBy();
152: return retVal;
153:
154: }
155:
156: }
|