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());
}
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;
}
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);
}
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);
}
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;
}
Aggregations