1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package com.sun.star.sdbcx.comp.postgresql; 23 24 import java.util.List; 25 26 import org.apache.openoffice.comp.sdbc.dbtools.comphelper.CompHelper; 27 import org.apache.openoffice.comp.sdbc.dbtools.sdbcx.OContainer; 28 import org.apache.openoffice.comp.sdbc.dbtools.sdbcx.descriptors.SdbcxTableDescriptor; 29 import org.apache.openoffice.comp.sdbc.dbtools.util.ComposeRule; 30 import org.apache.openoffice.comp.sdbc.dbtools.util.DbTools; 31 import org.apache.openoffice.comp.sdbc.dbtools.util.DbTools.NameComponents; 32 import org.apache.openoffice.comp.sdbc.dbtools.util.PropertyIds; 33 import org.apache.openoffice.comp.sdbc.dbtools.util.StandardSQLState; 34 35 import com.sun.star.beans.UnknownPropertyException; 36 import com.sun.star.beans.XPropertySet; 37 import com.sun.star.container.ElementExistException; 38 import com.sun.star.container.NoSuchElementException; 39 import com.sun.star.lang.IllegalArgumentException; 40 import com.sun.star.lang.WrappedTargetException; 41 import com.sun.star.sdbc.SQLException; 42 import com.sun.star.sdbc.XDatabaseMetaData; 43 import com.sun.star.sdbc.XResultSet; 44 import com.sun.star.sdbc.XRow; 45 import com.sun.star.sdbc.XStatement; 46 import com.sun.star.sdbcx.XDrop; 47 import com.sun.star.uno.Any; 48 import com.sun.star.uno.AnyConverter; 49 import com.sun.star.uno.UnoRuntime; 50 51 public class PostgresqlTables extends OContainer { 52 private XDatabaseMetaData metadata; 53 private PostgresqlCatalog catalog; 54 PostgresqlTables(Object lock, XDatabaseMetaData metadata, PostgresqlCatalog catalog, List<String> names)55 public PostgresqlTables(Object lock, XDatabaseMetaData metadata, PostgresqlCatalog catalog, List<String> names) throws ElementExistException { 56 super(lock, true, names); 57 this.metadata = metadata; 58 this.catalog = catalog; 59 } 60 61 @Override createObject(String name)62 public XPropertySet createObject(String name) throws SQLException { 63 NameComponents nameComponents = DbTools.qualifiedNameComponents(metadata, name, ComposeRule.InDataManipulation); 64 Object queryCatalog = nameComponents.getCatalog().isEmpty() ? Any.VOID : nameComponents.getCatalog(); 65 XPropertySet ret = null; 66 XResultSet results = null; 67 try { 68 results = metadata.getTables( 69 queryCatalog, nameComponents.getSchema(), nameComponents.getTable(), new String[] { "VIEW", "TABLE", "%" }); 70 if (results != null) { 71 XRow row = UnoRuntime.queryInterface(XRow.class, results); 72 if (results.next()) { 73 String type = row.getString(4); 74 String remarks = row.getString(5); 75 ret = new PostgresqlTable(metadata.getConnection(), this, nameComponents.getTable(), 76 nameComponents.getCatalog(), nameComponents.getSchema(), remarks, type); 77 } 78 } 79 } finally { 80 CompHelper.disposeComponent(results); 81 } 82 return ret; 83 84 } 85 86 @Override dropObject(int index, String name)87 public void dropObject(int index, String name) throws SQLException { 88 try { 89 Object object = getObject(index); 90 91 NameComponents nameComponents = DbTools.qualifiedNameComponents(metadata, name, ComposeRule.InDataManipulation); 92 93 boolean isView = false; 94 XPropertySet propertySet = UnoRuntime.queryInterface(XPropertySet.class, object); 95 if (propertySet != null) { 96 isView = AnyConverter.toString(propertySet.getPropertyValue(PropertyIds.TYPE.name)).equals("VIEW"); 97 } 98 99 String composedName = DbTools.composeTableName(metadata, nameComponents.getCatalog(), nameComponents.getSchema(), nameComponents.getTable(), 100 true, ComposeRule.InDataManipulation); 101 if (isView) { 102 XDrop dropView = UnoRuntime.queryInterface(XDrop.class, catalog.getViews()); 103 String unquotedName = DbTools.composeTableName(metadata, nameComponents.getCatalog(), nameComponents.getSchema(), nameComponents.getTable(), 104 false, ComposeRule.InDataManipulation); 105 dropView.dropByName(unquotedName); 106 return; 107 } 108 109 String sql = "DROP TABLE " + composedName; 110 111 XStatement statement = null; 112 try { 113 statement = metadata.getConnection().createStatement(); 114 statement.execute(sql); 115 } finally { 116 CompHelper.disposeComponent(statement); 117 } 118 } catch (IllegalArgumentException | UnknownPropertyException | WrappedTargetException | NoSuchElementException wrappedTargetException) { 119 throw new SQLException("Error", this, StandardSQLState.SQL_GENERAL_ERROR.text(), 0, wrappedTargetException); 120 } 121 } 122 123 @Override impl_refresh()124 public void impl_refresh() { 125 catalog.refreshObjects(); 126 } 127 128 @Override createDescriptor()129 public XPropertySet createDescriptor() { 130 return new SdbcxTableDescriptor(true); 131 } 132 133 @Override appendObject(String name, XPropertySet descriptor)134 public XPropertySet appendObject(String name, XPropertySet descriptor) throws SQLException { 135 createTable(descriptor); 136 return createObject(name); 137 } 138 createTable(XPropertySet descriptor)139 void createTable(XPropertySet descriptor) throws SQLException { 140 XStatement statement = null; 141 try { 142 String sql = DbTools.createSqlCreateTableStatement(descriptor, metadata.getConnection(), null, "(M,D)"); 143 statement = metadata.getConnection().createStatement(); 144 statement.execute(sql); 145 } finally { 146 CompHelper.disposeComponent(statement); 147 } 148 } 149 } 150