use of com.alibaba.druid.sql.repository.SchemaObject in project canal by alibaba.
the class FastsqlSchemaTest method test_persistent.
@Test
public void test_persistent() throws Throwable {
SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
String sql = " create table example_vc_tbl( c1 int not null auto_increment primary key," + "c2 varchar(70), vc1 int as (length(c2)) virtual," + "DIM_SUM varchar(128) AS (MD5(UPPER(CONCAT(c2, c1)))) PERSISTENT)";
repository.console(sql);
repository.setDefaultSchema("test");
SchemaObject table = repository.findTable("example_vc_tbl");
System.out.println(table.getStatement().toString());
Assert.assertTrue(table.findColumn("c1") != null);
}
use of com.alibaba.druid.sql.repository.SchemaObject in project canal by alibaba.
the class MemoryTableMeta method snapshot.
public Map<String, String> snapshot() {
Map<String, String> schemaDdls = new HashMap<>();
for (Schema schema : repository.getSchemas()) {
StringBuffer data = new StringBuffer(4 * 1024);
for (String table : schema.showTables()) {
SchemaObject schemaObject = schema.findTable(table);
schemaObject.getStatement().output(data);
data.append("; \n");
}
schemaDdls.put(schema.getName(), data.toString());
}
return schemaDdls;
}
use of com.alibaba.druid.sql.repository.SchemaObject in project druid by alibaba.
the class SchemaStatVisitor method visit.
public boolean visit(SQLPropertyExpr x) {
Column column = null;
String ident = SQLUtils.normalize(x.getName());
SQLTableSource tableSource = x.getResolvedTableSource();
if (tableSource instanceof SQLSubqueryTableSource) {
SQLSelect subSelect = ((SQLSubqueryTableSource) tableSource).getSelect();
SQLSelectQueryBlock subQuery = subSelect.getQueryBlock();
if (subQuery != null) {
SQLTableSource subTableSource = subQuery.findTableSourceWithColumn(x.nameHashCode64());
if (subTableSource != null) {
tableSource = subTableSource;
}
}
}
if (tableSource instanceof SQLExprTableSource) {
SQLExpr expr = ((SQLExprTableSource) tableSource).getExpr();
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr table = (SQLIdentifierExpr) expr;
SQLTableSource resolvedTableSource = table.getResolvedTableSource();
if (resolvedTableSource instanceof SQLExprTableSource) {
expr = ((SQLExprTableSource) resolvedTableSource).getExpr();
}
} else if (expr instanceof SQLPropertyExpr) {
SQLPropertyExpr table = (SQLPropertyExpr) expr;
SQLTableSource resolvedTableSource = table.getResolvedTableSource();
if (resolvedTableSource instanceof SQLExprTableSource) {
expr = ((SQLExprTableSource) resolvedTableSource).getExpr();
}
}
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr table = (SQLIdentifierExpr) expr;
SQLTableSource resolvedTableSource = table.getResolvedTableSource();
if (resolvedTableSource instanceof SQLWithSubqueryClause.Entry) {
return false;
}
String tableName = table.getName();
SchemaObject schemaObject = ((SQLExprTableSource) tableSource).getSchemaObject();
if (schemaObject != null && schemaObject.getStatement() instanceof SQLCreateTableStatement && !"*".equals(ident)) {
SQLColumnDefinition columnDef = schemaObject.findColumn(x.nameHashCode64());
if (columnDef == null) {
column = addColumn("UNKNOWN", ident);
}
}
if (column == null) {
column = addColumn(table, ident);
}
if (isParentGroupBy(x)) {
this.groupByColumns.add(column);
}
} else if (expr instanceof SQLPropertyExpr) {
SQLPropertyExpr table = (SQLPropertyExpr) expr;
column = addColumn(table, ident);
if (column != null && isParentGroupBy(x)) {
this.groupByColumns.add(column);
}
} else if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
if ("table".equalsIgnoreCase(methodInvokeExpr.getMethodName()) && methodInvokeExpr.getArguments().size() == 1 && methodInvokeExpr.getArguments().get(0) instanceof SQLName) {
SQLName table = (SQLName) methodInvokeExpr.getArguments().get(0);
String tableName = null;
if (table instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) table;
SQLIdentifierExpr owner = (SQLIdentifierExpr) propertyExpr.getOwner();
if (propertyExpr.getResolvedTableSource() != null && propertyExpr.getResolvedTableSource() instanceof SQLExprTableSource) {
SQLExpr resolveExpr = ((SQLExprTableSource) propertyExpr.getResolvedTableSource()).getExpr();
if (resolveExpr instanceof SQLName) {
tableName = resolveExpr.toString() + "." + propertyExpr.getName();
}
}
}
if (tableName == null) {
tableName = table.toString();
}
column = addColumn(tableName, ident);
}
}
} else if (tableSource instanceof SQLWithSubqueryClause.Entry || tableSource instanceof SQLSubqueryTableSource || tableSource instanceof SQLUnionQueryTableSource || tableSource instanceof SQLValuesTableSource || tableSource instanceof SQLLateralViewTableSource) {
return false;
} else {
if (x.getResolvedProcudure() != null) {
return false;
}
if (x.getResolvedOwnerObject() instanceof SQLParameter) {
return false;
}
boolean skip = false;
for (SQLObject parent = x.getParent(); parent != null; parent = parent.getParent()) {
if (parent instanceof SQLSelectQueryBlock) {
SQLTableSource from = ((SQLSelectQueryBlock) parent).getFrom();
if (from instanceof SQLValuesTableSource) {
skip = true;
break;
}
} else if (parent instanceof SQLSelectQuery) {
break;
}
}
if (!skip) {
column = handleUnknownColumn(ident);
}
}
if (column != null) {
SQLObject parent = x.getParent();
if (parent instanceof SQLSelectOrderByItem) {
parent = parent.getParent();
if (parent instanceof SQLIndexDefinition) {
parent = parent.getParent();
}
}
if (parent instanceof SQLPrimaryKey) {
column.setPrimaryKey(true);
} else if (parent instanceof SQLUnique) {
column.setUnique(true);
}
setColumn(x, column);
}
return false;
}
use of com.alibaba.druid.sql.repository.SchemaObject in project druid by alibaba.
the class SchemaStatVisitor method getColumn.
protected Column getColumn(SQLExpr expr) {
final SQLExpr original = expr;
// unwrap
expr = unwrapExpr(expr);
if (expr instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
SQLExpr owner = propertyExpr.getOwner();
String column = SQLUtils.normalize(propertyExpr.getName());
if (owner instanceof SQLName) {
SQLName table = (SQLName) owner;
SQLObject resolvedOwnerObject = propertyExpr.getResolvedOwnerObject();
if (resolvedOwnerObject instanceof SQLSubqueryTableSource || resolvedOwnerObject instanceof SQLCreateProcedureStatement || resolvedOwnerObject instanceof SQLCreateFunctionStatement) {
table = null;
}
if (resolvedOwnerObject instanceof SQLExprTableSource) {
SQLExpr tableSourceExpr = ((SQLExprTableSource) resolvedOwnerObject).getExpr();
if (tableSourceExpr instanceof SQLName) {
table = (SQLName) tableSourceExpr;
}
} else if (resolvedOwnerObject instanceof SQLValuesTableSource) {
return null;
}
if (table != null) {
String tableName;
if (table instanceof SQLIdentifierExpr) {
tableName = ((SQLIdentifierExpr) table).normalizedName();
} else if (table instanceof SQLPropertyExpr) {
tableName = ((SQLPropertyExpr) table).normalizedName();
} else {
tableName = table.toString();
}
long tableHashCode64 = table.hashCode64();
if (resolvedOwnerObject instanceof SQLExprTableSource) {
SchemaObject schemaObject = ((SQLExprTableSource) resolvedOwnerObject).getSchemaObject();
if (schemaObject != null && schemaObject.getStatement() instanceof SQLCreateTableStatement) {
SQLColumnDefinition columnDef = schemaObject.findColumn(propertyExpr.nameHashCode64());
if (columnDef == null) {
tableName = "UNKNOWN";
tableHashCode64 = FnvHash.Constants.UNKNOWN;
}
}
}
long basic = tableHashCode64;
basic ^= '.';
basic *= FnvHash.PRIME;
long columnHashCode64 = FnvHash.hashCode64(basic, column);
Column columnObj = this.columns.get(columnHashCode64);
if (columnObj == null) {
columnObj = new Column(tableName, column, columnHashCode64);
if (!(resolvedOwnerObject instanceof SQLSubqueryTableSource || resolvedOwnerObject instanceof SQLWithSubqueryClause.Entry)) {
this.columns.put(columnHashCode64, columnObj);
}
}
return columnObj;
}
}
return null;
}
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
if (identifierExpr.getResolvedParameter() != null) {
return null;
}
if (identifierExpr.getResolvedTableSource() instanceof SQLSubqueryTableSource) {
return null;
}
if (identifierExpr.getResolvedDeclareItem() != null || identifierExpr.getResolvedParameter() != null) {
return null;
}
String column = identifierExpr.getName();
SQLName table = null;
SQLTableSource tableSource = identifierExpr.getResolvedTableSource();
if (tableSource instanceof SQLExprTableSource) {
SQLExpr tableSourceExpr = ((SQLExprTableSource) tableSource).getExpr();
if (tableSourceExpr != null && !(tableSourceExpr instanceof SQLName)) {
tableSourceExpr = unwrapExpr(tableSourceExpr);
}
if (tableSourceExpr instanceof SQLName) {
table = (SQLName) tableSourceExpr;
}
}
if (table != null) {
long tableHashCode64 = table.hashCode64();
long basic = tableHashCode64;
basic ^= '.';
basic *= FnvHash.PRIME;
long columnHashCode64 = FnvHash.hashCode64(basic, column);
final Column old = columns.get(columnHashCode64);
if (old != null) {
return old;
}
return new Column(table.toString(), column, columnHashCode64);
}
return new Column("UNKNOWN", column);
}
if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
List<SQLExpr> arguments = methodInvokeExpr.getArguments();
long nameHash = methodInvokeExpr.methodNameHashCode64();
if (nameHash == FnvHash.Constants.DATE_FORMAT) {
if (arguments.size() == 2 && arguments.get(0) instanceof SQLName && arguments.get(1) instanceof SQLCharExpr) {
return getColumn(arguments.get(0));
}
}
}
return null;
}
use of com.alibaba.druid.sql.repository.SchemaObject in project druid by alibaba.
the class ResolveTest_1 method test_0.
public void test_0() throws Exception {
String sql = "select * from a.b";
SQLSelectStatement stmt = (SQLSelectStatement) SQLUtils.parseStatements(sql, DbType.mysql).get(0);
repository.resolve(stmt);
stmt.accept(new SetCatalogVisitor());
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
SQLExprTableSource tableSource = (SQLExprTableSource) queryBlock.getFrom();
final SchemaObject schemaObject = tableSource.getSchemaObject();
assertEquals("b", schemaObject.getName());
assertEquals("ots.a", schemaObject.getSchema().getName());
assertEquals("ots", schemaObject.getSchema().getCatalog());
SQLCreateTableStatement createTable = (SQLCreateTableStatement) schemaObject.getStatement();
assertNotNull(createTable);
System.out.println(queryBlock.toString());
}
Aggregations