mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
druid 1.2.0, improved sql parser.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,14 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.repository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
@@ -30,42 +22,73 @@ import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 21/07/2017.
|
||||
*/
|
||||
public class Schema {
|
||||
private String name;
|
||||
private String catalog;
|
||||
private String name;
|
||||
protected final Map<Long, SchemaObject> objects = new ConcurrentHashMap<Long, SchemaObject>(16, 0.75f, 1);
|
||||
protected final Map<Long, SchemaObject> functions = new ConcurrentHashMap<Long, SchemaObject>(16, 0.75f, 1);
|
||||
private SchemaRepository repository;
|
||||
|
||||
protected final Map<Long, SchemaObject> objects = new ConcurrentHashMap<Long, SchemaObject>();
|
||||
|
||||
protected final Map<Long, SchemaObject> functions = new ConcurrentHashMap<Long, SchemaObject>();
|
||||
|
||||
private SchemaRepository repository;
|
||||
|
||||
public Schema(SchemaRepository repository) {
|
||||
protected Schema(SchemaRepository repository) {
|
||||
this(repository, null);
|
||||
}
|
||||
|
||||
public Schema(SchemaRepository repository, String name) {
|
||||
protected Schema(SchemaRepository repository, String name) {
|
||||
this.repository = repository;
|
||||
this.setName(name);
|
||||
}
|
||||
|
||||
protected Schema(SchemaRepository repository, String catalog, String name) {
|
||||
this.repository = repository;
|
||||
this.catalog = catalog;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public SchemaRepository getRepository() {
|
||||
return repository;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
public String getSimpleName() {
|
||||
if (name != null) {
|
||||
int p = name.indexOf('.');
|
||||
if (p != -1) {
|
||||
SQLExpr expr = SQLUtils.toSQLExpr(name, repository.dbType);
|
||||
if (expr instanceof SQLPropertyExpr) {
|
||||
return ((SQLPropertyExpr) expr).getSimpleName();
|
||||
} else {
|
||||
return name.substring(p + 1);
|
||||
}
|
||||
} else {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
|
||||
if (name != null && name.indexOf('.') != -1) {
|
||||
SQLExpr expr = SQLUtils.toSQLExpr(name, repository.dbType);
|
||||
if (expr instanceof SQLPropertyExpr) {
|
||||
catalog = ((SQLPropertyExpr) expr).getOwnernName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SchemaObject findTable(String tableName) {
|
||||
long hashCode64 = FnvHash.hashCode64(tableName);
|
||||
@@ -82,6 +105,21 @@ public class Schema {
|
||||
return null;
|
||||
}
|
||||
|
||||
public SchemaObject findView(String viewName) {
|
||||
long hashCode64 = FnvHash.hashCode64(viewName);
|
||||
return findView(hashCode64);
|
||||
}
|
||||
|
||||
public SchemaObject findView(long nameHashCode64) {
|
||||
SchemaObject object = objects.get(nameHashCode64);
|
||||
|
||||
if (object != null && object.getType() == SchemaObjectType.View) {
|
||||
return object;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public SchemaObject findTableOrView(String tableName) {
|
||||
long hashCode64 = FnvHash.hashCode64(tableName);
|
||||
return findTableOrView(hashCode64);
|
||||
@@ -118,7 +156,7 @@ public class Schema {
|
||||
|
||||
public SchemaObject findTable(SQLTableSource tableSource, String alias) {
|
||||
if (tableSource instanceof SQLExprTableSource) {
|
||||
if (alias.equalsIgnoreCase(tableSource.computeAlias())) {
|
||||
if (StringUtils.equalsIgnoreCase(alias, tableSource.computeAlias())) {
|
||||
SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource;
|
||||
|
||||
SchemaObject tableObject = exprTableSource.getSchemaObject();
|
||||
@@ -322,4 +360,8 @@ public class Schema {
|
||||
Collections.sort(tables);
|
||||
return tables;
|
||||
}
|
||||
|
||||
public String getCatalog() {
|
||||
return catalog;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,25 +17,115 @@ package com.alibaba.druid.sql.repository;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUniqueConstraint;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 03/06/2017.
|
||||
*/
|
||||
public interface SchemaObject {
|
||||
public class SchemaObject {
|
||||
private final Schema schema;
|
||||
private final String name;
|
||||
private final long hashCode64;
|
||||
|
||||
SQLStatement getStatement();
|
||||
private final SchemaObjectType type;
|
||||
private SQLStatement statement;
|
||||
private long rowCount = -1;
|
||||
|
||||
SQLColumnDefinition findColumn(String columName);
|
||||
SQLColumnDefinition findColumn(long columNameHash);
|
||||
protected SchemaObject(Schema schema, String name, SchemaObjectType type) {
|
||||
this(schema, name, type, null);
|
||||
}
|
||||
|
||||
boolean matchIndex(String columnName);
|
||||
protected SchemaObject(Schema schema, String name, SchemaObjectType type, SQLStatement statement) {
|
||||
this.schema = schema;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.statement = statement;
|
||||
|
||||
boolean matchKey(String columnName);
|
||||
this.hashCode64 = FnvHash.hashCode64(name);
|
||||
}
|
||||
|
||||
SchemaObjectType getType();
|
||||
public SchemaObject clone() {
|
||||
SchemaObject x = new SchemaObject(schema, name, type);
|
||||
|
||||
String getName();
|
||||
long nameHashCode64();
|
||||
if (statement != null) {
|
||||
x.statement = statement.clone();
|
||||
}
|
||||
x.rowCount = rowCount;
|
||||
|
||||
long getRowCount();
|
||||
return x;
|
||||
}
|
||||
|
||||
public long nameHashCode64() {
|
||||
return hashCode64;
|
||||
}
|
||||
|
||||
public static enum Type {
|
||||
Sequence, Table, View, Index, Function
|
||||
}
|
||||
|
||||
public SQLStatement getStatement() {
|
||||
return statement;
|
||||
}
|
||||
|
||||
public SQLColumnDefinition findColumn(String columName) {
|
||||
long hash = FnvHash.hashCode64(columName);
|
||||
return findColumn(hash);
|
||||
}
|
||||
|
||||
public SQLColumnDefinition findColumn(long columNameHash) {
|
||||
if (statement == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (statement instanceof SQLCreateTableStatement) {
|
||||
return ((SQLCreateTableStatement) statement).findColumn(columNameHash);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean matchIndex(String columnName) {
|
||||
if (statement == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (statement instanceof SQLCreateTableStatement) {
|
||||
SQLTableElement index = ((SQLCreateTableStatement) statement).findIndex(columnName);
|
||||
return index != null;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean matchKey(String columnName) {
|
||||
if (statement == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (statement instanceof SQLCreateTableStatement) {
|
||||
SQLTableElement index = ((SQLCreateTableStatement) statement).findIndex(columnName);
|
||||
return index instanceof SQLUniqueConstraint;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public SchemaObjectType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public long getRowCount() {
|
||||
return rowCount;
|
||||
}
|
||||
|
||||
public Schema getSchema() {
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.repository;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUniqueConstraint;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 08/06/2017.
|
||||
*/
|
||||
public class SchemaObjectImpl implements SchemaObject {
|
||||
private final String name;
|
||||
private final long hashCode64;
|
||||
|
||||
private final SchemaObjectType type;
|
||||
private SQLStatement statement;
|
||||
|
||||
public long rowCount = -1;
|
||||
|
||||
public SchemaObjectImpl(String name, SchemaObjectType type) {
|
||||
this(name, type, null);
|
||||
}
|
||||
|
||||
public SchemaObjectImpl(String name, SchemaObjectType type, SQLStatement statement) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.statement = statement;
|
||||
|
||||
this.hashCode64 = FnvHash.hashCode64(name);
|
||||
}
|
||||
|
||||
public long nameHashCode64() {
|
||||
return hashCode64;
|
||||
}
|
||||
|
||||
public static enum Type {
|
||||
Sequence, Table, View, Index, Function
|
||||
}
|
||||
|
||||
public SQLStatement getStatement() {
|
||||
return statement;
|
||||
}
|
||||
|
||||
public SQLColumnDefinition findColumn(String columName) {
|
||||
long hash = FnvHash.hashCode64(columName);
|
||||
return findColumn(hash);
|
||||
}
|
||||
|
||||
public SQLColumnDefinition findColumn(long columNameHash) {
|
||||
if (statement == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (statement instanceof SQLCreateTableStatement) {
|
||||
return ((SQLCreateTableStatement) statement).findColumn(columNameHash);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean matchIndex(String columnName) {
|
||||
if (statement == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (statement instanceof SQLCreateTableStatement) {
|
||||
SQLTableElement index = ((SQLCreateTableStatement) statement).findIndex(columnName);
|
||||
return index != null;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean matchKey(String columnName) {
|
||||
if (statement == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (statement instanceof SQLCreateTableStatement) {
|
||||
SQLTableElement index = ((SQLCreateTableStatement) statement).findIndex(columnName);
|
||||
return index instanceof SQLUniqueConstraint;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SchemaObjectType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getRowCount() {
|
||||
return rowCount;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,13 +15,8 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.repository;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.DruidRuntimeException;
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.FastsqlException;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
@@ -29,68 +24,78 @@ import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateFunctionStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateSequenceStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropSequenceStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLShowTablesStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLShowColumnsStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLShowCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
|
||||
import com.alibaba.druid.sql.repository.function.Function;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 03/06/2017.
|
||||
*/
|
||||
public class SchemaRepository {
|
||||
private static Log LOG = LogFactory.getLog(SchemaRepository.class);
|
||||
private Schema defaultSchema;
|
||||
protected String dbType;
|
||||
protected SQLASTVisitor consoleVisitor;
|
||||
|
||||
private Schema defaultSchema;
|
||||
protected DbType dbType;
|
||||
protected DbType schemaDbType;
|
||||
protected SQLASTVisitor consoleVisitor;
|
||||
protected Map<String, Schema> schemas = new LinkedHashMap<String, Schema>();
|
||||
protected final Map<Long, Function> internalFunctions = new ConcurrentHashMap<Long, Function>(16, 0.75f, 1);
|
||||
protected SchemaLoader schemaLoader;
|
||||
|
||||
public SchemaRepository() {
|
||||
|
||||
}
|
||||
|
||||
public SchemaRepository(String dbType) {
|
||||
this.dbType = dbType;
|
||||
public SchemaRepository(DbType dbType) {
|
||||
this (dbType, dbType);
|
||||
}
|
||||
|
||||
if (JdbcConstants.MYSQL.equals(dbType) || JdbcConstants.OCEANBASE.equals(dbType)) {
|
||||
consoleVisitor = new MySqlConsoleSchemaVisitor();
|
||||
} else if (JdbcConstants.ORACLE.equals(dbType) || JdbcConstants.OCEANBASE_ORACLE.equals(dbType)) {
|
||||
consoleVisitor = new OracleConsoleSchemaVisitor();
|
||||
} else {
|
||||
consoleVisitor = new DefaultConsoleSchemaVisitor();
|
||||
public SchemaRepository(DbType dbType, DbType schemaDbType) {
|
||||
if (dbType == null) {
|
||||
dbType = DbType.other;
|
||||
}
|
||||
if (schemaDbType == null) {
|
||||
schemaDbType = dbType;
|
||||
}
|
||||
|
||||
this.dbType = dbType;
|
||||
this.schemaDbType = schemaDbType;
|
||||
|
||||
switch (dbType) {
|
||||
case mysql:
|
||||
consoleVisitor = new MySqlConsoleSchemaVisitor();
|
||||
break;
|
||||
case oracle:
|
||||
consoleVisitor = new OracleConsoleSchemaVisitor();
|
||||
break;
|
||||
default:
|
||||
consoleVisitor = new DefaultConsoleSchemaVisitor();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Schema> schemas = new LinkedHashMap<String, Schema>();
|
||||
|
||||
public String getDbType() {
|
||||
public DbType getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
@@ -113,20 +118,18 @@ public class SchemaRepository {
|
||||
return;
|
||||
}
|
||||
|
||||
if (defaultSchema == null) {
|
||||
if (this.defaultSchema != null
|
||||
&& this.defaultSchema.getName() == null) {
|
||||
this.defaultSchema.setName(name);
|
||||
if (this.defaultSchema != null
|
||||
&& this.defaultSchema.getName() == null) {
|
||||
this.defaultSchema.setName(name);
|
||||
|
||||
schemas.put(normalizedName, this.defaultSchema);
|
||||
return;
|
||||
}
|
||||
|
||||
defaultSchema = new Schema(this);
|
||||
defaultSchema.setName(name);
|
||||
schemas.put(normalizedName, defaultSchema);
|
||||
this.defaultSchema = defaultSchema;
|
||||
schemas.put(normalizedName, this.defaultSchema);
|
||||
return;
|
||||
}
|
||||
|
||||
defaultSchema = new Schema(this);
|
||||
defaultSchema.setName(name);
|
||||
schemas.put(normalizedName, defaultSchema);
|
||||
this.defaultSchema = defaultSchema;
|
||||
}
|
||||
|
||||
public Schema findSchema(String schema) {
|
||||
@@ -141,15 +144,21 @@ public class SchemaRepository {
|
||||
name = SQLUtils.normalize(name);
|
||||
String normalizedName = name.toLowerCase();
|
||||
|
||||
if (getDefaultSchema() != null && defaultSchema.getName() == null) {
|
||||
if (getDefaultSchema() != null && defaultSchema.getName() == null && create) {
|
||||
defaultSchema.setName(name);
|
||||
schemas.put(normalizedName, defaultSchema);
|
||||
return defaultSchema;
|
||||
}
|
||||
|
||||
Schema schema = schemas.get(normalizedName);
|
||||
if (schema == null) {
|
||||
schema = new Schema(this, name);
|
||||
if (schema == null && create) {
|
||||
int p = name.indexOf('.');
|
||||
|
||||
String catalog = null;
|
||||
if (p != -1) {
|
||||
catalog = name.substring(0, p);
|
||||
}
|
||||
schema = new Schema(this, catalog, name);
|
||||
schemas.put(normalizedName, schema);
|
||||
}
|
||||
return schema;
|
||||
@@ -168,7 +177,60 @@ public class SchemaRepository {
|
||||
}
|
||||
|
||||
public SchemaObject findTable(String tableName) {
|
||||
return getDefaultSchema().findTable(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);
|
||||
}
|
||||
|
||||
public SchemaObject findView(String viewName) {
|
||||
SchemaObject object = getDefaultSchema()
|
||||
.findView(viewName);
|
||||
|
||||
if (object != null) {
|
||||
return object;
|
||||
}
|
||||
|
||||
String ddl = loadDDL(viewName);
|
||||
if (ddl == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
acceptDDL(ddl);
|
||||
|
||||
return getDefaultSchema()
|
||||
.findView(viewName);
|
||||
}
|
||||
|
||||
public SchemaObject findTable(long tableNameHash) {
|
||||
return getDefaultSchema()
|
||||
.findTable(tableNameHash);
|
||||
}
|
||||
|
||||
public SchemaObject findTableOrView(String tableName) {
|
||||
@@ -194,6 +256,30 @@ public class SchemaRepository {
|
||||
}
|
||||
}
|
||||
|
||||
String ddl = loadDDL(tableName);
|
||||
if (ddl == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
acceptDDL(ddl);
|
||||
|
||||
// double check
|
||||
object = schema.findTableOrView(tableName);
|
||||
if (object != null) {
|
||||
return object;
|
||||
}
|
||||
|
||||
for (Schema s : this.schemas.values()) {
|
||||
if (s == schema) {
|
||||
continue;
|
||||
}
|
||||
|
||||
object = schema.findTableOrView(tableName);
|
||||
if (object != null) {
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -206,14 +292,23 @@ public class SchemaRepository {
|
||||
}
|
||||
|
||||
public void acceptDDL(String ddl) {
|
||||
acceptDDL(ddl, dbType);
|
||||
acceptDDL(ddl, schemaDbType);
|
||||
}
|
||||
|
||||
public void acceptDDL(String ddl, String dbType) {
|
||||
public SchemaObject acceptDDL(String ddl, DbType dbType) {
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(ddl, dbType);
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
accept(stmt);
|
||||
if (stmt instanceof SQLCreateTableStatement) {
|
||||
SchemaObject schemaObject = acceptCreateTable((SQLCreateTableStatement) stmt);
|
||||
if (stmtList.size() == 1) {
|
||||
return schemaObject;
|
||||
}
|
||||
} else {
|
||||
accept(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void accept(SQLStatement stmt) {
|
||||
@@ -269,36 +364,62 @@ public class SchemaRepository {
|
||||
resolveVisitor.visit(stmt);
|
||||
}
|
||||
|
||||
public void resolve(SQLSelectQueryBlock queryBlock, SchemaResolveVisitor.Option... options) {
|
||||
if (queryBlock == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
SchemaResolveVisitor resolveVisitor = createResolveVisitor(options);
|
||||
resolveVisitor.visit(queryBlock);
|
||||
}
|
||||
|
||||
public void resolve(SQLStatement stmt, SchemaResolveVisitor.Option... options) {
|
||||
if (stmt == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
SchemaResolveVisitor resolveVisitor = createResolveVisitor(options);
|
||||
stmt.accept(resolveVisitor);
|
||||
if (stmt instanceof SQLSelectStatement) {
|
||||
resolveVisitor.visit((SQLSelectStatement) stmt);
|
||||
} else {
|
||||
stmt.accept(resolveVisitor);
|
||||
}
|
||||
}
|
||||
|
||||
private SchemaResolveVisitor createResolveVisitor(SchemaResolveVisitor.Option... options) {
|
||||
int optionsValue = SchemaResolveVisitor.Option.of(options);
|
||||
|
||||
SchemaResolveVisitor resolveVisitor;
|
||||
if (JdbcUtils.isMysqlDbType(dbType) || JdbcConstants.SQLITE.equals(dbType)) {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.MySqlResolveVisitor(this, optionsValue);
|
||||
} else if (JdbcUtils.isOracleDbType(dbType)) {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.OracleResolveVisitor(this, optionsValue);
|
||||
} else if (JdbcUtils.isPgsqlDbType(dbType)) {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.PGResolveVisitor(this, optionsValue);
|
||||
} else if (JdbcUtils.isSqlserverDbType(dbType)) {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.SQLServerResolveVisitor(this, optionsValue);
|
||||
} else if (JdbcConstants.DB2.equals(dbType)) {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.DB2ResolveVisitor(this, optionsValue);
|
||||
} else if (JdbcConstants.ODPS.equals(dbType)) {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.OdpsResolveVisitor(this, optionsValue);
|
||||
} else if (JdbcConstants.HIVE.equals(dbType)) {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.HiveResolveVisitor(this, optionsValue);
|
||||
} else {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.SQLResolveVisitor(this, optionsValue);
|
||||
switch (dbType) {
|
||||
case mysql:
|
||||
case mariadb:
|
||||
case sqlite:
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.MySqlResolveVisitor(this, optionsValue);
|
||||
break;
|
||||
case oracle:
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.OracleResolveVisitor(this, optionsValue);
|
||||
break;
|
||||
case db2:
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.DB2ResolveVisitor(this, optionsValue);
|
||||
break;
|
||||
case odps:
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.OdpsResolveVisitor(this, optionsValue);
|
||||
break;
|
||||
case hive:
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.HiveResolveVisitor(this, optionsValue);
|
||||
break;
|
||||
case postgresql:
|
||||
case edb:
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.PGResolveVisitor(this, optionsValue);
|
||||
break;
|
||||
case sqlserver:
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.SQLServerResolveVisitor(this, optionsValue);
|
||||
break;
|
||||
default:
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.SQLResolveVisitor(this, optionsValue);
|
||||
break;
|
||||
}
|
||||
|
||||
return resolveVisitor;
|
||||
}
|
||||
|
||||
@@ -324,8 +445,8 @@ public class SchemaRepository {
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(input, dbType);
|
||||
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
if (stmt instanceof MySqlShowColumnsStatement) {
|
||||
MySqlShowColumnsStatement showColumns = ((MySqlShowColumnsStatement) stmt);
|
||||
if (stmt instanceof SQLShowColumnsStatement) {
|
||||
SQLShowColumnsStatement showColumns = ((SQLShowColumnsStatement) stmt);
|
||||
SQLName db = showColumns.getDatabase();
|
||||
Schema schema;
|
||||
if (db == null) {
|
||||
@@ -347,8 +468,8 @@ public class SchemaRepository {
|
||||
MySqlCreateTableStatement createTableStmt = (MySqlCreateTableStatement) schemaObject.getStatement();
|
||||
createTableStmt.showCoumns(buf);
|
||||
}
|
||||
} else if (stmt instanceof MySqlShowCreateTableStatement) {
|
||||
MySqlShowCreateTableStatement showCreateTableStmt = (MySqlShowCreateTableStatement) stmt;
|
||||
} else if (stmt instanceof SQLShowCreateTableStatement) {
|
||||
SQLShowCreateTableStatement showCreateTableStmt = (SQLShowCreateTableStatement) stmt;
|
||||
SQLName table = showCreateTableStmt.getName();
|
||||
SchemaObject schemaObject = findTable(table);
|
||||
if (schemaObject == null) {
|
||||
@@ -389,21 +510,60 @@ public class SchemaRepository {
|
||||
|
||||
return buf.toString();
|
||||
} catch (IOException ex) {
|
||||
throw new DruidRuntimeException("exeucte command error.", ex);
|
||||
throw new FastsqlException("exeucte command error.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public SchemaObject findTable(SQLName name) {
|
||||
if (name instanceof SQLIdentifierExpr) {
|
||||
return findTable(((SQLIdentifierExpr) name).getName());
|
||||
return findTable(
|
||||
((SQLIdentifierExpr) name).getName());
|
||||
}
|
||||
|
||||
if (name instanceof SQLPropertyExpr) {
|
||||
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) name;
|
||||
String schema = propertyExpr.getOwnernName();
|
||||
SQLExpr owner = propertyExpr.getOwner();
|
||||
String schema;
|
||||
String catalog = null;
|
||||
if (owner instanceof SQLIdentifierExpr) {
|
||||
schema = ((SQLIdentifierExpr) owner).getName();
|
||||
} else if (owner instanceof SQLPropertyExpr){
|
||||
schema = ((SQLPropertyExpr) owner).getName();
|
||||
catalog = ((SQLPropertyExpr) owner).getOwnernName();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
long tableHashCode64 = propertyExpr.nameHashCode64();
|
||||
|
||||
Schema schemaObj = findSchema(schema);
|
||||
Schema schemaObj = findSchema(schema, false);
|
||||
if (schemaObj != null) {
|
||||
SchemaObject table = schemaObj.findTable(tableHashCode64);
|
||||
if (table != null) {
|
||||
return table;
|
||||
}
|
||||
}
|
||||
|
||||
String ddl = loadDDL(catalog, schema, propertyExpr.getName());
|
||||
|
||||
if (ddl == null) {
|
||||
schemaObj = findSchema(schema, true);
|
||||
} else {
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(ddl, schemaDbType);
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
accept(stmt);
|
||||
}
|
||||
|
||||
if (stmtList.size() == 1) {
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
if (stmt instanceof SQLCreateTableStatement) {
|
||||
SQLCreateTableStatement createStmt = (SQLCreateTableStatement) stmt;
|
||||
String schemaName = createStmt.getSchema();
|
||||
schemaObj = findSchema(schemaName, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (schemaObj == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -433,9 +593,9 @@ public class SchemaRepository {
|
||||
MySqlCreateTableStatement createTableStmt = (MySqlCreateTableStatement) schemaObject.getStatement();
|
||||
if (createTableStmt != null) {
|
||||
createTableStmt.setName(to.clone());
|
||||
acceptCreateTable(createTableStmt);
|
||||
}
|
||||
|
||||
schema.objects.put(to.hashCode64(), schemaObject);
|
||||
schema.objects.remove(nameHashCode64);
|
||||
}
|
||||
return true;
|
||||
@@ -466,6 +626,11 @@ public class SchemaRepository {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(HiveCreateTableStatement x) {
|
||||
acceptCreateTable(x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(MySqlCreateTableStatement x) {
|
||||
acceptCreateTable(x);
|
||||
return false;
|
||||
@@ -597,6 +762,11 @@ public class SchemaRepository {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(HiveCreateTableStatement x) {
|
||||
acceptCreateTable(x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLDropTableStatement x) {
|
||||
acceptDropTable(x);
|
||||
return false;
|
||||
@@ -633,7 +803,7 @@ public class SchemaRepository {
|
||||
}
|
||||
}
|
||||
|
||||
boolean acceptCreateTable(MySqlCreateTableStatement x) {
|
||||
SchemaObject acceptCreateTable(MySqlCreateTableStatement x) {
|
||||
SQLExprTableSource like = x.getLike();
|
||||
if (like != null) {
|
||||
SchemaObject table = findTable((SQLName) like.getExpr());
|
||||
@@ -642,24 +812,27 @@ public class SchemaRepository {
|
||||
MySqlCreateTableStatement stmtCloned = stmt.clone();
|
||||
stmtCloned.setName(x.getName().clone());
|
||||
acceptCreateTable((SQLCreateTableStatement) stmtCloned);
|
||||
return false;
|
||||
return table;
|
||||
}
|
||||
}
|
||||
|
||||
return acceptCreateTable((SQLCreateTableStatement) x);
|
||||
}
|
||||
|
||||
boolean acceptCreateTable(SQLCreateTableStatement x) {
|
||||
SchemaObject acceptCreateTable(SQLCreateTableStatement x) {
|
||||
SQLCreateTableStatement x1 = x.clone();
|
||||
String schemaName = x1.getSchema();
|
||||
|
||||
Schema schema = findSchema(schemaName, true);
|
||||
|
||||
SQLSelect select = x1.getSelect();
|
||||
|
||||
if (select != null) {
|
||||
select.accept(createResolveVisitor(SchemaResolveVisitor.Option.ResolveAllColumn));
|
||||
|
||||
SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock();
|
||||
this.resolve(queryBlock);
|
||||
|
||||
if (queryBlock != null) {
|
||||
List<SQLSelectItem> selectList = queryBlock.getSelectList();
|
||||
for (SQLSelectItem selectItem : selectList) {
|
||||
@@ -669,13 +842,39 @@ public class SchemaRepository {
|
||||
continue;
|
||||
}
|
||||
|
||||
SQLColumnDefinition column = null;
|
||||
if (selectItemExpr instanceof SQLName) {
|
||||
final SQLColumnDefinition resolvedColumn = ((SQLName) selectItemExpr).getResolvedColumn();
|
||||
if (resolvedColumn != null) {
|
||||
column = new SQLColumnDefinition();
|
||||
column.setDataType(
|
||||
selectItem.computeDataType());
|
||||
if (DbType.mysql == dbType) {
|
||||
if (resolvedColumn.getDefaultExpr() != null) {
|
||||
column.setDefaultExpr(resolvedColumn.getDefaultExpr().clone());
|
||||
}
|
||||
if (resolvedColumn.getConstraints().size() > 0) {
|
||||
for (SQLColumnConstraint constraint : resolvedColumn.getConstraints()) {
|
||||
column.addConstraint(constraint.clone());
|
||||
}
|
||||
}
|
||||
if (resolvedColumn.getComment() != null) {
|
||||
column.setComment(resolvedColumn.getComment());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (column == null) {
|
||||
column = new SQLColumnDefinition();
|
||||
column.setDataType(
|
||||
selectItem.computeDataType());
|
||||
}
|
||||
|
||||
String name = selectItem.computeAlias();
|
||||
SQLDataType dataType = selectItem.computeDataType();
|
||||
SQLColumnDefinition column = new SQLColumnDefinition();
|
||||
column.setName(name);
|
||||
column.setDataType(dataType);
|
||||
column.setDbType(dbType);
|
||||
x1.getTableElementList().add(column);
|
||||
x1.addColumn(column);
|
||||
}
|
||||
if (x1.getTableElementList().size() > 0) {
|
||||
x1.setSelect(null);
|
||||
@@ -716,9 +915,9 @@ public class SchemaRepository {
|
||||
LOG.info("replaced table '" + name + "'");
|
||||
}
|
||||
|
||||
table = new SchemaObjectImpl(name, SchemaObjectType.Table, x1);
|
||||
table = new SchemaObject(schema, name, SchemaObjectType.Table, x1);
|
||||
schema.objects.put(table.nameHashCode64(), table);
|
||||
return true;
|
||||
return table;
|
||||
}
|
||||
|
||||
boolean acceptDropTable(SQLDropTableStatement x) {
|
||||
@@ -745,7 +944,7 @@ public class SchemaRepository {
|
||||
return false;
|
||||
}
|
||||
|
||||
SchemaObject object = new SchemaObjectImpl(name, SchemaObjectType.View, x.clone());
|
||||
SchemaObject object = new SchemaObject(schema, name, SchemaObjectType.View, x.clone());
|
||||
schema.objects.put(object.nameHashCode64(), object);
|
||||
return true;
|
||||
}
|
||||
@@ -761,12 +960,15 @@ public class SchemaRepository {
|
||||
return false;
|
||||
}
|
||||
|
||||
SchemaObject object = new SchemaObjectImpl(name, SchemaObjectType.View, x.clone());
|
||||
SchemaObject object = new SchemaObject(schema, name, SchemaObjectType.View, x.clone());
|
||||
schema.objects.put(object.nameHashCode64(), object);
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean acceptDropIndex(SQLDropIndexStatement x) {
|
||||
if (x.getTableName() == null) {
|
||||
return false;
|
||||
}
|
||||
SQLName table = x.getTableName().getName();
|
||||
SchemaObject object = findTable(table);
|
||||
|
||||
@@ -787,7 +989,7 @@ public class SchemaRepository {
|
||||
Schema schema = findSchema(schemaName, true);
|
||||
|
||||
String name = x.getName().getSimpleName();
|
||||
SchemaObject object = new SchemaObjectImpl(name, SchemaObjectType.Index, x.clone());
|
||||
SchemaObject object = new SchemaObject(schema, name, SchemaObjectType.Index, x.clone());
|
||||
schema.objects.put(object.nameHashCode64(), object);
|
||||
|
||||
return true;
|
||||
@@ -798,7 +1000,7 @@ public class SchemaRepository {
|
||||
Schema schema = findSchema(schemaName, true);
|
||||
|
||||
String name = x.getName().getSimpleName();
|
||||
SchemaObject object = new SchemaObjectImpl(name, SchemaObjectType.Function, x.clone());
|
||||
SchemaObject object = new SchemaObject(schema, name, SchemaObjectType.Function, x.clone());
|
||||
schema.functions.put(object.nameHashCode64(), object);
|
||||
|
||||
return true;
|
||||
@@ -825,7 +1027,7 @@ public class SchemaRepository {
|
||||
Schema schema = findSchema(schemaName, true);
|
||||
|
||||
String name = x.getName().getSimpleName();
|
||||
SchemaObject object = new SchemaObjectImpl(name, SchemaObjectType.Sequence);
|
||||
SchemaObject object = new SchemaObject(schema, name, SchemaObjectType.Sequence);
|
||||
schema.objects.put(object.nameHashCode64(), object);
|
||||
return false;
|
||||
}
|
||||
@@ -838,4 +1040,72 @@ public class SchemaRepository {
|
||||
schema.objects.remove(nameHashCode64);
|
||||
return false;
|
||||
}
|
||||
|
||||
public SQLDataType findFuntionReturnType(long functionNameHashCode) {
|
||||
if (functionNameHashCode == FnvHash.Constants.LEN || functionNameHashCode == FnvHash.Constants.LENGTH) {
|
||||
return SQLIntegerExpr.DATA_TYPE;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected String loadDDL(String table) {
|
||||
if (table == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
table = SQLUtils.normalize(table, schemaDbType);
|
||||
|
||||
if (schemaLoader != null) {
|
||||
return schemaLoader.loadDDL(null, null, table);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected String loadDDL(String schema, String table) {
|
||||
if (table == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
table = SQLUtils.normalize(table, dbType);
|
||||
if (schema != null) {
|
||||
schema = SQLUtils.normalize(schema, dbType);
|
||||
}
|
||||
|
||||
if (schemaLoader != null) {
|
||||
return schemaLoader.loadDDL(null, schema, table);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected String loadDDL(String catalog, String schema, String table) {
|
||||
if (table == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
table = SQLUtils.normalize(table, dbType);
|
||||
if (schema != null) {
|
||||
schema = SQLUtils.normalize(schema, dbType);
|
||||
}
|
||||
if (catalog != null) {
|
||||
catalog = SQLUtils.normalize(catalog, dbType);
|
||||
}
|
||||
|
||||
if (schemaLoader != null) {
|
||||
return schemaLoader.loadDDL(catalog, schema, table);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SchemaLoader getSchemaLoader() {
|
||||
return schemaLoader;
|
||||
}
|
||||
|
||||
public void setSchemaLoader(SchemaLoader schemaLoader) {
|
||||
this.schemaLoader = schemaLoader;
|
||||
}
|
||||
|
||||
public static interface SchemaLoader {
|
||||
String loadDDL(String catalog, String schema, String objectName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,24 +15,26 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.repository;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLDeclareItem;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 03/08/2017.
|
||||
*/
|
||||
public interface SchemaResolveVisitor extends SQLASTVisitor {
|
||||
|
||||
boolean isEnabled(Option option);
|
||||
int getOptions();
|
||||
|
||||
public static enum Option {
|
||||
ResolveAllColumn,
|
||||
ResolveIdentifierAlias
|
||||
ResolveIdentifierAlias,
|
||||
CheckColumnAmbiguous
|
||||
;
|
||||
private Option() {
|
||||
mask = (1 << ordinal());
|
||||
@@ -95,6 +97,10 @@ public interface SchemaResolveVisitor extends SQLASTVisitor {
|
||||
}
|
||||
|
||||
public void addTableSource(long alias_hash, SQLTableSource tableSource) {
|
||||
if (tableSourceMap == null) {
|
||||
tableSourceMap = new HashMap<Long, SQLTableSource>();
|
||||
}
|
||||
|
||||
tableSourceMap.put(alias_hash, tableSource);
|
||||
}
|
||||
|
||||
@@ -111,5 +117,27 @@ public interface SchemaResolveVisitor extends SQLASTVisitor {
|
||||
}
|
||||
return declares.get(nameHash);
|
||||
}
|
||||
|
||||
protected SQLTableSource findTableSource(long nameHash) {
|
||||
SQLTableSource table = null;
|
||||
if (tableSourceMap != null) {
|
||||
table = tableSourceMap.get(nameHash);
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
protected SQLTableSource findTableSourceRecursive(long nameHash) {
|
||||
for (Context ctx = this; ctx != null; ctx = ctx.parent) {
|
||||
if (ctx.tableSourceMap != null) {
|
||||
SQLTableSource table = ctx.tableSourceMap.get(nameHash);
|
||||
if (table != null) {
|
||||
return table;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
package com.alibaba.druid.sql.repository.function;
|
||||
|
||||
public interface Function {
|
||||
FunctionType getType();
|
||||
|
||||
FunctionHandler findHandler();
|
||||
FunctionHandler findHandler(String signature);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.alibaba.druid.sql.repository.function;
|
||||
|
||||
public interface FunctionHandler {
|
||||
String getSignature();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.alibaba.druid.sql.repository.function;
|
||||
|
||||
public enum FunctionType {
|
||||
Scalar,
|
||||
Aggregate,
|
||||
Window
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.alibaba.druid.sql.repository.function;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.sql.Time;
|
||||
|
||||
public class SignatureUtils {
|
||||
public static Type getJavaType(String signature) {
|
||||
if (signature == null || signature.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
char c0 = signature.charAt(0);
|
||||
if (signature.length() == 1) {
|
||||
switch (c0) {
|
||||
case 'b':
|
||||
return byte.class;
|
||||
case 's':
|
||||
return short.class;
|
||||
case 'i':
|
||||
return int.class;
|
||||
case 'j':
|
||||
return long.class;
|
||||
case 'f':
|
||||
return float.class;
|
||||
case 'd':
|
||||
return double.class;
|
||||
case 'c':
|
||||
return char.class;
|
||||
|
||||
case 'g':
|
||||
return String.class; // not null
|
||||
case 'a':
|
||||
return java.sql.Date.class; // not null
|
||||
case 't':
|
||||
return Time.class; // not null
|
||||
case 'p':
|
||||
return java.sql.Timestamp.class; // not null
|
||||
|
||||
case 'B':
|
||||
return Byte.class;
|
||||
case 'S':
|
||||
return Short.class;
|
||||
case 'I':
|
||||
return Integer.class;
|
||||
case 'J':
|
||||
return Long.class;
|
||||
case 'F':
|
||||
return Float.class;
|
||||
case 'D':
|
||||
return Double.class;
|
||||
case 'C':
|
||||
return Character.class;
|
||||
|
||||
case 'G':
|
||||
return String.class;
|
||||
case 'A':
|
||||
return java.sql.Date.class;
|
||||
case 'T':
|
||||
return Time.class;
|
||||
case 'P':
|
||||
return java.sql.Timestamp.class;
|
||||
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
throw new UnsupportedOperationException("type : " + signature + " is not support.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.alibaba.druid.sql.repository.function;
|
||||
|
||||
public class Signatures {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user