use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.
the class MySQLDDLParser method tableOption.
private boolean tableOption(TableOptions options) throws SQLSyntaxErrorException {
Identifier id = null;
Expression expr = null;
os: switch(lexer.token()) {
case KW_CHARACTER:
lexer.nextToken();
match(KW_SET);
if (lexer.token() == OP_EQUALS) {
lexer.nextToken();
}
id = identifier();
options.setCharSet(id);
break;
case KW_COLLATE:
lexer.nextToken();
if (lexer.token() == OP_EQUALS) {
lexer.nextToken();
}
id = identifier();
options.setCollation(id);
break;
case KW_DEFAULT:
// | [DEFAULT] COLLATE [=] collation_name
switch(lexer.nextToken()) {
case KW_CHARACTER:
lexer.nextToken();
match(KW_SET);
if (lexer.token() == OP_EQUALS) {
lexer.nextToken();
}
id = identifier();
options.setCharSet(id);
break os;
case KW_COLLATE:
lexer.nextToken();
if (lexer.token() == OP_EQUALS) {
lexer.nextToken();
}
id = identifier();
options.setCollation(id);
break os;
case IDENTIFIER:
SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
if (si != null) {
switch(si) {
case CHARSET:
lexer.nextToken();
if (lexer.token() == OP_EQUALS) {
lexer.nextToken();
}
id = identifier();
options.setCharSet(id);
break os;
}
}
default:
lexer.addCacheToke(KW_DEFAULT);
return false;
}
case KW_INDEX:
// | INDEX DIRECTORY [=] 'absolute path to directory'
lexer.nextToken();
if (lexer.token() == IDENTIFIER && "DIRECTORY".equals(lexer.stringValueUppercase())) {
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
options.setIndexDir((LiteralString) exprParser.expression());
break;
}
lexer.addCacheToke(KW_INDEX);
return true;
case KW_UNION:
// | UNION [=] (tbl_name[,tbl_name]...)
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
match(PUNC_LEFT_PAREN);
List<Identifier> union = new ArrayList<Identifier>(2);
for (int j = 0; lexer.token() != PUNC_RIGHT_PAREN; ++j) {
if (j > 0)
match(PUNC_COMMA);
id = identifier();
union.add(id);
}
match(PUNC_RIGHT_PAREN);
options.setUnion(union);
break os;
case IDENTIFIER:
SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
if (si != null) {
switch(si) {
case CHARSET:
// CHARSET [=] charset_name
lexer.nextToken();
if (lexer.token() == OP_EQUALS) {
lexer.nextToken();
}
id = identifier();
options.setCharSet(id);
break os;
case ENGINE:
// ENGINE [=] engine_name
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
id = identifier();
options.setEngine(id);
break os;
case AUTO_INCREMENT:
// | AUTO_INCREMENT [=] value
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
expr = exprParser.expression();
options.setAutoIncrement(expr);
break os;
case AVG_ROW_LENGTH:
// | AVG_ROW_LENGTH [=] value
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
expr = exprParser.expression();
options.setAvgRowLength(expr);
break os;
case CHECKSUM:
// | CHECKSUM [=] {0 | 1}
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
switch(lexer.token()) {
case LITERAL_BOOL_FALSE:
lexer.nextToken();
options.setCheckSum(false);
case LITERAL_BOOL_TRUE:
lexer.nextToken();
options.setCheckSum(true);
break;
case LITERAL_NUM_PURE_DIGIT:
int intVal = lexer.integerValue().intValue();
lexer.nextToken();
if (intVal == 0) {
options.setCheckSum(false);
} else {
options.setCheckSum(true);
}
break;
default:
throw new SQLSyntaxErrorException("table option of CHECKSUM error");
}
break os;
case DELAY_KEY_WRITE:
// | DELAY_KEY_WRITE [=] {0 | 1}
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
switch(lexer.token()) {
case LITERAL_BOOL_FALSE:
lexer.nextToken();
options.setDelayKeyWrite(false);
case LITERAL_BOOL_TRUE:
lexer.nextToken();
options.setDelayKeyWrite(true);
break;
case LITERAL_NUM_PURE_DIGIT:
int intVal = lexer.integerValue().intValue();
lexer.nextToken();
if (intVal == 0) {
options.setDelayKeyWrite(false);
} else {
options.setDelayKeyWrite(true);
}
break;
default:
throw new SQLSyntaxErrorException("table option of DELAY_KEY_WRITE error");
}
break os;
case COMMENT:
// | COMMENT [=] 'string'
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
options.setComment((LiteralString) exprParser.expression());
break os;
case CONNECTION:
// | CONNECTION [=] 'connect_string'
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
options.setConnection((LiteralString) exprParser.expression());
break os;
case DATA:
// | DATA DIRECTORY [=] 'absolute path to directory'
lexer.nextToken();
matchIdentifier("DIRECTORY");
if (lexer.token() == OP_EQUALS) {
lexer.nextToken();
}
options.setDataDir((LiteralString) exprParser.expression());
break os;
case INSERT_METHOD:
// | INSERT_METHOD [=] { NO | FIRST | LAST }
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
switch(matchIdentifier("NO", "FIRST", "LAST")) {
case 0:
options.setInsertMethod(TableOptions.InsertMethod.NO);
break;
case 1:
options.setInsertMethod(TableOptions.InsertMethod.FIRST);
break;
case 2:
options.setInsertMethod(TableOptions.InsertMethod.LAST);
break;
}
break os;
case KEY_BLOCK_SIZE:
// | KEY_BLOCK_SIZE [=] value
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
options.setKeyBlockSize(exprParser.expression());
break os;
case MAX_ROWS:
// | MAX_ROWS [=] value
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
options.setMaxRows(exprParser.expression());
break os;
case MIN_ROWS:
// | MIN_ROWS [=] value
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
options.setMinRows(exprParser.expression());
break os;
case PACK_KEYS:
// | PACK_KEYS [=] {0 | 1 | DEFAULT}
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
switch(lexer.token()) {
case LITERAL_BOOL_FALSE:
lexer.nextToken();
options.setPackKeys(TableOptions.PackKeys.FALSE);
break;
case LITERAL_BOOL_TRUE:
lexer.nextToken();
options.setPackKeys(TableOptions.PackKeys.TRUE);
break;
case LITERAL_NUM_PURE_DIGIT:
int intVal = lexer.integerValue().intValue();
lexer.nextToken();
if (intVal == 0) {
options.setPackKeys(TableOptions.PackKeys.FALSE);
} else {
options.setPackKeys(TableOptions.PackKeys.TRUE);
}
break;
case KW_DEFAULT:
lexer.nextToken();
options.setPackKeys(TableOptions.PackKeys.DEFAULT);
break;
default:
throw new SQLSyntaxErrorException("table option of PACK_KEYS error");
}
break os;
case PASSWORD:
// | PASSWORD [=] 'string'
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
options.setPassword((LiteralString) exprParser.expression());
break os;
case ROW_FORMAT:
// {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
if (lexer.nextToken() == OP_EQUALS) {
lexer.nextToken();
}
switch(lexer.token()) {
case KW_DEFAULT:
lexer.nextToken();
options.setRowFormat(TableOptions.RowFormat.DEFAULT);
break os;
case IDENTIFIER:
SpecialIdentifier sid = specialIdentifiers.get(lexer.stringValueUppercase());
if (sid != null) {
switch(sid) {
case DYNAMIC:
lexer.nextToken();
options.setRowFormat(TableOptions.RowFormat.DYNAMIC);
break os;
case FIXED:
lexer.nextToken();
options.setRowFormat(TableOptions.RowFormat.FIXED);
break os;
case COMPRESSED:
lexer.nextToken();
options.setRowFormat(TableOptions.RowFormat.COMPRESSED);
break os;
case REDUNDANT:
lexer.nextToken();
options.setRowFormat(TableOptions.RowFormat.REDUNDANT);
break os;
case COMPACT:
lexer.nextToken();
options.setRowFormat(TableOptions.RowFormat.COMPACT);
break os;
}
}
default:
throw new SQLSyntaxErrorException("table option of ROW_FORMAT error");
}
}
}
default:
return false;
}
return true;
}
use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.
the class MySQLLexer method nextToken.
public MySQLToken nextToken() throws SQLSyntaxErrorException {
if (tokenCache2 != null) {
tokenCache2 = null;
return tokenCache;
}
if (tokenCache != null) {
tokenCache = null;
return token;
}
if (token == MySQLToken.EOF) {
throw new SQLSyntaxErrorException("eof for sql is already reached, cannot get new token");
}
MySQLToken t;
do {
skipSeparator();
t = nextTokenInternal();
} while (inCStyleComment && inCStyleCommentIgnore || MySQLToken.PUNC_C_STYLE_COMMENT_END == t);
return t;
}
use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.
the class MySQLExprParser method ordinaryFunction.
/**
* id has been consumed. id must be a function name. current token must be
* {@link MySQLToken#PUNC_LEFT_PAREN}
*
* @param idUpper must be name of a function
* @return never null
*/
private FunctionExpression ordinaryFunction(String id, String idUpper) throws SQLSyntaxErrorException {
idUpper = Identifier.unescapeName(idUpper);
match(PUNC_LEFT_PAREN);
FunctionExpression funcExpr;
if (lexer.token() == PUNC_RIGHT_PAREN) {
lexer.nextToken();
funcExpr = functionManager.createFunctionExpression(idUpper, null);
} else {
List<Expression> args = expressionList(new LinkedList<Expression>());
funcExpr = functionManager.createFunctionExpression(idUpper, args);
}
if (funcExpr == null) {
throw new SQLSyntaxErrorException(id + "() is not a function");
}
funcExpr.setCacheEvalRst(cacheEvalRst);
return funcExpr;
}
use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.
the class SimpleHintParser method process.
@Override
public void process(CobarHint hint, String hintName, String sql) throws SQLSyntaxErrorException {
Object value = parsePrimary(hint, sql);
if (value instanceof Long)
value = ((Long) value).intValue();
Map<String, Object> properties = new HashMap<String, Object>(1, 1);
properties.put(hintName, value);
try {
ParameterMapping.mapping(hint, properties);
} catch (Throwable t) {
throw new SQLSyntaxErrorException(t);
}
}
use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.
the class SQLParserDelegateTest method testProperlyEnd.
public void testProperlyEnd() throws SQLSyntaxErrorException {
String sql = "select * from tb1;";
SQLStatement stmt = SQLParserDelegate.parse(sql);
Assert.assertEquals(DMLSelectStatement.class, stmt.getClass());
sql = "select * from tb1 ;;; ";
stmt = SQLParserDelegate.parse(sql);
Assert.assertEquals(DMLSelectStatement.class, stmt.getClass());
sql = "select * from tb1 /***/ ";
stmt = SQLParserDelegate.parse(sql);
Assert.assertEquals(DMLSelectStatement.class, stmt.getClass());
sql = "select * from tb1 , ";
try {
stmt = SQLParserDelegate.parse(sql);
Assert.fail("should detect inproperly end");
} catch (SQLSyntaxErrorException e) {
}
sql = "select * from tb1 ;, ";
try {
stmt = SQLParserDelegate.parse(sql);
Assert.fail("should detect inproperly end");
} catch (SQLSyntaxErrorException e) {
}
}
Aggregations