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.io.IOException;
024: import java.util.Iterator;
025:
026: import nickyb.sqleonardo.common.util.Store;
027: import nickyb.sqleonardo.environment.Application;
028: import nickyb.sqleonardo.querybuilder.QueryModel;
029: import nickyb.sqleonardo.querybuilder.syntax.QueryExpression;
030: import nickyb.sqleonardo.querybuilder.syntax.QueryTokens;
031:
032: public class FileStreamLQY {
033: /* reader */
034: public static QueryModel read(String filename) throws IOException,
035: ClassNotFoundException {
036: QueryModel model;
037:
038: Store qstore = new Store();
039: qstore.load(filename);
040:
041: qstore.mount(Application.ENTRY_INFO);
042: String version = qstore.jump("version").get(0).toString();
043: if (version.length() > 7)
044: version = version.substring(0, 7);
045:
046: if (Double.valueOf(version).doubleValue() < 2006.05) {
047: model = new QueryModel();
048: FileStreamOLD.convert(qstore, model);
049: } else {
050: qstore.mount("$QUERY");
051:
052: Object schema = null;
053: if (qstore.canJump("schema"))
054: schema = qstore.jump("schema").get(0);
055:
056: model = new QueryModel(schema == null ? null : schema
057: .toString());
058: model.setQueryExpression(read(qstore, 0));
059:
060: qstore.home();
061: qstore.jump("order_by_clause");
062: for (Iterator i = qstore.jump().iterator(); i.hasNext();) {
063: QueryTokens.Sort token = toOrder(i.next());
064: model.addOrderByClause(token);
065: }
066: }
067:
068: return model;
069: }
070:
071: private static QueryExpression read(Store qstore, int idx) {
072: qstore.home();
073: if (!qstore.canJump("query_expression_" + idx))
074: return null;
075:
076: QueryExpression qe = new QueryExpression();
077:
078: qstore.jump("query_expression_" + idx);
079: qstore.jump("quantifier");
080: qe.getQuerySpecification().setQuantifier(
081: toShort(qstore.jump().get(0)).shortValue());
082:
083: qstore.home();
084: qstore.jump("query_expression_" + idx);
085: qstore.jump("is_asterisk");
086: qe.getQuerySpecification().setAsterisk(
087: toBoolean(qstore.jump().get(0)).booleanValue());
088:
089: qstore.home();
090: qstore.jump("query_expression_" + idx);
091: qstore.jump("select_list");
092: for (Iterator i = qstore.jump().iterator(); i.hasNext();) {
093: QueryTokens._Expression token = toExpression(i.next());
094: qe.getQuerySpecification().addSelectList(token);
095: }
096:
097: qstore.home();
098: qstore.jump("query_expression_" + idx);
099: qstore.jump("from_clause");
100: for (Iterator i = qstore.jump().iterator(); i.hasNext();) {
101: QueryTokens._TableReference token = toTableReference(i
102: .next());
103: qe.getQuerySpecification().addFromClause(token);
104: }
105:
106: qstore.home();
107: qstore.jump("query_expression_" + idx);
108: qstore.jump("where_clause");
109: for (Iterator i = qstore.jump().iterator(); i.hasNext();) {
110: QueryTokens.Condition token = toCondition(i.next());
111: qe.getQuerySpecification().addWhereClause(token);
112: }
113:
114: qstore.home();
115: qstore.jump("query_expression_" + idx);
116: qstore.jump("group_by_clause");
117: for (Iterator i = qstore.jump().iterator(); i.hasNext();) {
118: QueryTokens.Group token = toGroup(i.next());
119: qe.getQuerySpecification().addGroupByClause(token);
120: }
121:
122: qstore.home();
123: qstore.jump("query_expression_" + idx);
124: qstore.jump("having_clause");
125: for (Iterator i = qstore.jump().iterator(); i.hasNext();) {
126: QueryTokens.Condition token = toCondition(i.next());
127: qe.getQuerySpecification().addHavingClause(token);
128: }
129:
130: qe.setUnion(read(qstore, idx + 1));
131:
132: return qe;
133: }
134:
135: private static Boolean toBoolean(Object o) {
136: return o == null ? null : (Boolean) o;
137: }
138:
139: private static Integer toInteger(Object o) {
140: return o == null ? null : (Integer) o;
141: }
142:
143: private static Short toShort(Object o) {
144: return o == null ? null : (Short) o;
145: }
146:
147: private static String toString(Object o) {
148: return o == null ? null : (String) o;
149: }
150:
151: private static QueryTokens._Expression toExpression(Object o) {
152: if (o == null)
153: return new QueryTokens.DefaultExpression(null);
154: return o instanceof String ? (QueryTokens._Expression) new QueryTokens.DefaultExpression(
155: o.toString())
156: : (QueryTokens._Expression) toColumn(o);
157: }
158:
159: private static QueryTokens._TableReference toTableReference(Object o) {
160: return ((Object[]) o).length == 3 ? (QueryTokens._TableReference) toTable(o)
161: : (QueryTokens._TableReference) toJoin(o);
162: }
163:
164: private static QueryTokens.Column toColumn(Object o) {
165: Object[] a = (Object[]) o;
166:
167: QueryTokens.Table table = toTable(a[0]);
168: QueryTokens.Column column = new QueryTokens.Column(table,
169: toString(a[1]));
170: column.setAlias(toString(a[2]));
171:
172: return column;
173: }
174:
175: private static QueryTokens.Table toTable(Object o) {
176: Object[] a = (Object[]) o;
177:
178: QueryTokens.Table table = new QueryTokens.Table(toString(a[0]),
179: toString(a[1]));
180: table.setAlias(toString(a[2]));
181:
182: return table;
183: }
184:
185: private static QueryTokens.Join toJoin(Object o) {
186: Object[] a = (Object[]) o;
187:
188: QueryTokens.Condition condition = toCondition(a[1]);
189: return new QueryTokens.Join(toInteger(a[0]).intValue(),
190: (QueryTokens.Column) condition.getLeft(), condition
191: .getOperator(), (QueryTokens.Column) condition
192: .getRight());
193: }
194:
195: private static QueryTokens.Condition toCondition(Object o) {
196: Object[] a = (Object[]) o;
197: return new QueryTokens.Condition(toString(a[0]),
198: toExpression(a[1]), toString(a[2]), toExpression(a[3]));
199: }
200:
201: private static QueryTokens.Group toGroup(Object o) {
202: return new QueryTokens.Group(toExpression(o));
203: }
204:
205: private static QueryTokens.Sort toOrder(Object o) {
206: Object[] a = (Object[]) o;
207: return new QueryTokens.Sort(toExpression(a[0]), toShort(a[1])
208: .shortValue());
209: }
210:
211: /* writer */
212: public static void write(String filename, QueryModel model)
213: throws IOException {
214: Store qstore = new Store();
215:
216: qstore.mount(Application.ENTRY_INFO);
217: qstore.jump("version").add(Application.getVersion());
218:
219: qstore.mount("$QUERY");
220: qstore.jump("schema").add(model.getSchema());
221: write(qstore, model.getQueryExpression(), 0);
222:
223: qstore.home();
224: qstore.jump("order_by_clause");
225: write(qstore, model.getOrderByClause());
226:
227: qstore.save(filename);
228: }
229:
230: private static void write(Store qstore, QueryExpression qe, int idx) {
231: if (qe == null)
232: return;
233:
234: qstore.home();
235: qstore.jump("query_expression_" + idx);
236: qstore.jump("quantifier").add(
237: new Short(qe.getQuerySpecification().getQuantifier()));
238:
239: qstore.home();
240: qstore.jump("query_expression_" + idx);
241: qstore.jump("is_asterisk")
242: .add(
243: new Boolean(qe.getQuerySpecification()
244: .isAsteriskSet()));
245:
246: qstore.home();
247: qstore.jump("query_expression_" + idx);
248: qstore.jump("select_list");
249: write(qstore, qe.getQuerySpecification().getSelectList());
250:
251: qstore.home();
252: qstore.jump("query_expression_" + idx);
253: qstore.jump("from_clause");
254: write(qstore, qe.getQuerySpecification().getFromClause());
255:
256: qstore.home();
257: qstore.jump("query_expression_" + idx);
258: qstore.jump("where_clause");
259: write(qstore, qe.getQuerySpecification().getWhereClause());
260:
261: qstore.home();
262: qstore.jump("query_expression_" + idx);
263: qstore.jump("group_by_clause");
264: write(qstore, qe.getQuerySpecification().getGroupByClause());
265:
266: qstore.home();
267: qstore.jump("query_expression_" + idx);
268: qstore.jump("having_clause");
269: write(qstore, qe.getQuerySpecification().getHavingClause());
270:
271: write(qstore, qe.getUnion(), idx + 1);
272: }
273:
274: private static void write(Store qstore, Object[] tokens) {
275: for (int i = 0; i < tokens.length; i++) {
276: Object row = null;
277:
278: if (tokens[i] instanceof QueryTokens._Expression)
279: row = toArray((QueryTokens._Expression) tokens[i]);
280: else if (tokens[i] instanceof QueryTokens.Condition)
281: row = toArray((QueryTokens.Condition) tokens[i]);
282: else if (tokens[i] instanceof QueryTokens.Join)
283: row = toArray((QueryTokens.Join) tokens[i]);
284: else if (tokens[i] instanceof QueryTokens.Sort)
285: row = toArray((QueryTokens.Sort) tokens[i]);
286: else if (tokens[i] instanceof QueryTokens.Group)
287: row = toArray((QueryTokens.Group) tokens[i]);
288: else if (tokens[i] instanceof QueryTokens.Table)
289: row = toArray((QueryTokens.Table) tokens[i]);
290: else
291: row = tokens[i].toString();
292:
293: qstore.jump().add(row);
294: }
295: }
296:
297: private static Object toArray(QueryTokens._Expression token) {
298: if (token == null)
299: return null;
300: return token instanceof QueryTokens.Column ? toArray((QueryTokens.Column) token)
301: : token.toString();
302: }
303:
304: private static Object toArray(QueryTokens.Column token) {
305: return new Object[] { toArray(token.getTable()),
306: token.getName(), token.getAlias() };
307: }
308:
309: private static Object toArray(QueryTokens.Condition token) {
310: return new Object[] { token.getAppend(),
311: toArray(token.getLeft()), token.getOperator(),
312: toArray(token.getRight()) };
313: }
314:
315: private static Object toArray(QueryTokens.Join token) {
316: return new Object[] { new Integer(token.getType()),
317: toArray(token.getCondition()) };
318: }
319:
320: private static Object toArray(QueryTokens.Table token) {
321: return new Object[] { token.getSchema(), token.getName(),
322: token.getAlias() };
323: }
324:
325: private static Object toArray(QueryTokens.Group token) {
326: Object expr = token.getExpression().toString();
327:
328: if (token.getExpression() instanceof QueryTokens.Column)
329: expr = toArray((QueryTokens.Column) token.getExpression());
330:
331: return expr;
332: }
333:
334: private static Object toArray(QueryTokens.Sort token) {
335: Object expr = token.getExpression().toString();
336:
337: if (token.getExpression() instanceof QueryTokens.Column)
338: expr = toArray((QueryTokens.Column) token.getExpression());
339:
340: return new Object[] {
341: expr,
342: new Short(
343: token.isAscending() ? QueryTokens.Sort.ASCENDING
344: : QueryTokens.Sort.DESCENDING) };
345: }
346: }
|