connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlCatalog.java | 6 connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlTables.java | 15 connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlViews.java | 163 ++++++++++ connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/OContainer.java | 9 connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/OView.java | 131 ++++++++ connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/descriptors/SdbcxViewDescriptor.java | 125 +++++++ connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/util/PropertyIds.java | 8 7 files changed, 449 insertions(+), 8 deletions(-)
New commits: commit a4621f906e51b07b4052dc6eac3b1210408e19e8 Author: Damjan Jovanovic <dam...@apache.org> Date: Tue Oct 24 04:34:26 2017 +0000 Implement views in the PostgreSQL driver. Patch by: me diff --git a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlCatalog.java b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlCatalog.java index ad66cf6de697..1845ec849033 100644 --- a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlCatalog.java +++ b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlCatalog.java @@ -71,7 +71,7 @@ public class PostgresqlCatalog extends OCatalog { String name = buildName(row); names.add(name); } - return new PostgresqlTables(this, metadata, this, names); + return new PostgresqlViews(this, metadata, this, names); } catch (ElementExistException | SQLException exception) { throw new com.sun.star.uno.RuntimeException("Error", exception); } finally { @@ -88,4 +88,8 @@ public class PostgresqlCatalog extends OCatalog { public OContainer refreshUsers() { return null; } + + synchronized OContainer getTablesInternal() { + return tables; + } } diff --git a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlTables.java b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlTables.java index 885cca5cdc3b..f2cc5c88fd8f 100644 --- a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlTables.java +++ b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlTables.java @@ -26,6 +26,7 @@ import java.util.List; import com.sun.star.beans.UnknownPropertyException; import com.sun.star.beans.XPropertySet; import com.sun.star.container.ElementExistException; +import com.sun.star.container.NoSuchElementException; import com.sun.star.lang.IllegalArgumentException; import com.sun.star.lang.WrappedTargetException; import com.sun.star.sdbc.SQLException; @@ -33,6 +34,7 @@ import com.sun.star.sdbc.XDatabaseMetaData; import com.sun.star.sdbc.XResultSet; import com.sun.star.sdbc.XRow; import com.sun.star.sdbc.XStatement; +import com.sun.star.sdbcx.XDrop; import com.sun.star.sdbcx.comp.postgresql.comphelper.CompHelper; import com.sun.star.sdbcx.comp.postgresql.sdbcx.OContainer; import com.sun.star.sdbcx.comp.postgresql.sdbcx.descriptors.SdbcxTableDescriptor; @@ -95,7 +97,15 @@ public class PostgresqlTables extends OContainer { String composedName = DbTools.composeTableName(metadata, nameComponents.getCatalog(), nameComponents.getSchema(), nameComponents.getTable(), true, ComposeRule.InDataManipulation); - String sql = String.format("DROP %s %s", isView ? "VIEW" : "TABLE", composedName); + if (isView) { + XDrop dropView = UnoRuntime.queryInterface(XDrop.class, catalog.getViews()); + String unquotedName = DbTools.composeTableName(metadata, nameComponents.getCatalog(), nameComponents.getSchema(), nameComponents.getTable(), + false, ComposeRule.InDataManipulation); + dropView.dropByName(unquotedName); + return; + } + + String sql = "DROP TABLE " + composedName; XStatement statement = null; try { @@ -104,8 +114,7 @@ public class PostgresqlTables extends OContainer { } finally { CompHelper.disposeComponent(statement); } - // FIXME: delete it from our views - } catch (IllegalArgumentException | UnknownPropertyException | WrappedTargetException wrappedTargetException) { + } catch (IllegalArgumentException | UnknownPropertyException | WrappedTargetException | NoSuchElementException wrappedTargetException) { throw new SQLException("Error", this, StandardSQLState.SQL_GENERAL_ERROR.text(), 0, wrappedTargetException); } } diff --git a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlViews.java b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlViews.java new file mode 100644 index 000000000000..767536e89cec --- /dev/null +++ b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/PostgresqlViews.java @@ -0,0 +1,163 @@ +/************************************************************** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.sun.star.sdbcx.comp.postgresql; + +import java.util.List; + +import com.sun.star.beans.UnknownPropertyException; +import com.sun.star.beans.XPropertySet; +import com.sun.star.container.ElementExistException; +import com.sun.star.lang.WrappedTargetException; +import com.sun.star.sdbc.SQLException; +import com.sun.star.sdbc.XDatabaseMetaData; +import com.sun.star.sdbc.XParameters; +import com.sun.star.sdbc.XPreparedStatement; +import com.sun.star.sdbc.XResultSet; +import com.sun.star.sdbc.XRow; +import com.sun.star.sdbc.XStatement; +import com.sun.star.sdbcx.CheckOption; +import com.sun.star.sdbcx.comp.postgresql.comphelper.CompHelper; +import com.sun.star.sdbcx.comp.postgresql.sdbcx.OContainer; +import com.sun.star.sdbcx.comp.postgresql.sdbcx.OView; +import com.sun.star.sdbcx.comp.postgresql.sdbcx.descriptors.SdbcxViewDescriptor; +import com.sun.star.sdbcx.comp.postgresql.util.ComposeRule; +import com.sun.star.sdbcx.comp.postgresql.util.DbTools; +import com.sun.star.sdbcx.comp.postgresql.util.DbTools.NameComponents; +import com.sun.star.sdbcx.comp.postgresql.util.Osl; +import com.sun.star.sdbcx.comp.postgresql.util.PropertyIds; +import com.sun.star.sdbcx.comp.postgresql.util.StandardSQLState; +import com.sun.star.uno.UnoRuntime; + +public class PostgresqlViews extends OContainer { + private PostgresqlCatalog catalog; + private XDatabaseMetaData metadata; + + public PostgresqlViews(Object lock, XDatabaseMetaData metadata, PostgresqlCatalog catalog, List<String> names) throws ElementExistException { + super(lock, true, names); + this.metadata = metadata; + this.catalog = catalog; + } + + @Override + protected XPropertySet createObject(String name) throws SQLException { + NameComponents nameComponents = DbTools.qualifiedNameComponents(metadata, name, ComposeRule.InDataManipulation); + + String sql = "SELECT view_definition,check_option FROM INFORMATION_SCHEMA.views WHERE "; + if (!nameComponents.getCatalog().isEmpty()) { + sql += "table_catalog=? AND "; + } + if (!nameComponents.getSchema().isEmpty()) { + sql += "table_schema=? AND "; + } + sql += "table_name=?"; + + final String command; + final String checkOption; + XPreparedStatement statement = null; + XResultSet results = null; + try { + statement = metadata.getConnection().prepareStatement(sql); + XParameters parameters = UnoRuntime.queryInterface(XParameters.class, statement); + int next = 1; + if (!nameComponents.getCatalog().isEmpty()) { + parameters.setString(next++, nameComponents.getCatalog()); + } + if (!nameComponents.getSchema().isEmpty()) { + parameters.setString(next++, nameComponents.getSchema()); + } + parameters.setString(next, nameComponents.getTable()); + results = statement.executeQuery(); + if (results.next()) { + XRow row = UnoRuntime.queryInterface(XRow.class, results); + command = row.getString(1); + checkOption = row.getString(2); + } else { + throw new SQLException("View not found", this, StandardSQLState.SQL_TABLE_OR_VIEW_NOT_FOUND.text(), 0, null); + } + } finally { + CompHelper.disposeComponent(results); + CompHelper.disposeComponent(statement); + } + + final int checkOptionInt; + if (checkOption.equals("NONE")) { + checkOptionInt = CheckOption.NONE; + } else if (checkOption.equals("LOCAL")) { + checkOptionInt = CheckOption.LOCAL; + } else if (checkOption.equals("CASCADED")) { + checkOptionInt = CheckOption.CASCADE; + } else { + throw new SQLException("Unsupported check option '" + checkOption + "'", this, + StandardSQLState.SQL_FEATURE_NOT_IMPLEMENTED.text(), 0, null); + } + + return new OView(nameComponents.getCatalog(), nameComponents.getSchema(), nameComponents.getTable(), isCaseSensitive(), + command, checkOptionInt); + } + + @Override + protected void dropObject(int index, String name) throws SQLException { + XStatement statement = null; + try { + Object object = getObject(index); + XPropertySet propertySet = UnoRuntime.queryInterface(XPropertySet.class, object); + Osl.ensure(propertySet != null, "Object returned from view collection isn't an XPropertySet"); + String sql = String.format("DROP VIEW %s", DbTools.composeTableName(metadata, propertySet, ComposeRule.InTableDefinitions, + false, false, true)); + + statement = metadata.getConnection().createStatement(); + statement.execute(sql); + } catch (WrappedTargetException exception) { + throw new SQLException("Error", this, StandardSQLState.SQL_GENERAL_ERROR.text(), 0, exception); + } finally { + CompHelper.disposeComponent(statement); + } + } + + @Override + protected XPropertySet createDescriptor() { + return new SdbcxViewDescriptor(isCaseSensitive()); + } + + @Override + protected XPropertySet appendObject(String _rForName, XPropertySet descriptor) throws SQLException { + XStatement statement = null; + try { + String sql = String.format("CREATE VIEW %s AS %s", + DbTools.composeTableName(metadata, descriptor, ComposeRule.InTableDefinitions, false, false, true), + descriptor.getPropertyValue(PropertyIds.COMMAND.name)); + statement = metadata.getConnection().createStatement(); + statement.execute(sql); + } catch (WrappedTargetException | UnknownPropertyException exception) { + throw new SQLException("Error", this, StandardSQLState.SQL_GENERAL_ERROR.text(), 0, exception); + } finally { + CompHelper.disposeComponent(statement); + } + // Append it to the tables container too: + catalog.getTablesInternal().insertElement(_rForName, null); + return createObject(_rForName); + } + + @Override + protected void impl_refresh() { + catalog.refreshObjects(); + } +} diff --git a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/OContainer.java b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/OContainer.java index 69c18bf885c3..3be4e5fe9b3f 100644 --- a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/OContainer.java +++ b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/OContainer.java @@ -280,6 +280,15 @@ public abstract class OContainer extends WeakBase implements } + public void insertElement(String name, XPropertySet element) { + synchronized (lock) { + if (!entriesByName.containsKey(name)) { + entriesByName.put(name, element); + namesByIndex.add(name); + } + } + } + // XDrop @Override diff --git a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/OView.java b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/OView.java new file mode 100644 index 000000000000..afe52aac4fe8 --- /dev/null +++ b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/OView.java @@ -0,0 +1,131 @@ +/************************************************************** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.sun.star.sdbcx.comp.postgresql.sdbcx; + +import com.sun.star.beans.PropertyAttribute; +import com.sun.star.container.XNamed; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.sdbcx.comp.postgresql.comphelper.PropertySetAdapter.PropertyGetter; +import com.sun.star.sdbcx.comp.postgresql.comphelper.PropertySetAdapter.PropertySetter; +import com.sun.star.sdbcx.comp.postgresql.util.PropertyIds; +import com.sun.star.uno.Type; + +public class OView extends ODescriptor + implements XNamed, XServiceInfo { + + private static String[] services = { + "com.sun.star.sdbcx.View" + }; + + protected String catalogName; + protected String schemaName; + protected String command; + protected int checkOption; + + public OView(String catalog, String schema, String name, boolean isCaseSensitive, String command, int checkOption) { + super(name, isCaseSensitive, true); + this.catalogName = catalog; + this.schemaName = schema; + this.command = command; + this.checkOption = checkOption; + registerProperties(); + } + + private void registerProperties() { + registerProperty(PropertyIds.CATALOGNAME.name, PropertyIds.CATALOGNAME.id, Type.STRING, PropertyAttribute.READONLY, + new PropertyGetter() { + @Override + public Object getValue() { + return catalogName; + + } + }, + new PropertySetter() { + @Override + public void setValue(Object value) { + catalogName = (String) value; + } + }); + registerProperty(PropertyIds.SCHEMANAME.name, PropertyIds.SCHEMANAME.id, Type.STRING, PropertyAttribute.READONLY, + new PropertyGetter() { + @Override + public Object getValue() { + return schemaName; + + } + }, + new PropertySetter() { + @Override + public void setValue(Object value) { + schemaName = (String) value; + } + }); + registerProperty(PropertyIds.COMMAND.name, PropertyIds.COMMAND.id, Type.STRING, PropertyAttribute.READONLY, + new PropertyGetter() { + @Override + public Object getValue() { + return command; + + } + }, + new PropertySetter() { + @Override + public void setValue(Object value) { + command = (String) value; + } + }); + registerProperty(PropertyIds.CHECKOPTION.name, PropertyIds.CHECKOPTION.id, Type.LONG, PropertyAttribute.READONLY, + new PropertyGetter() { + @Override + public Object getValue() { + return checkOption; + + } + }, + new PropertySetter() { + @Override + public void setValue(Object value) { + checkOption = (int) value; + } + }); + } + + // XServiceInfo + + public String getImplementationName() { + return getClass().getName(); + } + + @Override + public String[] getSupportedServiceNames() { + return services.clone(); + } + + @Override + public boolean supportsService(String serviceName) { + for (String service : getSupportedServiceNames()) { + if (service.equals(serviceName)) { + return true; + } + } + return false; + } +} diff --git a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/descriptors/SdbcxViewDescriptor.java b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/descriptors/SdbcxViewDescriptor.java new file mode 100644 index 000000000000..51b64ccdf07f --- /dev/null +++ b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/sdbcx/descriptors/SdbcxViewDescriptor.java @@ -0,0 +1,125 @@ +/************************************************************** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.sun.star.sdbcx.comp.postgresql.sdbcx.descriptors; + +import com.sun.star.lang.XServiceInfo; +import com.sun.star.sdbcx.comp.postgresql.comphelper.PropertySetAdapter.PropertyGetter; +import com.sun.star.sdbcx.comp.postgresql.comphelper.PropertySetAdapter.PropertySetter; +import com.sun.star.sdbcx.comp.postgresql.sdbcx.ODescriptor; +import com.sun.star.sdbcx.comp.postgresql.util.PropertyIds; +import com.sun.star.uno.Type; + +public class SdbcxViewDescriptor extends ODescriptor implements XServiceInfo { + + private static final String[] services = { + "com.sun.star.sdbcx.ViewDescriptor" + }; + + protected String catalogName; + protected String schemaName; + protected String command; + protected int checkOption; + + public SdbcxViewDescriptor(boolean isCaseSensitive) { + super("", isCaseSensitive, false); + registerProperties(); + } + + private void registerProperties() { + registerProperty(PropertyIds.CATALOGNAME.name, PropertyIds.CATALOGNAME.id, Type.STRING, (short)0, + new PropertyGetter() { + @Override + public Object getValue() { + return catalogName; + + } + }, + new PropertySetter() { + @Override + public void setValue(Object value) { + catalogName = (String) value; + } + }); + registerProperty(PropertyIds.SCHEMANAME.name, PropertyIds.SCHEMANAME.id, Type.STRING, (short)0, + new PropertyGetter() { + @Override + public Object getValue() { + return schemaName; + + } + }, + new PropertySetter() { + @Override + public void setValue(Object value) { + schemaName = (String) value; + } + }); + registerProperty(PropertyIds.COMMAND.name, PropertyIds.COMMAND.id, Type.STRING, (short)0, + new PropertyGetter() { + @Override + public Object getValue() { + return command; + + } + }, + new PropertySetter() { + @Override + public void setValue(Object value) { + command = (String) value; + } + }); + registerProperty(PropertyIds.CHECKOPTION.name, PropertyIds.CHECKOPTION.id, Type.LONG, (short)0, + new PropertyGetter() { + @Override + public Object getValue() { + return checkOption; + + } + }, + new PropertySetter() { + @Override + public void setValue(Object value) { + checkOption = (int) value; + } + }); + } + + // XServiceInfo + + public String getImplementationName() { + return getClass().getName(); + } + + @Override + public String[] getSupportedServiceNames() { + return services.clone(); + } + + @Override + public boolean supportsService(String serviceName) { + for (String service : getSupportedServiceNames()) { + if (service.equals(serviceName)) { + return true; + } + } + return false; + } +} commit 5a9482b784d1cdbf2a0c6b7331a86881320e5a95 Author: Damjan Jovanovic <dam...@apache.org> Date: Tue Oct 24 04:29:04 2017 +0000 Fix a serious bug in naming PropertyIds variables of the PostgreSQL database driver. Patch by: me diff --git a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/util/PropertyIds.java b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/util/PropertyIds.java index 0e59e0d9b8d4..0a096e36c6a6 100644 --- a/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/util/PropertyIds.java +++ b/connectivity/java/sdbc_postgresql/src/com/sun/star/sdbcx/comp/postgresql/util/PropertyIds.java @@ -53,10 +53,10 @@ public enum PropertyIds { ISASCENDING (28, "IsAscending"), SCHEMANAME (29, "SchemaName"), CATALOGNAME (30, "CatalogName"), - COMMAND (31, "CheckOption"), - CHECKOPTION (32, "Password"), - PASSWORD (33, "RelatedColumn"), - RELATEDCOLUMN (34, ""), + COMMAND (31, "Command"), + CHECKOPTION (32, "CheckOption"), + PASSWORD (33, "Password"), + RELATEDCOLUMN (34, "RelatedColumn"), FUNCTION (35, "Function"), TABLENAME (36, "TableName"), REALNAME (37, "RealName"), _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits