Search in sources :

Example 1 with SQLObject

use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.

the class MySqlOutputVisitor method visit.

public boolean visit(MySqlCreateTableStatement x) {
    print0(ucase ? "CREATE " : "create ");
    for (SQLCommentHint hint : x.getHints()) {
        hint.accept(this);
        print(' ');
    }
    if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) {
        print0(ucase ? "TEMPORARY TABLE " : "temporary table ");
    } else {
        print0(ucase ? "TABLE " : "table ");
    }
    if (x.isIfNotExiists()) {
        print0(ucase ? "IF NOT EXISTS " : "if not exists ");
    }
    printTableSourceExpr(x.getName());
    if (x.getLike() != null) {
        print0(ucase ? " LIKE " : " like ");
        x.getLike().accept(this);
    }
    int size = x.getTableElementList().size();
    if (size > 0) {
        print0(" (");
        incrementIndent();
        println();
        for (int i = 0; i < size; ++i) {
            if (i != 0) {
                print0(", ");
                println();
            }
            x.getTableElementList().get(i).accept(this);
        }
        decrementIndent();
        println();
        print(')');
    }
    for (Map.Entry<String, SQLObject> option : x.getTableOptions().entrySet()) {
        String key = option.getKey();
        print(' ');
        print0(ucase ? key : key.toLowerCase());
        if ("TABLESPACE".equals(key)) {
            print(' ');
            option.getValue().accept(this);
            continue;
        } else if ("UNION".equals(key)) {
            print0(" = (");
            option.getValue().accept(this);
            print(')');
            continue;
        }
        print0(" = ");
        option.getValue().accept(this);
    }
    if (x.getPartitioning() != null) {
        println();
        x.getPartitioning().accept(this);
    }
    if (x.getTableGroup() != null) {
        println();
        print0(ucase ? "TABLEGROUP " : "tablegroup ");
        x.getTableGroup().accept(this);
    }
    if (x.getSelect() != null) {
        incrementIndent();
        println();
        x.getSelect().accept(this);
        decrementIndent();
    }
    for (SQLCommentHint hint : x.getOptionHints()) {
        print(' ');
        hint.accept(this);
    }
    return false;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) Map(java.util.Map) MySqlForceIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) MySqlUseIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint) MySqlIgnoreIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint)

Example 2 with SQLObject

use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.

the class OracleSchemaStatVisitor method visit.

@Override
public boolean visit(ConditionalInsertClauseItem x) {
    SQLObject parent = x.getParent();
    if (parent instanceof ConditionalInsertClause) {
        parent = parent.getParent();
    }
    if (parent instanceof OracleMultiInsertStatement) {
        SQLSelect subQuery = ((OracleMultiInsertStatement) parent).getSubQuery();
        if (subQuery != null) {
            String table = (String) subQuery.getAttribute("_table_");
            setCurrentTable(x, table);
        }
    }
    x.getWhen().accept(this);
    x.getThen().accept(this);
    restoreCurrentTable(x);
    return false;
}
Also used : ConditionalInsertClause(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleMultiInsertStatement.ConditionalInsertClause) SQLObject(com.alibaba.druid.sql.ast.SQLObject) OracleMultiInsertStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleMultiInsertStatement) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect)

Example 3 with SQLObject

use of com.alibaba.druid.sql.ast.SQLObject 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;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) ArrayList(java.util.ArrayList) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) ArrayList(java.util.ArrayList) List(java.util.List) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 4 with SQLObject

use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.

the class ParameterizedOutputVisitorUtils method merge.

