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