1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.sql.compile.Visitable; 25 import org.apache.derby.iapi.sql.compile.Visitor; 26 27 import org.apache.derby.iapi.services.context.ContextManager; 28 29 import org.apache.derby.iapi.services.sanity.SanityManager; 30 31 import org.apache.derby.iapi.error.StandardException; 32 33 import org.apache.derby.iapi.sql.compile.CompilerContext; 34 import org.apache.derby.iapi.sql.compile.C_NodeTypes; 35 import org.apache.derby.iapi.sql.compile.NodeFactory; 36 37 import org.apache.derby.iapi.sql.conn.Authorizer; 38 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 39 40 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 41 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 42 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 43 44 import org.apache.derby.iapi.sql.depend.DependencyManager; 45 import org.apache.derby.iapi.sql.depend.Dependent; 46 import org.apache.derby.iapi.sql.depend.ProviderInfo; 47 import org.apache.derby.iapi.sql.depend.ProviderList; 48 49 import org.apache.derby.iapi.reference.SQLState; 50 import org.apache.derby.iapi.reference.Limits; 51 52 import org.apache.derby.iapi.sql.execute.ConstantAction; 53 54 import org.apache.derby.impl.sql.execute.ColumnInfo; 55 import org.apache.derby.catalog.UUID; 56 57 63 64 public class CreateViewNode extends DDLStatementNode 65 { 66 ResultColumnList resultColumns; 67 ResultSetNode queryExpression; 68 String qeText; 69 int checkOption; 70 ProviderInfo[] providerInfos; 71 ColumnInfo[] colInfos; 72 73 74 87 88 public void init(Object newObjectName, 89 Object resultColumns, 90 Object queryExpression, 91 Object checkOption, 92 Object qeText) 93 throws StandardException 94 { 95 initAndCheck(newObjectName); 96 this.resultColumns = (ResultColumnList) resultColumns; 97 this.queryExpression = (ResultSetNode) queryExpression; 98 this.checkOption = ((Integer ) checkOption).intValue(); 99 this.qeText = ((String ) qeText).trim(); 100 101 implicitCreateSchema = true; 102 } 103 104 110 111 public String toString() 112 { 113 if (SanityManager.DEBUG) 114 { 115 return super.toString() + 116 "checkOption: " + checkOption + "\n" + 117 "qeText: " + qeText + "\n"; 118 } 119 else 120 { 121 return ""; 122 } 123 } 124 125 public String statementToString() 126 { 127 return "CREATE VIEW"; 128 } 129 130 136 137 public void printSubNodes(int depth) 138 { 139 if (SanityManager.DEBUG) 140 { 141 super.printSubNodes(depth); 142 143 if (resultColumns != null) 144 { 145 printLabel(depth, "resultColumns: "); 146 resultColumns.treePrint(depth + 1); 147 } 148 149 printLabel(depth, "queryExpression: "); 150 queryExpression.treePrint(depth + 1); 151 } 152 } 153 154 156 public int getCheckOption() { return checkOption; } 157 158 public ProviderInfo[] getProviderInfo() { return providerInfos; } 159 160 public ColumnInfo[] getColumnInfo() { return colInfos; } 161 162 164 174 public QueryTreeNode bind() throws StandardException 175 { 176 CompilerContext cc = getCompilerContext(); 177 DataDictionary dataDictionary = getDataDictionary(); 178 ResultColumnList qeRCL; 179 String duplicateColName; 180 181 183 providerInfos = bindViewDefinition 184 ( dataDictionary, cc, getLanguageConnectionContext(), 185 getNodeFactory(), 186 queryExpression, 187 getContextManager() 188 ); 189 190 qeRCL = queryExpression.getResultColumns(); 191 192 196 if (resultColumns != null) 197 { 198 if (resultColumns.size() != qeRCL.size()) 199 { 200 throw StandardException.newException(SQLState.LANG_VIEW_DEFINITION_R_C_L_MISMATCH, 201 getFullName()); 202 } 203 qeRCL.copyResultColumnNames(resultColumns); 204 } 205 206 209 duplicateColName = qeRCL.verifyUniqueNames((resultColumns == null) ? true : false); 210 if (duplicateColName != null) 211 { 212 throw StandardException.newException(SQLState.LANG_DUPLICATE_COLUMN_NAME_CREATE_VIEW, duplicateColName); 213 } 214 215 216 if (queryExpression.getResultColumns().size() > Limits.DB2_MAX_COLUMNS_IN_VIEW) 217 { 218 throw StandardException.newException(SQLState.LANG_TOO_MANY_COLUMNS_IN_TABLE_OR_VIEW, 219 String.valueOf(queryExpression.getResultColumns().size()), 220 getRelativeName(), 221 String.valueOf(Limits.DB2_MAX_COLUMNS_IN_VIEW)); 222 } 223 224 colInfos = new ColumnInfo[queryExpression.getResultColumns().size()]; 226 genColumnInfos(colInfos); 227 228 return this; 229 } 230 231 241 242 private ProviderInfo[] bindViewDefinition( DataDictionary dataDictionary, 243 CompilerContext compilerContext, 244 LanguageConnectionContext lcc, 245 NodeFactory nodeFactory, 246 ResultSetNode queryExpr, 247 ContextManager cm) 248 throws StandardException 249 { 250 FromList fromList = (FromList) nodeFactory.getNode( 251 C_NodeTypes.FROM_LIST, 252 nodeFactory.doJoinOrderOptimization(), 253 cm); 254 255 ProviderList prevAPL = compilerContext.getCurrentAuxiliaryProviderList(); 256 ProviderList apl = new ProviderList(); 257 258 try { 259 compilerContext.setCurrentAuxiliaryProviderList(apl); 260 compilerContext.pushCurrentPrivType(Authorizer.SELECT_PRIV); 261 262 263 queryExpr = queryExpr.bindNonVTITables(dataDictionary, fromList); 264 queryExpr = queryExpr.bindVTITables(fromList); 265 266 267 queryExpr.bindExpressions(fromList); 268 269 if (queryExpr instanceof SelectNode) 271 { 272 if (queryExpr.referencesSessionSchema()) 274 throw StandardException.newException(SQLState.LANG_OPERATION_NOT_ALLOWED_ON_SESSION_SCHEMA_TABLES); 275 } 276 277 queryExpr.bindResultColumns(fromList); 279 } 280 finally 281 { 282 compilerContext.popCurrentPrivType(); 283 compilerContext.setCurrentAuxiliaryProviderList(prevAPL); 284 } 285 286 DependencyManager dm = dataDictionary.getDependencyManager(); 287 ProviderInfo[] providerInfos = dm.getPersistentProviderInfos(apl); 288 dm.clearColumnInfoInProviders(apl); 291 292 293 if (SanityManager.DEBUG) 294 { 295 SanityManager.ASSERT(fromList.size() == 0, 296 "fromList.size() is expected to be 0, not " + fromList.size() + 297 " on return from RS.bindExpressions()"); 298 } 299 300 return providerInfos; 301 } 302 303 310 public boolean referencesSessionSchema() 311 throws StandardException 312 { 313 return (queryExpression.referencesSessionSchema()); 316 } 317 318 323 public ConstantAction makeConstantAction() throws StandardException 324 { 325 328 return getGenericConstantActionFactory().getCreateViewConstantAction(getSchemaDescriptor().getSchemaName(), 329 getRelativeName(), 330 TableDescriptor.VIEW_TYPE, 331 qeText, 332 checkOption, 333 colInfos, 334 providerInfos, 335 (UUID)null); } 338 339 344 private void genColumnInfos(ColumnInfo[] colInfos) 345 { 346 ResultColumnList rcl = queryExpression.getResultColumns(); 347 int rclSize = rcl.size(); 348 349 for (int index = 0; index < rclSize; index++) 350 { 351 ResultColumn rc = (ResultColumn) rcl.elementAt(index); 352 353 colInfos[index] = new ColumnInfo(rc.getName(), 355 rc.getType(), 356 null, 357 null, 358 null, 359 null, 360 ColumnInfo.CREATE, 361 0, 0, 0); 362 } 363 } 364 365 368 369 374 ResultSetNode getParsedQueryExpression() { return queryExpression; } 375 376 377 382 383 384 392 public Visitable accept(Visitor v) 393 throws StandardException 394 { 395 Visitable returnNode = v.visit(this); 396 397 if (v.skipChildren(this)) 398 { 399 return returnNode; 400 } 401 402 if (!v.stopTraversal()) 403 { 404 super.accept(v); 405 } 406 407 if (queryExpression != null && !v.stopTraversal()) 408 { 409 queryExpression = (ResultSetNode)queryExpression.accept(v); 410 } 411 412 return returnNode; 413 } 414 415 } 416 | Popular Tags |