public static SQLBinaryOpExpr merge(ParameterizedVisitor v, SQLBinaryOpExpr x) {
    SQLExpr left = x.getLeft();
    SQLExpr right = x.getRight();
    SQLObject parent = x.getParent();
    if (left instanceof SQLLiteralExpr && right instanceof SQLLiteralExpr) {
        if (//
        x.getOperator() == SQLBinaryOperator.Equality || x.getOperator() == SQLBinaryOperator.NotEqual) {
            if ((left instanceof SQLIntegerExpr) && (right instanceof SQLIntegerExpr)) {
                if (((SQLIntegerExpr) left).getNumber().intValue() < 100) {
                    left.putAttribute(ATTR_PARAMS_SKIP, true);
                }
                if (((SQLIntegerExpr) right).getNumber().intValue() < 100) {
                    right.putAttribute(ATTR_PARAMS_SKIP, true);
                }
            } else {
                left.putAttribute(ATTR_PARAMS_SKIP, true);
                right.putAttribute(ATTR_PARAMS_SKIP, true);
            }
        }
        return x;
    }
    for (; ; ) {
        if (x.getRight() instanceof SQLBinaryOpExpr) {
            if (x.getLeft() instanceof SQLBinaryOpExpr) {
                SQLBinaryOpExpr leftBinaryExpr = (SQLBinaryOpExpr) x.getLeft();
                if (leftBinaryExpr.getRight().equals(x.getRight())) {
                    x = leftBinaryExpr;
                    v.incrementReplaceCunt();
                    continue;
                }
            }
            SQLExpr mergedRight = merge(v, (SQLBinaryOpExpr) x.getRight());
            if (mergedRight != x.getRight()) {
                x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), mergedRight);
                v.incrementReplaceCunt();
            }
            x.setParent(parent);
        }
        break;
    }
    if (x.getLeft() instanceof SQLBinaryOpExpr) {
        SQLExpr mergedLeft = merge(v, (SQLBinaryOpExpr) x.getLeft());
        if (mergedLeft != x.getLeft()) {
            SQLBinaryOpExpr tmp = new SQLBinaryOpExpr(mergedLeft, x.getOperator(), x.getRight());
            tmp.setParent(parent);
            x = tmp;
            v.incrementReplaceCunt();
        }
    }
    // ID = ? OR ID = ? => ID = ?
    if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
        if ((x.getLeft() instanceof SQLBinaryOpExpr) && (x.getRight() instanceof SQLBinaryOpExpr)) {
            SQLBinaryOpExpr leftBinary = (SQLBinaryOpExpr) x.getLeft();
            SQLBinaryOpExpr rightBinary = (SQLBinaryOpExpr) x.getRight();
            if (mergeEqual(leftBinary, rightBinary)) {
                v.incrementReplaceCunt();
                leftBinary.setParent(x.getParent());
                putMergedArribute(leftBinary, rightBinary);
                return leftBinary;
            }
            if (//
            isLiteralExpr(leftBinary.getLeft()) && leftBinary.getOperator() == SQLBinaryOperator.BooleanOr) {
                if (mergeEqual(leftBinary.getRight(), x.getRight())) {
                    v.incrementReplaceCunt();
                    putMergedArribute(leftBinary, rightBinary);
                    return leftBinary;
                }
            }
        }
    }
    return x;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 5 with SQLObject

use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.

the class SchemaStatVisitor method visit.

public boolean visit(SQLColumnDefinition x) {
    String tableName = null;
    {
        SQLObject parent = x.getParent();
        if (parent instanceof SQLCreateTableStatement) {
            tableName = ((SQLCreateTableStatement) parent).getName().toString();
        }
    }
    if (tableName == null) {
        return true;
    }
    String columnName = x.getName().toString();
    Column column = addColumn(tableName, columnName);
    if (x.getDataType() != null) {
        column.setDataType(x.getDataType().getName());
    }
    for (SQLColumnConstraint item : x.getConstraints()) {
        if (item instanceof SQLPrimaryKey) {
            column.setPrimaryKey(true);
        } else if (item instanceof SQLUnique) {
            column.setUnique(true);
        }
    }
    return false;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) Column(com.alibaba.druid.stat.TableStat.Column)

Aggregations

SQLObject (com.alibaba.druid.sql.ast.SQLObject)23 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)8 Column (com.alibaba.druid.stat.TableStat.Column)4 SQLName (com.alibaba.druid.sql.ast.SQLName)3 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)2 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 MySqlForceIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint)2 MySqlIgnoreIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint)2 MySqlUseIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint)2 WallContext (com.alibaba.druid.wall.WallContext)2 Map (java.util.Map)2 DruidRuntimeException (com.alibaba.druid.DruidRuntimeException)1 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)1 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1 SQLOrderingSpecification (com.alibaba.druid.sql.ast.SQLOrderingSpecification)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1