01: /*
02: * Geotools2 - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002, Geotools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package org.geotools.data.sql;
18:
19: import java.io.IOException;
20: import java.io.Reader;
21: import java.io.StringReader;
22:
23: import org.geotools.data.DataSourceException;
24:
25: import net.sf.jsqlparser.parser.CCJSqlParserManager;
26: import net.sf.jsqlparser.statement.Statement;
27: import net.sf.jsqlparser.statement.select.Select;
28: import net.sf.jsqlparser.statement.select.SelectBody;
29:
30: /**
31: * Just a utility class to parse a string with a plain SQL SELECT statement into
32: * an POJO object model as defined by <a href="http://jsqlparser.sf.net">jsqlparser</a>
33: *
34: * @author Gabriel Roldan, Axios Engineering
35: * @version $Id: SqlParser.java 25699 2007-05-31 15:55:07Z desruisseaux $
36: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/sql-datastore/src/org/geotools/data/sql/SqlParser.java $
37: * @since 2.3.x
38: */
39: public class SqlParser {
40:
41: public static SelectBody parse(String selectStatement)
42: throws IOException {
43: CCJSqlParserManager pm = new CCJSqlParserManager();
44: Reader reader = new StringReader(selectStatement);
45: Statement statement;
46: try {
47: statement = pm.parse(reader);
48: } catch (Exception e) {
49: throw new DataSourceException("parsing select statement: "
50: + e.getCause().getMessage(), e);
51: }
52: if (!(statement instanceof Select)) { //either PlainSelect or Union
53: throw new IllegalArgumentException(
54: "expected select or union statement: " + statement);
55: }
56: SelectBody selectBody = ((Select) statement).getSelectBody();
57: return selectBody;
58: }
59: }
|