📄 sqlparser.java
字号:
private CommandSelect singleSelect() throws SQLException{
CommandSelect selCmd = new CommandSelect(con.log);
SQLToken token;
// scan for prefix like DISTINCT, ALL and the TOP clause; sample: SELECT TOP 15 ...
Switch: while(true){
token = nextToken(MISSING_EXPRESSION);
switch(token.value){
case SQLTokenizer.TOP:
token = nextToken(MISSING_EXPRESSION);
try{
int maxRows = Integer.parseInt(token.getName(sql));
selCmd.setMaxRows(maxRows);
}catch(NumberFormatException e){
throw createSyntaxError(token, Language.STXADD_NOT_NUMBER, token.getName(sql));
}
break;
case SQLTokenizer.ALL:
selCmd.setDistinct(false);
break;
case SQLTokenizer.DISTINCT:
selCmd.setDistinct(true);
break;
default:
previousToken();
break Switch;
}
}
while(true){
Expression column = expression(selCmd, 0);
selCmd.addColumnExpression( column );
token = nextToken();
if(token == null) return selCmd; // SELECT without FROM
boolean as = false;
if(token.value == SQLTokenizer.AS){
token = nextToken(MISSING_EXPRESSION);
as = true;
}
if(as || (!isKeyword(token))){
String alias = getIdentifier( token);
column.setAlias( alias );
token = nextToken();
if(token == null) return selCmd; // SELECT without FROM
}
switch(token.value){
case SQLTokenizer.COMMA:
if(column == null) throw createSyntaxError( token, MISSING_EXPRESSION );
column = null;
break;
case SQLTokenizer.FROM:
if(column == null) throw createSyntaxError( token, MISSING_EXPRESSION );
column = null;
from(selCmd);
return selCmd;
default:
if(!isKeyword(token))
throw createSyntaxError( token, new int[]{SQLTokenizer.COMMA, SQLTokenizer.FROM} );
previousToken();
return selCmd;
}
}
}
final private CommandSelect select() throws SQLException{
CommandSelect selCmd = singleSelect();
SQLToken token = nextToken();
UnionAll union = null;
while(token != null && token.value == SQLTokenizer.UNION){
if(union == null){
union = new UnionAll();
union.addDataSource(new ViewResult( con, selCmd ));
selCmd = new CommandSelect(con.log);
selCmd.setSource( union );
DataSources from = new DataSources();
from.add(union);
selCmd.setTables( from );
selCmd.addColumnExpression( new ExpressionName("*") );
}
nextToken(MISSING_ALL);
nextToken(MISSING_SELECT);
union.addDataSource( new ViewResult( con, singleSelect() ) );
token = nextToken();
}
if(token != null && token.value == SQLTokenizer.ORDER){
order( selCmd );
}else{
previousToken();
}
return selCmd;
}
private Command delete() throws SQLException{
CommandDelete cmd = new CommandDelete(con.log);
nextToken(MISSING_FROM);
from(cmd);
SQLToken token = nextToken();
if(token != null){
if(token.value != SQLTokenizer.WHERE)
throw this.createSyntaxError(token, MISSING_WHERE);
where(cmd);
}
return cmd;
}
private Command truncate() throws SQLException{
CommandDelete cmd = new CommandDelete(con.log);
nextToken(MISSING_TABLE);
from(cmd);
return cmd;
}
private Command insert() throws SQLException{
SQLToken token = nextToken( MISSING_INTO );
CommandInsert cmd = new CommandInsert( con.log, nextIdentifier() );
int parthesisCount = 0;
token = nextToken(MISSING_PARENTHESIS_VALUES_SELECT);
if(token.value == SQLTokenizer.PARENTHESIS_L){
token = nextToken(MISSING_EXPRESSION);
if(token.value == SQLTokenizer.SELECT){
parthesisCount++;
cmd.noColumns = true;
}else{
previousToken();
Expressions list = expressionParenthesisList(cmd);
for(int i=0; i<list.size(); i++){
cmd.addColumnExpression( list.get( i ) );
}
token = nextToken(MISSING_PARENTHESIS_VALUES_SELECT);
}
}else cmd.noColumns = true;
Switch: while(true)
switch(token.value){
case SQLTokenizer.VALUES:{
token = nextToken(MISSING_PARENTHESIS_L);
cmd.addValues( expressionParenthesisList(cmd) );
return cmd;
}
case SQLTokenizer.SELECT:
cmd.addValues( select() );
while(parthesisCount-- > 0){
nextToken(MISSING_PARENTHESIS_R);
}
return cmd;
case SQLTokenizer.PARENTHESIS_L:
token = nextToken(MISSING_PARENTHESIS_VALUES_SELECT);
parthesisCount++;
continue Switch;
default:
throw new Error();
}
}
private Command update() throws SQLException{
CommandUpdate cmd = new CommandUpdate(con.log);
// read table name
DataSources tables = new DataSources();
cmd.setTables( tables );
cmd.setSource( rowSource( cmd, tables, 0 ) );
SQLToken token = nextToken(MISSING_SET);
while(true){
token = nextToken();
Expression dest = expressionSingle( cmd, token);
if(dest.getType() != Expression.NAME) throw createSyntaxError( token, MISSING_IDENTIFIER );
nextToken(MISSING_EQUALS);
Expression src = expression(cmd, 0);
cmd.addSetting( dest, src);
token = nextToken();
if(token == null) break;
switch(token.value){
case SQLTokenizer.WHERE:
where(cmd);
return cmd;
case SQLTokenizer.COMMA:
continue;
default: throw createSyntaxError( token, MISSING_WHERE_COMMA );
}
}
return cmd;
}
private Command create() throws SQLException{
while(true){
SQLToken token = nextToken(COMMANDS_CREATE);
switch(token.value){
case SQLTokenizer.DATABASE:
return createDatabase();
case SQLTokenizer.TABLE:
return createTable();
case SQLTokenizer.VIEW:
return createView();
case SQLTokenizer.INDEX:
return createIndex(false);
case SQLTokenizer.PROCEDURE:
return createProcedure();
case SQLTokenizer.UNIQUE:
do{
token = nextToken(COMMANDS_CREATE_UNIQUE);
}while(token.value == SQLTokenizer.INDEX);
return createIndex(true);
case SQLTokenizer.NONCLUSTERED:
case SQLTokenizer.CLUSTERED:
continue;
default:
throw createSyntaxError( token, COMMANDS_CREATE );
}
}
}
private CommandCreateDatabase createDatabase() throws SQLException{
SQLToken token = nextToken();
if(token == null) throw createSyntaxError( token, MISSING_EXPRESSION );
return new CommandCreateDatabase( con.log, token.getName(sql));
}
private CommandTable createTable() throws SQLException{
String catalog;
String tableName = catalog = nextIdentifier();
tableName = nextIdentiferPart(tableName);
if(tableName == catalog) catalog = null;
CommandTable cmdCreate = new CommandTable( con.log, catalog, tableName, SQLTokenizer.CREATE );
SQLToken token = nextToken( MISSING_PARENTHESIS_L );
nextCol:
while(true){
token = nextToken( MISSING_EXPRESSION );
String constraintName;
if(token.value == SQLTokenizer.CONSTRAINT){
// reading a CONSTRAINT with name
constraintName = nextIdentifier();
token = nextToken( MISSING_KEYTYPE );
}else{
constraintName = null;
}
switch(token.value){
case SQLTokenizer.PRIMARY:
case SQLTokenizer.UNIQUE:
case SQLTokenizer.FOREIGN:
IndexDescription index = index(cmdCreate, token.value, tableName, constraintName, null);
if(token.value == SQLTokenizer.FOREIGN){
nextToken( MISSING_REFERENCES );
String pk = nextIdentifier();
Expressions expressions = new Expressions();
Strings columns = new Strings();
expressionDefList( cmdCreate, expressions, columns );
IndexDescription pkIndex = new IndexDescription( null, pk, SQLTokenizer.UNIQUE, expressions, columns);
ForeignKey foreignKey = new ForeignKey(pk, pkIndex, tableName, index);
cmdCreate.addForeingnKey(foreignKey);
}else{
cmdCreate.addIndex( index );
}
token = nextToken( MISSING_COMMA_PARENTHESIS );
switch(token.value){
case SQLTokenizer.PARENTHESIS_R:
return cmdCreate;
case SQLTokenizer.COMMA:
continue nextCol;
}
}
// the token is a column name
token = addColumn( token, cmdCreate );
if(token == null){
throw createSyntaxError(token, MISSING_COMMA_PARENTHESIS);
}
switch(token.value){
case SQLTokenizer.PARENTHESIS_R:
return cmdCreate;
case SQLTokenizer.COMMA:
continue nextCol;
default:
throw createSyntaxError(token, MISSING_COMMA_PARENTHESIS);
}
}
}
/**
* Parse a Column and add it to the Command. If the column is unique or primary
* then an index is added.
* @param token the SQLToken with the column name
* @return the token of the delimiter
*/
private SQLToken addColumn(SQLToken token, CommandTable cmdCreate) throws SQLException{
String colName = getIdentifier( token );
Column col = datatype(false);
col.setName( colName );
token = nextToken();
boolean nullableWasSet = false;
boolean defaultWasSet = col.isAutoIncrement(); // with data type COUNTER already this value is set
while(true){
if(token == null){
cmdCreate.addColumn( col );
return null;
}
switch(token.value){
case SQLTokenizer.PARENTHESIS_R:
case SQLTokenizer.COMMA:
cmdCreate.addColumn( col );
return token;
case SQLTokenizer.DEFAULT:
if(defaultWasSet) throw createSyntaxError( token, MISSING_COMMA_PARENTHESIS );
int offset = token.offset + token.length;
token = nextToken();
if(token != null) offset = token.offset;
previousToken();
Expression expr = expression(cmdCreate, 0);
SQLToken last = lastToken();
int length = last.offset + last.length - offset;
String def = new String( sql, offset, length );
col.setDefaultValue( expr, def );
defaultWasSet = true;
break;
case SQLTokenizer.IDENTITY:
if(defaultWasSet) throw createSyntaxError( token, MISSING_COMMA_PARENTHESIS );
col.setAutoIncrement(true);
defaultWasSet = true;
break;
case SQLTokenizer.NULL:
if(nullableWasSet) throw createSyntaxError( token, MISSING_COMMA_PARENTHESIS );
//col.setNullable(true); is already default
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -