Search in sources :

Example 1 with SQLTextLiteralExpr

use of com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr in project Mycat-Server by MyCATApache.

the class ServerLoadDataInfileHandler method parseLoadDataPram.

private void parseLoadDataPram() {
    loadData = new LoadData();
    SQLTextLiteralExpr rawLineEnd = (SQLTextLiteralExpr) statement.getLinesTerminatedBy();
    String lineTerminatedBy = rawLineEnd == null ? "\n" : rawLineEnd.getText();
    loadData.setLineTerminatedBy(lineTerminatedBy);
    SQLTextLiteralExpr rawFieldEnd = (SQLTextLiteralExpr) statement.getColumnsTerminatedBy();
    String fieldTerminatedBy = rawFieldEnd == null ? "\t" : rawFieldEnd.getText();
    loadData.setFieldTerminatedBy(fieldTerminatedBy);
    SQLTextLiteralExpr rawEnclosed = (SQLTextLiteralExpr) statement.getColumnsEnclosedBy();
    String enclose = rawEnclosed == null ? null : rawEnclosed.getText();
    loadData.setEnclose(enclose);
    SQLTextLiteralExpr escapseExpr = (SQLTextLiteralExpr) statement.getColumnsEscaped();
    String escapse = escapseExpr == null ? "\\" : escapseExpr.getText();
    loadData.setEscape(escapse);
    String charset = statement.getCharset() != null ? statement.getCharset() : serverConnection.getCharset();
    loadData.setCharset(charset);
    loadData.setFileName(fileName);
}
Also used : LoadData(io.mycat.sqlengine.mpp.LoadData) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr)

Example 2 with SQLTextLiteralExpr

use of com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr 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;
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLValuableExpr(com.alibaba.druid.sql.ast.expr.SQLValuableExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) List(java.util.List) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) HashSet(java.util.HashSet) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr)

Aggregations

SQLTextLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)1 SQLValuableExpr (com.alibaba.druid.sql.ast.expr.SQLValuableExpr)1 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)1 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)1 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)1 LoadData (io.mycat.sqlengine.mpp.LoadData)1 HashSet (java.util.HashSet)1 List (java.util.List)1