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 org.apache.openoffice.comp.sdbc.dbtools.sdbcx; 23 24 import java.util.List; 25 26 import org.apache.openoffice.comp.sdbc.dbtools.sdbcx.descriptors.SdbcxKeyColumnDescriptor; 27 import org.apache.openoffice.comp.sdbc.dbtools.util.PropertyIds; 28 import org.apache.openoffice.comp.sdbc.dbtools.util.StandardSQLState; 29 30 import com.sun.star.beans.UnknownPropertyException; 31 import com.sun.star.beans.XPropertySet; 32 import com.sun.star.container.ElementExistException; 33 import com.sun.star.lang.IllegalArgumentException; 34 import com.sun.star.lang.WrappedTargetException; 35 import com.sun.star.sdbc.SQLException; 36 import com.sun.star.sdbc.XResultSet; 37 import com.sun.star.sdbc.XRow; 38 import com.sun.star.uno.AnyConverter; 39 import com.sun.star.uno.UnoRuntime; 40 41 public class OKeyColumnContainer extends OContainer { 42 private OKey key; 43 OKeyColumnContainer(Object lock, OKey key, List<String> columnNames)44 public OKeyColumnContainer(Object lock, OKey key, List<String> columnNames) throws ElementExistException { 45 super(lock, true, columnNames); 46 this.key = key; 47 } 48 49 @Override createDescriptor()50 protected XPropertySet createDescriptor() { 51 return new SdbcxKeyColumnDescriptor(isCaseSensitive()); 52 } 53 54 @Override createObject(String name)55 protected XPropertySet createObject(String name) throws SQLException { 56 try { 57 XPropertySet tableProperties = UnoRuntime.queryInterface(XPropertySet.class, key.getTable()); 58 Object catalog = tableProperties.getPropertyValue(PropertyIds.CATALOGNAME.name); 59 String schema = AnyConverter.toString(tableProperties.getPropertyValue(PropertyIds.SCHEMANAME.name)); 60 String table = AnyConverter.toString(tableProperties.getPropertyValue(PropertyIds.NAME.name)); 61 62 XResultSet results = key.getTable().getConnection().getMetaData().getImportedKeys(catalog, schema, table); 63 String refColumnName = ""; 64 if (results != null) { 65 XRow row = UnoRuntime.queryInterface(XRow.class, results); 66 while (results.next()) { 67 if (row.getString(8).equals(name) && key.getName().equals(row.getString(12))) { 68 refColumnName = row.getString(4); 69 break; 70 } 71 } 72 } 73 74 XPropertySet ret = null; 75 // now describe the column name and set its related column 76 results = key.getTable().getConnection().getMetaData().getColumns(catalog, schema, table, name); 77 if (results != null) { 78 XRow row = UnoRuntime.queryInterface(XRow.class, results); 79 if (results.next()) { 80 if (row.getString(4).equals(name)) { 81 int dataType = row.getInt(5); 82 String typeName = row.getString(6); 83 int size = row.getInt(7); 84 int dec = row.getInt(9); 85 int nul = row.getInt(11); 86 String columnDef = ""; 87 try { 88 columnDef = row.getString(13); 89 } catch (SQLException sqlException) { 90 // sometimes we get an error when asking for this param 91 } 92 ret = new OKeyColumn(refColumnName, name, typeName, 93 "", columnDef, nul, size, dec, dataType, false, false, false, isCaseSensitive()); 94 95 } 96 } 97 } 98 return ret; 99 } catch (WrappedTargetException | UnknownPropertyException | IllegalArgumentException exception) { 100 throw new SQLException("Error", this, StandardSQLState.SQL_GENERAL_ERROR.text(), 0, exception); 101 } 102 } 103 104 @Override impl_refresh()105 protected void impl_refresh() { 106 } 107 108 @Override appendObject(String _rForName, XPropertySet descriptor)109 protected XPropertySet appendObject(String _rForName, XPropertySet descriptor) throws SQLException { 110 throw new SQLException("Cannot change a key's columns, please delete and re-create the key instead"); 111 } 112 113 @Override dropObject(int index, String name)114 protected void dropObject(int index, String name) throws SQLException { 115 throw new SQLException("Cannot change a key's columns, please delete and re-create the key instead"); 116 } 117 } 118