Search in sources :

Example 11 with SchemaObject

use of com.alibaba.druid.sql.repository.SchemaObject in project druid by alibaba.

the class ResolveTest_1 method test_1.

public void test_1() throws Exception {
    String sql = "insert into foo select * from bar";
    SQLInsertStatement stmt = (SQLInsertStatement) SQLUtils.parseStatements(sql, DbType.mysql).get(0);
    repository.resolve(stmt);
    stmt.accept(new SetCatalogVisitor());
    SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) stmt.getQuery().getQuery();
    SQLExprTableSource tableSource = (SQLExprTableSource) queryBlock.getFrom();
    final SchemaObject schemaObject = tableSource.getSchemaObject();
    assertEquals("bar", schemaObject.getName());
    assertEquals("oss.world", schemaObject.getSchema().getName());
    assertEquals("oss", schemaObject.getSchema().getCatalog());
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject)

Example 12 with SchemaObject

use of com.alibaba.druid.sql.repository.SchemaObject in project canal by alibaba.

the class MemoryTableMeta method find.

@Override
public TableMeta find(String schema, String table) {
    List<String> keys = Arrays.asList(schema, table);
    TableMeta tableMeta = tableMetas.get(keys);
    if (tableMeta == null) {
        synchronized (this) {
            tableMeta = tableMetas.get(keys);
            if (tableMeta == null) {
                Schema schemaRep = repository.findSchema(schema);
                if (schemaRep == null) {
                    return null;
                }
                SchemaObject data = schemaRep.findTable(table);
                if (data == null) {
                    return null;
                }
                SQLStatement statement = data.getStatement();
                if (statement == null) {
                    return null;
                }
                if (statement instanceof SQLCreateTableStatement) {
                    tableMeta = parse((SQLCreateTableStatement) statement);
                }
                if (tableMeta != null) {
                    if (table != null) {
                        tableMeta.setTable(table);
                    }
                    if (schema != null) {
                        tableMeta.setSchema(schema);
                    }
                    tableMetas.put(keys, tableMeta);
                }
            }
        }
    }
    return tableMeta;
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) Schema(com.alibaba.druid.sql.repository.Schema) TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 13 with SchemaObject

use of com.alibaba.druid.sql.repository.SchemaObject in project canal by alibaba.

the class FastsqlSchemaTest method test_json_index.

@Test
public void test_json_index() throws Throwable {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = " CREATE TABLE `articles` ( `article_id` bigint NOT NULL AUTO_INCREMENT," + " `tags` json DEFAULT NULL, PRIMARY KEY (`article_id`)," + " KEY `articles_tags` ((cast(json_extract(`tags`,_utf8mb4'$[*]') as char(40) array)))" + ") ENGINE=InnoDB AUTO_INCREMENT=1054 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
    repository.console(sql);
    repository.setDefaultSchema("test");
    SchemaObject table = repository.findTable("articles");
    System.out.println(table.getStatement().toString());
    Assert.assertTrue(table.findColumn("article_id") != null);
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository) Test(org.junit.Test)

Example 14 with SchemaObject

use of com.alibaba.druid.sql.repository.SchemaObject in project canal by alibaba.

the class FastsqlSchemaTest method test_block_format.

@Test
public void test_block_format() throws Throwable {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = " CREATE TABLE `parent` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT," + "`created_at` timestamp NULL DEFAULT NULL, " + "PRIMARY KEY (`id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 BLOCK_FORMAT=ENCRYPTED";
    repository.console(sql);
    repository.setDefaultSchema("test");
    SchemaObject table = repository.findTable("parent");
    System.out.println(table.getStatement().toString());
    Assert.assertTrue(table.findColumn("id") != null);
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository) Test(org.junit.Test)

Example 15 with SchemaObject

use of com.alibaba.druid.sql.repository.SchemaObject in project druid by alibaba.

the class SQLRefactorVisitor method visit.

public boolean visit(SQLPropertyExpr x) {
    TableMapping mapping = null;
    SchemaObject schemaObject = null;
    boolean aliasOwer = false;
    SQLObject ownerObject = x.getResolvedOwnerObject();
    if (ownerObject instanceof SQLExprTableSource) {
        SQLExprTableSource exprTableSource = (SQLExprTableSource) ownerObject;
        if (exprTableSource.getAlias() != null && x.getOwner() instanceof SQLIdentifierExpr) {
            if (FnvHash.hashCode64(exprTableSource.getAlias()) == ((SQLIdentifierExpr) x.getOwner()).nameHashCode64()) {
                aliasOwer = true;
            }
        }
        mapping = findMapping(exprTableSource);
        schemaObject = (exprTableSource).getSchemaObject();
    }
    if (mapping == null) {
        return false;
    }
    String srcName = x.getName();
    String mappingColumn = mapping.getMappingColumn(srcName);
    if (mappingColumn != null) {
        x.setName(quote(mappingColumn));
    }
    SQLObject parent = x.getParent();
    if (parent instanceof SQLSelectItem && ((SQLSelectItem) parent).getAlias() == null) {
        ((SQLSelectItem) parent).setAlias(srcName);
    }
    if (x.getOwner() instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) x.getOwner()).nameHashCode64() == mapping.getSrcTableHash() && !aliasOwer) {
        x.setOwner(new SQLIdentifierExpr(quote(mapping.getDestTable())));
    }
    return false;
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Aggregations

SchemaObject (com.alibaba.druid.sql.repository.SchemaObject)23 SchemaRepository (com.alibaba.druid.sql.repository.SchemaRepository)13 Test (org.junit.Test)10 Column (com.alibaba.druid.stat.TableStat.Column)3 Schema (com.alibaba.druid.sql.repository.Schema)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 Token (com.alibaba.druid.sql.parser.Token)1 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)1 TableMeta (com.alibaba.otter.canal.parse.inbound.TableMeta)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1