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 import java.util.Map;
26 
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.IndexOutOfBoundsException;
31 import com.sun.star.sdbc.SQLException;
32 import com.sun.star.sdbc.XConnection;
33 import com.sun.star.sdbcx.comp.postgresql.comphelper.CompHelper;
34 import com.sun.star.sdbcx.comp.postgresql.sdbcx.OColumnContainer;
35 import com.sun.star.sdbcx.comp.postgresql.sdbcx.OContainer;
36 import com.sun.star.sdbcx.comp.postgresql.sdbcx.OIndexContainer;
37 import com.sun.star.sdbcx.comp.postgresql.sdbcx.OKey;
38 import com.sun.star.sdbcx.comp.postgresql.sdbcx.OKeyContainer;
39 import com.sun.star.sdbcx.comp.postgresql.sdbcx.OTable;
40 import com.sun.star.sdbcx.comp.postgresql.sdbcx.SqlTableHelper;
41 import com.sun.star.sdbcx.comp.postgresql.sdbcx.SqlTableHelper.ColumnDescription;
42 import com.sun.star.sdbcx.comp.postgresql.sdbcx.descriptors.SdbcxTableDescriptor;
43 
44 public class PostgresqlTable extends OTable {
45     private PostgresqlTable(Object lock, XConnection connection, OContainer tables, String name,
46             String catalogName, String schemaName, String description, String type) {
47         super(lock, name, true, connection, tables);
48         super.catalogName = catalogName;
49         super.schemaName = schemaName;
50         super.description = description;
51         super.type = type;
52     }
53 
54     public static PostgresqlTable create(XConnection connection, OContainer tables, String name,
55             String catalogName, String schemaName, String description, String type) {
56         Object lock = new Object();
57         return new PostgresqlTable(lock, connection, tables, name, catalogName, schemaName, description, type);
58     }
59 
60     @Override
61     public XPropertySet createDataDescriptor() {
62         SdbcxTableDescriptor descriptor = SdbcxTableDescriptor.create(true);
63         synchronized (lock) {
64             CompHelper.copyProperties(this, descriptor);
65         }
66         return descriptor;
67     }
68 
69     @Override
70     public void setName(String name) {
71         // TODO Auto-generated method stub
72 
73     }
74 
75     @Override
76     public void rename(String name) throws SQLException, ElementExistException {
77         // TODO Auto-generated method stub
78 
79     }
80 
81     @Override
82     public void alterColumnByIndex(int index, XPropertySet descriptor) throws SQLException, IndexOutOfBoundsException {
83         // TODO Auto-generated method stub
84 
85     }
86 
87     @Override
88     public void alterColumnByName(String name, XPropertySet descriptor) throws SQLException, NoSuchElementException {
89         // TODO Auto-generated method stub
90 
91     }
92 
93     @Override
94     protected OContainer refreshColumns() {
95         try {
96             List<ColumnDescription> columns = new SqlTableHelper().readColumns(getConnection().getMetaData(), catalogName, schemaName, getName());
97             return new OColumnContainer(lock, isCaseSensitive(), columns, this, getConnection().getMetaData());
98         } catch (ElementExistException elementExistException) {
99             return null;
100         } catch (SQLException sqlException) {
101             return null;
102         }
103     }
104 
105     @Override
106     protected OContainer refreshIndexes() {
107         try {
108             List<String> indexes = new SqlTableHelper().readIndexes(getConnection().getMetaData(), catalogName, schemaName, getName(), this);
109             return new OIndexContainer(lock, indexes, isCaseSensitive(), this);
110         } catch (ElementExistException elementExistException) {
111             return null;
112         } catch (SQLException sqlException) {
113             return null;
114         }
115     }
116 
117     @Override
118     protected OContainer refreshKeys() {
119         try {
120             Map<String, OKey> keys = new SqlTableHelper().readKeys(
121                     getConnection().getMetaData(), catalogName, schemaName, getName(), isCaseSensitive(), this);
122             return OKeyContainer.create(isCaseSensitive(), keys, this);
123         } catch (ElementExistException elementExistException) {
124             return null;
125         } catch (SQLException sqlException) {
126             return null;
127         }
128     }
129 
130 
131 }
132