use of com.alibaba.druid.DbType in project druid by alibaba.
the class SqlTypeUDF method evaluate.
public String evaluate(String sql, String dbTypeName, boolean throwError) {
if (sql == null || sql.isEmpty()) {
return null;
}
DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
SQLType sqlType = null;
try {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
if (stmtList.size() == 0) {
return SQLType.EMPTY.name();
}
int setCnt = 0, notSetCnt = 0;
SQLStatement notSetStmt = null;
for (SQLStatement stmt : stmtList) {
if (stmt instanceof SQLSetStatement) {
setCnt++;
} else {
notSetStmt = stmt;
notSetCnt++;
}
}
if (setCnt == stmtList.size()) {
Lexer lexer = SQLParserUtils.createLexer(sql, dbType);
sqlType = lexer.scanSQLTypeV2();
if (sqlType != null) {
return sqlType.name();
}
return SQLType.SET.name();
}
if (notSetCnt == 1) {
Lexer lexer = SQLParserUtils.createLexer(sql, dbType);
sqlType = lexer.scanSQLTypeV2();
sqlType = SqlTypeUDF.getStmtSqlType(notSetStmt, sqlType);
} else {
for (SQLStatement stmt : stmtList) {
if (stmt instanceof OdpsQueryAliasStatement || stmt instanceof OdpsDeclareVariableStatement) {
return SQLType.SCRIPT.name();
}
}
Set<String> typeNameSet = new TreeSet<>();
for (SQLStatement stmt : stmtList) {
if (stmt instanceof SQLSetStatement) {
continue;
}
SQLType type = SqlTypeUDF.getStmtSqlType(stmt, null);
if (type != null) {
typeNameSet.add(type.name());
}
}
if (typeNameSet.size() == 1) {
return typeNameSet.stream().findFirst().get();
}
if (typeNameSet.size() > 0) {
StringBuffer buf = new StringBuffer();
for (String s : typeNameSet) {
if (buf.length() != 0) {
buf.append(',');
}
buf.append(s);
}
return "MULTI:" + buf.toString();
}
sqlType = SQLType.MULTI;
}
} catch (ParserException ex) {
sql = sql.trim();
int semiIndex = sql.indexOf(';');
if (semiIndex == sql.length() - 1 || semiIndex == -1 && sql.indexOf('\n') == -1) {
String singleLineSqlType = getSqlTypeForSingleLineSql(sql, dbType);
if (singleLineSqlType != null) {
return singleLineSqlType;
}
} else {
if (sql.lastIndexOf('\n', semiIndex) == -1) {
String firstSql = sql.substring(0, semiIndex).trim().toLowerCase();
if (firstSql.startsWith("set ")) {
String restSql = sql.substring(semiIndex + 1);
return evaluate(restSql, dbTypeName, throwError);
}
}
}
try {
Lexer lexer = SQLParserUtils.createLexer(sql, dbType);
int semiCnt = 0;
for_: for (Token token = null; ; ) {
lexer.nextToken();
if (token == Token.VARIANT && lexer.token() == Token.COLONEQ) {
return SQLType.SCRIPT.name();
}
token = lexer.token();
switch(token) {
case EOF:
case ERROR:
break for_;
case SEMI:
semiCnt++;
break;
default:
break;
}
}
} catch (ParserException ignored) {
}
sqlType = SQLType.ERROR;
} catch (Throwable ex) {
if (throwError) {
throw new IllegalArgumentException("error sql : \n" + sql, ex);
}
sqlType = SQLType.ERROR;
}
if (sqlType != null) {
return sqlType.name();
}
return SQLType.UNKNOWN.name();
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class SqlParams method evaluate.
public String evaluate(String sql, String dbTypeName, boolean throwError) {
try {
DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
List<Object> outParameters = new ArrayList<Object>();
ParameterizedOutputVisitorUtils.parameterize(sql, dbType, outParameters);
return JSONUtils.toJSONString(outParameters);
} catch (ParserException ex) {
if (throwError) {
throw new IllegalArgumentException("error sql : \n" + sql, ex);
}
return null;
}
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class SqlSyntaxCheck method evaluate.
public Boolean evaluate(String sql, String dbTypeName, boolean throwError) {
if (sql == null || sql.length() == 0) {
return null;
}
DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
try {
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> statementList = parser.parseStatementList();
return true;
} catch (ParserException ex) {
if (throwError) {
throw new IllegalArgumentException("error sql : \n" + sql, ex);
}
return false;
}
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class ExportConditionColumns method evaluate.
public String evaluate(String sql, String dbTypeName) {
DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
try {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
for (SQLStatement stmt : statementList) {
stmt.accept(visitor);
}
StringBuffer buf = new StringBuffer();
for (TableStat.Column column : visitor.getColumns()) {
if ((!column.isWhere()) && !column.isJoin()) {
continue;
}
if (buf.length() != 0) {
buf.append(',');
}
buf.append(column.toString());
}
return buf.toString();
} catch (Exception ex) {
System.err.println("error sql : " + sql);
ex.printStackTrace();
return null;
}
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class SqlFormat method evaluate.
public String evaluate(String sql, String dbTypeName, boolean throwError) {
DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
try {
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> statementList = parser.parseStatementList();
return SQLUtils.toSQLString(statementList, dbType);
} catch (Exception ex) {
if (throwError) {
throw new IllegalArgumentException("error sql : \n" + sql, ex);
}
return sql;
}
}
Aggregations