package com.quadcap.sql;
/* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
*
* This software is distributed under the Quadcap Free Software License.
* This software may be used or modified for any purpose, personal or
* commercial. Open Source redistributions are permitted. Commercial
* redistribution of larger works derived from, or works which bundle
* this software requires a "Commercial Redistribution License"; see
* http://www.quadcap.com/purchase.
*
* Redistributions qualify as "Open Source" under one of the following terms:
*
* Redistributions are made at no charge beyond the reasonable cost of
* materials and delivery.
*
* Redistributions are accompanied by a copy of the Source Code or by an
* irrevocable offer to provide a copy of the Source Code for up to three
* years at the cost of materials and delivery. Such redistributions
* must allow further use, modification, and redistribution of the Source
* Code under substantially the same terms as this license.
*
* Redistributions of source code must retain the copyright notices as they
* appear in each source code file, these license terms, and the
* disclaimer/limitation of liability set forth as paragraph 6 below.
*
* Redistributions in binary form must reproduce this Copyright Notice,
* these license terms, and the disclaimer/limitation of liability set
* forth as paragraph 6 below, in the documentation and/or other materials
* provided with the distribution.
*
* The Software is provided on an "AS IS" basis. No warranty is
* provided that the Software is free of defects, or fit for a
* particular purpose.
*
* Limitation of Liability. Quadcap Software shall not be liable
* for any damages suffered by the Licensee or any third party resulting
* from use of the Software.
*/
import java.io.IOException;
import antlr.Token;
import antlr.TokenStream;
import com.quadcap.util.Debug;
/**
* Handle JDBC escape processing as a token stream.
*
* @author Stan Bailes
*/
public class JdbcEscapeTokenStream implements TokenStream, SQLTokenTypes {
TokenStream in;
int escCnt = 0;
public JdbcEscapeTokenStream(TokenStream in) {
this.in = in;
}
/*{jdbcEscape.xml-10}
* <section name="JDBC Escape Processing">
*
* <p>The following JDBC escape syntax is recognized and translated
* to the appropriate underlying SQL syntax.</p>
*
* <table>
* <tgroup cols="3">
* <thead>
* <row>
* <th>JDBC Syntax</th><th>SQL Syntax</th><th>Description</th>
* </row>
* </thead>
* <tbody>
* <row>
* <entry>{escape '<i>character</i>'}</entry>
* <entry>escape '<i>character</i>'</entry>
* <entry>Specify the character used to escape '%' and '_'
* in SQL <code>LIKE</code> clauses.</entry>
*
* </row>
* <row>
* <entry>{fn <i>functionExpression</i>}</entry>
* <entry><i>functionExpression</i></entry>
* <entry>Use a database scalar function. Since the ODBC CLI functions
* specified by JDBC are all directly implemented by QED, this
* escape is basically just a pass-through.</entry>
*
* </row>
* <row>
* <entry>{d <i>'yyyy-mm-dd'</i>}</entry>
* <entry>date <i>'yyyy-mm-dd'</i></entry>
* <entry>The JDBC 'd' escape for a date literal is translated into
* the SQL92 'date' syntax supported by the database.</entry>
*
* </row>
* <row>
* <entry>{t <i>'hh:mm:ss'</i>}</entry>
* <entry>time <i>'hh:mm:ss'</i></entry>
* <entry>The JDBC 't' escape for a time literal is translated into
* the SQL92 'time' syntax supported by the database.</entry>
*
* </row>
* <row>
* <entry>{ts <i>'yyyy-mm-dd hh:mm:ss.f . . .'</i>}</entry>
* <entry>timestamp <i>'yyyy-mm-dd hh:mm:ss.f . . .'</i></entry>
* <entry>The JDBC 'ts' escape for a timestamp literal is translated into
* the SQL92 'timestamp' syntax supported by the database.</entry>
*
* </row>
* <row>
* <entry>{oj <i>outer-join</i>}</entry>
* <entry><i>outer-join</i></entry>
* <entry>The JDBC 'oj' escape for a timestamp literal is translated into
* the SQL92 'outer join' syntax supported by the database.</entry>
*
* </row>
* </tbody>
* </tgroup>
* </table>
* </section>
*/
public Token nextToken() throws antlr.TokenStreamException {
Token t = in.nextToken();
int typ = t.getType();
while (typ == RCURLY) {
if (--escCnt < 0) throw new antlr.TokenStreamException("unexpected '}'");
t = in.nextToken();
typ = t.getType();
}
if (typ == LCURLY) {
escCnt++;
t = in.nextToken();
typ = t.getType();
if (typ == ID) {
String txt = t.getText().toLowerCase();
if (txt.equals("fn") || txt.equals("oj")) {
t = in.nextToken();
} else if (txt.equals("escape")) {
} else if (txt.equals("d")) {
t = new Token(LITERAL_date, "date");
} else if (txt.equals("t")) {
t = new Token(LITERAL_time, "time");
} else if (txt.equals("ts")) {
t = new Token(LITERAL_timestamp, "timestamp");
} else {
throw new antlr.TokenStreamException("Unrecognized escape: " +
t.getText());
}
}
}
//Debug.println("ESC token = " + t);
return t;
}
}
|