use of com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr in project sharding-jdbc by dangdangdotcom.
the class MySQLSelectVisitor method visit.
/**
* LIMIT 解析.
*
* @param x LIMIT表达式
* @return false 停止遍历AST
*/
@Override
public boolean visit(final MySqlSelectQueryBlock.Limit x) {
if (getParseContext().getParseContextIndex() > 0) {
return super.visit(x);
}
print("LIMIT ");
int offset = 0;
int offSetIndex = -1;
if (null != x.getOffset()) {
if (x.getOffset() instanceof SQLNumericLiteralExpr) {
offset = ((SQLNumericLiteralExpr) x.getOffset()).getNumber().intValue();
printToken(Limit.OFFSET_NAME, String.valueOf(offset));
print(", ");
} else {
offset = ((Number) getParameters().get(((SQLVariantRefExpr) x.getOffset()).getIndex())).intValue();
offSetIndex = ((SQLVariantRefExpr) x.getOffset()).getIndex();
print("?, ");
}
}
int rowCount;
int rowCountIndex = -1;
if (x.getRowCount() instanceof SQLNumericLiteralExpr) {
rowCount = ((SQLNumericLiteralExpr) x.getRowCount()).getNumber().intValue();
printToken(Limit.COUNT_NAME, String.valueOf(rowCount));
} else {
rowCount = ((Number) getParameters().get(((SQLVariantRefExpr) x.getRowCount()).getIndex())).intValue();
rowCountIndex = ((SQLVariantRefExpr) x.getRowCount()).getIndex();
print("?");
}
if (offset < 0 || rowCount < 0) {
throw new SQLParserException("LIMIT offset and row count can not be a negative value");
}
// "LIMIT {rowCount} OFFSET {offset}" will transform to "LIMIT {offset}, {rowCount}".So exchange parameter index
if (offSetIndex > -1 && rowCountIndex > -1 && offSetIndex > rowCountIndex) {
int tmp = rowCountIndex;
rowCountIndex = offSetIndex;
offSetIndex = tmp;
}
getParseContext().getParsedResult().getMergeContext().setLimit(new Limit(offset, rowCount, offSetIndex, rowCountIndex));
return false;
}
use of com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr in project druid by alibaba.
the class MySqlMockExecuteHandlerImpl method executeQueryFromDual.
public ResultSet executeQueryFromDual(MockStatementBase statement, SQLSelectQueryBlock query) throws SQLException {
MockResultSet rs = statement.getConnection().getDriver().createMockResultSet(statement);
MockResultSetMetaData metaData = rs.getMockMetaData();
Object[] row = new Object[query.getSelectList().size()];
for (int i = 0, size = query.getSelectList().size(); i < size; ++i) {
ColumnMetaData column = new ColumnMetaData();
SQLSelectItem item = query.getSelectList().get(i);
SQLExpr expr = item.getExpr();
if (expr instanceof SQLIntegerExpr) {
row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
column.setColumnType(Types.INTEGER);
} else if (expr instanceof SQLNumberExpr) {
row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
column.setColumnType(Types.DECIMAL);
} else if (expr instanceof SQLCharExpr) {
row[i] = ((SQLCharExpr) expr).getText();
column.setColumnType(Types.VARCHAR);
} else if (expr instanceof SQLNCharExpr) {
row[i] = ((SQLNCharExpr) expr).getText();
column.setColumnType(Types.NVARCHAR);
} else if (expr instanceof SQLBooleanExpr) {
row[i] = ((SQLBooleanExpr) expr).getValue();
column.setColumnType(Types.NVARCHAR);
} else if (expr instanceof SQLNullExpr) {
row[i] = null;
} else if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
if ("NOW".equalsIgnoreCase(methodInvokeExpr.getMethodName())) {
row[i] = new Timestamp(System.currentTimeMillis());
} else {
throw new SQLException("TODO");
}
} else if (expr instanceof SQLVariantRefExpr) {
SQLVariantRefExpr varExpr = (SQLVariantRefExpr) expr;
int varIndex = varExpr.getIndex();
if (statement instanceof MockPreparedStatement) {
MockPreparedStatement mockPstmt = (MockPreparedStatement) statement;
row[i] = mockPstmt.getParameters().get(varIndex);
} else {
row[i] = null;
}
} else {
row[i] = null;
}
metaData.getColumns().add(column);
}
rs.getRows().add(row);
return rs;
}
use of com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr in project druid by alibaba.
the class PagerUtils method getLimit.
/**
*
* @param sql
* @param dbType
* @return if not exists limit, return -1;
*/
public static int getLimit(String sql, String dbType) {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
if (stmtList.size() != 1) {
return -1;
}
SQLStatement stmt = stmtList.get(0);
if (stmt instanceof SQLSelectStatement) {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery query = selectStmt.getSelect().getQuery();
if (query instanceof SQLSelectQueryBlock) {
if (query instanceof MySqlSelectQueryBlock) {
SQLLimit limit = ((MySqlSelectQueryBlock) query).getLimit();
if (limit == null) {
return -1;
}
SQLExpr rowCountExpr = limit.getRowCount();
if (rowCountExpr instanceof SQLNumericLiteralExpr) {
int rowCount = ((SQLNumericLiteralExpr) rowCountExpr).getNumber().intValue();
return rowCount;
}
return Integer.MAX_VALUE;
}
if (query instanceof OdpsSelectQueryBlock) {
SQLLimit limit = ((OdpsSelectQueryBlock) query).getLimit();
SQLExpr rowCountExpr = limit != null ? limit.getRowCount() : null;
if (rowCountExpr instanceof SQLNumericLiteralExpr) {
int rowCount = ((SQLNumericLiteralExpr) rowCountExpr).getNumber().intValue();
return rowCount;
}
return Integer.MAX_VALUE;
}
return -1;
}
}
return -1;
}
use of com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr in project druid by alibaba.
the class OracleExprParser method primaryRest.
public SQLExpr primaryRest(SQLExpr expr) {
if (expr.getClass() == SQLIdentifierExpr.class) {
String ident = ((SQLIdentifierExpr) expr).getName();
if ("TIMESTAMP".equalsIgnoreCase(ident)) {
if (lexer.token() != Token.LITERAL_ALIAS && lexer.token() != Token.LITERAL_CHARS) {
return new SQLIdentifierExpr("TIMESTAMP");
}
SQLTimestampExpr timestamp = new SQLTimestampExpr();
String literal = lexer.stringVal();
timestamp.setLiteral(literal);
accept(Token.LITERAL_CHARS);
if (identifierEquals("AT")) {
lexer.nextToken();
acceptIdentifier("TIME");
acceptIdentifier("ZONE");
String timezone = lexer.stringVal();
timestamp.setTimeZone(timezone);
accept(Token.LITERAL_CHARS);
}
return primaryRest(timestamp);
}
}
if (lexer.token() == Token.IDENTIFIER && expr instanceof SQLNumericLiteralExpr) {
String ident = lexer.stringVal();
if (ident.length() == 1) {
char unit = ident.charAt(0);
switch(unit) {
case 'K':
case 'M':
case 'G':
case 'T':
case 'P':
case 'E':
case 'k':
case 'm':
case 'g':
case 't':
case 'p':
case 'e':
expr = new OracleSizeExpr(expr, OracleSizeExpr.Unit.valueOf(ident.toUpperCase()));
lexer.nextToken();
break;
default:
break;
}
}
}
if (lexer.token() == Token.DOTDOT) {
lexer.nextToken();
SQLExpr upBound = expr();
return new OracleRangeExpr(expr, upBound);
}
if (lexer.token() == Token.MONKEYS_AT) {
lexer.nextToken();
OracleDbLinkExpr dblink = new OracleDbLinkExpr();
dblink.setExpr(expr);
if (lexer.token() == Token.BANG) {
dblink.setDbLink("!");
lexer.nextToken();
} else {
String link = lexer.stringVal();
accept(Token.IDENTIFIER);
dblink.setDbLink(link);
}
expr = dblink;
}
if (identifierEquals("DAY") || identifierEquals("YEAR")) {
lexer.mark();
String name = lexer.stringVal();
lexer.nextToken();
if (lexer.token() == Token.COMMA) {
lexer.reset();
return expr;
}
OracleIntervalExpr interval = new OracleIntervalExpr();
interval.setValue(expr);
OracleIntervalType type = OracleIntervalType.valueOf(name);
interval.setType(type);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
if (lexer.token() != Token.LITERAL_INT) {
throw new ParserException("syntax error");
}
interval.setPrecision(lexer.integerValue().intValue());
lexer.nextToken();
accept(Token.RPAREN);
}
accept(Token.TO);
if (identifierEquals("SECOND")) {
lexer.nextToken();
interval.setToType(OracleIntervalType.SECOND);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
if (lexer.token() != Token.LITERAL_INT) {
throw new ParserException("syntax error");
}
interval.setFactionalSecondsPrecision(lexer.integerValue().intValue());
lexer.nextToken();
accept(Token.RPAREN);
}
} else {
interval.setToType(OracleIntervalType.MONTH);
lexer.nextToken();
}
expr = interval;
}
if (identifierEquals("AT")) {
char markChar = lexer.current();
int markBp = lexer.bp();
lexer.nextToken();
if (identifierEquals("LOCAL")) {
lexer.nextToken();
expr = new OracleDatetimeExpr(expr, new SQLIdentifierExpr("LOCAL"));
} else {
if (identifierEquals("TIME")) {
lexer.nextToken();
} else {
lexer.reset(markBp, markChar, Token.IDENTIFIER);
return expr;
}
acceptIdentifier("ZONE");
SQLExpr timeZone = primary();
expr = new OracleDatetimeExpr(expr, timeZone);
}
}
SQLExpr restExpr = super.primaryRest(expr);
if (restExpr != expr && restExpr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvoke = (SQLMethodInvokeExpr) restExpr;
if (methodInvoke.getParameters().size() == 1) {
SQLExpr paramExpr = methodInvoke.getParameters().get(0);
if (paramExpr instanceof SQLIdentifierExpr && "+".equals(((SQLIdentifierExpr) paramExpr).getName())) {
OracleOuterExpr outerExpr = new OracleOuterExpr();
if (methodInvoke.getOwner() == null) {
outerExpr.setExpr(new SQLIdentifierExpr(methodInvoke.getMethodName()));
} else {
outerExpr.setExpr(new SQLPropertyExpr(methodInvoke.getOwner(), methodInvoke.getMethodName()));
}
return outerExpr;
}
}
}
return restExpr;
}
use of com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr in project druid by alibaba.
the class ExportParameterVisitorUtils method exportParameter.
public static SQLExpr exportParameter(final List<Object> parameters, final SQLExpr param) {
Object value = null;
boolean replace = false;
if (param instanceof SQLCharExpr) {
value = ((SQLCharExpr) param).getText();
replace = true;
}
if (param instanceof SQLBooleanExpr) {
value = ((SQLBooleanExpr) param).getValue();
replace = true;
}
if (param instanceof SQLNumericLiteralExpr) {
value = ((SQLNumericLiteralExpr) param).getNumber();
replace = true;
}
if (replace) {
SQLObject parent = param.getParent();
if (parent != null) {
List<SQLObject> mergedList = (List<SQLObject>) parent.getAttribute(ParameterizedOutputVisitorUtils.ATTR_MERGED);
if (mergedList != null) {
List<Object> mergedListParams = new ArrayList<Object>(mergedList.size() + 1);
for (int i = 0; i < mergedList.size(); ++i) {
SQLObject item = mergedList.get(i);
if (item instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpItem = (SQLBinaryOpExpr) item;
exportParameter(mergedListParams, binaryOpItem.getRight());
}
}
if (mergedListParams.size() > 0) {
mergedListParams.add(0, value);
value = mergedListParams;
}
}
}
parameters.add(value);
return new SQLVariantRefExpr("?");
}
return param;
}
Aggregations