use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project Mycat-Server by MyCATApache.
the class ServerLoadDataInfileHandler method buildResultSet.
private RouteResultset buildResultSet(Map<String, LoadData> routeMap) {
// 强制local
statement.setLocal(true);
// 默认druid会过滤掉路径的分隔符,所以这里重新设置下
SQLLiteralExpr fn = new SQLCharExpr(fileName);
statement.setFileName(fn);
String srcStatement = statement.toString();
RouteResultset rrs = new RouteResultset(srcStatement, ServerParse.LOAD_DATA_INFILE_SQL);
rrs.setLoadData(true);
rrs.setStatement(srcStatement);
rrs.setAutocommit(serverConnection.isAutocommit());
rrs.setFinishedRoute(true);
int size = routeMap.size();
RouteResultsetNode[] routeResultsetNodes = new RouteResultsetNode[size];
int index = 0;
for (String dn : routeMap.keySet()) {
RouteResultsetNode rrNode = new RouteResultsetNode(dn, ServerParse.LOAD_DATA_INFILE_SQL, srcStatement);
rrNode.setSource(rrs);
rrNode.setTotalNodeSize(size);
rrNode.setStatement(srcStatement);
LoadData newLoadData = new LoadData();
ObjectUtil.copyProperties(loadData, newLoadData);
newLoadData.setLocal(true);
LoadData loadData1 = routeMap.get(dn);
// if (isHasStoreToFile)
if (// 此处判断是否有保存分库load的临时文件dn1.txt/dn2.txt,不是判断是否有clientTemp.txt
loadData1.getFileName() != null) {
newLoadData.setFileName(loadData1.getFileName());
} else {
newLoadData.setData(loadData1.getData());
}
rrNode.setLoadData(newLoadData);
routeResultsetNodes[index] = rrNode;
index++;
}
rrs.setNodes(routeResultsetNodes);
return rrs;
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project Mycat-Server by MyCATApache.
the class GlobalTableUtil method addColumnIfCreate.
static String addColumnIfCreate(String sql, SQLStatement statement) {
if (isCreate(statement) && sql.trim().toUpperCase().startsWith("CREATE TABLE ") && !hasGlobalColumn(statement)) {
SQLColumnDefinition column = new SQLColumnDefinition();
column.setDataType(new SQLCharacterDataType("bigint"));
column.setName(new SQLIdentifierExpr(GLOBAL_TABLE_MYCAT_COLUMN));
column.setComment(new SQLCharExpr("全局表保存修改时间戳的字段名"));
((SQLCreateTableStatement) statement).getTableElementList().add(column);
}
return statement.toString();
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project druid by alibaba.
the class SQLTransformUtils method transformOracleToPostgresql.
public static SQLExpr transformOracleToPostgresql(SQLMethodInvokeExpr x) {
final long nameHashCode64 = x.methodNameHashCode64();
List<SQLExpr> parameters = x.getArguments();
if (nameHashCode64 == FnvHash.Constants.SYS_GUID) {
SQLMethodInvokeExpr uuid_generate_v4 = new SQLMethodInvokeExpr("uuid_generate_v4");
uuid_generate_v4.setParent(x.getParent());
return uuid_generate_v4;
}
if (nameHashCode64 == FnvHash.Constants.TRUNC) {
if (parameters.size() == 1) {
SQLExpr param0 = parameters.get(0);
if (param0 instanceof OracleSysdateExpr || (param0 instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) param0).nameHashCode64() == FnvHash.Constants.CURRENT_TIMESTAMP)) {
SQLMethodInvokeExpr current_timestamp = new SQLMethodInvokeExpr("CURRENT_TIMESTAMP");
current_timestamp.addArgument(new SQLIntegerExpr(0));
current_timestamp.setParent(x.getParent());
return current_timestamp;
}
}
}
if (nameHashCode64 == FnvHash.Constants.CURRENT_TIMESTAMP) {
if (parameters.size() == 0 && x.getParent() instanceof SQLColumnDefinition) {
SQLDataType dataType = ((SQLColumnDefinition) x.getParent()).getDataType();
if (dataType.nameHashCode64() == FnvHash.Constants.TIMESTAMP && dataType.getArguments().size() == 1) {
x.addArgument(dataType.getArguments().get(0).clone());
} else {
x.addArgument(new SQLIntegerExpr(0));
}
return x;
}
}
if (nameHashCode64 == FnvHash.Constants.SYSTIMESTAMP) {
SQLMethodInvokeExpr xx = x.clone();
xx.setMethodName("SYSTIMESTAMP");
xx.setParent(x.getParent());
return xx;
}
if (nameHashCode64 == FnvHash.Constants.LOCALTIMESTAMP) {
SQLMethodInvokeExpr xx = x.clone();
xx.setMethodName("LOCALTIMESTAMP");
xx.setParent(x.getParent());
return xx;
}
if (nameHashCode64 == FnvHash.Constants.USERENV) {
if (x.getArguments().size() == 1) {
SQLExpr param0 = x.getArguments().get(0);
if (param0 instanceof SQLCharExpr) {
String text = ((SQLCharExpr) param0).getText();
if ("SESSIONID".equalsIgnoreCase(text)) {
SQLMethodInvokeExpr xx = new SQLMethodInvokeExpr();
xx.setMethodName("get_session_id");
xx.setParent(x.getParent());
return xx;
}
}
}
}
if (nameHashCode64 == FnvHash.Constants.USERENV) {
if (x.getArguments().size() == 1) {
SQLExpr param0 = x.getArguments().get(0);
if (param0 instanceof SQLCharExpr) {
String text = ((SQLCharExpr) param0).getText();
if ("SESSIONID".equalsIgnoreCase(text)) {
SQLMethodInvokeExpr xx = new SQLMethodInvokeExpr();
xx.setMethodName("get_session_id");
xx.setParent(x.getParent());
return xx;
}
}
}
}
if (nameHashCode64 == FnvHash.Constants.NUMTODSINTERVAL) {
if (x.getArguments().size() == 2) {
SQLExpr param0 = x.getArguments().get(0);
SQLExpr param1 = x.getArguments().get(1);
if (param0 instanceof SQLIntegerExpr && param1 instanceof SQLCharExpr) {
String text = ((SQLCharExpr) param1).getText();
if ("DAY".equalsIgnoreCase(text)) {
SQLIntervalExpr intervalExpr = new SQLIntervalExpr();
intervalExpr.setValue(new SQLCharExpr(param0.toString() + " DAYS"));
intervalExpr.setParent(x.getParent());
return intervalExpr;
}
}
}
}
return x;
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project druid by alibaba.
the class ToDate method eval.
public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
final List<SQLExpr> arguments = x.getArguments();
if (arguments.size() == 0) {
return EVAL_ERROR;
}
if (arguments.size() == 2 && arguments.get(0) instanceof SQLCharExpr && arguments.get(1) instanceof SQLCharExpr) {
String chars = ((SQLCharExpr) arguments.get(0)).getText();
String format = ((SQLCharExpr) arguments.get(1)).getText();
if (format.equals("yyyymmdd")) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
try {
return dateFormat.parse(chars);
} catch (ParseException e) {
// skip
return false;
}
}
}
return null;
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr 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).getBooleanValue();
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;
}
Aggregations