KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > JdbcEscapeTokenStream


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import antlr.Token;
44 import antlr.TokenStream;
45
46 import com.quadcap.util.Debug;
47
48 /**
49  * Handle JDBC escape processing as a token stream.
50  *
51  * @author Stan Bailes
52  */

53 public class JdbcEscapeTokenStream implements TokenStream, SQLTokenTypes {
54     TokenStream in;
55     int escCnt = 0;
56
57     public JdbcEscapeTokenStream(TokenStream in) {
58     this.in = in;
59     }
60
61     /*{jdbcEscape.xml-10}
62      * <section name="JDBC Escape Processing">
63      *
64      * <p>The following JDBC escape syntax is recognized and translated
65      * to the appropriate underlying SQL syntax.</p>
66      *
67      * <table>
68      * <tgroup cols="3">
69      * <thead>
70      * <row>
71      * <th>JDBC Syntax</th><th>SQL Syntax</th><th>Description</th>
72      * </row>
73      * </thead>
74      * <tbody>
75      * <row>
76      * <entry>{escape '<i>character</i>'}</entry>
77      * <entry>escape '<i>character</i>'</entry>
78      * <entry>Specify the character used to escape '%' and '_'
79      * in SQL <code>LIKE</code> clauses.</entry>
80      *
81      * </row>
82      * <row>
83      * <entry>{fn <i>functionExpression</i>}</entry>
84      * <entry><i>functionExpression</i></entry>
85      * <entry>Use a database scalar function. Since the ODBC CLI functions
86      * specified by JDBC are all directly implemented by QED, this
87      * escape is basically just a pass-through.</entry>
88      *
89      * </row>
90      * <row>
91      * <entry>{d <i>'yyyy-mm-dd'</i>}</entry>
92      * <entry>date <i>'yyyy-mm-dd'</i></entry>
93      * <entry>The JDBC 'd' escape for a date literal is translated into
94      * the SQL92 'date' syntax supported by the database.</entry>
95      *
96      * </row>
97      * <row>
98      * <entry>{t <i>'hh:mm:ss'</i>}</entry>
99      * <entry>time <i>'hh:mm:ss'</i></entry>
100      * <entry>The JDBC 't' escape for a time literal is translated into
101      * the SQL92 'time' syntax supported by the database.</entry>
102      *
103      * </row>
104      * <row>
105      * <entry>{ts <i>'yyyy-mm-dd hh:mm:ss.f . . .'</i>}</entry>
106      * <entry>timestamp <i>'yyyy-mm-dd hh:mm:ss.f . . .'</i></entry>
107      * <entry>The JDBC 'ts' escape for a timestamp literal is translated into
108      * the SQL92 'timestamp' syntax supported by the database.</entry>
109      *
110      * </row>
111      * <row>
112      * <entry>{oj <i>outer-join</i>}</entry>
113      * <entry><i>outer-join</i></entry>
114      * <entry>The JDBC 'oj' escape for a timestamp literal is translated into
115      * the SQL92 'outer join' syntax supported by the database.</entry>
116      *
117      * </row>
118      * </tbody>
119      * </tgroup>
120      * </table>
121      * </section>
122      */

123
124     public Token nextToken() throws antlr.TokenStreamException {
125     Token t = in.nextToken();
126     int typ = t.getType();
127     while (typ == RCURLY) {
128         if (--escCnt < 0) throw new antlr.TokenStreamException("unexpected '}'");
129         t = in.nextToken();
130         typ = t.getType();
131     }
132     if (typ == LCURLY) {
133         escCnt++;
134         t = in.nextToken();
135         typ = t.getType();
136         if (typ == ID) {
137         String JavaDoc txt = t.getText().toLowerCase();
138         if (txt.equals("fn") || txt.equals("oj")) {
139             t = in.nextToken();
140         } else if (txt.equals("escape")) {
141         } else if (txt.equals("d")) {
142             t = new Token(LITERAL_date, "date");
143         } else if (txt.equals("t")) {
144             t = new Token(LITERAL_time, "time");
145         } else if (txt.equals("ts")) {
146             t = new Token(LITERAL_timestamp, "timestamp");
147         } else {
148             throw new antlr.TokenStreamException("Unrecognized escape: " +
149                                                          t.getText());
150         }
151             }
152     }
153     //Debug.println("ESC token = " + t);
154
return t;
155     }
156 }
157
158
Popular Tags