use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.
the class MySqlSelectIntoParser method query.
@Override
public SQLSelectQuery query(SQLObject parent, boolean acceptUnion) {
if (lexer.token() == (Token.LPAREN)) {
lexer.nextToken();
SQLSelectQuery select = query();
accept(Token.RPAREN);
return queryRest(select, acceptUnion);
}
MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock();
if (lexer.token() == Token.SELECT) {
lexer.nextToken();
if (lexer.token() == Token.HINT) {
this.exprParser.parseHints(queryBlock.getHints());
}
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
}
if (lexer.token() == (Token.DISTINCT)) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
} else if (lexer.identifierEquals("DISTINCTROW")) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCTROW);
lexer.nextToken();
} else if (lexer.token() == (Token.ALL)) {
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
lexer.nextToken();
}
if (lexer.identifierEquals("HIGH_PRIORITY")) {
queryBlock.setHignPriority(true);
lexer.nextToken();
}
if (lexer.identifierEquals("STRAIGHT_JOIN")) {
queryBlock.setStraightJoin(true);
lexer.nextToken();
}
if (lexer.identifierEquals("SQL_SMALL_RESULT")) {
queryBlock.setSmallResult(true);
lexer.nextToken();
}
if (lexer.identifierEquals("SQL_BIG_RESULT")) {
queryBlock.setBigResult(true);
lexer.nextToken();
}
if (lexer.identifierEquals("SQL_BUFFER_RESULT")) {
queryBlock.setBufferResult(true);
lexer.nextToken();
}
if (lexer.identifierEquals("SQL_CACHE")) {
queryBlock.setCache(true);
lexer.nextToken();
}
if (lexer.identifierEquals("SQL_NO_CACHE")) {
queryBlock.setCache(false);
lexer.nextToken();
}
if (lexer.identifierEquals("SQL_CALC_FOUND_ROWS")) {
queryBlock.setCalcFoundRows(true);
lexer.nextToken();
}
parseSelectList(queryBlock);
argsList = parseIntoArgs();
}
parseFrom(queryBlock);
parseWhere(queryBlock);
parseGroupBy(queryBlock);
queryBlock.setOrderBy(this.exprParser.parseOrderBy());
if (lexer.token() == Token.LIMIT) {
queryBlock.setLimit(this.exprParser.parseLimit());
}
if (lexer.token() == Token.PROCEDURE) {
lexer.nextToken();
throw new ParserException("TODO. " + lexer.info());
}
parseInto(queryBlock);
if (lexer.token() == Token.FOR) {
lexer.nextToken();
accept(Token.UPDATE);
queryBlock.setForUpdate(true);
}
if (lexer.token() == Token.LOCK) {
lexer.nextToken();
accept(Token.IN);
acceptIdentifier("SHARE");
acceptIdentifier("MODE");
queryBlock.setLockInShareMode(true);
}
return queryRest(queryBlock, acceptUnion);
}
use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.
the class OracleCreateTableParser method parseCreateTable.
public OracleCreateTableStatement parseCreateTable(boolean acceptCreate) {
OracleCreateTableStatement stmt = (OracleCreateTableStatement) super.parseCreateTable(acceptCreate);
if (lexer.token() == Token.OF) {
lexer.nextToken();
stmt.setOf(this.exprParser.name());
if (lexer.identifierEquals("OIDINDEX")) {
lexer.nextToken();
OracleCreateTableStatement.OIDIndex oidIndex = new OracleCreateTableStatement.OIDIndex();
if (lexer.token() != Token.LPAREN) {
oidIndex.setName(this.exprParser.name());
}
accept(Token.LPAREN);
this.getExprParser().parseSegmentAttributes(oidIndex);
accept(Token.RPAREN);
stmt.setOidIndex(oidIndex);
}
}
for (; ; ) {
this.getExprParser().parseSegmentAttributes(stmt);
if (lexer.identifierEquals(FnvHash.Constants.IN_MEMORY_METADATA)) {
lexer.nextToken();
stmt.setInMemoryMetadata(true);
continue;
} else if (lexer.identifierEquals(FnvHash.Constants.CURSOR_SPECIFIC_SEGMENT)) {
lexer.nextToken();
stmt.setCursorSpecificSegment(true);
continue;
} else if (lexer.identifierEquals(FnvHash.Constants.NOPARALLEL)) {
lexer.nextToken();
stmt.setParallel(false);
continue;
} else if (lexer.identifierEquals(FnvHash.Constants.PARALLEL)) {
lexer.nextToken();
stmt.setParallel(true);
if (lexer.token() == Token.LITERAL_INT) {
stmt.setParallelValue(this.exprParser.primary());
}
continue;
} else if (lexer.token() == Token.CACHE) {
lexer.nextToken();
stmt.setCache(Boolean.TRUE);
continue;
} else if (lexer.token() == Token.NOCACHE) {
lexer.nextToken();
stmt.setCache(Boolean.FALSE);
continue;
} else if (lexer.token() == Token.ENABLE) {
lexer.nextToken();
if (lexer.token() == Token.ROW) {
lexer.nextToken();
acceptIdentifier("MOVEMENT");
stmt.setEnableRowMovement(Boolean.TRUE);
} else {
throw new ParserException("TODO : " + lexer.info());
}
// stmt.setEnable(Boolean.TRUE);
continue;
} else if (lexer.token() == Token.DISABLE) {
lexer.nextToken();
if (lexer.token() == Token.ROW) {
lexer.nextToken();
acceptIdentifier("MOVEMENT");
stmt.setEnableRowMovement(Boolean.FALSE);
} else {
throw new ParserException("TODO : " + lexer.info());
}
// stmt.setEnable(Boolean.FALSE);
continue;
} else if (lexer.token() == Token.ON) {
lexer.nextToken();
accept(Token.COMMIT);
if (lexer.identifierEquals("PRESERVE")) {
lexer.nextToken();
acceptIdentifier("ROWS");
stmt.setOnCommitPreserveRows(true);
} else {
accept(Token.DELETE);
acceptIdentifier("ROWS");
stmt.setOnCommitDeleteRows(true);
}
continue;
} else if (lexer.identifierEquals("STORAGE")) {
OracleStorageClause storage = ((OracleExprParser) this.exprParser).parseStorage();
stmt.setStorage(storage);
continue;
} else if (lexer.identifierEquals("ORGANIZATION")) {
parseOrganization(stmt);
continue;
} else if (lexer.identifierEquals(FnvHash.Constants.CLUSTER)) {
lexer.nextToken();
SQLName cluster = this.exprParser.name();
stmt.setCluster(cluster);
accept(Token.LPAREN);
this.exprParser.names(stmt.getClusterColumns(), cluster);
accept(Token.RPAREN);
continue;
// } else if (lexer.token() == Token.STORAGE) {
// OracleStorageClause storage = ((OracleExprParser) this.exprParser).parseStorage();
// stmt.setStorage(storage);
// continue;
} else if (lexer.identifierEquals("MONITORING")) {
lexer.nextToken();
stmt.setMonitoring(true);
continue;
} else if (lexer.identifierEquals(FnvHash.Constants.INCLUDING)) {
lexer.nextToken();
this.exprParser.names(stmt.getIncluding(), stmt);
acceptIdentifier("OVERFLOW");
continue;
} else if (lexer.token() == Token.LOB) {
OracleLobStorageClause lobStorage = ((OracleExprParser) this.exprParser).parseLobStorage();
stmt.setLobStorage(lobStorage);
continue;
} else if (lexer.token() == Token.SEGMENT) {
lexer.nextToken();
accept(Token.CREATION);
if (lexer.token() == Token.IMMEDIATE) {
lexer.nextToken();
stmt.setDeferredSegmentCreation(DeferredSegmentCreation.IMMEDIATE);
} else {
accept(Token.DEFERRED);
stmt.setDeferredSegmentCreation(DeferredSegmentCreation.DEFERRED);
}
continue;
} else if (lexer.token() == Token.COLUMN) {
lexer.nextToken();
SQLName name = this.exprParser.name();
if (lexer.token() == Token.NOT) {
lexer.nextToken();
}
if (lexer.identifierEquals(FnvHash.Constants.SUBSTITUTABLE)) {
lexer.nextToken();
acceptIdentifier("AT");
accept(Token.ALL);
acceptIdentifier("LEVELS");
}
// skip
continue;
} else if (lexer.identifierEquals(FnvHash.Constants.VARRAY)) {
lexer.nextToken();
SQLName name = this.exprParser.name();
accept(Token.STORE);
accept(Token.AS);
if (lexer.identifierEquals(FnvHash.Constants.BASICFILE)) {
lexer.nextToken();
}
this.getExprParser().parseLobStorage();
throw new ParserException("TODO : " + lexer.info());
} else if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
accept(Token.BY);
if (lexer.identifierEquals("RANGE")) {
SQLPartitionByRange partitionByRange = this.getExprParser().partitionByRange();
this.getExprParser().partitionClauseRest(partitionByRange);
stmt.setPartitioning(partitionByRange);
continue;
} else if (lexer.identifierEquals("HASH")) {
SQLPartitionByHash partitionByHash = this.getExprParser().partitionByHash();
this.getExprParser().partitionClauseRest(partitionByHash);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (; ; ) {
SQLPartition partition = this.getExprParser().parsePartition();
partitionByHash.addPartition(partition);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else if (lexer.token() == Token.RPAREN) {
lexer.nextToken();
break;
}
throw new ParserException("TODO : " + lexer.info());
}
}
stmt.setPartitioning(partitionByHash);
continue;
} else if (lexer.identifierEquals("LIST")) {
SQLPartitionByList partitionByList = partitionByList();
this.getExprParser().partitionClauseRest(partitionByList);
stmt.setPartitioning(partitionByList);
continue;
} else {
throw new ParserException("TODO : " + lexer.info());
}
} else if (lexer.identifierEquals(FnvHash.Constants.XMLTYPE)) {
lexer.nextToken();
if (lexer.token() == Token.COLUMN) {
lexer.nextToken();
}
OracleXmlColumnProperties xmlColumnProperties = new OracleXmlColumnProperties();
xmlColumnProperties.setColumn(this.exprParser.name());
if (lexer.token() == Token.STORE) {
lexer.nextToken();
accept(Token.AS);
OracleXmlColumnProperties.OracleXMLTypeStorage storage = new OracleXmlColumnProperties.OracleXMLTypeStorage();
if (lexer.identifierEquals("SECUREFILE")) {
storage.setSecureFile(true);
lexer.nextToken();
} else if (lexer.identifierEquals("BASICFILE")) {
storage.setBasicFile(true);
lexer.nextToken();
}
if (lexer.identifierEquals("BINARY")) {
lexer.nextToken();
acceptIdentifier("XML");
storage.setBinaryXml(true);
} else if (lexer.identifierEquals("CLOB")) {
lexer.nextToken();
storage.setClob(true);
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
OracleLobParameters lobParameters = new OracleLobParameters();
for_: for (; ; ) {
switch(lexer.token()) {
case TABLESPACE:
{
lexer.nextToken();
SQLName tableSpace = this.exprParser.name();
lobParameters.setTableSpace(tableSpace);
}
continue for_;
case ENABLE:
case DISABLE:
{
Boolean enable = lexer.token() == Token.ENABLE;
lexer.nextToken();
accept(Token.STORAGE);
accept(Token.IN);
accept(Token.ROW);
lobParameters.setEnableStorageInRow(enable);
}
continue for_;
case CHUNK:
lexer.nextToken();
SQLExpr chunk = this.exprParser.expr();
lobParameters.setChunk(chunk);
continue for_;
case NOCACHE:
lexer.nextToken();
lobParameters.setCache(false);
continue for_;
case LOGGING:
lexer.nextToken();
lobParameters.setLogging(true);
continue for_;
case NOCOMPRESS:
lexer.nextToken();
lobParameters.setCompress(false);
continue for_;
case KEEP_DUPLICATES:
lexer.nextToken();
lobParameters.setKeepDuplicates(true);
continue for_;
case STORAGE:
OracleStorageClause storageClause = this.getExprParser().parseStorage();
lobParameters.setStorage(storageClause);
continue for_;
case IDENTIFIER:
long hash = lexer.hash_lower();
if (hash == FnvHash.Constants.PCTVERSION) {
lobParameters.setPctVersion(this.exprParser.primary());
lexer.nextToken();
continue for_;
}
break for_;
default:
break for_;
}
}
accept(Token.RPAREN);
storage.setLobParameters(lobParameters);
}
}
for (; ; ) {
if (lexer.identifierEquals(FnvHash.Constants.ALLOW)) {
lexer.nextToken();
if (lexer.identifierEquals("NONSCHEMA")) {
lexer.nextToken();
xmlColumnProperties.setAllowNonSchema(true);
} else if (lexer.identifierEquals("ANYSCHEMA")) {
lexer.nextToken();
xmlColumnProperties.setAllowAnySchema(true);
} else {
throw new ParserException("TODO : " + lexer.info());
}
continue;
} else if (lexer.identifierEquals(FnvHash.Constants.DISALLOW)) {
lexer.nextToken();
if (lexer.identifierEquals("NONSCHEMA")) {
lexer.nextToken();
xmlColumnProperties.setAllowNonSchema(false);
} else if (lexer.identifierEquals("ANYSCHEMA")) {
lexer.nextToken();
xmlColumnProperties.setAllowAnySchema(false);
} else {
throw new ParserException("TODO : " + lexer.info());
}
continue;
}
break;
}
// throw new ParserException("TODO : " + lexer.info());
stmt.setXmlTypeColumnProperties(xmlColumnProperties);
continue;
}
break;
}
if (lexer.token() == Token.AS) {
lexer.nextToken();
SQLSelect select = new OracleSelectParser(exprParser).select();
stmt.setSelect(select);
}
return stmt;
}
use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.
the class OracleCreateTableParser method parseCreateTableSupplementalLogingProps.
protected SQLTableElement parseCreateTableSupplementalLogingProps() {
acceptIdentifier("SUPPLEMENTAL");
acceptIdentifier("LOG");
if (lexer.token() == Token.GROUP) {
lexer.nextToken();
OracleSupplementalLogGrp logGrp = new OracleSupplementalLogGrp();
logGrp.setGroup(this.exprParser.name());
accept(Token.LPAREN);
for (; ; ) {
SQLName column = this.exprParser.name();
if (lexer.identifierEquals("NO")) {
lexer.nextToken();
acceptIdentifier("LOG");
column.putAttribute("NO LOG", Boolean.TRUE);
}
logGrp.addColumn(column);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
if (lexer.token() == Token.RPAREN) {
break;
}
throw new ParserException("TODO " + lexer.info());
}
accept(Token.RPAREN);
if (lexer.identifierEquals("ALWAYS")) {
lexer.nextToken();
logGrp.setAlways(true);
}
return logGrp;
} else if (lexer.identifierEquals(FnvHash.Constants.DATA)) {
lexer.nextToken();
OracleSupplementalIdKey idKey = new OracleSupplementalIdKey();
accept(Token.LPAREN);
for (; ; ) {
if (lexer.token() == Token.ALL) {
lexer.nextToken();
idKey.setAll(true);
} else if (lexer.token() == Token.PRIMARY) {
lexer.nextToken();
accept(Token.KEY);
idKey.setPrimaryKey(true);
} else if (lexer.token() == Token.UNIQUE) {
lexer.nextToken();
if (lexer.token() == Token.INDEX) {
lexer.nextToken();
idKey.setUniqueIndex(true);
} else {
idKey.setUnique(true);
}
} else if (lexer.token() == Token.FOREIGN) {
lexer.nextToken();
accept(Token.KEY);
idKey.setForeignKey(true);
}
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
if (lexer.token() == Token.RPAREN) {
break;
}
throw new ParserException("TODO " + lexer.info());
}
accept(Token.RPAREN);
acceptIdentifier("COLUMNS");
return idKey;
}
throw new ParserException("TODO " + lexer.info());
}
use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.
the class SqlParamsFill method evaluate.
public String evaluate(String sql, String params, String dbTypeName, boolean throwError) {
try {
DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
List<Object> inputParams = (List<Object>) JSONUtils.parse(params);
return ParameterizedOutputVisitorUtils.restore(sql, dbType, inputParams);
} catch (ParserException ex) {
if (throwError) {
throw new IllegalArgumentException("error sql : \n" + sql, ex);
}
return null;
}
}
use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.
the class WallProvider method checkInternal.
private WallCheckResult checkInternal(String sql) {
checkCount.incrementAndGet();
WallContext context = WallContext.current();
if (config.isDoPrivilegedAllow() && ispPrivileged()) {
WallCheckResult checkResult = new WallCheckResult();
checkResult.setSql(sql);
return checkResult;
}
// first step, check whiteList
boolean mulltiTenant = config.getTenantTablePattern() != null && config.getTenantTablePattern().length() > 0;
if (!mulltiTenant) {
WallCheckResult checkResult = checkWhiteAndBlackList(sql);
if (checkResult != null) {
checkResult.setSql(sql);
return checkResult;
}
}
hardCheckCount.incrementAndGet();
final List<Violation> violations = new ArrayList<Violation>();
List<SQLStatement> statementList = new ArrayList<SQLStatement>();
boolean syntaxError = false;
boolean endOfComment = false;
try {
SQLStatementParser parser = createParser(sql);
parser.getLexer().setCommentHandler(WallCommentHandler.instance);
if (!config.isCommentAllow()) {
// deny comment
parser.getLexer().setAllowComment(false);
}
if (!config.isCompleteInsertValuesCheck()) {
parser.setParseCompleteValues(false);
parser.setParseValuesSize(config.getInsertValuesCheckSize());
}
parser.parseStatementList(statementList);
final Token lastToken = parser.getLexer().token();
if (lastToken != Token.EOF && config.isStrictSyntaxCheck()) {
violations.add(new IllegalSQLObjectViolation(ErrorCode.SYNTAX_ERROR, "not terminal sql, token " + lastToken, sql));
}
endOfComment = parser.getLexer().isEndOfComment();
} catch (NotAllowCommentException e) {
violations.add(new IllegalSQLObjectViolation(ErrorCode.COMMENT_STATEMENT_NOT_ALLOW, "comment not allow", sql));
incrementCommentDeniedCount();
} catch (ParserException e) {
syntaxErrorCount.incrementAndGet();
syntaxError = true;
if (config.isStrictSyntaxCheck()) {
violations.add(new SyntaxErrorViolation(e, sql));
}
} catch (Exception e) {
if (config.isStrictSyntaxCheck()) {
violations.add(new SyntaxErrorViolation(e, sql));
}
}
if (statementList.size() > 1 && !config.isMultiStatementAllow()) {
violations.add(new IllegalSQLObjectViolation(ErrorCode.MULTI_STATEMENT, "multi-statement not allow", sql));
}
WallVisitor visitor = createWallVisitor();
visitor.setSqlEndOfComment(endOfComment);
if (statementList.size() > 0) {
boolean lastIsHint = false;
for (int i = 0; i < statementList.size(); i++) {
SQLStatement stmt = statementList.get(i);
if ((i == 0 || lastIsHint) && stmt instanceof MySqlHintStatement) {
lastIsHint = true;
continue;
}
try {
stmt.accept(visitor);
} catch (ParserException e) {
violations.add(new SyntaxErrorViolation(e, sql));
}
}
}
if (visitor.getViolations().size() > 0) {
violations.addAll(visitor.getViolations());
}
Map<String, WallSqlTableStat> tableStat = context.getTableStats();
boolean updateCheckHandlerEnable = false;
{
WallUpdateCheckHandler updateCheckHandler = config.getUpdateCheckHandler();
if (updateCheckHandler != null) {
for (SQLStatement stmt : statementList) {
if (stmt instanceof SQLUpdateStatement) {
SQLUpdateStatement updateStmt = (SQLUpdateStatement) stmt;
SQLName table = updateStmt.getTableName();
if (table != null) {
String tableName = table.getSimpleName();
Set<String> updateCheckColumns = config.getUpdateCheckTable(tableName);
if (updateCheckColumns != null && updateCheckColumns.size() > 0) {
updateCheckHandlerEnable = true;
break;
}
}
}
}
}
}
WallSqlStat sqlStat = null;
if (violations.size() > 0) {
violationCount.incrementAndGet();
if ((!updateCheckHandlerEnable) && sql.length() < MAX_SQL_LENGTH) {
sqlStat = addBlackSql(sql, tableStat, context.getFunctionStats(), violations, syntaxError);
}
} else {
if ((!updateCheckHandlerEnable) && sql.length() < MAX_SQL_LENGTH) {
boolean selectLimit = false;
if (config.getSelectLimit() > 0) {
for (SQLStatement stmt : statementList) {
if (stmt instanceof SQLSelectStatement) {
selectLimit = true;
break;
}
}
}
if (!selectLimit) {
sqlStat = addWhiteSql(sql, tableStat, context.getFunctionStats(), syntaxError);
}
}
}
if (sqlStat == null && updateCheckHandlerEnable) {
sqlStat = new WallSqlStat(tableStat, context.getFunctionStats(), violations, syntaxError);
}
Map<String, WallSqlTableStat> tableStats = null;
Map<String, WallSqlFunctionStat> functionStats = null;
if (context != null) {
tableStats = context.getTableStats();
functionStats = context.getFunctionStats();
recordStats(tableStats, functionStats);
}
WallCheckResult result;
if (sqlStat != null) {
context.setSqlStat(sqlStat);
result = new WallCheckResult(sqlStat, statementList);
} else {
result = new WallCheckResult(null, violations, tableStats, functionStats, statementList, syntaxError);
}
String resultSql;
if (visitor.isSqlModified()) {
resultSql = SQLUtils.toSQLString(statementList, dbType);
} else {
resultSql = sql;
}
result.setSql(resultSql);
result.setUpdateCheckItems(visitor.getUpdateCheckItems());
return result;
}
Aggregations