Search in sources :

Example 31 with DbType

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

the class SchemaStatTest3 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "select count(*) from t";
    DbType dbType = JdbcConstants.MYSQL;
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    SQLStatement stmt = parser.parseStatementList().get(0);
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(visitor);
    System.out.println(visitor.getColumns());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertTrue(visitor.containsColumn("t", "*"));
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 32 with DbType

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

the class SchemaStatTest5 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "create table t_org (fid int, name varchar(256))";
    DbType dbType = JdbcConstants.MYSQL;
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    SQLStatement stmt = stmtList.get(0);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(statVisitor);
    System.out.println(statVisitor.getColumns());
    System.out.println(statVisitor.getTables());
    System.out.println(statVisitor.getConditions());
    Assert.assertEquals(2, statVisitor.getColumns().size());
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 33 with DbType

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

the class SchemaStatTest_odps method test_schemaStat.

public void test_schemaStat() throws Exception {
    File file = new File("/Users/wenshao/Downloads/odps_sql_1.txt");
    String sql = FileUtils.readFileToString(file);
    DbType dbType = DbType.odps;
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    System.out.println("stmtList size : " + stmtList.size());
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
    for (SQLStatement stmt : stmtList) {
        stmt.accept(statVisitor);
    }
    Set<TableStat.Relationship> relationships = statVisitor.getRelationships();
    for (TableStat.Relationship relationship : relationships) {
        // table1.id = table2.id
        System.out.println(relationship);
    }
    // System.out.println(statVisitor.getColumns());
    // System.out.println(statVisitor.getGroupByColumns()); // group by
    // group by
    System.out.println("relationships : " + statVisitor.getRelationships());
    System.out.println(statVisitor.getConditions());
// assertEquals(3, relationships.size());
// 
// Assert.assertEquals(21, statVisitor.getColumns().size());
// Assert.assertEquals(20, statVisitor.getConditions().size());
// assertEquals(1, statVisitor.getFunctions().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) TableStat(com.alibaba.druid.stat.TableStat) File(java.io.File) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 34 with DbType

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

the class DruidDataSource method initCheck.

protected void initCheck() throws SQLException {
    DbType dbType = DbType.of(this.dbTypeName);
    if (dbType == DbType.oracle) {
        isOracle = true;
        if (driver.getMajorVersion() < 10) {
            throw new SQLException("not support oracle driver " + driver.getMajorVersion() + "." + driver.getMinorVersion());
        }
        if (driver.getMajorVersion() == 10 && isUseOracleImplicitCache()) {
            this.getConnectProperties().setProperty("oracle.jdbc.FreeMemoryOnEnterImplicitCache", "true");
        }
        oracleValidationQueryCheck();
    } else if (dbType == DbType.db2) {
        db2ValidationQueryCheck();
    } else if (dbType == DbType.mysql || JdbcUtils.MYSQL_DRIVER_6.equals(this.driverClass)) {
        isMySql = true;
    }
    if (removeAbandoned) {
        LOG.warn("removeAbandoned is true, not use in production.");
    }
}
Also used : SQLException(java.sql.SQLException) DbType(com.alibaba.druid.DbType)

Example 35 with DbType

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

the class SchemaRepository method findTable.

public SchemaObject findTable(String tableName) {
    if (tableName.indexOf('.') != -1) {
        SQLExpr expr = SQLUtils.toSQLExpr(tableName, dbType);
        if (!(expr instanceof SQLIdentifierExpr)) {
            return findTable((SQLName) expr);
        }
    }
    SchemaObject object = getDefaultSchema().findTable(tableName);
    if (object != null) {
        return object;
    }
    String ddl = loadDDL(tableName);
    if (ddl == null) {
        return null;
    }
    DbType schemaDbType = this.schemaDbType;
    if (schemaDbType == null) {
        schemaDbType = dbType;
    }
    SchemaObject schemaObject = acceptDDL(ddl, schemaDbType);
    if (schemaObject != null) {
        return schemaObject;
    }
    return getDefaultSchema().findTable(tableName);
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) DbType(com.alibaba.druid.DbType)

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