Search in sources :

Example 71 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SQLObjectImpl method output.

public void output(Appendable buf) {
    DbType dbType = null;
    if (this instanceof OracleSQLObject) {
        dbType = DbType.oracle;
    } else if (this instanceof MySqlObject) {
        dbType = DbType.mysql;
    } else if (this instanceof PGSQLObject) {
        dbType = DbType.postgresql;
    } else if (this instanceof SQLDbTypedObject) {
        dbType = ((SQLDbTypedObject) this).getDbType();
    }
    accept(SQLUtils.createOutputVisitor(buf, dbType));
}
Also used : PGSQLObject(com.alibaba.druid.sql.dialect.postgresql.ast.PGSQLObject) OracleSQLObject(com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject) MySqlObject(com.alibaba.druid.sql.dialect.mysql.ast.MySqlObject) DbType(com.alibaba.druid.DbType)

Example 72 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class FromSubqueryResolver method resolve.

public static List<SQLStatement> resolve(SQLCreateViewStatement stmt) {
    List<SQLStatement> targetList = new ArrayList<SQLStatement>();
    targetList.add(stmt);
    String viewName = SQLUtils.normalize(stmt.getName().getSimpleName());
    FromSubqueryResolver visitor = new FromSubqueryResolver(targetList, viewName);
    SQLWithSubqueryClause withSubqueryClause = stmt.getSubQuery().getWithSubQuery();
    if (withSubqueryClause != null) {
        stmt.getSubQuery().setWithSubQuery(null);
        for (SQLWithSubqueryClause.Entry entry : withSubqueryClause.getEntries()) {
            String entryName = entry.getAlias();
            SQLCreateViewStatement entryStmt = new SQLCreateViewStatement();
            entryStmt.setOrReplace(true);
            entryStmt.setDbType(stmt.getDbType());
            String entryViewName = visitor.generateSubViewName();
            entryStmt.setName(entryViewName);
            entryStmt.setSubQuery(entry.getSubQuery());
            visitor.targetList.add(0, entryStmt);
            visitor.mappings.put(entryName, entryViewName);
            entryStmt.accept(visitor);
        }
    }
    stmt.accept(visitor);
    DbType dbType = stmt.getDbType();
    for (int i = 0; i < targetList.size() - 1; ++i) {
        SQLCreateViewStatement targetStmt = (SQLCreateViewStatement) targetList.get(i);
        targetStmt.setOrReplace(true);
        targetStmt.setDbType(dbType);
        targetStmt.setAfterSemi(true);
    }
    return targetList;
}
Also used : ArrayList(java.util.ArrayList) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 73 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SQLDataTypeValidator method check.

public static void check(List<SQLStatement> stmtList) {
    if (stmtList.size() == 0) {
        return;
    }
    DbType dbType = stmtList.get(0).getDbType();
    SQLDataTypeValidator v = of(dbType);
    check(stmtList, dbType);
}
Also used : DbType(com.alibaba.druid.DbType)

Example 74 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class WallFilter method init.

@Override
public synchronized void init(DataSourceProxy dataSource) {
    if (dataSource == null) {
        LOG.error("dataSource should not be null");
        return;
    }
    if (this.dbTypeName == null || this.dbTypeName.trim().length() == 0) {
        if (dataSource.getDbType() != null) {
            this.dbTypeName = dataSource.getDbType();
        } else {
            this.dbTypeName = JdbcUtils.getDbType(dataSource.getRawJdbcUrl(), "");
        }
    }
    if (dbTypeName == null) {
        dbTypeName = JdbcUtils.getDbType(dataSource.getUrl(), null);
    }
    DbType dbType = DbType.of(this.dbTypeName);
    switch(dbType) {
        case mysql:
        case oceanbase:
        case drds:
        case mariadb:
        case tidb:
        case h2:
        case presto:
        case trino:
            if (config == null) {
                config = new WallConfig(MySqlWallProvider.DEFAULT_CONFIG_DIR);
            }
            provider = new MySqlWallProvider(config);
            break;
        case oracle:
        case ali_oracle:
        case oceanbase_oracle:
            if (config == null) {
                config = new WallConfig(OracleWallProvider.DEFAULT_CONFIG_DIR);
            }
            provider = new OracleWallProvider(config);
            break;
        case sqlserver:
        case jtds:
            if (config == null) {
                config = new WallConfig(SQLServerWallProvider.DEFAULT_CONFIG_DIR);
            }
            provider = new SQLServerWallProvider(config);
            break;
        case postgresql:
        case edb:
        case polardb:
        case greenplum:
        case gaussdb:
            if (config == null) {
                config = new WallConfig(PGWallProvider.DEFAULT_CONFIG_DIR);
            }
            provider = new PGWallProvider(config);
            break;
        case db2:
            if (config == null) {
                config = new WallConfig(DB2WallProvider.DEFAULT_CONFIG_DIR);
            }
            provider = new DB2WallProvider(config);
            break;
        case sqlite:
            if (config == null) {
                config = new WallConfig(SQLiteWallProvider.DEFAULT_CONFIG_DIR);
            }
            provider = new SQLiteWallProvider(config);
            break;
        case clickhouse:
            if (config == null) {
                config = new WallConfig(ClickhouseWallProvider.DEFAULT_CONFIG_DIR);
            }
            provider = new ClickhouseWallProvider(config);
            break;
        default:
            throw new IllegalStateException("dbType not support : " + dbType + ", url " + dataSource.getUrl());
    }
    provider.setName(dataSource.getName());
    this.inited = true;
}
Also used : DbType(com.alibaba.druid.DbType)

Example 75 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class ExportConditions method evaluate.

public String evaluate(String sql, String dbTypeName, Boolean compactValues) {
    DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
    try {
        List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
        SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
        for (SQLStatement stmt : statementList) {
            stmt.accept(visitor);
        }
        List<List<Object>> rows = new ArrayList<List<Object>>();
        List<Condition> conditions = visitor.getConditions();
        for (int i = 0; i < conditions.size(); ++i) {
            TableStat.Condition condition = conditions.get(i);
            Column column = condition.getColumn();
            String operator = condition.getOperator();
            List<Object> values = condition.getValues();
            List<Object> row = new ArrayList<Object>();
            row.add(column.getTable());
            row.add(column.getName());
            row.add(operator);
            if (values.size() == 0) {
                row.add(null);
            } else if (values.size() == 1) {
                if (compactValues != null && compactValues.booleanValue()) {
                    row.add(values);
                } else {
                    row.add(values.get(0));
                }
            } else {
                row.add(values);
            }
            rows.add(row);
        }
        return JSONUtils.toJSONString(rows);
    } catch (Exception ex) {
        System.err.println("error sql : " + sql);
        ex.printStackTrace();
        return null;
    }
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) ArrayList(java.util.ArrayList) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) Column(com.alibaba.druid.stat.TableStat.Column) Condition(com.alibaba.druid.stat.TableStat.Condition) List(java.util.List) ArrayList(java.util.ArrayList) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Aggregations

DbType (com.alibaba.druid.DbType)114 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)76 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)70 ArrayList (java.util.ArrayList)50 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)45 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)21 TableStat (com.alibaba.druid.stat.TableStat)10 JSONArray (com.alibaba.fastjson.JSONArray)10 MySqlExportParameterVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor)5 ExportParameterVisitor (com.alibaba.druid.sql.visitor.ExportParameterVisitor)5 ParserException (com.alibaba.druid.sql.parser.ParserException)4 Map (java.util.Map)4 JdbcParameter (com.alibaba.druid.proxy.jdbc.JdbcParameter)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)2 OracleSQLObject (com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject)2