1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.db.PropertyInfo |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | 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, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.db; |
22 | |
23 | import org.apache.derby.iapi.error.StandardException; |
24 | import org.apache.derby.iapi.error.PublicAPI; |
25 | |
26 | import org.apache.derby.iapi.sql.Activation; |
27 | import org.apache.derby.iapi.sql.conn.Authorizer; |
28 | import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; |
29 | import org.apache.derby.iapi.sql.conn.ConnectionUtil; |
30 | |
31 | import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; |
32 | import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext; |
33 | import org.apache.derby.iapi.sql.dictionary.DataDictionary; |
34 | import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; |
35 | import org.apache.derby.iapi.sql.dictionary.TableDescriptor; |
36 | |
37 | import org.apache.derby.iapi.store.access.ConglomerateController; |
38 | import org.apache.derby.iapi.store.access.TransactionController; |
39 | |
40 | import org.apache.derby.iapi.services.property.PropertyUtil; |
41 | import org.apache.derby.iapi.reference.SQLState; |
42 | |
43 | import java.util.Properties; |
44 | import java.sql.SQLException; |
45 | |
46 | /** |
47 | * PropertyInfo is a class with static methods that retrieve the properties |
48 | * associated with a table or index and set and retrieve properties associated |
49 | * with a database. |
50 | * |
51 | * <P> |
52 | This class can only be used within an SQL-J statement, a Java procedure or a server side Java method. |
53 | <p>This class can be accessed using the class alias <code> PROPERTYINFO </code> in SQL-J statements. |
54 | */ |
55 | public final class PropertyInfo |
56 | { |
57 | |
58 | /** |
59 | * Get the Properties associated with a given table. |
60 | * |
61 | * @param schemaName The name of the schema that the table is in. |
62 | * @param tableName The name of the table. |
63 | * |
64 | * @return Properties The Properties associated with the specified table. |
65 | * (An empty Properties is returned if the table does not exist.) |
66 | * @exception SQLException on error |
67 | */ |
68 | public static Properties getTableProperties(String schemaName, String tableName) |
69 | throws SQLException |
70 | { |
71 | return PropertyInfo.getConglomerateProperties( schemaName, tableName, false ); |
72 | } |
73 | |
74 | /** |
75 | * Get the Properties associated with a given index. |
76 | * |
77 | * @param schemaName The name of the schema that the index is in. |
78 | * @param indexName The name of the index. |
79 | * |
80 | * @return Properties The Properties associated with the specified index. |
81 | * (An empty Properties is returned if the index does not exist.) |
82 | * @exception SQLException on error |
83 | */ |
84 | public static Properties getIndexProperties(String schemaName, String indexName) |
85 | throws SQLException |
86 | { |
87 | return PropertyInfo.getConglomerateProperties( schemaName, indexName, true ); |
88 | } |
89 | |
90 | /** |
91 | Fetch the value of a property of the database on the current connection. |
92 | |
93 | @param key the property key |
94 | |
95 | @return the value of the property or null if the property is not set. |
96 | |
97 | @exception SQLException on error |
98 | */ |
99 | public static String getDatabaseProperty(String key) throws SQLException { |
100 | LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC(); |
101 | |
102 | try { |
103 | return PropertyUtil.getDatabaseProperty(lcc.getTransactionExecute(), key); |
104 | } catch (StandardException se) { |
105 | throw PublicAPI.wrapStandardException(se); |
106 | } |
107 | } |
108 | |
109 | /** |
110 | Set or delete the value of a property of the database on the current connection. |
111 | |
112 | @param key the property key |
113 | @param value the new value, if null the property is deleted. |
114 | |
115 | @exception SQLException on error |
116 | */ |
117 | public static void setDatabaseProperty(String key, String value) throws SQLException |
118 | { |
119 | LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC(); |
120 | |
121 | try { |
122 | Authorizer a = lcc.getAuthorizer(); |
123 | a.authorize((Activation) null, Authorizer.PROPERTY_WRITE_OP); |
124 | |
125 | // Get the current transaction controller |
126 | TransactionController tc = lcc.getTransactionExecute(); |
127 | |
128 | tc.setProperty(key, value, false); |
129 | } catch (StandardException se) { |
130 | throw PublicAPI.wrapStandardException(se); |
131 | } |
132 | } |
133 | |
134 | /** |
135 | Internal use only. |
136 | */ |
137 | private PropertyInfo() {} |
138 | |
139 | |
140 | ////////////////////////////////////////////////////////////////////////////// |
141 | // |
142 | // PRIVATE METHODS |
143 | // |
144 | ///////////////////////////////////////////////////////////////////////////// |
145 | |
146 | /** |
147 | * Get the Properties associated with a given conglomerate |
148 | * |
149 | * @param schemaName The name of the schema that the conglomerate is in. |
150 | * @param conglomerateName The name of the conglomerate. |
151 | * |
152 | * @return Properties The Properties associated with the specified conglomerate. |
153 | * (An empty Properties is returned if the conglomerate does not exist.) |
154 | * @exception SQLException on error |
155 | */ |
156 | private static Properties getConglomerateProperties( String schemaName, String conglomerateName, boolean isIndex ) |
157 | throws SQLException |
158 | { |
159 | long conglomerateNumber; |
160 | |
161 | // find the language context. |
162 | LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC(); |
163 | |
164 | // Get the current transaction controller |
165 | TransactionController tc = lcc.getTransactionExecute(); |
166 | |
167 | try { |
168 | |
169 | // find the DataDictionary |
170 | DataDictionary dd = lcc.getDataDictionary(); |
171 | |
172 | |
173 | // get the SchemaDescriptor |
174 | SchemaDescriptor sd = dd.getSchemaDescriptor(schemaName, tc, true); |
175 | if ( !isIndex) |
176 | { |
177 | // get the TableDescriptor for the table |
178 | TableDescriptor td = dd.getTableDescriptor(conglomerateName, sd); |
179 | |
180 | // Return an empty Properties if table does not exist or if it is for a view. |
181 | if ((td == null) || td.getTableType() == TableDescriptor.VIEW_TYPE) { return new Properties(); } |
182 | |
183 | conglomerateNumber = td.getHeapConglomerateId(); |
184 | } |
185 | else |
186 | { |
187 | // get the ConglomerateDescriptor for the index |
188 | ConglomerateDescriptor cd = dd.getConglomerateDescriptor(conglomerateName, sd, false); |
189 | |
190 | // Return an empty Properties if index does not exist |
191 | if (cd == null) { return new Properties(); } |
192 | |
193 | conglomerateNumber = cd.getConglomerateNumber(); |
194 | } |
195 | |
196 | ConglomerateController cc = tc.openConglomerate( |
197 | conglomerateNumber, |
198 | false, |
199 | 0, |
200 | TransactionController.MODE_RECORD, |
201 | TransactionController.ISOLATION_SERIALIZABLE); |
202 | |
203 | Properties properties = tc.getUserCreateConglomPropList(); |
204 | cc.getTableProperties( properties ); |
205 | |
206 | cc.close(); |
207 | return properties; |
208 | |
209 | } catch (StandardException se) { |
210 | throw PublicAPI.wrapStandardException(se); |
211 | } |
212 | |
213 | } |
214 | } |