📄 tinysqlparser.java
字号:
if ( ft2.countFields() != 2 ) throwException(4);
columnList.addElement(ft2.getField(0));
contextList.addElement(inputKeyWord);
valueList.addElement(UtilString.removeQuotes(ft2.getField(1)));
} else if ( inputKeyWord.equals("WHERE") ) {
whereClause = new tinySQLWhere(nextField,tables);
} else if ( !inputKeyWord.equals("TABLE") ) {
throwException(10);
}
}
lastKeyWord = inputKeyWord;
}
public void validateTable(String tableSpec) throws tinySQLException
{
validateTable(tableSpec,false);
}
public void validateTable(String tableSpec,boolean closeTable)
throws tinySQLException
{
/*
* Create a table object for each table used in the SELECT statement
* and store these objects in the tables Hashtable. Save the original
* list of table names to set the default selection order.
*
* If closeTable is true the table object will be closed after it is
* validated (for DROP TABLE and ALTER TABLE commands).
*/
String tableName,tableAlias,tableNameAndAlias,sortName;
tinySQLTable addTable,sortTable;
boolean tableAdded;
FieldTokenizer ftTable;
int i,addRowCount,sortRowCount;
ftTable = new FieldTokenizer(tableSpec,' ',false);
tableName = ftTable.getField(0);
tableAlias = (ftTable.getField(1,tableName)).toUpperCase();
tableNameAndAlias = tableName + "->" + tableAlias;
addTable = dbEngine.getTable(tableNameAndAlias);
addTable.GoTop();
addRowCount = addTable.GetRowCount();
if ( closeTable ) addTable.close();
if ( tinySQLGlobals.PARSER_DEBUG )
System.out.println("Add table " + tableNameAndAlias + " to tables");
tables.put(tableNameAndAlias,addTable);
tableAdded = false;
for (i = 0; i < tableList.size(); i++)
{
sortName = (String)tableList.elementAt(i);
sortTable = (tinySQLTable)tables.get(sortName);
sortRowCount = sortTable.GetRowCount();
/*
* Sort the table selections from smallest to largest table to
* enhance the query performance.
*/
if ( addRowCount > sortRowCount ) continue;
tableList.insertElementAt(tableNameAndAlias,i);
tableAdded = true;
break;
}
if ( !tableAdded ) tableList.addElement(tableNameAndAlias);
if ( tinySQLGlobals.PARSER_DEBUG )
{
System.out.println("Table selection order");
for ( i = 0; i < tableList.size(); i++ )
{
sortName = (String)tableList.elementAt(i);
sortTable = (tinySQLTable)tables.get(sortName);
sortRowCount = sortTable.GetRowCount();
System.out.println(sortName + " " + sortRowCount);
}
}
}
/*
* Validate the column specifications by checking against the tables.
*/
public void validateColumns() throws tinySQLException
{
String columnName,columnAlias,columnContext;
tsColumn columnObject;
boolean selectStar;
tinySQLTable jtbl;
int i,j;
/*
* Check for a column named *
*/
selectStar = false;
for (i = 0; i < columnList.size(); i++)
{
columnName = (String)columnList.elementAt(i);
columnContext = (String)contextList.elementAt(i);
if ( columnName.equals("*") )
{
if ( !columnContext.equals("SELECT") )
throw new tinySQLException("* must be a SELECT column.");
selectStar = true;
break;
}
}
if ( selectStar)
{
/*
* A column * has been found. Delete the existing list of SELECT
* columns and replace by using an enumeration variable to cycle through
* the columns in the tables Hashtable.
*/
for ( i = 0; i < columnList.size(); i++ )
{
columnContext = (String)contextList.elementAt(i);
if ( columnContext.equals("SELECT") )
{
columnList.removeElementAt(i);
contextList.removeElementAt(i);
columnAliasList.removeElementAt(i);
}
}
for ( i = 0; i < tableList.size(); i++ )
{
jtbl = (tinySQLTable)tables.get((String)tableList.elementAt(i));
/*
* Expand to all columns.
*/
for ( j = 0; j < jtbl.columnNameKeys.size(); j++ )
{
columnName = (String)jtbl.columnNameKeys.elementAt(j);
columnList.addElement(jtbl.table + "->" + jtbl.tableAlias
+ "." + columnName);
columnAliasList.addElement(columnName);
contextList.addElement("SELECT");
}
}
}
/*
* Build a column object for each selected column.
*/
if ( tables == (Hashtable)null )
System.out.println("*****Column validation - no tables defined.");
for (i = 0; i < columnList.size(); i++)
{
columnName = (String)columnList.elementAt(i);
columnContext = (String)contextList.elementAt(i);
columnAlias = (String)null;
if ( i < columnAliasList.size() )
columnAlias = (String)columnAliasList.elementAt(i);
columnObject = new tsColumn(columnName,tables,columnContext);
columnObject.alias = UtilString.removeQuotes(columnAlias);
columns.addElement(columnObject);
}
}
/*
* Parse out the column definition for a CREATE statement.
*/
public tsColumn parseColumnDefn(String columnDefn) throws tinySQLException
{
tsColumn createColumn;
int i;
FieldTokenizer ft;
String columnName,fieldString,tempString,colTypeStr,colTypeSpec;
ft = new FieldTokenizer(columnDefn.toUpperCase(),' ',false);
/*
* A column definition must consist of a column name followed by a
* column specification.
*/
if ( ft.countFields() < 2 ) throwException(2);
columnName = ft.getField(0);
/*
* Check for quotes around a column name that may contain blanks.
*/
if ( columnName.charAt(0) == '"' &
columnName.charAt(columnName.length() - 1) == '"' )
columnName = columnName.substring(1,columnName.length() - 1);
if ( columnName.length() > 11 )
{
columnName = tinySQLGlobals.getShortName(columnName);
}
createColumn = new tsColumn(columnName);
colTypeStr = "";
for ( i = 1; i < ft.countFields(); i++ )
colTypeStr += ft.getField(1);
ft = new FieldTokenizer(colTypeStr,'(',false);
colTypeStr = ft.getField(0);
createColumn.size = 10;
createColumn.decimalPlaces = 0;
if ( colTypeStr.equals("FLOAT") )
{
createColumn.size = 12;
createColumn.decimalPlaces = 2;
}
colTypeSpec = ft.getField(1);
if ( !colTypeSpec.equals("NULL") )
{
/*
* Parse out the scale and precision if supplied.
*/
ft = new FieldTokenizer(colTypeSpec,',',false);
createColumn.size = ft.getInt(0,8);
createColumn.decimalPlaces = ft.getInt(1,0);
}
createColumn.type = Integer.MIN_VALUE;
for ( i = 0; i < colTypeNames.length; i++ )
if ( colTypeStr.equals(colTypeNames[i]) )
createColumn.type = colTypes[i];
if ( createColumn.type == Integer.MIN_VALUE ) throwException(8);
if ( tinySQLGlobals.PARSER_DEBUG )
System.out.println("Column " + createColumn.name
+ ", type is " + createColumn.type + ",size is " + createColumn.size
+ ",precision is " + createColumn.decimalPlaces);
return createColumn;
}
/*
* This method is used to identify SQL key words, and the order in which they
* should appear in the SQL statement.
*/
public int getKeywordIndex(String inputContext,String inputWord)
{
String[][] sqlSyntax = {{"SELECT","FROM","WHERE","GROUP","ORDER","BY"},
{"INSERT","INTO","VALUES"},
{"DROP","TABLE"},
{"DELETE","FROM","WHERE"},
{"CREATE","TABLE"},
{"UPDATE","SET","WHERE"},
{"ALTER","TABLE","DROP","MODIFY","ADD","RENAME"}};
int i,j;
for ( i = 0; i < sqlSyntax.length; i++ )
{
for ( j = 0; j < sqlSyntax[i].length; j++ )
{
if ( sqlSyntax[i][0].equals(inputContext) &
sqlSyntax[i][j].equals(inputWord) )
return j;
}
}
return Integer.MIN_VALUE;
}
/*
* Add an action Hashtable to the list of actions
*/
public void addAction () throws tinySQLException, CloneNotSupportedException
{
int i,columnCount;
tsColumn checkColumn,orderColumn;
Hashtable newAction = new Hashtable();
newAction.put("TYPE", statementType);
if ( statementType.equals("SELECT") )
{
newAction.put("TABLES",tables);
if ( whereClause != (tinySQLWhere)null )
newAction.put("WHERE",whereClause);
/*
* Validate the column specifications and expand * if present
*/
validateColumns();
/*
* If no ORDER BY clause was specified, default to the list of
* SELECT columns.
*/
if ( defaultOrderBy )
{
columnCount = columns.size();
for ( i = 0; i < columnCount; i++ )
{
orderColumn = (tsColumn)(columns.elementAt(i));
if ( orderColumn.getContext("SELECT") )
{
orderColumn.addContext("ORDER");
}
}
}
newAction.put("COLUMNS",columns);
if ( orderType != (String)null ) newAction.put("ORDER_TYPE",orderType);
if ( distinct ) newAction.put("DISTINCT","TRUE");
} else if ( statementType.equals("DROP_TABLE") ) {
newAction.put("TABLE",tableName);
} else if ( statementType.equals("CREATE_TABLE") ) {
newAction.put("TABLE",tableName);
newAction.put("COLUMN_DEF",columnList);
} else if ( statementType.equals("ALTER_RENAME") ) {
newAction.put("TABLE",tableName);
newAction.put("OLD_COLUMN",oldColumnName);
newAction.put("NEW_COLUMN",newColumnName);
} else if ( statementType.equals("ALTER_ADD") ) {
newAction.put("TABLE",tableName);
newAction.put("COLUMN_DEF",columnList);
} else if ( statementType.equals("ALTER_DROP") ) {
newAction.put("TABLE",tableName);
newAction.put("COLUMNS",columnList);
} else if ( statementType.equals("DELETE") ) {
newAction.put("TABLE",tableName);
if ( whereClause != (tinySQLWhere)null )
newAction.put("WHERE",whereClause);
} else if ( statementType.equals("INSERT") |
statementType.equals("UPDATE") ) {
newAction.put("TABLE",tableName);
if ( columnList.size() != valueList.size() ) throwException(5);
newAction.put("COLUMNS",columnList);
newAction.put("VALUES",valueList);
if ( whereClause != (tinySQLWhere)null )
newAction.put("WHERE",whereClause);
}
actionList.addElement(newAction);
}
public void throwException(int exceptionNumber) throws tinySQLException
{
throwException(exceptionNumber,(String)null);
}
public void throwException(int exceptionNumber, String parameter)
throws tinySQLException
{
String exMsg = (String)null;
if ( exceptionNumber == 1 )
exMsg = "CREATE TABLE must be followed by a table name and a list"
+ " of column specifications enclosed in brackets.";
else if ( exceptionNumber == 2 )
exMsg = "A column specification must consist of a column name"
+ " followed by a column type specification.";
else if ( exceptionNumber == 3 )
exMsg = "INTO should be followed by a table name and "
+ "a list of columns enclosed in backets.";
else if ( exceptionNumber == 4 )
exMsg = "SET must be followed by assignments in the form"
+ " <columnName>=<value>.";
else if ( exceptionNumber == 5 )
exMsg = "INSERT statement number of columns and values provided"
+ " do not match.";
else if ( exceptionNumber == 6 )
exMsg = "BY cannot be the first keyword.";
else if ( exceptionNumber == 7 )
exMsg = "ORDER BY can only be followed by the ASC or DESC directives";
else if ( exceptionNumber == 8 )
exMsg = "Supported column types are INT,CHAR,FLOAT,DATE";
else if ( exceptionNumber == 9 )
exMsg = "Expecting SELECT, INSERT, ALTER, etc. in " + statementType;
else if ( exceptionNumber == 10 )
exMsg = "Unrecognized keyword ";
throw new tinySQLException(exMsg);
}
public Vector getActions()
{
return actionList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -