提交 14961e1c 编写于 作者: S serge-rider

Object lookup model refactoring


Former-commit-id: 819b8117
上级 03939fd4
......@@ -43,7 +43,7 @@ import java.util.Set;
/**
* Generic tables cache implementation
*/
public class TableCache extends JDBCStructCache<GenericStructContainer, GenericTable, GenericTableColumn> implements JDBCObjectLookup<GenericStructContainer> {
public class TableCache extends JDBCStructCache<GenericStructContainer, GenericTable, GenericTableColumn> implements JDBCObjectLookup<GenericStructContainer, GenericTable> {
private static final Log log = Log.getLog(TableCache.class);
......@@ -85,7 +85,7 @@ public class TableCache extends JDBCStructCache<GenericStructContainer, GenericT
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull GenericStructContainer owner)
throws SQLException
{
return prepareLookupStatement(session, owner, null);
return prepareLookupStatement(session, owner, null, null);
}
@Nullable
......@@ -202,12 +202,13 @@ public class TableCache extends JDBCStructCache<GenericStructContainer, GenericT
);
}
@NotNull
@Override
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull GenericStructContainer owner, @Nullable String objectName) throws SQLException {
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull GenericStructContainer owner, @Nullable GenericTable object, @Nullable String objectName) throws SQLException {
return session.getMetaData().getTables(
owner.getCatalog() == null ? null : owner.getCatalog().getName(),
owner.getSchema() == null ? null : owner.getSchema().getName(),
objectName == null ? owner.getDataSource().getAllObjectsPattern() : objectName,
object == null && objectName == null ? owner.getDataSource().getAllObjectsPattern() : (object != null ? object.getName() : objectName),
null).getSourceStatement();
}
}
......@@ -320,7 +320,7 @@ public class MySQLCatalog implements DBSCatalog, DBPSaveableObject, DBPRefreshab
return name + " [" + dataSource.getContainer().getName() + "]";
}
public static class TableCache extends JDBCStructCache<MySQLCatalog, MySQLTableBase, MySQLTableColumn> implements JDBCObjectLookup<MySQLCatalog> {
public static class TableCache extends JDBCStructCache<MySQLCatalog, MySQLTableBase, MySQLTableColumn> implements JDBCObjectLookup<MySQLCatalog, MySQLTableBase> {
protected TableCache()
{
......@@ -331,7 +331,7 @@ public class MySQLCatalog implements DBSCatalog, DBPSaveableObject, DBPRefreshab
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull MySQLCatalog owner)
throws SQLException
{
return prepareLookupStatement(session, owner, null);
return prepareLookupStatement(session, owner, null, null);
}
@Override
......@@ -374,11 +374,14 @@ public class MySQLCatalog implements DBSCatalog, DBPSaveableObject, DBPRefreshab
return new MySQLTableColumn(table, dbResult);
}
@NotNull
@Override
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull MySQLCatalog owner, @Nullable String objectName) throws SQLException {
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull MySQLCatalog owner, @Nullable MySQLTableBase object, @Nullable String objectName) throws SQLException {
return session.prepareStatement(
"SHOW FULL TABLES FROM " + DBUtils.getQuotedIdentifier(owner) +
(CommonUtils.isEmpty(objectName) ? "" : " LIKE '" + SQLUtils.escapeString(objectName) + "'"));
(object == null && objectName == null ?
"" :
" LIKE '" + SQLUtils.escapeString(object != null ? object.getName() : objectName) + "'"));
}
}
......@@ -547,7 +550,7 @@ public class MySQLCatalog implements DBSCatalog, DBPSaveableObject, DBPRefreshab
/**
* Procedures cache implementation
*/
static class ProceduresCache extends JDBCStructCache<MySQLCatalog, MySQLProcedure, MySQLProcedureParameter> implements JDBCObjectLookup<MySQLCatalog> {
static class ProceduresCache extends JDBCStructCache<MySQLCatalog, MySQLProcedure, MySQLProcedureParameter> implements JDBCObjectLookup<MySQLCatalog, MySQLProcedure> {
ProceduresCache()
{
......@@ -558,7 +561,7 @@ public class MySQLCatalog implements DBSCatalog, DBPSaveableObject, DBPRefreshab
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull MySQLCatalog owner)
throws SQLException
{
return prepareLookupStatement(session, owner, null);
return prepareLookupStatement(session, owner, null, null);
}
@Override
......@@ -621,17 +624,18 @@ public class MySQLCatalog implements DBSCatalog, DBPSaveableObject, DBPRefreshab
parameterType);
}
@NotNull
@Override
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull MySQLCatalog owner, @Nullable String objectName) throws SQLException {
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull MySQLCatalog owner, @Nullable MySQLProcedure object, @Nullable String objectName) throws SQLException {
JDBCPreparedStatement dbStat = session.prepareStatement(
"SELECT * FROM " + MySQLConstants.META_TABLE_ROUTINES +
"\nWHERE " + MySQLConstants.COL_ROUTINE_SCHEMA + "=?" +
(CommonUtils.isEmpty(objectName) ? "" : " AND " + MySQLConstants.COL_ROUTINE_NAME + "=?") +
(object == null && objectName == null ? "" : " AND " + MySQLConstants.COL_ROUTINE_NAME + "=?") +
"\nORDER BY " + MySQLConstants.COL_ROUTINE_NAME
);
dbStat.setString(1, owner.getName());
if (!CommonUtils.isEmpty(objectName)) {
dbStat.setString(2, objectName);
if (object != null || objectName != null) {
dbStat.setString(2, object != null ? object.getName() : objectName);
}
return dbStat;
}
......
......@@ -351,7 +351,7 @@ public class OracleSchema extends OracleGlobalObject implements DBSSchema, DBPRe
return tableColumn;
}
public static class TableCache extends JDBCStructCache<OracleSchema, OracleTableBase, OracleTableColumn> implements JDBCObjectLookup<OracleSchema> {
public static class TableCache extends JDBCStructCache<OracleSchema, OracleTableBase, OracleTableColumn> implements JDBCObjectLookup<OracleSchema, OracleTableBase> {
private static final Comparator<? super OracleTableColumn> ORDER_COMPARATOR = new Comparator<OracleTableColumn>() {
@Override
......@@ -370,25 +370,26 @@ public class OracleSchema extends OracleGlobalObject implements DBSSchema, DBPRe
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull OracleSchema owner)
throws SQLException
{
return prepareLookupStatement(session, owner, null);
return prepareLookupStatement(session, owner, null, null);
}
@NotNull
@Override
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull OracleSchema owner, @Nullable String objectName) throws SQLException {
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull OracleSchema owner, @Nullable OracleTableBase object, @Nullable String objectName) throws SQLException {
final JDBCPreparedStatement dbStat = session.prepareStatement(
"\tSELECT " + OracleUtils.getSysCatalogHint(owner.getDataSource()) + " t.OWNER,t.TABLE_NAME as TABLE_NAME,'TABLE' as OBJECT_TYPE,'VALID' as STATUS,t.TABLE_TYPE_OWNER,t.TABLE_TYPE,t.TABLESPACE_NAME,t.PARTITIONED,t.IOT_TYPE,t.IOT_NAME,t.TEMPORARY,t.SECONDARY,t.NESTED,t.NUM_ROWS \n" +
"\tFROM SYS.ALL_ALL_TABLES t\n" +
"\tWHERE t.OWNER=? AND NESTED='NO'" + (objectName == null ? "": " AND t.TABLE_NAME=?") + "\n" +
"\tWHERE t.OWNER=? AND NESTED='NO'" + (object == null && objectName == null ? "": " AND t.TABLE_NAME=?") + "\n" +
"UNION ALL\n" +
"\tSELECT " + OracleUtils.getSysCatalogHint(owner.getDataSource()) + " o.OWNER,o.OBJECT_NAME as TABLE_NAME,'VIEW' as OBJECT_TYPE,o.STATUS,NULL,NULL,NULL,NULL,NULL,NULL,o.TEMPORARY,o.SECONDARY,NULL,NULL \n" +
"\tFROM SYS.ALL_OBJECTS o \n" +
"\tWHERE o.OWNER=? AND o.OBJECT_TYPE='VIEW'" + (objectName == null ? "": " AND o.OBJECT_NAME=?") + "\n"
"\tWHERE o.OWNER=? AND o.OBJECT_TYPE='VIEW'" + (object == null && objectName == null ? "": " AND o.OBJECT_NAME=?") + "\n"
);
int index = 1;
dbStat.setString(index++, owner.getName());
if (objectName != null) dbStat.setString(index++, objectName);
if (object != null || objectName != null) dbStat.setString(index++, object != null ? object.getName() : objectName);
dbStat.setString(index++, owner.getName());
if (objectName != null) dbStat.setString(index, objectName);
if (object != null || objectName != null) dbStat.setString(index, object != null ? object.getName() : objectName);
return dbStat;
}
......
......@@ -31,6 +31,7 @@ import org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCCompositeCache;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCObjectCache;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCObjectLookup;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCStructCache;
import org.jkiss.dbeaver.model.meta.Association;
import org.jkiss.dbeaver.model.meta.Property;
......@@ -710,7 +711,7 @@ public class PostgreSchema implements DBSSchema, DBPNamedObject2, DBPSaveableObj
/**
* Procedures cache implementation
*/
class ProceduresCache extends JDBCObjectCache<PostgreSchema, PostgreProcedure> {
static class ProceduresCache extends JDBCObjectCache<PostgreSchema, PostgreProcedure> implements JDBCObjectLookup<PostgreSchema, PostgreProcedure> {
ProceduresCache()
{
......@@ -728,7 +729,7 @@ public class PostgreSchema implements DBSSchema, DBPNamedObject2, DBPSaveableObj
"WHERE p.pronamespace=?\n" +
"ORDER BY p.proname"
);
dbStat.setLong(1, getObjectId());
dbStat.setLong(1, owner.getObjectId());
return dbStat;
}
......@@ -736,7 +737,25 @@ public class PostgreSchema implements DBSSchema, DBPNamedObject2, DBPSaveableObj
protected PostgreProcedure fetchObject(@NotNull JDBCSession session, @NotNull PostgreSchema owner, @NotNull JDBCResultSet dbResult)
throws SQLException, DBException
{
return new PostgreProcedure(PostgreSchema.this, dbResult);
return new PostgreProcedure(owner, dbResult);
}
@NotNull
@Override
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull PostgreSchema owner, @Nullable PostgreProcedure object, @Nullable String objectName) throws SQLException {
JDBCPreparedStatement dbStat = session.prepareStatement(
"SELECT p.oid,p.*,d.description\n" +
"FROM pg_catalog.pg_proc p\n" +
"LEFT OUTER JOIN pg_catalog.pg_description d ON d.objoid=p.oid\n" +
"WHERE p.pronamespace=?" +
(object == null ? "" : " AND p.oid=?") +
"\nORDER BY p.proname"
);
dbStat.setLong(1, owner.getObjectId());
if (object != null) {
dbStat.setLong(2, object.getObjectId());
}
return dbStat;
}
}
......
......@@ -89,7 +89,7 @@ public abstract class JDBCObjectCache<OWNER extends DBSObject, OBJECT extends DB
if (!isCached()) {
this.loadObjects(monitor, owner);
} else if (this instanceof JDBCObjectLookup) {
OBJECT newObject = this.reloadObject(monitor, owner, (JDBCObjectLookup<OWNER>) this, objectName);
OBJECT newObject = this.reloadObject(monitor, owner, oldObject);
if (this instanceof JDBCStructCache) {
JDBCStructCache structCache = (JDBCStructCache) this;
if (structCache.isChildrenCached(oldObject)) {
......@@ -170,15 +170,17 @@ public abstract class JDBCObjectCache<OWNER extends DBSObject, OBJECT extends DB
}
}
protected OBJECT reloadObject(DBRProgressMonitor monitor, OWNER owner, JDBCObjectLookup<OWNER> objectLookup, String objectName)
protected OBJECT reloadObject(DBRProgressMonitor monitor, OWNER owner, OBJECT object)
throws DBException
{
@SuppressWarnings("unchecked")
JDBCObjectLookup<OWNER, OBJECT> objectLookup = (JDBCObjectLookup<OWNER, OBJECT>) this;
DBPDataSource dataSource = owner.getDataSource();
if (dataSource == null) {
throw new DBException("Not connected to database");
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Reload object '" + objectName + "' from " + owner.getName())) {
try (JDBCStatement dbStat = objectLookup.prepareLookupStatement(session, owner, objectName)) {
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Reload object '" + object.getName() + "' from " + owner.getName())) {
try (JDBCStatement dbStat = objectLookup.prepareLookupStatement(session, owner, object, null)) {
dbStat.setFetchSize(1);
dbStat.executeStatement();
JDBCResultSet dbResult = dbStat.getResultSet();
......
......@@ -28,10 +28,15 @@ import java.sql.SQLException;
/**
* Extension of {@link JDBCObjectCache} - support object lookup by name
*/
public interface JDBCObjectLookup<OWNER extends DBSObject>
public interface JDBCObjectLookup<OWNER extends DBSObject, OBJECT extends DBSObject>
{
JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull OWNER owner, @Nullable String objectName)
/**
* Creates statement to read just one object.
* Parameter @object OR @objectName may be specified to find an object
* @throws SQLException
*/
@NotNull
JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull OWNER owner, @Nullable OBJECT object, @Nullable String objectName)
throws SQLException;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册