KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > CreateViewConstantAction


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.CreateViewConstantAction
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.impl.sql.execute;
23
24 import org.apache.derby.iapi.sql.execute.ConstantAction;
25
26 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
27 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
28 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
29 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList;
30 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
31 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
32 import org.apache.derby.iapi.sql.dictionary.ViewDescriptor;
33 import org.apache.derby.iapi.sql.depend.DependencyManager;
34 import org.apache.derby.iapi.store.access.TransactionController;
35
36 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
37
38 import org.apache.derby.iapi.sql.depend.Provider;
39 import org.apache.derby.iapi.sql.depend.ProviderInfo;
40
41 import org.apache.derby.iapi.reference.SQLState;
42
43 import org.apache.derby.iapi.sql.Activation;
44
45 import org.apache.derby.iapi.error.StandardException;
46
47 import org.apache.derby.iapi.services.sanity.SanityManager;
48
49 import org.apache.derby.catalog.UUID;
50
51 /**
52  * This class describes actions that are ALWAYS performed for a
53  * CREATE VIEW Statement at Execution time.
54  *
55  * @author Jerry Brenner.
56  */

57
58 class CreateViewConstantAction extends DDLConstantAction
59 {
60     
61     private final String JavaDoc tableName;
62     private final String JavaDoc schemaName;
63     private final String JavaDoc viewText;
64     private final int tableType;
65     private final int checkOption;
66     private final ColumnInfo[] columnInfo;
67     private final ProviderInfo[] providerInfo;
68     private final UUID compSchemaId;
69     
70     // CONSTRUCTORS
71
/**
72      * Make the ConstantAction for a CREATE VIEW statement.
73      *
74      * @param schemaName name for the schema that view lives in.
75      * @param tableName Name of table.
76      * @param tableType Type of table (e.g., BASE).
77      * @param viewText Text of query expression for view definition
78      * @param checkOption Check option type
79      * @param columnInfo Information on all the columns in the table.
80      * @param providerInfo Information on all the Providers
81      * @param compSchemaId Compilation Schema Id
82      * (REMIND tableDescriptor ignored)
83      */

84     CreateViewConstantAction(
85                                 String JavaDoc schemaName,
86                                 String JavaDoc tableName,
87                                 int tableType,
88                                 String JavaDoc viewText,
89                                 int checkOption,
90                                 ColumnInfo[] columnInfo,
91                                 ProviderInfo[] providerInfo,
92                                 UUID compSchemaId)
93     {
94         this.schemaName = schemaName;
95         this.tableName = tableName;
96         this.tableType = tableType;
97         this.viewText = viewText;
98         this.checkOption = checkOption;
99         this.columnInfo = columnInfo;
100         this.providerInfo = providerInfo;
101         this.compSchemaId = compSchemaId;
102
103         if (SanityManager.DEBUG)
104         {
105             SanityManager.ASSERT(schemaName != null, "Schema name is null");
106         }
107     }
108
109     // OBJECT METHODS
110

111     public String JavaDoc toString()
112     {
113         return constructToString("CREATE VIEW ", tableName);
114     }
115
116     // INTERFACE METHODS
117

118
119     /**
120      * This is the guts of the Execution-time logic for CREATE TABLE.
121      *
122      * @see ConstantAction#executeConstantAction
123      *
124      * @exception StandardException Thrown on failure
125      */

126     public void executeConstantAction( Activation activation )
127                         throws StandardException
128     {
129         TableDescriptor td;
130         UUID toid;
131         SchemaDescriptor schemaDescriptor;
132         ColumnDescriptor columnDescriptor;
133         ViewDescriptor vd;
134
135         LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
136         DataDictionary dd = lcc.getDataDictionary();
137         DependencyManager dm = dd.getDependencyManager();
138         TransactionController tc = lcc.getTransactionExecute();
139
140         /*
141         ** Inform the data dictionary that we are about to write to it.
142         ** There are several calls to data dictionary "get" methods here
143         ** that might be done in "read" mode in the data dictionary, but
144         ** it seemed safer to do this whole operation in "write" mode.
145         **
146         ** We tell the data dictionary we're done writing at the end of
147         ** the transaction.
148         */

149         dd.startWriting(lcc);
150
151         SchemaDescriptor sd = DDLConstantAction.getSchemaDescriptorForCreate(dd, activation, schemaName);
152
153         /* Create a new table descriptor.
154          * (Pass in row locking, even though meaningless for views.)
155          */

156         DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
157         td = ddg.newTableDescriptor(tableName,
158                                     sd,
159                                     tableType,
160                                     TableDescriptor.ROW_LOCK_GRANULARITY);
161
162         dd.addDescriptor(td, sd, DataDictionary.SYSTABLES_CATALOG_NUM, false, tc);
163         toid = td.getUUID();
164
165         // for each column, stuff system.column
166
ColumnDescriptor[] cdlArray = new ColumnDescriptor[columnInfo.length];
167         int index = 1;
168         for (int ix = 0; ix < columnInfo.length; ix++)
169         {
170             columnDescriptor = new ColumnDescriptor(
171                                    columnInfo[ix].name,
172                                    index++,
173                                    columnInfo[ix].dataType,
174                                    columnInfo[ix].defaultValue,
175                                    columnInfo[ix].defaultInfo,
176                                    td,
177                                    (UUID) null,
178                                    columnInfo[ix].autoincStart,
179                                    columnInfo[ix].autoincInc
180                                );
181             cdlArray[ix] = columnDescriptor;
182         }
183
184         dd.addDescriptorArray(cdlArray, td,
185                               DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
186
187         // add columns to the column descriptor list.
188
ColumnDescriptorList cdl = td.getColumnDescriptorList();
189         for (int i = 0; i < cdlArray.length; i++)
190             cdl.add(cdlArray[i]);
191
192         /* Get and add a view descriptor */
193         vd = ddg.newViewDescriptor(toid, tableName, viewText,
194                                     checkOption,
195                                     (compSchemaId == null) ?
196                                         lcc.getDefaultSchema().getUUID() :
197                                         compSchemaId);
198
199         for (int ix = 0; ix < providerInfo.length; ix++)
200         {
201             /* We should always be able to find the Provider */
202             try
203             {
204                 Provider provider = (Provider) providerInfo[ix].
205                                         getDependableFinder().
206                                             getDependable(
207                                                 providerInfo[ix].getObjectId());
208                 if (provider == null) //see beetle 4444
209
{
210                     throw StandardException.newException(SQLState.LANG_OBJECT_NOT_FOUND, "OBJECT", providerInfo[ix].getObjectId());
211                 }
212                 dm.addDependency(vd, provider, lcc.getContextManager());
213             }
214             catch(java.sql.SQLException JavaDoc te)
215             {
216                 // we should allow timeout to be thrown
217
throw StandardException.plainWrapException(te);
218             }
219         }
220         //store view's dependency on various privileges in the dependeny system
221
storeViewTriggerDependenciesOnPrivileges(activation, vd);
222
223         dd.addDescriptor(vd, sd, DataDictionary.SYSVIEWS_CATALOG_NUM, true, tc);
224     }
225 }
226
Popular Tags