📄 tinysqlparser.java
字号:
/*
* tinySQLParser
*
* $Author: davis $
* $Date: 2004/12/18 21:28:17 $
* $Revision: 1.1 $
*
* This simple token based parser replaces the CUP generated parser
* simplifying extensions and reducing the total amount of code in
* tinySQL considerably.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Revision History;
*
* Written by Davis Swan in April, 2004.
*/
package com.sqlmagic.tinysql;
import java.io.*;
import java.util.*;
import java.text.*;
import java.sql.Types;
public class tinySQLParser
{
Vector columnList,tableList,actionList,valueList,contextList,
columnAliasList,columns;
Hashtable tables;
tinySQL dbEngine;
tinySQLWhere whereClause;
String tableName,tableAlias,dataDir;
String statementType=(String)null;
String lastKeyWord=(String)null,orderType=(String)null;
String oldColumnName=(String)null,newColumnName=(String)null;
String[] colTypeNames = {"INT","FLOAT","CHAR","DATE"};
int[] colTypes = {Types.INTEGER,Types.FLOAT,Types.CHAR,Types.DATE};
boolean distinct=false,defaultOrderBy=true;
public tinySQLParser(InputStream sqlInput,tinySQL inputEngine)
throws tinySQLException
{
StreamTokenizer st;
FieldTokenizer ft;
Reader r;
String nextToken,upperField,nextField,keyWord=(String)null;
StringBuffer cmdBuffer,inputSQLBuffer;
int lastIndex,keyIndex;
r = new BufferedReader(new InputStreamReader(sqlInput));
dbEngine = inputEngine;
actionList = new Vector();
columnList = new Vector();
columns = new Vector();
columnAliasList = new Vector();
contextList = new Vector();
valueList = new Vector();
tableName = (String)null;
whereClause = (tinySQLWhere)null;
/*
* The tableList is a list of table names, in the optimal order
* in which they should be scanned for the SELECT phrase.
* The Hashtable tables contains table objects keyed by table
* alias and name.
*/
tableList = new Vector();
tables = new Hashtable();
tables.put("TABLE_SELECT_ORDER",tableList);
try
{
st = new StreamTokenizer(r);
st.eolIsSignificant(false);
st.wordChars('\'','}');
st.wordChars('?','?');
st.wordChars('"','.');
st.ordinaryChars('0','9');
st.wordChars('0','9');
cmdBuffer = new StringBuffer();
inputSQLBuffer = new StringBuffer();
while ( st.nextToken() != StreamTokenizer.TT_EOF)
{
if ( st.ttype == StreamTokenizer.TT_WORD )
nextToken = st.sval.trim();
else
continue;
if ( inputSQLBuffer.length() > 0 ) inputSQLBuffer.append(" ");
inputSQLBuffer.append(nextToken);
}
ft = new FieldTokenizer(inputSQLBuffer.toString(),' ',false);
while ( ft.hasMoreFields() )
{
nextField = ft.nextField();
upperField = nextField.toUpperCase();
if ( statementType == (String)null )
{
statementType = upperField;
lastIndex = getKeywordIndex(statementType,statementType);
if ( lastIndex != 0 ) throwException(9);
keyWord = statementType;
} else {
keyIndex = getKeywordIndex(statementType,upperField);
if ( keyIndex < 0 )
{
if ( cmdBuffer.length() > 0 ) cmdBuffer.append(" ");
cmdBuffer.append(nextField);
} else {
setPhrase(keyWord,cmdBuffer.toString());
cmdBuffer = new StringBuffer();
keyWord = upperField;
if ( tinySQLGlobals.PARSER_DEBUG )
System.out.println("Found keyword " + keyWord);
}
}
}
if ( keyWord != (String)null ) setPhrase(keyWord,cmdBuffer.toString());
addAction();
if ( tinySQLGlobals.PARSER_DEBUG )
System.out.println("SQL:"+inputSQLBuffer.toString());
} catch ( Exception ex ) {
if ( tinySQLGlobals.DEBUG ) ex.printStackTrace(System.out);
throw new tinySQLException(ex.getMessage());
}
}
/*
* This method sets up particular phrase elements for the SQL command.
* Examples would be a list of selected columns and tables for a SELECT
* statement, or a list of column definitions for a CREATE TABLE
* statement. These phrase elements will be added to the action list
* once the entire statement has been parsed.
*/
public void setPhrase(String inputKeyWord,String inputString)
throws tinySQLException
{
String nextField,upperField,colTypeStr,colTypeSpec,
fieldString,syntaxErr,tempString,columnName,columnAlias;
StringBuffer colTypeBuffer,concatBuffer;
FieldTokenizer ft1,ft2,ft3;
tsColumn createColumn;
int i,j,k,lenc,colType,countFields;
/*
* Handle compound keywords.
*/
if ( inputString == (String)null )
{
lastKeyWord = inputKeyWord;
return;
} else if ( inputString.trim().length() == 0 ) {
lastKeyWord = inputKeyWord;
return;
}
if ( tinySQLGlobals.PARSER_DEBUG )
System.out.println("setPhrase " + inputString);
ft1 = new FieldTokenizer(inputString,',',false);
while ( ft1.hasMoreFields() )
{
nextField = ft1.nextField().trim();
if ( tinySQLGlobals.PARSER_DEBUG )
System.out.println(inputKeyWord + " field is " + nextField);
upperField = nextField.toUpperCase();
if ( inputKeyWord.equals("SELECT") )
{
/*
* Check for the keyword DISTINCT
*/
if (nextField.toUpperCase().startsWith("DISTINCT") )
{
distinct = true;
nextField = nextField.substring(9).trim();
}
/*
* Check for and set column alias.
*/
ft2 = new FieldTokenizer(nextField,' ',false);
columnName = ft2.getField(0);
columnAlias = (String)null;
/*
* A column alias can be preceded by the keyword AS which will
* be ignored by tinySQL.
*/
if ( ft2.countFields() == 2 ) columnAlias = ft2.getField(1);
else if ( ft2.countFields() == 3 ) columnAlias = ft2.getField(2);
/*
* Check for column concatenation using the | symbol
*/
ft2 = new FieldTokenizer(columnName,'|',false);
if ( ft2.countFields() > 1 )
{
concatBuffer = new StringBuffer("CONCAT(");
while ( ft2.hasMoreFields() )
{
if ( concatBuffer.length() > 7 )
concatBuffer.append(",");
concatBuffer.append(ft2.nextField());
}
columnName = concatBuffer.toString() + ")";
}
columnList.addElement(columnName);
columnAliasList.addElement(columnAlias);
contextList.addElement(inputKeyWord);
} else if ( inputKeyWord.equals("TABLE") ) {
/*
* If the input keyword is TABLE, update the statement type to be a
* compound type such as CREATE_TABLE, DROP_TABLE, or ALTER_TABLE.
*/
if ( !statementType.equals("INSERT") )
statementType = statementType + "_TABLE";
if ( statementType.equals("CREATE_TABLE") )
{
/*
* Parse out the column definition.
*/
ft2 = new FieldTokenizer(nextField,'(',false);
if ( ft2.countFields() != 2 ) throwException(1);
tableName = ft2.getField(0);
fieldString = ft2.getField(1);
ft2 = new FieldTokenizer(fieldString,',',false);
while ( ft2.hasMoreFields() )
{
tempString = ft2.nextField();
createColumn = parseColumnDefn(tempString);
if ( createColumn != (tsColumn)null )
columnList.addElement(createColumn);
}
} else if ( statementType.equals("DROP_TABLE") ) {
/*
* Handle dropping of non-existent tables
*/
tableName = upperField;
try
{
validateTable(upperField,true);
} catch ( Exception dropEx ) {
throw new tinySQLException("Table " + tableName
+ " does not exist.");
}
} else {
tableName = upperField;
validateTable(upperField,true);
}
} else if ( inputKeyWord.equals("BY") ) {
/*
* Set up Group by and Order by columns.
*/
if ( lastKeyWord == (String)null )
{
throwException(6);
} else {
ft3 = new FieldTokenizer(upperField,' ',false);
columnList.addElement(ft3.getField(0));
if ( ft3.countFields() == 2 )
{
/*
* ASC or DESC are the only allowable directives after GROUP BY
*/
if ( ft3.getField(1).startsWith("ASC") |
ft3.getField(1).startsWith("DESC") )
orderType = ft3.getField(1);
else
throwException(7);
}
if ( lastKeyWord.equals("ORDER") ) defaultOrderBy = false;
contextList.addElement(lastKeyWord);
}
} else if ( inputKeyWord.equals("DROP") ) {
/*
* Parse list of columns to be dropped.
*/
statementType = "ALTER_DROP";
ft2 = new FieldTokenizer(upperField,' ',false);
while ( ft2.hasMoreFields() )
{
columnList.addElement(UtilString.removeQuotes(ft2.nextField()));
}
} else if ( inputKeyWord.equals("RENAME") ) {
/*
* Parse old and new column name.
*/
statementType = "ALTER_RENAME";
ft2 = new FieldTokenizer(upperField,' ',false);
oldColumnName = ft2.getField(0);
newColumnName = ft2.getField(1);
if ( newColumnName.equals("TO") & ft2.countFields() == 3 )
newColumnName = ft2.getField(2);
if ( newColumnName.length() > 11 )
newColumnName = tinySQLGlobals.getShortName(newColumnName);
} else if ( inputKeyWord.equals("ADD") ) {
/*
* Parse definition of columns to be added.
*/
statementType = "ALTER_ADD";
createColumn = parseColumnDefn(nextField);
if ( createColumn != (tsColumn)null )
columnList.addElement(createColumn);
} else if ( inputKeyWord.equals("FROM") ) {
/*
* Check for valid table
*/
tableName = upperField;
validateTable(tableName);
} else if ( inputKeyWord.equals("INTO") ) {
ft2 = new FieldTokenizer(nextField,'(',false);
if ( ft2.countFields() != 2 ) throwException(3);
tableName = ft2.getField(0).toUpperCase();
validateTable(tableName);
fieldString = ft2.getField(1).toUpperCase();
ft2 = new FieldTokenizer(fieldString,',',false);
while ( ft2.hasMoreFields() )
{
tempString = UtilString.removeQuotes(ft2.nextField());
columnList.addElement(tempString);
contextList.addElement(inputKeyWord);
}
} else if ( inputKeyWord.equals("VALUES") ) {
ft2 = new FieldTokenizer(nextField,'(',false);
fieldString = ft2.getField(0);
ft2 = new FieldTokenizer(fieldString,',',false);
while ( ft2.hasMoreFields() )
{
tempString = UtilString.removeQuotes(ft2.nextField());
tempString = UtilString.replaceAll(tempString,"''","'");
valueList.addElement(tempString);
}
} else if ( inputKeyWord.equals("UPDATE") ) {
tableName = nextField.toUpperCase();
validateTable(tableName);
} else if ( inputKeyWord.equals("SET") ) {
/*
* Parse the update column name/value pairs
*/
ft2 = new FieldTokenizer(nextField,'=',false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -