KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > db > PropertyInfo


1 /*
2
3    Derby - Class org.apache.derby.iapi.db.PropertyInfo
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.db;
23
24 import org.apache.derby.iapi.error.StandardException;
25 import org.apache.derby.iapi.error.PublicAPI;
26
27 import org.apache.derby.iapi.sql.Activation;
28 import org.apache.derby.iapi.sql.conn.Authorizer;
29 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
30 import org.apache.derby.iapi.sql.conn.ConnectionUtil;
31
32 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
33 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
34 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
35 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
36 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
37
38 import org.apache.derby.iapi.store.access.ConglomerateController;
39 import org.apache.derby.iapi.store.access.TransactionController;
40
41 import org.apache.derby.iapi.services.property.PropertyUtil;
42 import org.apache.derby.iapi.reference.SQLState;
43
44 import java.util.Properties JavaDoc;
45 import java.sql.SQLException JavaDoc;
46
47 /**
48   * PropertyInfo is a class with static methods that retrieve the properties
49   * associated with a table or index and set and retrieve properties associated
50   * with a database.
51   *
52   * <P>
53   This class can only be used within an SQL-J statement, a Java procedure or a server side Java method.
54   <p>This class can be accessed using the class alias <code> PROPERTYINFO </code> in SQL-J statements.
55   */

56 public final class PropertyInfo
57 {
58
59     /**
60      * Get the Properties associated with a given table.
61      *
62      * @param schemaName The name of the schema that the table is in.
63      * @param tableName The name of the table.
64      *
65      * @return Properties The Properties associated with the specified table.
66      * (An empty Properties is returned if the table does not exist.)
67      * @exception SQLException on error
68      */

69     public static Properties JavaDoc getTableProperties(String JavaDoc schemaName, String JavaDoc tableName)
70         throws SQLException JavaDoc
71     {
72         return PropertyInfo.getConglomerateProperties( schemaName, tableName, false );
73     }
74
75     /**
76      * Get the Properties associated with a given index.
77      *
78      * @param schemaName The name of the schema that the index is in.
79      * @param indexName The name of the index.
80      *
81      * @return Properties The Properties associated with the specified index.
82      * (An empty Properties is returned if the index does not exist.)
83      * @exception SQLException on error
84      */

85     public static Properties JavaDoc getIndexProperties(String JavaDoc schemaName, String JavaDoc indexName)
86         throws SQLException JavaDoc
87     {
88         return PropertyInfo.getConglomerateProperties( schemaName, indexName, true );
89     }
90
91     /**
92         Fetch the value of a property of the database on the current connection.
93
94         @param key the property key
95
96         @return the value of the property or null if the property is not set.
97
98         @exception SQLException on error
99     */

100     public static String JavaDoc getDatabaseProperty(String JavaDoc key) throws SQLException JavaDoc {
101         LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
102
103         try {
104             return PropertyUtil.getDatabaseProperty(lcc.getTransactionExecute(), key);
105         } catch (StandardException se) {
106             throw PublicAPI.wrapStandardException(se);
107         }
108     }
109
110     /**
111         Set or delete the value of a property of the database on the current connection.
112
113         @param key the property key
114         @param value the new value, if null the property is deleted.
115
116         @exception SQLException on error
117     */

118     public static void setDatabaseProperty(String JavaDoc key, String JavaDoc value) throws SQLException JavaDoc
119     {
120         LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
121
122         try {
123         Authorizer a = lcc.getAuthorizer();
124         a.authorize((Activation) null, Authorizer.PROPERTY_WRITE_OP);
125
126         // Get the current transaction controller
127
TransactionController tc = lcc.getTransactionExecute();
128
129         tc.setProperty(key, value, false);
130         } catch (StandardException se) {
131             throw PublicAPI.wrapStandardException(se);
132         }
133     }
134
135     /**
136       Internal use only.
137       */

138     private PropertyInfo() {}
139
140
141     //////////////////////////////////////////////////////////////////////////////
142
//
143
// PRIVATE METHODS
144
//
145
/////////////////////////////////////////////////////////////////////////////
146

147     /**
148      * Get the Properties associated with a given conglomerate
149      *
150      * @param schemaName The name of the schema that the conglomerate is in.
151      * @param conglomerateName The name of the conglomerate.
152      *
153      * @return Properties The Properties associated with the specified conglomerate.
154      * (An empty Properties is returned if the conglomerate does not exist.)
155      * @exception SQLException on error
156      */

157     private static Properties JavaDoc getConglomerateProperties( String JavaDoc schemaName, String JavaDoc conglomerateName, boolean isIndex )
158         throws SQLException JavaDoc
159     {
160         long conglomerateNumber;
161
162         // find the language context.
163
LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
164
165         // Get the current transaction controller
166
TransactionController tc = lcc.getTransactionExecute();
167
168         try {
169
170         // find the DataDictionary
171
DataDictionary dd = lcc.getDataDictionary();
172
173
174         // get the SchemaDescriptor
175
SchemaDescriptor sd = dd.getSchemaDescriptor(schemaName, tc, true);
176         if ( !isIndex)
177         {
178             // get the TableDescriptor for the table
179
TableDescriptor td = dd.getTableDescriptor(conglomerateName, sd);
180
181             // Return an empty Properties if table does not exist or if it is for a view.
182
if ((td == null) || td.getTableType() == TableDescriptor.VIEW_TYPE) { return new Properties JavaDoc(); }
183
184             conglomerateNumber = td.getHeapConglomerateId();
185         }
186         else
187         {
188             // get the ConglomerateDescriptor for the index
189
ConglomerateDescriptor cd = dd.getConglomerateDescriptor(conglomerateName, sd, false);
190
191             // Return an empty Properties if index does not exist
192
if (cd == null) { return new Properties JavaDoc(); }
193
194             conglomerateNumber = cd.getConglomerateNumber();
195         }
196
197         ConglomerateController cc = tc.openConglomerate(
198                 conglomerateNumber,
199                 false,
200                 0,
201                 TransactionController.MODE_RECORD,
202                 TransactionController.ISOLATION_SERIALIZABLE);
203
204         Properties JavaDoc properties = tc.getUserCreateConglomPropList();
205         cc.getTableProperties( properties );
206
207         cc.close();
208         return properties;
209
210         } catch (StandardException se) {
211             throw PublicAPI.wrapStandardException(se);
212         }
213
214     }
215 }
216
Popular Tags