use of com.alibaba.druid.sql.ast.expr.SQLNumberExpr in project druid by alibaba.
the class EqualTest_number method test_exits.
public void test_exits() throws Exception {
String sql = "3.5";
String sql_c = "3.51";
SQLNumberExpr exprA, exprB, exprC;
{
OracleExprParser parser = new OracleExprParser(sql);
exprA = (SQLNumberExpr) parser.expr();
}
{
OracleExprParser parser = new OracleExprParser(sql);
exprB = (SQLNumberExpr) parser.expr();
}
{
OracleExprParser parser = new OracleExprParser(sql_c);
exprC = (SQLNumberExpr) parser.expr();
}
Assert.assertEquals(exprA, exprB);
Assert.assertNotEquals(exprA, exprC);
Assert.assertTrue(exprA.equals(exprA));
Assert.assertFalse(exprA.equals(new Object()));
Assert.assertEquals(exprA.hashCode(), exprB.hashCode());
Assert.assertEquals(new SQLNumberExpr(), new SQLNumberExpr());
Assert.assertEquals(new SQLNumberExpr().hashCode(), new SQLNumberExpr().hashCode());
}
use of com.alibaba.druid.sql.ast.expr.SQLNumberExpr in project sharding-jdbc by dangdangdotcom.
the class MySQLInsertVisitor method supplyAutoIncrementColumn.
private void supplyAutoIncrementColumn(final Collection<String> autoIncrementColumns, final String tableName, final List<SQLExpr> columns, final List<SQLExpr> values) {
boolean isPreparedStatement = !getParameters().isEmpty();
GeneratedKeyContext generatedKeyContext = getParseContext().getParsedResult().getGeneratedKeyContext();
if (isPreparedStatement) {
generatedKeyContext.getColumns().addAll(autoIncrementColumns);
}
TableRule tableRule = getParseContext().getShardingRule().findTableRule(tableName);
for (String each : autoIncrementColumns) {
SQLExpr sqlExpr;
Object id = tableRule.generateId(each);
generatedKeyContext.putValue(each, id);
if (isPreparedStatement) {
sqlExpr = new SQLVariantRefExpr("?");
getParameters().add(id);
((SQLVariantRefExpr) sqlExpr).setIndex(getParametersSize() - 1);
} else {
sqlExpr = (id instanceof Number) ? new SQLNumberExpr((Number) id) : new SQLCharExpr((String) id);
}
getParseContext().addCondition(each, tableName, BinaryOperator.EQUAL, sqlExpr, getDatabaseType(), getParameters());
columns.add(new SQLIdentifierExpr(each));
values.add(sqlExpr);
}
}
use of com.alibaba.druid.sql.ast.expr.SQLNumberExpr in project Mycat-Server by MyCATApache.
the class HintSQLHandler method parseProcedure.
private Procedure parseProcedure(String sql, Map hintMap) {
boolean fields = hintMap.containsKey("list_fields");
boolean isResultList = hintMap != null && ("list".equals(hintMap.get("result_type")) || fields);
Procedure procedure = new Procedure();
procedure.setOriginSql(sql);
procedure.setResultList(isResultList);
List<String> sqls = Splitter.on(";").trimResults().splitToList(sql);
Set<String> outSet = new HashSet<>();
for (int i = sqls.size() - 1; i >= 0; i--) {
String s = sqls.get(i);
if (Strings.isNullOrEmpty(s)) {
continue;
}
SQLStatementParser parser = new MySqlStatementParser(s);
SQLStatement statement = parser.parseStatement();
if (statement instanceof SQLSelectStatement) {
MySqlSelectQueryBlock selectQuery = (MySqlSelectQueryBlock) ((SQLSelectStatement) statement).getSelect().getQuery();
if (selectQuery != null) {
List<SQLSelectItem> selectItems = selectQuery.getSelectList();
for (SQLSelectItem selectItem : selectItems) {
String select = selectItem.toString();
outSet.add(select);
procedure.getSelectColumns().add(select);
}
}
procedure.setSelectSql(s);
} else if (statement instanceof SQLCallStatement) {
SQLCallStatement sqlCallStatement = (SQLCallStatement) statement;
procedure.setName(sqlCallStatement.getProcedureName().getSimpleName());
List<SQLExpr> paramterList = sqlCallStatement.getParameters();
for (int i1 = 0; i1 < paramterList.size(); i1++) {
SQLExpr sqlExpr = paramterList.get(i1);
String pName = sqlExpr.toString();
String pType = outSet.contains(pName) ? ProcedureParameter.OUT : ProcedureParameter.IN;
ProcedureParameter parameter = new ProcedureParameter();
parameter.setIndex(i1 + 1);
parameter.setName(pName);
parameter.setParameterType(pType);
if (pName.startsWith("@")) {
procedure.getParamterMap().put(pName, parameter);
} else {
procedure.getParamterMap().put(String.valueOf(i1 + 1), parameter);
}
}
procedure.setCallSql(s);
} else if (statement instanceof SQLSetStatement) {
procedure.setSetSql(s);
SQLSetStatement setStatement = (SQLSetStatement) statement;
List<SQLAssignItem> sets = setStatement.getItems();
for (SQLAssignItem set : sets) {
String name = set.getTarget().toString();
SQLExpr value = set.getValue();
ProcedureParameter parameter = procedure.getParamterMap().get(name);
if (parameter != null) {
if (value instanceof SQLIntegerExpr) {
parameter.setValue(((SQLIntegerExpr) value).getNumber());
parameter.setJdbcType(Types.INTEGER);
} else if (value instanceof SQLNumberExpr) {
parameter.setValue(((SQLNumberExpr) value).getNumber());
parameter.setJdbcType(Types.NUMERIC);
} else if (value instanceof SQLTextLiteralExpr) {
parameter.setValue(((SQLTextLiteralExpr) value).getText());
parameter.setJdbcType(Types.VARCHAR);
} else if (value instanceof SQLValuableExpr) {
parameter.setValue(((SQLValuableExpr) value).getValue());
parameter.setJdbcType(Types.VARCHAR);
}
}
}
}
}
if (fields) {
String list_fields = (String) hintMap.get("list_fields");
List<String> listFields = Splitter.on(",").trimResults().splitToList(list_fields);
for (String field : listFields) {
if (!procedure.getParamterMap().containsKey(field)) {
ProcedureParameter parameter = new ProcedureParameter();
parameter.setParameterType(ProcedureParameter.OUT);
parameter.setName(field);
parameter.setJdbcType(-10);
parameter.setIndex(procedure.getParamterMap().size() + 1);
procedure.getParamterMap().put(field, parameter);
}
}
procedure.getListFields().addAll(listFields);
}
return procedure;
}
use of com.alibaba.druid.sql.ast.expr.SQLNumberExpr in project druid by alibaba.
the class PagerUtils method limitOracle.
private static String limitOracle(SQLSelect select, String dbType, int offset, int count) {
SQLSelectQuery query = select.getQuery();
if (query instanceof SQLSelectQueryBlock) {
OracleSelectQueryBlock queryBlock = (OracleSelectQueryBlock) query;
if (queryBlock.getGroupBy() == null && select.getOrderBy() == null && offset <= 0) {
SQLExpr condition = new //
SQLBinaryOpExpr(//
new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.LessThanOrEqual, //
new SQLNumberExpr(count), JdbcConstants.ORACLE);
if (queryBlock.getWhere() == null) {
queryBlock.setWhere(condition);
} else {
queryBlock.setWhere(new //
SQLBinaryOpExpr(//
queryBlock.getWhere(), //
SQLBinaryOperator.BooleanAnd, //
condition, JdbcConstants.ORACLE));
}
return SQLUtils.toSQLString(select, dbType);
}
}
OracleSelectQueryBlock countQueryBlock = new OracleSelectQueryBlock();
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr("ROWNUM"), "RN"));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
countQueryBlock.setWhere(new //
SQLBinaryOpExpr(//
new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.LessThanOrEqual, //
new SQLNumberExpr(count + offset), JdbcConstants.ORACLE));
if (offset <= 0) {
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
OracleSelectQueryBlock offsetQueryBlock = new OracleSelectQueryBlock();
offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
offsetQueryBlock.setWhere(new //
SQLBinaryOpExpr(//
new SQLIdentifierExpr("RN"), //
SQLBinaryOperator.GreaterThan, //
new SQLNumberExpr(offset), JdbcConstants.ORACLE));
return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
use of com.alibaba.druid.sql.ast.expr.SQLNumberExpr in project druid by alibaba.
the class PagerUtils method limitSQLServer.
private static String limitSQLServer(SQLSelect select, String dbType, int offset, int count) {
SQLSelectQuery query = select.getQuery();
SQLBinaryOpExpr gt = new //
SQLBinaryOpExpr(//
new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.GreaterThan, //
new SQLNumberExpr(offset), JdbcConstants.SQL_SERVER);
SQLBinaryOpExpr lteq = new //
SQLBinaryOpExpr(//
new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.LessThanOrEqual, //
new SQLNumberExpr(count + offset), JdbcConstants.SQL_SERVER);
SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq, JdbcConstants.SQL_SERVER);
if (query instanceof SQLSelectQueryBlock) {
SQLServerSelectQueryBlock queryBlock = (SQLServerSelectQueryBlock) query;
if (offset <= 0) {
queryBlock.setTop(new SQLServerTop(new SQLNumberExpr(count)));
return SQLUtils.toSQLString(select, dbType);
}
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
SQLOrderBy orderBy = select.getOrderBy();
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
countQueryBlock.setWhere(pageCondition);
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
if (offset <= 0) {
countQueryBlock.setTop(new SQLServerTop(new SQLNumberExpr(count)));
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
SQLOrderBy orderBy = select.getOrderBy();
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
SQLServerSelectQueryBlock offsetQueryBlock = new SQLServerSelectQueryBlock();
offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
offsetQueryBlock.setWhere(pageCondition);
return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
Aggregations