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 
23 
24 package connectivity.tools;
25 
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.container.XNameAccess;
28 import com.sun.star.sdbc.ColumnValue;
29 import com.sun.star.sdbcx.XColumnsSupplier;
30 import com.sun.star.sdbcx.XDataDescriptorFactory;
31 import com.sun.star.sdbcx.XTablesSupplier;
32 import com.sun.star.uno.UnoRuntime;
33 import connectivity.tools.sdb.Connection;
34 
35 /** is a very simply descriptor of a HSQL table, to be used with a HsqlDatabase.createTable method
36  */
37 public class HsqlTableDescriptor
38 {
39     private String                  m_name;
40     private HsqlColumnDescriptor[]  m_columns;
41 
42     /** Creates a new instance of HsqlTableDescriptor */
HsqlTableDescriptor( String _name, HsqlColumnDescriptor[] _columns )43     public HsqlTableDescriptor( String _name, HsqlColumnDescriptor[] _columns )
44     {
45         m_name = _name;
46         m_columns = _columns;
47     }
48 
49     /** returns the name of the table
50      */
getName()51     public String getName()
52     {
53         return m_name;
54     }
55 
56     /** returns the set of column descriptors for the table
57      */
getColumns()58     public HsqlColumnDescriptor[] getColumns()
59     {
60         return m_columns;
61     }
62 
createSdbcxDescriptor( Connection _forConnection )63     public XPropertySet createSdbcxDescriptor( Connection _forConnection )
64     {
65         XTablesSupplier suppTables = UnoRuntime.queryInterface( XTablesSupplier.class, _forConnection.getXConnection() );
66         XDataDescriptorFactory tableDescFac = UnoRuntime.queryInterface( XDataDescriptorFactory.class, suppTables.getTables() );
67         XPropertySet tableDesc = tableDescFac.createDataDescriptor();
68 
69         try
70         {
71             tableDesc.setPropertyValue( "Name", getName() );
72         }
73         catch ( Exception e ) { e.printStackTrace( System.err ); }
74 
75         XColumnsSupplier suppDescCols = UnoRuntime.queryInterface( XColumnsSupplier.class, tableDesc );
76 
77         XNameAccess descColumns = suppDescCols.getColumns();
78         XDataDescriptorFactory columnDescFac = UnoRuntime.queryInterface( XDataDescriptorFactory.class, descColumns );
79 
80         HsqlColumnDescriptor[] myColumns = getColumns();
81         for ( int i = 0; i < myColumns.length; ++i )
82         {
83             XPropertySet columnDesc = columnDescFac.createDataDescriptor();
84             try
85             {
86                 columnDesc.setPropertyValue( "Name", myColumns[i].getName() );
87                 columnDesc.setPropertyValue( "IsNullable", new Integer( myColumns[i].isRequired() ? ColumnValue.NO_NULLS : ColumnValue.NULLABLE) );
88                 columnDesc.setPropertyValue( "TypeName", myColumns[i].getTypeName() );
89                 if ( myColumns[i].isPrimaryKey() || myColumns[i].isForeignKey() )
90                     // not yet implemented
91                     throw new java.lang.UnsupportedOperationException("creating a primary or foreign key via SDBCX not yet implemented" );
92             }
93             catch( com.sun.star.uno.Exception e ) { e.printStackTrace( System.err ); }
94         }
95 
96         return tableDesc;
97     }
98 }
99