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 com.sun.star.beans.UnknownPropertyException; 27 import com.sun.star.beans.XPropertySet; 28 import com.sun.star.container.ElementExistException; 29 import com.sun.star.container.NoSuchElementException; 30 import com.sun.star.lang.IllegalArgumentException; 31 import com.sun.star.lang.WrappedTargetException; 32 import com.sun.star.sdbc.SQLException; 33 import com.sun.star.sdbc.XDatabaseMetaData; 34 import com.sun.star.sdbc.XResultSet; 35 import com.sun.star.sdbc.XRow; 36 import com.sun.star.sdbc.XStatement; 37 import com.sun.star.sdbcx.XDrop; 38 import com.sun.star.sdbcx.comp.postgresql.comphelper.CompHelper; 39 import com.sun.star.sdbcx.comp.postgresql.sdbcx.OContainer; 40 import com.sun.star.sdbcx.comp.postgresql.sdbcx.descriptors.SdbcxTableDescriptor; 41 import com.sun.star.sdbcx.comp.postgresql.util.ComposeRule; 42 import com.sun.star.sdbcx.comp.postgresql.util.DbTools; 43 import com.sun.star.sdbcx.comp.postgresql.util.DbTools.NameComponents; 44 import com.sun.star.sdbcx.comp.postgresql.util.PropertyIds; 45 import com.sun.star.sdbcx.comp.postgresql.util.StandardSQLState; 46 import com.sun.star.uno.Any; 47 import com.sun.star.uno.AnyConverter; 48 import com.sun.star.uno.UnoRuntime; 49 50 public class PostgresqlTables extends OContainer { 51 private XDatabaseMetaData metadata; 52 private PostgresqlCatalog catalog; 53 54 public PostgresqlTables(Object lock, XDatabaseMetaData metadata, PostgresqlCatalog catalog, List<String> names) throws ElementExistException { 55 super(lock, true, names); 56 this.metadata = metadata; 57 this.catalog = catalog; 58 } 59 60 @Override 61 public XPropertySet createObject(String name) throws SQLException { 62 NameComponents nameComponents = DbTools.qualifiedNameComponents(metadata, name, ComposeRule.InDataManipulation); 63 Object queryCatalog = nameComponents.getCatalog().isEmpty() ? Any.VOID : nameComponents.getCatalog(); 64 XPropertySet ret = null; 65 XResultSet results = null; 66 try { 67 results = metadata.getTables( 68 queryCatalog, nameComponents.getSchema(), nameComponents.getTable(), new String[] { "VIEW", "TABLE", "%" }); 69 if (results != null) { 70 XRow row = UnoRuntime.queryInterface(XRow.class, results); 71 if (results.next()) { 72 String type = row.getString(4); 73 String remarks = row.getString(5); 74 ret = new PostgresqlTable(metadata.getConnection(), this, nameComponents.getTable(), 75 nameComponents.getCatalog(), nameComponents.getSchema(), remarks, type); 76 } 77 } 78 } finally { 79 CompHelper.disposeComponent(results); 80 } 81 return ret; 82 83 } 84 85 @Override 86 public void dropObject(int index, String name) throws SQLException { 87 try { 88 Object object = getObject(index); 89 90 NameComponents nameComponents = DbTools.qualifiedNameComponents(metadata, name, ComposeRule.InDataManipulation); 91 92 boolean isView = false; 93 XPropertySet propertySet = UnoRuntime.queryInterface(XPropertySet.class, object); 94 if (propertySet != null) { 95 isView = AnyConverter.toString(propertySet.getPropertyValue(PropertyIds.TYPE.name)).equals("VIEW"); 96 } 97 98 String composedName = DbTools.composeTableName(metadata, nameComponents.getCatalog(), nameComponents.getSchema(), nameComponents.getTable(), 99 true, ComposeRule.InDataManipulation); 100 if (isView) { 101 XDrop dropView = UnoRuntime.queryInterface(XDrop.class, catalog.getViews()); 102 String unquotedName = DbTools.composeTableName(metadata, nameComponents.getCatalog(), nameComponents.getSchema(), nameComponents.getTable(), 103 false, ComposeRule.InDataManipulation); 104 dropView.dropByName(unquotedName); 105 return; 106 } 107 108 String sql = "DROP TABLE " + composedName; 109 110 XStatement statement = null; 111 try { 112 statement = metadata.getConnection().createStatement(); 113 statement.execute(sql); 114 } finally { 115 CompHelper.disposeComponent(statement); 116 } 117 } catch (IllegalArgumentException | UnknownPropertyException | WrappedTargetException | NoSuchElementException wrappedTargetException) { 118 throw new SQLException("Error", this, StandardSQLState.SQL_GENERAL_ERROR.text(), 0, wrappedTargetException); 119 } 120 } 121 122 @Override 123 public void impl_refresh() { 124 catalog.refreshObjects(); 125 } 126 127 @Override 128 public XPropertySet createDescriptor() { 129 return new SdbcxTableDescriptor(true); 130 } 131 132 @Override 133 public XPropertySet appendObject(String name, XPropertySet descriptor) throws SQLException { 134 createTable(descriptor); 135 return createObject(name); 136 } 137 138 void createTable(XPropertySet descriptor) throws SQLException { 139 XStatement statement = null; 140 try { 141 String sql = DbTools.createSqlCreateTableStatement(descriptor, metadata.getConnection(), null, "(M,D)"); 142 statement = metadata.getConnection().createStatement(); 143 statement.execute(sql); 144 } finally { 145 CompHelper.disposeComponent(statement); 146 } 147 } 148 } 149