reservedWords.addAll(Arrays.asList("GROUP","USER","SESSION","PASSWORD","RESOURCE","START","SIZE","UID","DESC","ORDER"));//more reserved words not returned by driver
ConnectionsqlConn=null;
if(!(conninstanceofOfflineConnection)){
try{
/*
* Don't try to call getWrappedConnection if the conn instance is
* is not a JdbcConnection. This happens for OfflineConnection.
@SuppressWarnings("HardCodedStringLiteral")Stringmessage="Cannot read from v$parameter: "+e.getMessage();
//noinspection HardCodedStringLiteral
LogService.getLog(getClass()).info(LogType.LOG,"Could not set check compatibility mode on OracleDatabase, assuming not running in any sort of compatibility mode: "+message);
// CORE-1768 - Oracle creates these for spatial indices and will remove them when the index is removed.
returntrue;
}else//noinspection HardCodedStringLiteral
if(example.getName().startsWith("MLOG$_")){//Created by materliaized view logs for every table that is part of a materialized view. Not available for DDL operations.
returntrue;
}else//noinspection HardCodedStringLiteral
if(example.getName().startsWith("RUPD$_")){//Created by materialized view log tables using primary keys. Not available for DDL operations.
if("CREATE$JAVA$LOB$TABLE".equals(example.getName())){//This table contains the name of the Java object, the date it was loaded, and has a BLOB column to store the Java object.
returntrue;
}else//noinspection HardCodedStringLiteral
if("JAVA$CLASS$MD5$TABLE".equals(example.getName())){//This is a hash table that tracks the loading of Java objects into a schema.
// Oracle supports Identity beginning with version 12c
booleanisAutoIncrementSupported=false;
try{
if(getDatabaseMajorVersion()>=12){
isAutoIncrementSupported=true;
}
// Returning true will generate create table command with 'IDENTITY' clause, example:
// CREATE TABLE AutoIncTest (IDPrimaryKey NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL, TypeID NUMBER(3) NOT NULL, Description NVARCHAR2(50), CONSTRAINT PK_AutoIncTest PRIMARY KEY (IDPrimaryKey));
// While returning false will continue to generate create table command without 'IDENTITY' clause, example:
// CREATE TABLE AutoIncTest (IDPrimaryKey NUMBER(19) NOT NULL, TypeID NUMBER(3) NOT NULL, Description NVARCHAR2(50), CONSTRAINT PK_AutoIncTest PRIMARY KEY (IDPrimaryKey));
}catch(DatabaseExceptionex){
isAutoIncrementSupported=false;
}
returnisAutoIncrementSupported;
}
// public Set<UniqueConstraint> findUniqueConstraints(String schema) throws DatabaseException {
// Set<UniqueConstraint> returnSet = new HashSet<UniqueConstraint>();
//
// List<Map> maps = new Executor(this).queryForList(new RawSqlStatement("SELECT UC.CONSTRAINT_NAME, UCC.TABLE_NAME, UCC.COLUMN_NAME FROM USER_CONSTRAINTS UC, USER_CONS_COLUMNS UCC WHERE UC.CONSTRAINT_NAME=UCC.CONSTRAINT_NAME AND CONSTRAINT_TYPE='U' ORDER BY UC.CONSTRAINT_NAME"));
//
// UniqueConstraint constraint = null;
// for (Map map : maps) {
// if (constraint == null || !constraint.getName().equals(constraint.getName())) {
// returnSet.add(constraint);
// Table table = new Table((String) map.get("TABLE_NAME"));
// constraint = new UniqueConstraint(map.get("CONSTRAINT_NAME").toString(), table);
userDefinedTypes.addAll(ExecutorService.getInstance().getExecutor(this).queryForList(newRawSqlStatement("SELECT DISTINCT TYPE_NAME FROM ALL_TYPES"),String.class));
}catch(DatabaseExceptione){//fall back to USER_TYPES if the user cannot see ALL_TYPES
//noinspection HardCodedStringLiteral
userDefinedTypes.addAll(ExecutorService.getInstance().getExecutor(this).queryForList(newRawSqlStatement("SELECT TYPE_NAME FROM USER_TYPES"),String.class));
sql+="WHERE c.TABLE_OWNER IN ('"+database.correctObjectName(catalogAndSchema.getCatalogName(),Schema.class)+"', "+getAllCatalogsStringScratchData()+")";
"db_name("+(databaseName==null?"":"db_id('"+databaseName+"')")+") AS TABLE_CAT, "+
"object_schema_name(c.object_id"+dbIdParam+") AS TABLE_SCHEM, "+
"object_name(c.object_id"+dbIdParam+") AS TABLE_NAME, "+
"c.name AS COLUMN_NAME, "+
"is_filestream, "+
"is_rowguidcol, "+
"CASE WHEN c.is_identity = 'true' THEN 'YES' ELSE 'NO' END as IS_AUTOINCREMENT, "+
"{REMARKS_COLUMN_PLACEHOLDER}"+
"t.name AS TYPE_NAME, "+
"dc.name as COLUMN_DEF_NAME, "+
"dc.definition as COLUMN_DEF, "+
// data type mapping from https://msdn.microsoft.com/en-us/library/ms378878(v=sql.110).aspx
"CASE t.name "+
"WHEN 'bigint' THEN "+Types.BIGINT+" "+
"WHEN 'binary' THEN "+Types.BINARY+" "+
"WHEN 'bit' THEN "+Types.BIT+" "+
"WHEN 'char' THEN "+Types.CHAR+" "+
"WHEN 'date' THEN "+Types.DATE+" "+
"WHEN 'datetime' THEN "+Types.TIMESTAMP+" "+
"WHEN 'datetime2' THEN "+Types.TIMESTAMP+" "+
"WHEN 'datetimeoffset' THEN -155 "+
"WHEN 'decimal' THEN "+Types.DECIMAL+" "+
"WHEN 'float' THEN "+Types.DOUBLE+" "+
"WHEN 'image' THEN "+Types.LONGVARBINARY+" "+
"WHEN 'int' THEN "+Types.INTEGER+" "+
"WHEN 'money' THEN "+Types.DECIMAL+" "+
"WHEN 'nchar' THEN "+Types.NCHAR+" "+
"WHEN 'ntext' THEN "+Types.LONGNVARCHAR+" "+
"WHEN 'numeric' THEN "+Types.NUMERIC+" "+
"WHEN 'nvarchar' THEN "+Types.NVARCHAR+" "+
"WHEN 'real' THEN "+Types.REAL+" "+
"WHEN 'smalldatetime' THEN "+Types.TIMESTAMP+" "+
"WHEN 'smallint' THEN "+Types.SMALLINT+" "+
"WHEN 'smallmoney' THEN "+Types.DECIMAL+" "+
"WHEN 'text' THEN "+Types.LONGVARCHAR+" "+
"WHEN 'time' THEN "+Types.TIME+" "+
"WHEN 'timestamp' THEN "+Types.BINARY+" "+
"WHEN 'tinyint' THEN "+Types.TINYINT+" "+
"WHEN 'udt' THEN "+Types.VARBINARY+" "+
"WHEN 'uniqueidentifier' THEN "+Types.CHAR+" "+
"WHEN 'varbinary' THEN "+Types.VARBINARY+" "+
"WHEN 'varbinary(max)' THEN "+Types.VARBINARY+" "+
"WHEN 'varchar' THEN "+Types.VARCHAR+" "+
"WHEN 'varchar(max)' THEN "+Types.VARCHAR+" "+
"WHEN 'xml' THEN "+Types.LONGVARCHAR+" "+
"WHEN 'LONGNVARCHAR' THEN "+Types.SQLXML+" "+
"ELSE "+Types.OTHER+" END AS data_type, "+
"CASE WHEN c.is_nullable = 'true' THEN 1 ELSE 0 END AS NULLABLE, "+
"10 as NUM_PREC_RADIX, "+
"c.column_id as ORDINAL_POSITION, "+
"c.scale as DECIMAL_DIGITS, "+
"c.max_length as COLUMN_SIZE, "+
"c.precision as DATA_PRECISION "+
"FROM "+databasePrefix+"sys.columns c "+
"inner join "+databasePrefix+"sys.types t on c.user_type_id=t.user_type_id "+
"{REMARKS_JOIN_PLACEHOLDER}"+
"left outer join "+databasePrefix+"sys.default_constraints dc on dc.parent_column_id = c.column_id AND dc.parent_object_id=c.object_id AND type_desc='DEFAULT_CONSTRAINT' "+
sql+=" and object_name(c.object_id"+dbIdParam+")='"+database.escapeStringForDatabase(tableName)+"'";
}
if(columnName!=null){
sql+=" and c.name='"+database.escapeStringForDatabase(columnName)+"'";
}
}
sql+="order by object_schema_name(c.object_id"+dbIdParam+"), object_name(c.object_id"+dbIdParam+"), c.column_id";
// sys.extended_properties is added to Azure on V12: https://feedback.azure.com/forums/217321-sql-database/suggestions/6549815-add-sys-extended-properties-for-meta-data-support
if((!((MSSQLDatabase)database).isAzureDb())// Either NOT AzureDB (=SQL Server 2008 or higher)
||(database.getDatabaseMajorVersion()>=12)){// or at least AzureDB v12
sqlToSelectNotNullConstraints+=" WHERE atc.OWNER='"+jdbcSchemaName+"' AND atc.hidden_column='NO' AND ac.CONSTRAINT_TYPE='C' and ac.search_condition is not null ";
}else{
sqlToSelectNotNullConstraints+=" WHERE atc.OWNER IN ('"+jdbcSchemaName+"', "+getAllCatalogsStringScratchData()+") "
+" AND atc.hidden_column='NO' AND ac.CONSTRAINT_TYPE='C' and ac.search_condition is not null ";
}
sqlToSelectNotNullConstraints+=(!bulk&&tableName!=null&&!tableName.isEmpty())?" AND atc.TABLE_NAME='"+jdbcTableName+"'":"";
Stringsql="SELECT null as TABLE_CAT, a.OWNER as TABLE_SCHEM, a.VIEW_NAME as TABLE_NAME, 'TABLE' as TABLE_TYPE, c.COMMENTS as REMARKS, TEXT as OBJECT_BODY";
if(database.getDatabaseMajorVersion()>10){
sql+=", EDITIONING_VIEW";
}
sql+=" from ALL_VIEWS a "+
"join ALL_TAB_COMMENTS c on a.VIEW_NAME=c.table_name and a.owner=c.owner ";
Stringsql="SELECT NULL AS table_cat, c.owner AS table_schem, c.table_name, c.column_name as COLUMN_NAME, c.position AS key_seq, c.constraint_name AS pk_name, k.VALIDATED as VALIDATED "+
"FROM all_cons_columns c, all_constraints k "+
"LEFT JOIN "+"user_recyclebin"+" d ON d.object_name=k.table_name "+
"CASE [ic].[is_descending_key] WHEN 0 THEN N'A' WHEN 1 THEN N'D' END AS [ASC_OR_DESC], "+
"[ic].[key_ordinal] AS [KEY_SEQ], "+
"[kc].[name] AS [PK_NAME] "+
"FROM [sys].[schemas] AS [s] "+
"INNER JOIN [sys].[tables] AS [t] "+
"ON [t].[schema_id] = [s].[schema_id] "+
"INNER JOIN [sys].[key_constraints] AS [kc] "+
"ON [kc].[parent_object_id] = [t].[object_id] "+
"INNER JOIN [sys].[indexes] AS [i] "+
"ON [i].[object_id] = [kc].[parent_object_id] "+
"AND [i].[index_id] = [kc].[unique_index_id] "+
"INNER JOIN [sys].[index_columns] AS [ic] "+
"ON [ic].[object_id] = [i].[object_id] "+
"AND [ic].[index_id] = [i].[index_id] "+
"INNER JOIN [sys].[columns] AS [c] "+
"ON [c].[object_id] = [ic].[object_id] "+
"AND [c].[column_id] = [ic].[column_id] "+
"WHERE [s].[name] = N'"+database.escapeStringForDatabase(catalogAndSchema.getSchemaName())+"' "+// The schema name was corrected in the customized CatalogAndSchema
Stringsql="SELECT NULL AS table_cat, c.owner AS table_schem, c.table_name, c.column_name, c.position AS key_seq,c.constraint_name AS pk_name, k.VALIDATED as VALIDATED FROM "+
"all_cons_columns c, "+
"all_constraints k "+
"LEFT JOIN "+"user_recyclebin"+" d ON d.object_name=k.table_name "+
sql+=" AND [TC].[TABLE_NAME] = N'"+database.escapeStringForDatabase(database.correctObjectName(tableName,Table.class))+"'";
}
}elseif(databaseinstanceofOracleDatabase){
warnAboutDbaRecycleBin();
sql="select uc.owner AS CONSTRAINT_SCHEM, uc.constraint_name, uc.table_name,uc.status,uc.deferrable,uc.deferred,ui.tablespace_name, ui.index_name, ui.owner as INDEX_CATALOG, uc.VALIDATED as VALIDATED, ac.COLUMN_NAME as COLUMN_NAME "+
"from all_constraints uc "+
"join all_indexes ui on uc.index_name = ui.index_name and uc.owner=ui.table_owner and uc.table_name=ui.table_name "+
"LEFT OUTER JOIN "+"user_recyclebin"+" d ON d.object_name=ui.table_name "+
"LEFT JOIN all_cons_columns ac ON ac.OWNER = uc.OWNER AND ac.TABLE_NAME = uc.TABLE_NAME AND ac.CONSTRAINT_NAME = uc.CONSTRAINT_NAME "+
sql+="and uc.owner IN ('"+jdbcSchemaName+"', "+getAllCatalogsStringScratchData()+")";
}
sql+="AND d.object_name IS NULL ";
if(tableName!=null){
sql+=" and uc.table_name = '"+tableName+"'";
}
}elseif(databaseinstanceofDB2Database){
// if we are on DB2 AS400 iSeries
if(database.getDatabaseProductName().startsWith("DB2 UDB for AS/400")){
sql="select constraint_name as constraint_name, table_name as table_name from QSYS2.TABLE_CONSTRAINTS where table_schema='"+jdbcSchemaName+"' and constraint_type='UNIQUE'";
if(tableName!=null){
sql+=" and table_name = '"+tableName+"'";
}
// DB2 z/OS
}
// here we are on DB2 UDB
else{
sql="select distinct k.constname as constraint_name, t.tabname as TABLE_NAME from syscat.keycoluse k, syscat.tabconst t "
+"where k.constname = t.constname "
+"and t.tabschema = '"+jdbcSchemaName+"' "
+"and t.type='U'";
if(tableName!=null){
sql+=" and t.tabname = '"+tableName+"'";
}
}
}elseif(databaseinstanceofDb2zDatabase){
sql="select distinct k.constname as constraint_name, t.tbname as TABLE_NAME from SYSIBM.SYSKEYCOLUSE k, SYSIBM.SYSTABCONST t "
+"where k.constname = t.constname "
+"and k.TBCREATOR = t.TBCREATOR "
+"and t.TBCREATOR = '"+jdbcSchemaName+"' ";
if(tableName!=null){
sql+=" and t.tbname = '"+tableName+"'";
}
}elseif(databaseinstanceofFirebirdDatabase){
sql="SELECT TRIM(RDB$INDICES.RDB$INDEX_NAME) AS CONSTRAINT_NAME, "+
"TRIM(RDB$INDICES.RDB$RELATION_NAME) AS TABLE_NAME "+