Search in sources :

Example 16 with SQLCharExpr

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;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) LoadData(io.mycat.sqlengine.mpp.LoadData) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 17 with SQLCharExpr

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();
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 18 with SQLCharExpr

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;
}
Also used : SQLIntervalExpr(com.alibaba.druid.sql.ast.expr.SQLIntervalExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) OracleSysdateExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSysdateExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 19 with SQLCharExpr

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;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 20 with SQLCharExpr

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;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLException(java.sql.SQLException) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) MockResultSet(com.alibaba.druid.mock.MockResultSet) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) Timestamp(java.sql.Timestamp) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) ColumnMetaData(com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData)

Aggregations

SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)37 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)16 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)15 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)8 SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)8 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)8 SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)7 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)6 TableConfig (io.mycat.config.model.TableConfig)6 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)6 SlotFunction (io.mycat.route.function.SlotFunction)6 SQLNonTransientException (java.sql.SQLNonTransientException)6 ParserException (com.alibaba.druid.sql.parser.ParserException)5 SQLName (com.alibaba.druid.sql.ast.SQLName)4 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)4 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4 RouteResultsetNode (io.mycat.route.RouteResultsetNode)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)3