KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > sqlparser > SqlParser


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.repository.sqlparser;
31
32 import java.io.BufferedReader JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileReader JavaDoc;
35
36 import com.genimen.djeneric.repository.sqlparser.core.ParseException;
37 import com.genimen.djeneric.repository.sqlparser.core.SqlParserEngine;
38 import com.genimen.djeneric.repository.sqlparser.core.TokenContext;
39 import com.genimen.djeneric.util.DjLogger;
40
41 public class SqlParser implements SqlParserEngineListener
42 {
43   String JavaDoc _theStatement = null;
44
45   public SqlParser()
46   {
47   }
48
49   public SqlParser(String JavaDoc stmt)
50   {
51     setStatement(stmt);
52   }
53
54   public void setStatement(String JavaDoc stmt)
55   {
56     _theStatement = stmt;
57   }
58
59   public String JavaDoc parse() throws Exception JavaDoc
60   {
61     SqlParserEngine parser = SqlParserEngine.parse(_theStatement, this);
62     return parser.getResult();
63   }
64
65   public void handleTable(TokenContext ctxt, StringBuffer JavaDoc tableName, StringBuffer JavaDoc tableAlias) throws ParseException
66   {
67     //System.out.println(ctxt + "->Table: " + tableName + " = " + tableAlias);
68
}
69
70   public void handleColumn(TokenContext ctxt, StringBuffer JavaDoc tableAlias, StringBuffer JavaDoc columnName) throws ParseException
71   {
72     //System.out.println(ctxt + "->Column: " + tableAlias + "." + columnName);
73
}
74
75   public void handleParameter(TokenContext ctxt, StringBuffer JavaDoc parameterName) throws ParseException
76   {
77     //System.out.println(ctxt + "->Parameter: " + parameterName);
78
}
79
80   public void handleColumnExpression(TokenContext ctxt, StringBuffer JavaDoc expression) throws ParseException
81   {
82     //System.out.println(ctxt + "->Expression: " + expression + " = " + aliasName);
83
}
84
85   public void handleSelectedColumnExpression(TokenContext ctxt, StringBuffer JavaDoc expression, StringBuffer JavaDoc aliasName)
86       throws ParseException
87   {
88     //System.out.println(ctxt + "->Expression: " + expression + " = " + aliasName);
89
}
90
91   public void handleSelectList(TokenContext ctxt, StringBuffer JavaDoc selectList) throws ParseException
92   {
93     //System.out.println(ctxt + "->SelectList: " + selectList);
94
}
95
96   public void handleTableList(TokenContext ctxt, StringBuffer JavaDoc tableList) throws ParseException
97   {
98     //System.out.println(ctxt + "->TableList: " + tableList);
99
}
100
101   public void handleGroupBy(TokenContext ctxt, StringBuffer JavaDoc groupBy) throws ParseException
102   {
103     //System.out.println(ctxt + "->GroupBy: " + groupBy);
104
}
105
106   public void handleWhereClause(TokenContext ctxt, StringBuffer JavaDoc where) throws ParseException
107   {
108     //System.out.println(ctxt + "->Where: " + where);
109
}
110
111   public void handleHaving(TokenContext ctxt, StringBuffer JavaDoc having) throws ParseException
112   {
113     //System.out.println(ctxt + "->Having: " + having);
114
}
115
116   public void handleOrderBy(TokenContext ctxt, StringBuffer JavaDoc orderBy) throws ParseException
117   {
118     //System.out.println(ctxt + "->OrderBy: " + orderBy);
119
}
120
121   public void handleFunction(TokenContext ctxt, StringBuffer JavaDoc functionName, StringBuffer JavaDoc args) throws ParseException
122   {
123     //System.out.println(ctxt + "->Function: " + functionName + "(" + args + ")");
124
}
125
126   public void enterStatement(TokenContext ctxt, int /*TokenContext.*/
127   statementType)
128   {
129   }
130
131   public void exitStatement(TokenContext ctxt, int /*TokenContext.*/
132   statementType, StringBuffer JavaDoc theStatement)
133   {
134   }
135
136   public void handleInsertColumnList(TokenContext ctxt, StringBuffer JavaDoc insertColumns) throws ParseException
137   {
138   }
139
140   public void handleInsertValueList(TokenContext ctxt, StringBuffer JavaDoc insertColumns) throws ParseException
141   {
142   }
143
144   public void handleSelectFullRecord(TokenContext ctxt, String JavaDoc tableAlias, StringBuffer JavaDoc fullRecordSelect)
145       throws ParseException
146   {
147   }
148
149   public static void main(String JavaDoc[] args)
150   {
151     try
152     {
153       if (args.length < 1)
154       {
155         System.out.println("Usage: java " + SqlParser.class.getName() + " <input file>");
156         return;
157       }
158       File JavaDoc inFile = new File JavaDoc(args[0]);
159       BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(inFile));
160       StringBuffer JavaDoc src = new StringBuffer JavaDoc(100);
161       String JavaDoc ln;
162       while ((ln = br.readLine()) != null)
163       {
164         src.append(ln);
165         src.append("\n");
166       }
167       br.close();
168
169       SqlParser parser = new SqlParser();
170       parser.setStatement(src.toString());
171
172       System.err.println(parser.parse());
173     }
174     catch (Exception JavaDoc x)
175     {
176       DjLogger.log(x);
177     }
178   }
179 }
Popular Tags