提交 659b9a3c 编写于 作者: L LonwoLonwo 提交者: Anastasiya Volkova

#11216 postgreSQL data types cache reading changed

上级 c138371a
......@@ -121,6 +121,8 @@ public class PostgreMessages extends NLS {
public static String dialog_setting_connection_show_templates_tip;
public static String dialog_setting_connection_show_not_available_for_conn;
public static String dialog_setting_connection_show_not_available_for_conn_tip;
public static String dialog_setting_connection_read_all_data_types;
public static String dialog_setting_connection_read_all_data_types_tip;
public static String dialog_setting_group_sql;
public static String dialog_setting_sql_dd_plain_label;
public static String dialog_setting_sql_dd_plain_tip;
......
......@@ -98,6 +98,8 @@ dialog_setting_connection_show_templates = Show template databases
dialog_setting_connection_show_templates_tip = Show template databases in database list.\nEnabled only if non-default databases are visible
dialog_setting_connection_show_not_available_for_conn = Show databases not available for connection
dialog_setting_connection_show_not_available_for_conn_tip =Show databases not available for connection in database list.\nEnabled only if non-default databases are visible
dialog_setting_connection_read_all_data_types = Read all data types
dialog_setting_connection_read_all_data_types_tip = Read all database data types. Including array types
dialog_setting_group_sql = SQL
dialog_setting_sql_dd_plain_label = Show $$ quote as
......
......@@ -53,6 +53,8 @@ dialog_setting_connection_show_templates = \u041F\u043E\u043A\u0430\u0437\u0430\
dialog_setting_connection_show_templates_tip = \u041F\u043E\u043A\u0430\u0437\u044B\u0432\u0430\u0442\u044C \u0448\u0430\u0431\u043B\u043E\u043D\u043D\u044B\u0435 \u0431\u0430\u0437\u044B \u0434\u0430\u043D\u043D\u044B\u0445 \u0432 \u043D\u0430\u0432\u0438\u0433\u0430\u0442\u043E\u0440\u0435.\n\u0412\u043A\u043B\u044E\u0447\u0435\u043D\u043E, \u0442\u043E\u043B\u044C\u043A\u043E \u0435\u0441\u043B\u0438 \u0432\u0438\u0434\u043D\u044B \u0432\u0441\u0435 \u0431\u0430\u0437\u044B \u0434\u0430\u043D\u043D\u044B\u0445.
dialog_setting_connection_show_not_available_for_conn = \u041F\u043E\u043A\u0430\u0437\u044B\u0432\u0430\u0442\u044C \u0431\u0430\u0437\u044B \u0434\u0430\u043D\u043D\u044B\u0445, \u043D\u0435\u0434\u043E\u0441\u0442\u0443\u043F\u043D\u044B\u0435 \u0434\u043B\u044F \u043F\u043E\u0434\u043A\u043B\u044E\u0447\u0435\u043D\u0438\u044F
dialog_setting_connection_show_not_available_for_conn_tip = \u041F\u043E\u043A\u0430\u0437\u044B\u0432\u0430\u0442\u044C \u0431\u0430\u0437\u044B \u0434\u0430\u043D\u043D\u044B\u0445, \u043D\u0435\u0434\u043E\u0441\u0442\u0443\u043F\u043D\u044B\u0435 \u0434\u043B\u044F \u043F\u043E\u0434\u043A\u043B\u044E\u0447\u0435\u043D\u0438\u044F, \u0432 \u043D\u0430\u0432\u0438\u0433\u0430\u0442\u043E\u0440\u0435.\n\u0412\u043A\u043B\u044E\u0447\u0435\u043D\u043E, \u0442\u043E\u043B\u044C\u043A\u043E \u0435\u0441\u043B\u0438 \u0432\u0438\u0434\u043D\u044B \u0432\u0441\u0435 \u0431\u0430\u0437\u044B \u0434\u0430\u043D\u043D\u044B\u0445.
dialog_setting_connection_read_all_data_types = \u0427\u0438\u0442\u0430\u0442\u044C \u0432\u0441\u0435 \u0442\u0438\u043F\u044B \u0434\u0430\u043D\u043D\u044B\u0445
dialog_setting_connection_read_all_data_types_tip = \u0427\u0438\u0442\u0430\u0442\u044C \u0432\u0441\u0435 \u0442\u0438\u043F\u044B \u0434\u0430\u043D\u043D\u044B\u0445. \u0412\u043A\u043B\u044E\u0447\u0430\u044F \u0442\u0438\u043F\u044B \u043C\u0430\u0441\u0441\u0438\u0432\u043E\u0432
dialog_setting_connection_user = \u041F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u0435\u043B\u044C
dialog_setting_sql_dd_code_block = \u0411\u043B\u043E\u043A \u043A\u043E\u0434\u0430
dialog_setting_sql_dd_plain_label = \u041F\u043E\u043A\u0430\u0437\u0430\u0442\u044C $$ \u043A\u0430\u0432\u044B\u0447\u043A\u0438 \u0432 \u0432\u0438\u0434\u0435
......
......@@ -44,6 +44,7 @@ public class PostgreConnectionPageAdvanced extends ConnectionPageAbstract
private Button showNonDefault;
private Button showTemplates;
private Button showUnavailable;
private Button readAllDataTypes;
private Combo ddPlainBehaviorCombo;
private Combo ddTagBehaviorCombo;
......@@ -84,6 +85,7 @@ public class PostgreConnectionPageAdvanced extends ConnectionPageAbstract
});
showTemplates = UIUtils.createCheckbox(secureGroup, PostgreMessages.dialog_setting_connection_show_templates, PostgreMessages.dialog_setting_connection_show_templates_tip, false, 2);
showUnavailable = UIUtils.createCheckbox(secureGroup, PostgreMessages.dialog_setting_connection_show_not_available_for_conn, PostgreMessages.dialog_setting_connection_show_not_available_for_conn_tip, false, 2);
readAllDataTypes = UIUtils.createCheckbox(secureGroup, PostgreMessages.dialog_setting_connection_read_all_data_types, PostgreMessages.dialog_setting_connection_read_all_data_types_tip, false, 2);
}
{
......@@ -130,6 +132,9 @@ public class PostgreConnectionPageAdvanced extends ConnectionPageAbstract
CommonUtils.getBoolean(connectionInfo.getProviderProperty(PostgreConstants.PROP_SHOW_UNAVAILABLE_DB),
globalPrefs.getBoolean(PostgreConstants.PROP_SHOW_UNAVAILABLE_DB)));
showUnavailable.setEnabled(showNonDefault.getSelection());
readAllDataTypes.setSelection(
CommonUtils.getBoolean(connectionInfo.getProviderProperty(PostgreConstants.PROP_READ_ALL_DATA_TYPES),
globalPrefs.getBoolean(PostgreConstants.PROP_READ_ALL_DATA_TYPES)));
ddPlainBehaviorCombo.select(CommonUtils.getBoolean(
connectionInfo.getProviderProperty(PostgreConstants.PROP_DD_PLAIN_STRING),
......@@ -147,6 +152,7 @@ public class PostgreConnectionPageAdvanced extends ConnectionPageAbstract
connectionCfg.setProviderProperty(PostgreConstants.PROP_SHOW_NON_DEFAULT_DB, String.valueOf(showNonDefault.getSelection()));
connectionCfg.setProviderProperty(PostgreConstants.PROP_SHOW_TEMPLATES_DB, String.valueOf(showTemplates.getSelection()));
connectionCfg.setProviderProperty(PostgreConstants.PROP_SHOW_UNAVAILABLE_DB, String.valueOf(showUnavailable.getSelection()));
connectionCfg.setProviderProperty(PostgreConstants.PROP_READ_ALL_DATA_TYPES, String.valueOf(readAllDataTypes.getSelection()));
connectionCfg.setProviderProperty(PostgreConstants.PROP_DD_PLAIN_STRING, String.valueOf(ddPlainBehaviorCombo.getSelectionIndex() == 0));
connectionCfg.setProviderProperty(PostgreConstants.PROP_DD_TAG_STRING, String.valueOf(ddTagBehaviorCombo.getSelectionIndex() == 0));
......
......@@ -43,6 +43,7 @@ public class PrefPagePostgreSQL extends AbstractPrefPage implements IWorkbenchPr
private Button showNonDefault;
private Button showTemplates;
private Button showUnavailable;
private Button readAllDataTypes;
private Combo ddPlainBehaviorCombo;
private Combo ddTagBehaviorCombo;
......@@ -93,6 +94,11 @@ public class PrefPagePostgreSQL extends AbstractPrefPage implements IWorkbenchPr
globalPrefs.getBoolean(PostgreConstants.PROP_SHOW_UNAVAILABLE_DB),
2
);
readAllDataTypes = UIUtils.createCheckbox(secureGroup,
PostgreMessages.dialog_setting_connection_read_all_data_types,
PostgreMessages.dialog_setting_connection_read_all_data_types_tip,
globalPrefs.getBoolean(PostgreConstants.PROP_READ_ALL_DATA_TYPES),
2);
}
{
......@@ -121,6 +127,7 @@ public class PrefPagePostgreSQL extends AbstractPrefPage implements IWorkbenchPr
preferenceStore.setValue(PostgreConstants.PROP_SHOW_NON_DEFAULT_DB, String.valueOf(showNonDefault.getSelection()));
preferenceStore.setValue(PostgreConstants.PROP_SHOW_TEMPLATES_DB, String.valueOf(showTemplates.getSelection()));
preferenceStore.setValue(PostgreConstants.PROP_SHOW_UNAVAILABLE_DB, String.valueOf(showUnavailable.getSelection()));
preferenceStore.setValue(PostgreConstants.PROP_READ_ALL_DATA_TYPES, String.valueOf(readAllDataTypes.getSelection()));
preferenceStore.setValue(PostgreConstants.PROP_DD_PLAIN_STRING, ddPlainBehaviorCombo.getSelectionIndex() == 0);
preferenceStore.setValue(PostgreConstants.PROP_DD_TAG_STRING, ddTagBehaviorCombo.getSelectionIndex() == 0);
......
......@@ -36,6 +36,7 @@ import org.jkiss.dbeaver.ui.editors.object.struct.CreateProcedurePage;
import org.jkiss.utils.CommonUtils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
......@@ -131,6 +132,7 @@ public class PostgreProcedureConfigurator implements DBEObjectConfigurator<Postg
}
{
List<PostgreDataType> dataTypes = new ArrayList<>(parent.getDatabase().getLocalDataTypes());
dataTypes.sort(Comparator.comparing(PostgreDataType::getName));
returnTypeCombo = UIUtils.createLabelCombo(group, "Return type", SWT.DROP_DOWN);
for (PostgreDataType dt : dataTypes) {
returnTypeCombo.add(dt.getName());
......
......@@ -44,6 +44,7 @@ public class PostgreConstants {
public static final String PROP_SHOW_NON_DEFAULT_DB = DBConstants.INTERNAL_PROP_PREFIX + "show-non-default-db@";
public static final String PROP_SHOW_UNAVAILABLE_DB = DBConstants.INTERNAL_PROP_PREFIX + "show-unavailable-db@";
public static final String PROP_SHOW_TEMPLATES_DB = DBConstants.INTERNAL_PROP_PREFIX + "show-template-db@";
public static final String PROP_READ_ALL_DATA_TYPES = DBConstants.INTERNAL_PROP_PREFIX + "read-all-data-types-db@";
public static final String PROP_DD_PLAIN_STRING = "postgresql.dd.plain.string";
public static final String PROP_DD_TAG_STRING = "postgresql.dd.tag.string";
......
......@@ -726,4 +726,8 @@ public class PostgreDataSource extends JDBCDataSource implements DBSInstanceCont
}
return null;
}
public boolean supportReadingAllDataTypes() {
return CommonUtils.toBoolean(getContainer().getActualConnectionConfiguration().getProviderProperty(PostgreConstants.PROP_READ_ALL_DATA_TYPES));
}
}
......@@ -49,7 +49,7 @@ import java.util.*;
/**
* PostgreTypeType
*/
public class PostgreDataType extends JDBCDataType<PostgreDatabase> implements PostgreClass, PostgreScriptObject, DBPQualifiedObject, DBPImageProvider
public class PostgreDataType extends JDBCDataType<PostgreSchema> implements PostgreClass, PostgreScriptObject, DBPQualifiedObject, DBPImageProvider
{
private static final Log log = Log.getLog(PostgreDataType.class);
......@@ -75,7 +75,6 @@ public class PostgreDataType extends JDBCDataType<PostgreDatabase> implements Po
private final boolean alias;
private long typeId;
private PostgreSchema typeSchema;
private PostgreTypeType typeType;
private PostgreTypeCategory typeCategory;
private DBPDataKind dataKind;
......@@ -111,9 +110,8 @@ public class PostgreDataType extends JDBCDataType<PostgreDatabase> implements Po
private final AttributeCache attributeCache;
private Object[] enumValues;
public PostgreDataType(@NotNull JDBCSession session, @NotNull PostgreDatabase owner, PostgreSchema schema, long typeId, int valueType, String name, int length, JDBCResultSet dbResult) throws DBException {
super(owner, valueType, name, null, false, true, length, -1, -1);
this.typeSchema = schema;
public PostgreDataType(@NotNull JDBCSession session, @NotNull PostgreSchema schema, long typeId, int valueType, String name, int length, JDBCResultSet dbResult) throws DBException {
super(schema, valueType, name, null, false, true, length, -1, -1);
this.alias = false;
if (schema.isCatalogSchema()) {
this.canonicalName = PostgreConstants.DATA_TYPE_CANONICAL_NAMES.get(name);
......@@ -210,8 +208,6 @@ public class PostgreDataType extends JDBCDataType<PostgreDatabase> implements Po
setName(aliasName);
this.alias = true;
this.typeSchema = realType.getParentObject().getCatalogSchema();
this.typeId = realType.typeId;
this.typeType = realType.typeType;
this.typeCategory = realType.typeCategory;
......@@ -245,9 +241,8 @@ public class PostgreDataType extends JDBCDataType<PostgreDatabase> implements Po
}
@ForTest
PostgreDataType(PostgreDatabase database, int valueType, String name) {
super(database, valueType, name, null, false, false, -1, -1, -1);
this.typeSchema = database.getCatalogSchema();
PostgreDataType(PostgreSchema schema, int valueType, String name) {
super(schema, valueType, name, null, false, false, -1, -1, -1);
alias = false;
ownerId = 0;
attributeCache = null;
......@@ -330,7 +325,7 @@ public class PostgreDataType extends JDBCDataType<PostgreDatabase> implements Po
@NotNull
@Override
public PostgreDatabase getDatabase() {
return getParentObject();
return getParentObject().getDatabase();
}
@Override
......@@ -585,17 +580,14 @@ public class PostgreDataType extends JDBCDataType<PostgreDatabase> implements Po
return enumValues;
}
public PostgreSchema getTypeSchema() {
return typeSchema;
}
@NotNull
@Override
public String getFullyQualifiedName(DBPEvaluationContext context) {
if (typeSchema == null || typeSchema.getName().equals(PostgreConstants.PUBLIC_SCHEMA_NAME) || typeSchema.getName().equals(PostgreConstants.CATALOG_SCHEMA_NAME)) {
final PostgreSchema owner = getParentObject();
if (owner == null || owner.getName().equals(PostgreConstants.PUBLIC_SCHEMA_NAME) || owner.getName().equals(PostgreConstants.CATALOG_SCHEMA_NAME)) {
return getName();
} else {
return DBUtils.getQuotedIdentifier(typeSchema) + "." + DBUtils.getQuotedIdentifier(this);
return DBUtils.getQuotedIdentifier(owner) + "." + DBUtils.getQuotedIdentifier(this);
}
}
......@@ -796,14 +788,19 @@ public class PostgreDataType extends JDBCDataType<PostgreDatabase> implements Po
public static PostgreDataType readDataType(@NotNull JDBCSession session, @NotNull PostgreDatabase database, @NotNull JDBCResultSet dbResult, boolean skipTables) throws SQLException, DBException
{
//long schemaId = JDBCUtils.safeGetLong(dbResult, "typnamespace");
long schemaId = JDBCUtils.safeGetLong(dbResult, "typnamespace");
PostgreSchema dataTypeSchema = database.getSchema(session.getProgressMonitor(), schemaId);
if (dataTypeSchema == null) {
return null;
}
long typeId = JDBCUtils.safeGetLong(dbResult, "oid"); //$NON-NLS-1$
String name = JDBCUtils.safeGetString(dbResult, "typname"); //$NON-NLS-1$
if (CommonUtils.isEmpty(name)) {
log.debug("Empty name for data type " + typeId);
return null;
}
if (skipTables) {
boolean readAllTypes = database.getDataSource().supportReadingAllDataTypes();
if (!readAllTypes && skipTables) {
String relKind = JDBCUtils.safeGetString(dbResult, "relkind"); //$NON-NLS-1$
if (relKind != null) {
try {
......@@ -999,17 +996,13 @@ public class PostgreDataType extends JDBCDataType<PostgreDatabase> implements Po
}
}
}
if (skipTables && valueType == Types.ARRAY) {
if (!readAllTypes && skipTables && valueType == Types.ARRAY) {
// Skip arrays as well
return null;
}
int schemaId = JDBCUtils.safeGetInt(dbResult, "typnamespace");
PostgreSchema dataTypeSchema = database.getSchema(session.getProgressMonitor(), schemaId);
return new PostgreDataType(
session,
database,
dataTypeSchema,
typeId,
valueType,
......
......@@ -40,7 +40,7 @@ import java.util.*;
/**
* PostgreDataTypeCache
*/
public class PostgreDataTypeCache extends JDBCObjectCache<PostgreDatabase, PostgreDataType>
public class PostgreDataTypeCache extends JDBCObjectCache<PostgreSchema, PostgreDataType>
{
private static final Log log = Log.getLog(PostgreDataTypeCache.class);
......@@ -56,13 +56,13 @@ public class PostgreDataTypeCache extends JDBCObjectCache<PostgreDatabase, Postg
}
@Override
protected synchronized void loadObjects(DBRProgressMonitor monitor, PostgreDatabase database) throws DBException {
super.loadObjects(monitor, database);
mapAliases(database);
protected synchronized void loadObjects(DBRProgressMonitor monitor, PostgreSchema schema) throws DBException {
super.loadObjects(monitor, schema);
mapAliases(schema);
}
void loadDefaultTypes(PostgreDatabase database) {
void loadDefaultTypes(PostgreSchema schema) {
List<PostgreDataType> types = new ArrayList<>();
for (Field oidField : PostgreOid.class.getDeclaredFields()) {
......@@ -74,12 +74,12 @@ public class PostgreDataTypeCache extends JDBCObjectCache<PostgreDatabase, Postg
String fieldName = oidField.getName().toLowerCase(Locale.ENGLISH);
if (fieldName.endsWith("_array")) {
fieldName = fieldName.substring(0, fieldName.length() - 6) + "_";
//PostgreDataType type = new PostgreDataType(database, CommonUtils.toInt(typeId), fieldName);
//PostgreDataType type = new PostgreDataType(schema, CommonUtils.toInt(typeId), fieldName);
//types.add(type);
// Ignore array types
continue;
} else {
PostgreDataType type = new PostgreDataType(database, CommonUtils.toInt(typeId), fieldName);
PostgreDataType type = new PostgreDataType(schema, CommonUtils.toInt(typeId), fieldName);
types.add(type);
}
} catch (Exception e) {
......@@ -88,15 +88,17 @@ public class PostgreDataTypeCache extends JDBCObjectCache<PostgreDatabase, Postg
}
setCache(types);
// Cache aliases
mapAliases(database);
mapAliases(schema);
}
private void mapAliases(PostgreDatabase database) {
void mapAliases(PostgreSchema schema) {
// Cache aliases
PostgreServerExtension serverType = database.getDataSource().getServerType();
mapDataTypeAliases(serverType.getDataTypeAliases(), false);
if (serverType.supportSerialTypes()) {
mapDataTypeAliases(PostgreConstants.SERIAL_TYPES, true);
if (schema.isCatalogSchema()) {
PostgreServerExtension serverType = schema.getDataSource().getServerType();
mapDataTypeAliases(serverType.getDataTypeAliases(), false);
if (serverType.supportSerialTypes()) {
mapDataTypeAliases(PostgreConstants.SERIAL_TYPES, true);
}
}
}
......@@ -163,7 +165,7 @@ public class PostgreDataTypeCache extends JDBCObjectCache<PostgreDatabase, Postg
}
}
private static String getBaseTypeNameClause(@NotNull PostgreDataSource dataSource) {
static String getBaseTypeNameClause(@NotNull PostgreDataSource dataSource) {
if (dataSource.isServerVersionAtLeast(7, 3)) {
return "format_type(nullif(t.typbasetype, 0), t.typtypmod) as base_type_name";
} else {
......@@ -173,29 +175,32 @@ public class PostgreDataTypeCache extends JDBCObjectCache<PostgreDatabase, Postg
@NotNull
@Override
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull PostgreDatabase owner) throws SQLException {
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull PostgreSchema owner) throws SQLException {
// Initially cache only base types (everything but composite and arrays)
boolean readAllTypes = owner.getDataSource().supportReadingAllDataTypes();
String sql =
"SELECT t.oid,t.*,c.relkind," + getBaseTypeNameClause(owner.getDataSource()) +", d.description" +
"\nFROM pg_catalog.pg_type t" +
"\nLEFT OUTER JOIN pg_catalog.pg_class c ON c.oid=t.typrelid" +
"\nLEFT OUTER JOIN pg_catalog.pg_description d ON t.oid=d.objoid" +
"\nWHERE t.typname IS NOT null" +
"\nAND t.typcategory <> 'A'" + // Do not read array types
"\nAND c.relkind is null or c.relkind = 'c'"; // 'c' == custom types
//"\nWHERE typnamespace=? " +
(readAllTypes ? "" : "\nAND t.typcategory <> 'A' AND c.relkind is null or c.relkind = 'c'") + // Do not read array types, unless the user has decided otherwise
//"\nAND c.relkind is null or c.relkind = 'c'"; // 'c' == custom types
"\nAND typnamespace=? ";
//"\nORDER by t.oid";
return session.prepareStatement(sql);
final JDBCPreparedStatement dbStat = session.prepareStatement(sql);
dbStat.setLong(1, owner.getObjectId());
return dbStat;
}
@Override
protected PostgreDataType fetchObject(@NotNull JDBCSession session, @NotNull PostgreDatabase owner, @NotNull JDBCResultSet dbResult) throws SQLException, DBException
protected PostgreDataType fetchObject(@NotNull JDBCSession session, @NotNull PostgreSchema owner, @NotNull JDBCResultSet dbResult) throws SQLException, DBException
{
return PostgreDataType.readDataType(session, owner, dbResult, true);
return PostgreDataType.readDataType(session, owner.getDatabase(), dbResult, true);
}
@Override
protected void invalidateObjects(DBRProgressMonitor monitor, PostgreDatabase database, Iterator<PostgreDataType> objectIter) {
protected void invalidateObjects(DBRProgressMonitor monitor, PostgreSchema schema, Iterator<PostgreDataType> objectIter) {
// Resolve value type IDs (#3731)
while (objectIter.hasNext()) {
PostgreDataType dt = objectIter.next();
......
......@@ -43,13 +43,12 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.model.struct.rdb.DBSCatalog;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.LongKeyMap;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.*;
/**
* PostgreDatabase
......@@ -97,7 +96,7 @@ public class PostgreDatabase extends JDBCRemoteInstance
public final AvailableExtensionCache availableExtensionCache = new AvailableExtensionCache();
public final CollationCache collationCache = new CollationCache();
public final TablespaceCache tablespaceCache = new TablespaceCache();
public final PostgreDataTypeCache dataTypeCache = new PostgreDataTypeCache();
public final LongKeyMap<PostgreDataType> dataTypeCache = new LongKeyMap<>();
public JDBCObjectLookupCache<PostgreDatabase, PostgreSchema> schemaCache;
......@@ -159,7 +158,7 @@ public class PostgreDatabase extends JDBCRemoteInstance
this.name = databaseName;
this.initCaches();
PostgreSchema sysSchema = new PostgreSchema(this, PostgreConstants.CATALOG_SCHEMA_NAME);
dataTypeCache.loadDefaultTypes(this);
sysSchema.getDataTypeCache().loadDefaultTypes(sysSchema);
schemaCache.cacheObject(sysSchema);
}
......@@ -503,7 +502,14 @@ public class PostgreDatabase extends JDBCRemoteInstance
@Override
public Collection<PostgreDataType> getLocalDataTypes() {
return dataTypeCache.getCachedObjects();
if (!CommonUtils.isEmpty(dataTypeCache)) {
return dataTypeCache.values();
}
final PostgreSchema schema = getCatalogSchema();
if (schema != null) {
return schema.getDataTypeCache().getCachedObjects();
}
return null;
}
@Override
......@@ -581,10 +587,45 @@ public class PostgreDatabase extends JDBCRemoteInstance
}
void cacheDataTypes(DBRProgressMonitor monitor, boolean forceRefresh) throws DBException {
if (!dataTypeCache.isFullyCached() || forceRefresh) {
dataTypeCache.clearCache();
if (dataTypeCache.isEmpty() || forceRefresh) {
dataTypeCache.clear();
// Cache data types
dataTypeCache.loadObjects(monitor, this);
boolean readAllTypes = getDataSource().supportReadingAllDataTypes();
String sql = "SELECT t.oid,t.*,c.relkind," + PostgreDataTypeCache.getBaseTypeNameClause(getDataSource()) +", d.description" +
"\nFROM pg_catalog.pg_type t" +
"\nLEFT OUTER JOIN pg_catalog.pg_class c ON c.oid=t.typrelid" +
"\nLEFT OUTER JOIN pg_catalog.pg_description d ON t.oid=d.objoid" +
"\nWHERE t.typname IS NOT null" +
(readAllTypes ? "" : "\nAND t.typcategory <> 'A' AND c.relkind is null or c.relkind = 'c'");
try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Read data types")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement(sql)) {
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
Set<PostgreSchema> schemaList = new HashSet<>();
while (dbResult.next()) {
PostgreDataType dataType = PostgreDataType.readDataType(session, this, dbResult, !readAllTypes);
if (dataType != null) {
PostgreSchema schema = dataType.getParentObject();
schemaList.add(schema);
schema.getDataTypeCache().cacheObject(dataType);
dataTypeCache.put(dataType.getObjectId(), dataType);
}
}
if (!schemaList.isEmpty()) {
for (PostgreSchema schema : schemaList) {
schema.getDataTypeCache().setFullCache(true);
}
}
PostgreSchema catalogSchema = getCatalogSchema();
if (catalogSchema != null) {
catalogSchema.getDataTypeCache().mapAliases(catalogSchema);
}
}
}
} catch (SQLException e) {
throw new DBException(e, getDataSource());
}
}
}
......@@ -712,14 +753,22 @@ public class PostgreDatabase extends JDBCRemoteInstance
if (typeId <= 0) {
return null;
}
PostgreDataType dataType = dataTypeCache.getDataType(typeId);
PostgreDataType dataType = dataTypeCache.get(typeId);
if (dataType != null) {
return dataType;
}
for (PostgreSchema schema : schemaCache.getCachedObjects()) {
dataType = schema.getDataTypeCache().getDataType(typeId);
if (dataType != null) {
dataTypeCache.put(typeId, dataType);
return dataType;
}
}
// Type not found. Let's resolve it
try {
dataType = PostgreDataTypeCache.resolveDataType(monitor, this, typeId);
dataTypeCache.cacheObject(dataType);
dataType.getParentObject().getDataTypeCache().cacheObject(dataType);
dataTypeCache.put(dataType.getObjectId(), dataType);
return dataType;
} catch (Exception e) {
log.debug("Can't resolve data type " + typeId, e);
......@@ -733,7 +782,33 @@ public class PostgreDatabase extends JDBCRemoteInstance
typeName = "_" + typeName.substring(0, typeName.length() - 2);
}
{
PostgreDataType dataType = dataTypeCache.getCachedObject(typeName);
// First check system catalog
final PostgreSchema schema = getCatalogSchema();
if (schema != null) {
final PostgreDataType dataType = schema.getDataTypeCache().getCachedObject(typeName);
if (dataType != null) {
return dataType;
}
}
}
// Check schemas in search path
List<String> searchPath = getMetaContext().getSearchPath();
for (String schemaName : searchPath) {
final PostgreSchema schema = schemaCache.getCachedObject(schemaName);
if (schema != null) {
final PostgreDataType dataType = schema.getDataTypeCache().getCachedObject(typeName);
if (dataType != null) {
return dataType;
}
}
}
// Check the rest
for (PostgreSchema schema : schemaCache.getCachedObjects()) {
if (searchPath.contains(schema.getName())) {
continue;
}
final PostgreDataType dataType = schema.getDataTypeCache().getCachedObject(typeName);
if (dataType != null) {
return dataType;
}
......@@ -746,7 +821,8 @@ public class PostgreDatabase extends JDBCRemoteInstance
// Type not found. Let's resolve it
try {
PostgreDataType dataType = PostgreDataTypeCache.resolveDataType(monitor, this, typeName);
dataTypeCache.cacheObject(dataType);
dataType.getParentObject().getDataTypeCache().cacheObject(dataType);
dataTypeCache.put(dataType.getObjectId(), dataType);
return dataType;
} catch (Exception e) {
log.debug("Can't resolve data type '" + typeName + "' in database '" + getName() + "'");
......
......@@ -611,7 +611,7 @@ public class PostgreProcedure extends AbstractProcedure<PostgreDataSource, Postg
paramsSignature.append(param.getName()).append(' ');
}
final PostgreDataType dataType = param.getParameterType();
final PostgreSchema typeContainer = dataType.getTypeSchema();
final PostgreSchema typeContainer = dataType.getParentObject();
if (typeContainer == null ||
typeContainer.isPublicSchema() ||
typeContainer.isCatalogSchema())
......
......@@ -88,7 +88,7 @@ public class PostgreSchema implements
private final ConstraintCache constraintCache;
private final ProceduresCache proceduresCache;
private final IndexCache indexCache;
private List<PostgreDataType> dataTypeList;
private final PostgreDataTypeCache dataTypeCache;
protected volatile boolean hasStatistics;
PostgreSchema(PostgreDatabase database, String name) {
......@@ -101,7 +101,7 @@ public class PostgreSchema implements
constraintCache = createConstraintCache();
indexCache = new IndexCache();
proceduresCache = createProceduresCache();
dataTypeList = new ArrayList<>();
dataTypeCache = new PostgreDataTypeCache();
}
@NotNull
......@@ -276,8 +276,8 @@ public class PostgreSchema implements
return indexCache;
}
public List<PostgreDataType> getDataTypeList() {
return dataTypeList;
public PostgreDataTypeCache getDataTypeCache() {
return dataTypeCache;
}
@Association
......@@ -455,16 +455,12 @@ public class PostgreSchema implements
//@Property
@Association
public Collection<PostgreDataType> getDataTypes(DBRProgressMonitor monitor) throws DBException {
if (CommonUtils.isEmpty(dataTypeList)) {
Collection<PostgreDataType> allDatabaseTypes = getDatabase().getLocalDataTypes();
for (PostgreDataType dt : allDatabaseTypes) {
if (dt.getTypeSchema() == this) {
dataTypeList.add(dt);
}
}
DBUtils.orderObjects(dataTypeList);
List<PostgreDataType> types = new ArrayList<>();
for (PostgreDataType dt : dataTypeCache.getAllObjects(monitor, this)) {
types.add(dt);
}
return dataTypeList;
DBUtils.orderObjects(types);
return types;
}
@Override
......@@ -509,8 +505,9 @@ public class PostgreSchema implements
*/
Collection<PostgreDataType> dataTypes = getDataTypes(monitor);
monitor.beginTask("Load data types", dataTypes.size());
boolean readAllTypes = getDatabase().getDataSource().supportReadingAllDataTypes();
for (PostgreDataType dataType : dataTypes) {
if (dataType.hasAttributes() || dataType.isArray()) {
if (!readAllTypes && (dataType.hasAttributes() || dataType.isArray())) {
// Skipp table types and arrays
continue;
}
......
......@@ -168,6 +168,10 @@ public abstract class AbstractObjectCache<OWNER extends DBSObject, OBJECT extend
return this.fullCache;
}
public void setFullCache(boolean fullCache) {
this.fullCache = fullCache;
}
@Override
public void clearCache()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册