001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
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
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.io;
022:
023: import java.util.Iterator;
024:
025: import nickyb.sqleonardo.common.util.Store;
026: import nickyb.sqleonardo.querybuilder.QueryModel;
027: import nickyb.sqleonardo.querybuilder.syntax.QueryExpression;
028: import nickyb.sqleonardo.querybuilder.syntax.QuerySpecification;
029: import nickyb.sqleonardo.querybuilder.syntax.QueryTokens;
030:
031: public class FileStreamOLD {
032: public static void convert(Store qstore, QueryModel model) {
033: QueryExpression qe = model.getQueryExpression();
034:
035: qstore.mount("$QUERY");
036: for (int i = 0; qstore.canJump("qry" + i); i++) {
037: if (i == 0) {
038: qstore.home();
039: qstore.jump("qry" + i);
040: for (Iterator it = qstore.jump("order").iterator(); it
041: .hasNext();) {
042: Object[] row = (Object[]) it.next();
043:
044: QueryTokens.Sort qtoken = new QueryTokens.Sort(
045: new QueryTokens.DefaultExpression(row[0]
046: .toString()), ((Boolean) row[1])
047: .booleanValue());
048: model.addOrderByClause(qtoken);
049: }
050: } else
051: qe.setUnion(qe = new QueryExpression());
052:
053: load("qry" + i, qstore, qe.getQuerySpecification());
054: qstore.home();
055: }
056: }
057:
058: private static void load(String sub, Store qstore,
059: QuerySpecification qs) {
060: qstore.home();
061: if (sub != null)
062: qstore.jump(sub);
063: boolean distinct = ((Boolean) qstore.jump("distinct").get(0))
064: .booleanValue();
065: qs.setQuantifier(distinct ? QuerySpecification.DISTINCT
066: : QuerySpecification.NONE);
067:
068: qstore.home();
069: if (sub != null)
070: qstore.jump(sub);
071: for (Iterator i = qstore.jump("tables").iterator(); i.hasNext();) {
072: Object[] row = (Object[]) i.next();
073:
074: QueryTokens.Table table = new QueryTokens.Table(
075: (row[0] == null ? null : row[0].toString()), row[1]
076: .toString());
077: if (row[2] != null)
078: table.setAlias(row[2].toString());
079:
080: qs.addFromClause(table);
081: }
082:
083: qstore.home();
084: if (sub != null)
085: qstore.jump(sub);
086: for (Iterator i = qstore.jump("expressions").iterator(); i
087: .hasNext();) {
088: Object row = i.next();
089: if (row instanceof Object[]) {
090: Object[] rowdata = (Object[]) row;
091: QueryTokens.Table table = new QueryTokens.Table(
092: (rowdata[0] == null ? null : rowdata[0]
093: .toString()), rowdata[1].toString());
094:
095: String column = rowdata[2].toString();
096: if (rowdata[3] != null)
097: table.setAlias(rowdata[3].toString());
098:
099: qs.addSelectList(new QueryTokens.Column(table, column));
100: } else {
101: qs.addSelectList(new QueryTokens.DefaultExpression(row
102: .toString()));
103: }
104: }
105:
106: qstore.home();
107: if (sub != null)
108: qstore.jump(sub);
109: for (Iterator i = qstore.jump("joins").iterator(); i.hasNext();) {
110: Object[] row = (Object[]) i.next();
111:
112: int idx = 0;
113: int type = ((Integer) row[idx]).intValue();
114:
115: String lschema = row[++idx] == null ? null : row[idx]
116: .toString();
117: String ltable = row[++idx].toString();
118: String lalias = null;
119: if (row.length == 10 && row[++idx] != null)
120: lalias = row[idx].toString();
121: String lname = row[++idx].toString();
122:
123: String operator = row[++idx].toString();
124:
125: String rschema = row[++idx] == null ? null : row[idx]
126: .toString();
127: String rtable = row[++idx].toString();
128: String ralias = null;
129: if (row.length == 10 && row[++idx] != null)
130: ralias = row[idx].toString();
131: String rname = row[++idx].toString();
132:
133: QueryTokens.Table ltoken = new QueryTokens.Table(lschema,
134: ltable);
135: if (lalias != null)
136: ltoken.setAlias(lalias);
137: QueryTokens.Column lcolumn = new QueryTokens.Column(ltoken,
138: lname);
139:
140: QueryTokens.Table rtoken = new QueryTokens.Table(rschema,
141: rtable);
142: if (ralias != null)
143: rtoken.setAlias(ralias);
144: QueryTokens.Column rcolumn = new QueryTokens.Column(rtoken,
145: rname);
146:
147: qs.addFromClause(new QueryTokens.Join(type, lcolumn,
148: operator, rcolumn));
149: }
150:
151: qstore.home();
152: if (sub != null)
153: qstore.jump(sub);
154: for (Iterator i = qstore.jump("where").iterator(); i.hasNext();) {
155: Object[] row = (Object[]) i.next();
156:
157: QueryTokens.Condition qtoken = new QueryTokens.Condition(
158: (row[0] == null ? null : row[0].toString()),
159: new QueryTokens.DefaultExpression(row[1].toString()),
160: row[2].toString(),
161: new QueryTokens.DefaultExpression(row[3].toString()));
162: qs.addWhereClause(qtoken);
163: }
164:
165: qstore.home();
166: if (sub != null)
167: qstore.jump(sub);
168: for (Iterator i = qstore.jump("group").iterator(); i.hasNext();) {
169: qs.addGroupByClause(new QueryTokens.Group(i.next()
170: .toString()));
171: }
172:
173: qstore.home();
174: if (sub != null)
175: qstore.jump(sub);
176: for (Iterator i = qstore.jump("having").iterator(); i.hasNext();) {
177: Object[] row = (Object[]) i.next();
178:
179: QueryTokens.Condition qtoken = new QueryTokens.Condition(
180: (row[0] == null ? null : row[0].toString()),
181: new QueryTokens.DefaultExpression(row[1].toString()),
182: row[2].toString(),
183: new QueryTokens.DefaultExpression(row[3].toString()));
184: qs.addHavingClause(qtoken);
185: }
186: }
187: }
|