use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.
the class SQLJudgeEngine method judge.
/**
* judge SQL Type only.
*
* @return SQL statement
*/
public SQLStatement judge() {
LexerEngine lexerEngine = LexerEngineFactory.newInstance(DatabaseType.MySQL, sql);
lexerEngine.nextToken();
while (true) {
TokenType tokenType = lexerEngine.getCurrentToken().getType();
if (tokenType instanceof Keyword) {
if (isDQL(tokenType)) {
return getDQLStatement();
}
if (isDML(tokenType)) {
return getDMLStatement(tokenType);
}
if (isDDL(tokenType)) {
return getDDLStatement();
}
if (isTCL(tokenType)) {
return getTCLStatement();
}
if (isDAL(tokenType)) {
return getDALStatement(tokenType, lexerEngine);
}
}
if (tokenType instanceof Assist && Assist.END == tokenType) {
throw new SQLParsingException("Unsupported SQL statement: [%s]", sql);
}
lexerEngine.nextToken();
}
}
use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.
the class Lexer method nextToken.
/**
* Analyse next token.
*/
public final void nextToken() {
skipIgnoredToken();
if (isVariableBegin()) {
currentToken = new Tokenizer(input, dictionary, offset).scanVariable();
} else if (isNCharBegin()) {
currentToken = new Tokenizer(input, dictionary, ++offset).scanChars();
} else if (isIdentifierBegin()) {
currentToken = new Tokenizer(input, dictionary, offset).scanIdentifier();
} else if (isHexDecimalBegin()) {
currentToken = new Tokenizer(input, dictionary, offset).scanHexDecimal();
} else if (isNumberBegin()) {
currentToken = new Tokenizer(input, dictionary, offset).scanNumber();
} else if (isSymbolBegin()) {
currentToken = new Tokenizer(input, dictionary, offset).scanSymbol();
} else if (isCharsBegin()) {
currentToken = new Tokenizer(input, dictionary, offset).scanChars();
} else if (isEnd()) {
currentToken = new Token(Assist.END, "", offset);
} else {
throw new SQLParsingException(this, Assist.ERROR);
}
offset = currentToken.getEndPosition();
}
use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.
the class OrderByClauseParser method parseSelectOrderByItem.
private OrderItem parseSelectOrderByItem(final SelectStatement selectStatement) {
SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
OrderDirection orderDirection = OrderDirection.ASC;
if (lexerEngine.skipIfEqual(DefaultKeyword.ASC)) {
orderDirection = OrderDirection.ASC;
} else if (lexerEngine.skipIfEqual(DefaultKeyword.DESC)) {
orderDirection = OrderDirection.DESC;
}
if (sqlExpression instanceof SQLNumberExpression) {
return new OrderItem(((SQLNumberExpression) sqlExpression).getNumber().intValue(), orderDirection, getNullOrderDirection());
}
if (sqlExpression instanceof SQLIdentifierExpression) {
return new OrderItem(SQLUtil.getExactlyValue(((SQLIdentifierExpression) sqlExpression).getName()), orderDirection, getNullOrderDirection(), selectStatement.getAlias(SQLUtil.getExactlyValue(((SQLIdentifierExpression) sqlExpression).getName())));
}
if (sqlExpression instanceof SQLPropertyExpression) {
SQLPropertyExpression sqlPropertyExpression = (SQLPropertyExpression) sqlExpression;
return new OrderItem(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName()), SQLUtil.getExactlyValue(sqlPropertyExpression.getName()), orderDirection, getNullOrderDirection(), selectStatement.getAlias(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName()) + "." + SQLUtil.getExactlyValue(sqlPropertyExpression.getName())));
}
if (sqlExpression instanceof SQLIgnoreExpression) {
SQLIgnoreExpression sqlIgnoreExpression = (SQLIgnoreExpression) sqlExpression;
return new OrderItem(sqlIgnoreExpression.getExpression(), orderDirection, getNullOrderDirection(), selectStatement.getAlias(sqlIgnoreExpression.getExpression()));
}
throw new SQLParsingException(lexerEngine);
}
use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.
the class AbstractCreateParser method parse.
@Override
public DDLStatement parse() {
lexerEngine.nextToken();
lexerEngine.skipAll(getSkippedKeywordsBetweenCreateIndexAndKeyword());
lexerEngine.skipAll(getSkippedKeywordsBetweenCreateAndKeyword());
DDLStatement result = new DDLStatement();
if (lexerEngine.skipIfEqual(DefaultKeyword.INDEX)) {
lexerEngine.skipAll(getSkippedKeywordsBetweenCreateIndexAndIndexName());
parseIndex(result);
} else if (lexerEngine.skipIfEqual(DefaultKeyword.TABLE)) {
lexerEngine.skipAll(getSkippedKeywordsBetweenCreateTableAndTableName());
} else {
throw new SQLParsingException("Can't support other CREATE grammar unless CREATE TABLE, CREATE INDEX.");
}
tableReferencesClauseParser.parseSingleTableWithoutAlias(result);
return result;
}
use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.
the class AbstractDropParser method parse.
@Override
public DDLStatement parse() {
lexerEngine.nextToken();
lexerEngine.skipAll(getSkippedKeywordsBetweenDropAndTable());
DDLStatement result = new DDLStatement();
if (lexerEngine.skipIfEqual(DefaultKeyword.INDEX)) {
lexerEngine.skipAll(getSkippedKeywordsBetweenDropIndexAndIndexName());
parseIndex(result);
} else if (lexerEngine.skipIfEqual(DefaultKeyword.TABLE)) {
lexerEngine.skipAll(getSkippedKeywordsBetweenDropTableAndTableName());
tableReferencesClauseParser.parseSingleTableWithoutAlias(result);
} else {
throw new SQLParsingException("Can't support other DROP grammar unless DROP TABLE, DROP INDEX.");
}
return result;
}
Aggregations