Package gudusoft.gsqlparser.sqlenv
Class TSQLEnv
Object
gudusoft.gsqlparser.sqlenv.TSQLEnv
- Direct Known Subclasses:
GrabitSQLEnv,MultipleGrabitSQLEnv,MultipleSQLDepSQLEnv,SQLDepSQLEnv,SqlflowSQLEnv,TDDLSQLEnv
SQL environment includes the metadata of a list of databases. The typical scenario is there is one database includes
some schemas, and each schema includes some tables, views, procedures and etc.
Database known as catalog in ANSI SQL.
Each catalog including a list of schemas.
Each schema including a list of schema objects such as table, procedure, function, trigger and more.
Implement your own concrete class derived from this class to get the metadata from a real database. Usually, this is done by querying the INFORMATION_SCHEMA in the
Database known as catalog in ANSI SQL.
Each catalog including a list of schemas.
Each schema including a list of schema objects such as table, procedure, function, trigger and more.
Implement your own concrete class derived from this class to get the metadata from a real database. Usually, this is done by querying the INFORMATION_SCHEMA in the
initSQLEnv() method which should be override
in your own class.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddFunction(TObjectName qualifiedFunctionName, boolean fromDDL) addFunction(String qualifiedFunctionName, boolean fromDDL) Add a function to SQLEnv, if a function with the same already exists, just return the existing one.addOraclePackage(String qualifiedProcedureName, boolean fromDDL) addProcedure(String qualifiedProcedureName, boolean fromDDL) addSQLRoutine(String qualifiedProcedureName, boolean fromDDL, ESQLDataObjectType type) Add a new table to the SQLEnv.addTrigger(String qualifiedTriggerName, boolean fromDDL) booleancolumnInTable(String qualifiedTablename, String columnName) static booleancompareColumn(EDbVendor dbVendor, TObjectName sourceColumn, TObjectName targetColumn) booleancompareColumn(String ident1, String ident2) static booleancompareIdentifier(EDbVendor dbVendor, ESQLDataObjectType objectType, TObjectName source, TObjectName target) static booleancompareIdentifier(EDbVendor dbVendor, ESQLDataObjectType objectType, String ident1, String ident2) booleancompareIdentifier(ESQLDataObjectType objectType, String ident1, String ident2) static booleancompareQualifiedColumn(EDbVendor dbVendor, String sourceColumn, String targetColumn, String sourceTable, String targetTable, String sourceSchema, String targetSchema, String sourceDatabase, String targetDatabase) static booleancompareQualifiedTable(EDbVendor dbVendor, String sourceTable, String targetTable, String sourceSchema, String targetSchema, String sourceDatabase, String targetDatabase) static booleancompareTable(EDbVendor dbVendor, TObjectName sourceTable, TObjectName targetTable) booleancompareTable(String ident1, String ident2) createSQLCatalog(String catalogName) create a new catalog/database in the SQL environment.createSQLSchema(String qualifiedSchemaName) create a new schema and add to the catalogstatic StringdelimitedChar(EDbVendor dbVendor) used to delimit a database identifier, such as [ used in SQL Server, ` used in MySQLprotected booleandoAddCatalog(TSQLCatalog sqlCatalog) add a catalog to the SQL environment, called internally.doAddSchemaObject(TObjectName qualifiedObjectName, ESQLDataObjectType objectType) doAddSchemaObject(String qualifiedObjectName, ESQLDataObjectType objectType) protected TSQLSchemaObjectdoSearchSchemaObject(String catalog, String schema, String table, ESQLDataObjectType objectType) static booleanendsWithDelimitedIdentifier(EDbVendor dbVendor, String identifier) getAFullQualifiedSchemaObjectName(String schemaObjectName) If the input name is fully qualified with db and schema, just return it without any modification.a list of catalog/database in this SQL environment.Get ICatalogProvider (Phase 3)Returns the Calcite-backed catalog store (Phase 3.5 integration).getColumnInTable(String qualifiedTablename, String columnName) getColumnsInTable(String qualifiedTablename, boolean columnNameOnly) static StringgetDatabaseName(TObjectName schemaObjectName) static StringgetDatabaseName(String schemaObjectName) the database vendor where this SQL environment is generated from.the current active catalog/database in the SQL environment.the current schema name in the SQL environment.Get IdentifierProfile (Phase 0)Get IdentifierService (Phase 0)intstatic StringgetObjectName(TObjectName schemaObjectName) static StringgetObjectName(String schemaObjectName) getProcedureParameterValue(String procedureName, int paramPos) static StringgetSchemaName(TObjectName schemaObjectName) static StringgetSchemaName(String schemaObjectName) getSQLCatalog(String catalogName, boolean createIfNotExist) get a catalog from the SQL environment if already exists, otherwise, create a new catalog.getSQLSchema(String qualifiedSchemaName, boolean createIfNotExist) get a schema from the specified catalog, if not exists, create a new schema in the catalog.static StringgetStmtSeparatorChar(EDbVendor dbVendor) voidgetVariableValue(String variableName, String variableValue) abstract voidThis method must be override in the subclass to build a SQL environment with real metadata.static booleanisDelimitedIdentifier(EDbVendor dbVendor, String identifier) booleanisDelimitedIdentifier(String identifier) booleanIf this option is enabled, SQLEnv will collect table/view/function/procedure metadata from the create table/create view/create function/create procedure statement during the parse of the SQL script.static booleanstatic booleanmatchSubObjectNameToWhole(EDbVendor dbVendor, ESQLDataObjectType sqlDataObjectType, String sub, String whole) 比较 一个数据库对象名是否等于或者属于另一个对象 等于 就是完全相等(根据不同数据库的比较规则) 属于 表示如下情况: 1.static StringnormalizeIdentifier(EDbVendor dbVendor, ESQLDataObjectType sqlDataObjectType, String identifier) 1.normalizeIdentifier(ESQLDataObjectType sqlDataObjectType, String identifier) protected booleanputSchemaObject(String schemaObjectName, TSQLSchemaObject schemaObject) put a schema object into the hashmap which can be used to find a schema object in a more efficient way.searchCatalog(String catalogName) search catalog in the catalog list, return null if not found.searchFunction(TObjectName qualifiedTablename) searchFunction(String qualifiedTablename) protected TSQLSchemaObjectsearchSchemaObject(TObjectName qualifiedName, ESQLDataObjectType objectType) searchSchemaObject(String qualifiedName, ESQLDataObjectType objectType) Phase 1: 使用 IdentifierService 的新方法处理多段名searchTable(TObjectName tableName) searchTable(String qualifiedTablename) find a table in the SQL environment by using a qualified table name: catalogName.schemaName.tableNamevoidsetDefaultCatalogName(String defaultCatalogName) voidsetDefaultSchemaName(String defaultSchemaName) voidsetDefaultServerName(String defaultServerName) voidsetEnableGetMetadataFromDDL(boolean enableGetMetadataFromDDL) static booleansupportCatalog(EDbVendor dbVendor) Whether this database support catalog or not?static booleansupportSchema(EDbVendor dbVendor) Whether this database support schema or not?toString()
-
Field Details
-
columnCollationCaseSensitive
-
functionCollationCaseSensitive
-
tableCollationCaseSensitive
-
catalogCollationCaseSensitive
-
defaultCollationCaseSensitive
-
isAliasReferenceForbidden
-
DEFAULT_SERVER_NAME
add a table- See Also:
-
DEFAULT_DB_NAME
- See Also:
-
DEFAULT_SCHEMA_NAME
- See Also:
-
-
Constructor Details
-
TSQLEnv
create a SQL environment.- Parameters:
dbVendor- the database vendor
-
-
Method Details
-
getStmtSeparatorChar
-
supportSchema
Whether this database support schema or not?- Parameters:
dbVendor-- Returns:
-
supportCatalog
Whether this database support catalog or not?- Parameters:
dbVendor-- Returns:
-
delimitedChar
used to delimit a database identifier, such as [ used in SQL Server, ` used in MySQL- Parameters:
dbVendor-- Returns:
-
isDelimitedIdentifier
-
endsWithDelimitedIdentifier
-
isDelimitedIdentifier
-
compareColumn
-
compareTable
-
compareIdentifier
-
compareColumn
public static boolean compareColumn(EDbVendor dbVendor, TObjectName sourceColumn, TObjectName targetColumn) -
compareTable
public static boolean compareTable(EDbVendor dbVendor, TObjectName sourceTable, TObjectName targetTable) -
compareQualifiedTable
-
compareQualifiedColumn
-
compareIdentifier
public static boolean compareIdentifier(EDbVendor dbVendor, ESQLDataObjectType objectType, TObjectName source, TObjectName target) -
compareIdentifier
public static boolean compareIdentifier(EDbVendor dbVendor, ESQLDataObjectType objectType, String ident1, String ident2) -
normalizeIdentifier
-
normalizeIdentifier
public static String normalizeIdentifier(EDbVendor dbVendor, ESQLDataObjectType sqlDataObjectType, String identifier) 1. remove delimited char if it's delimited/quoted identifier 2. change the case of the name in the same way as it saved to the information_schema- Parameters:
dbVendor-sqlDataObjectType-identifier-- Returns:
-
matchSubObjectNameToWhole
public static boolean matchSubObjectNameToWhole(EDbVendor dbVendor, ESQLDataObjectType sqlDataObjectType, String sub, String whole) 比较 一个数据库对象名是否等于或者属于另一个对象 等于 就是完全相等(根据不同数据库的比较规则) 属于 表示如下情况: 1. column1 -> 属于 -> table1.column1 2. table1 -> 属于 -> db1.schema1.table1 3. `schema1.table1` -> 属于 -> `db1`.`schema1`.`table1` 4. `schema1.table1` -> 不属于 -> `db1`.`schema2`.`table1`- Parameters:
sub-whole-- Returns:
-
setEnableGetMetadataFromDDL
-
isEnableGetMetadataFromDDL
If this option is enabled, SQLEnv will collect table/view/function/procedure metadata from the create table/create view/create function/create procedure statement during the parse of the SQL script.
A TSQLEnv object instance must be passed toTGSqlParserbefore parsing the SQL script. And this TSQLEnv instance can be passed to anotherTGSqlParserobject with the collected database metadata.
Default value is true.- Returns:
-
getDBVendor
the database vendor where this SQL environment is generated from.- Returns:
- the database vendor
-
getCatalogStore
Returns the Calcite-backed catalog store (Phase 3.5 integration).Note: This method delegates to the underlying CatalogStoreProvider. Direct access to CatalogStore is provided for backward compatibility with existing test code.
- Returns:
- the catalog store
-
getIdentifierService
Get IdentifierService (Phase 0)- Returns:
- IdentifierService instance
-
getIdentifierProfile
Get IdentifierProfile (Phase 0)- Returns:
- IdentifierProfile instance
-
getCatalogProvider
Get ICatalogProvider (Phase 3)- Returns:
- ICatalogProvider instance
-
initSQLEnv
This method must be override in the subclass to build a SQL environment with real metadata.
this usually done by querying the INFORMATION_SCHEMA -
getCatalogList
a list of catalog/database in this SQL environment.- Returns:
- a list of catalog/database
-
doAddCatalog
add a catalog to the SQL environment, called internally.- Parameters:
sqlCatalog- catalog- Returns:
- return false if a catalog with the same name already exists.
-
putSchemaObject
put a schema object into the hashmap which can be used to find a schema object in a more efficient way.- Parameters:
schemaObjectName- name of schema objectschemaObject- instance of a schema object- Returns:
- always return true.
-
doSearchSchemaObject
protected TSQLSchemaObject doSearchSchemaObject(String catalog, String schema, String table, ESQLDataObjectType objectType) -
searchSchemaObject
protected TSQLSchemaObject searchSchemaObject(TObjectName qualifiedName, ESQLDataObjectType objectType) -
searchSchemaObject
Phase 1: 使用 IdentifierService 的新方法处理多段名 -
searchTable
find a table in the SQL environment by using a qualified table name: catalogName.schemaName.tableName- Parameters:
qualifiedTablename- , can be catalog.schema.table, or .schema.table, use currentCatalogName or iterate all catalogs or catalog..table, use current schema name or iterate all schema under catalog or ..table, use currentCatalogName or iterate all catalogs, use current schema or iterate all schema- Returns:
- a table
-
searchTable
-
getDefaultCatalogName
the current active catalog/database in the SQL environment. If search a table in syntax like this: .schemaName.tableName and this current catalog name is not null, then, search the table in this syntax: currentCatalogName.schemaName.tableName. If the current catalog name is null, search all catalogs in the catalog list.- Returns:
- the name of the current active catalog/database
-
getDefaultSchemaName
the current schema name in the SQL environment.
If search a table in syntax like this: catalogname..tableName and this current schema name is not null, then, search the table in this syntax: catalogName.currentSchemaName.tableName. If the current schema name is null, search all schemas under catalogName- Returns:
- the default schema name
-
setDefaultCatalogName
-
setDefaultSchemaName
-
getDefaultServerName
-
setDefaultServerName
-
createSQLCatalog
create a new catalog/database in the SQL environment.- Parameters:
catalogName- catalog name- Returns:
- instance of the created catalog
-
getSQLCatalog
get a catalog from the SQL environment if already exists, otherwise, create a new catalog.- Parameters:
catalogName- catalog namecreateIfNotExist- if this value is true, then create a new catalog if it's not exists- Returns:
- a catalog instance
-
searchCatalog
search catalog in the catalog list, return null if not found.- Parameters:
catalogName- catalog name- Returns:
- null if not found.
-
createSQLSchema
create a new schema and add to the catalog- Parameters:
qualifiedSchemaName- must be a qualified name like: catalog.schema. Otherwise, a null exception will be raised.- Returns:
- a new schema instance
-
getSQLSchema
get a schema from the specified catalog, if not exists, create a new schema in the catalog.- Parameters:
qualifiedSchemaName- must be a qualified name like: catalog.schema. Otherwise, a null exception will be raised.createIfNotExist- if this value is true, then create a new schema if it's not exists- Returns:
- a schema instance
-
getColumnsInTable
- Parameters:
qualifiedTablename- 需要完整的tablename,例如 catalog.schema.table, 如果仅传入 table name, 需要利用 default catalog, default schema 拼接完整的名称: catalog.schema.tablecolumnNameOnly-- Returns:
-
columnInTable
-
getColumnInTable
-
doAddSchemaObject
public TSQLSchemaObject doAddSchemaObject(String qualifiedObjectName, ESQLDataObjectType objectType) -
doAddSchemaObject
public TSQLSchemaObject doAddSchemaObject(TObjectName qualifiedObjectName, ESQLDataObjectType objectType) -
addFunction
Add a function to SQLEnv, if a function with the same already exists, just return the existing one.- Parameters:
qualifiedFunctionName-- Returns:
- function
-
addFunction
-
addOraclePackage
-
addProcedure
-
addSQLRoutine
public TSQLRoutine addSQLRoutine(String qualifiedProcedureName, boolean fromDDL, ESQLDataObjectType type) -
addTrigger
-
addTable
Add a new table to the SQLEnv. If the table with the same name already exists in the SQLEnv, the existing table will be returned.
dbName.schemaName.tablename. If dbName is not specified,getDefaultCatalogName()will be used to put this table in. If schemaName is not specified,getDefaultSchemaName()will be used to put this table in.
If the specified database and schema are not already exists, a new database/schema will be generated automatically.- Parameters:
qualifiedTableName- qualified table namefromDDL- is this table generated from a create table statement- Returns:
- SQL Table
-
addView
-
getObjectName
-
getObjectName
-
getDatabaseName
-
getDatabaseName
-
getSchemaName
-
getSchemaName
-
searchFunction
-
searchFunction
-
getAFullQualifiedSchemaObjectName
If the input name is fully qualified with db and schema, just return it without any modification. If the schema name is missed, usegetDefaultSchemaName()instead If the database name is missed, usegetDefaultCatalogName()instead some sample result:
db.schema.table .schema.table,getDefaultCatalogName()is null ..table, bothgetDefaultCatalogName()andgetDefaultSchemaName()are null.- Parameters:
schemaObjectName-- Returns:
-
getNumberOfTables
-
toString
-
getProcedureParameterValue
-
getVariableValue
-
isUsedBySqlflow
-