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.comphelper.CompHelper;
27 import org.apache.openoffice.comp.sdbc.dbtools.sdbcx.descriptors.SdbcxIndexColumnDescriptor;
28 import org.apache.openoffice.comp.sdbc.dbtools.util.PropertyIds;
29 import org.apache.openoffice.comp.sdbc.dbtools.util.StandardSQLState;
30 
31 import com.sun.star.beans.UnknownPropertyException;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.container.ElementExistException;
34 import com.sun.star.lang.IllegalArgumentException;
35 import com.sun.star.lang.WrappedTargetException;
36 import com.sun.star.sdbc.SQLException;
37 import com.sun.star.sdbc.XResultSet;
38 import com.sun.star.sdbc.XRow;
39 import com.sun.star.uno.AnyConverter;
40 import com.sun.star.uno.UnoRuntime;
41 
42 public class OIndexColumnContainer extends OContainer {
43     private OIndex index;
44 
OIndexColumnContainer(Object lock, OIndex index, List<String> columnNames)45     public OIndexColumnContainer(Object lock, OIndex index, List<String> columnNames) throws ElementExistException {
46         super(lock, true, columnNames);
47         this.index = index;
48     }
49 
50     @Override
createDescriptor()51     protected XPropertySet createDescriptor() {
52         return new SdbcxIndexColumnDescriptor(isCaseSensitive());
53     }
54 
55     @Override
createObject(String name)56     protected XPropertySet createObject(String name) throws SQLException {
57         try {
58             Object catalog = index.getTable().getPropertyValue(PropertyIds.CATALOGNAME.name);
59             String schema = AnyConverter.toString(index.getTable().getPropertyValue(PropertyIds.SCHEMANAME.name));
60             String table = AnyConverter.toString(index.getTable().getPropertyValue(PropertyIds.NAME.name));
61 
62             boolean isAscending = true;
63             XResultSet results = null;
64             try {
65                 results = index.getTable().getConnection().getMetaData().getIndexInfo(catalog, schema, table, false, false);
66                 if (results != null) {
67                     XRow row = UnoRuntime.queryInterface(XRow.class, results);
68                     while (results.next()) {
69                         if (row.getString(9).equals(name)) {
70                             isAscending = !row.getString(10).equals("D");
71                         }
72                     }
73                 }
74             } finally {
75                 CompHelper.disposeComponent(results);
76             }
77 
78             XPropertySet ret = null;
79             results = null;
80             try {
81                 results = index.getTable().getConnection().getMetaData().getColumns(catalog, schema, table, name);
82                 if (results != null) {
83                     XRow row = UnoRuntime.queryInterface(XRow.class, results);
84                     while (results.next()) {
85                         if (row.getString(4).equals(name)) {
86                             int dataType = row.getInt(5);
87                             String typeName = row.getString(6);
88                             int size = row.getInt(7);
89                             int dec = row.getInt(9);
90                             int nul = row.getInt(11);
91                             String columnDef = row.getString(13);
92 
93                             ret = new OIndexColumn(isAscending, name, typeName, columnDef, "",
94                                     nul, size, dec, dataType, false, false, false, isCaseSensitive());
95                             break;
96                         }
97                     }
98                 }
99             } finally {
100                 CompHelper.disposeComponent(results);
101             }
102 
103             return ret;
104         } catch (WrappedTargetException | UnknownPropertyException | IllegalArgumentException exception) {
105             throw new SQLException("Error", this, StandardSQLState.SQL_GENERAL_ERROR.text(), 0, exception);
106         }
107     }
108 
109     @Override
impl_refresh()110     protected void impl_refresh() {
111         // FIXME
112     }
113 
114     @Override
appendObject(String _rForName, XPropertySet descriptor)115     protected XPropertySet appendObject(String _rForName, XPropertySet descriptor) throws SQLException {
116         throw new SQLException("Unsupported");
117     }
118 
119     @Override
dropObject(int index, String name)120     protected void dropObject(int index, String name) throws SQLException {
121         throw new SQLException("Unsupported");
122     }
123 }
124