1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.services.context.ContextManager; 25 26 import org.apache.derby.iapi.services.compiler.MethodBuilder; 27 28 import org.apache.derby.iapi.services.sanity.SanityManager; 29 30 import org.apache.derby.iapi.sql.compile.CompilerContext; 31 import org.apache.derby.iapi.sql.compile.Parser; 32 import org.apache.derby.iapi.sql.compile.C_NodeTypes; 33 34 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 35 36 import org.apache.derby.iapi.types.TypeId; 37 38 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 39 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 40 import org.apache.derby.iapi.sql.dictionary.DefaultDescriptor; 41 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 42 43 import org.apache.derby.iapi.error.StandardException; 44 45 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder; 46 47 import org.apache.derby.catalog.types.DefaultInfoImpl; 48 49 import java.util.Vector ; 50 51 54 public class DefaultNode extends ValueNode 55 { 56 private String columnName; 57 private String defaultText; 58 private ValueNode defaultTree; 59 60 66 public void init( 67 Object defaultTree, 68 Object defaultText) 69 { 70 this.defaultTree = (ValueNode) defaultTree; 71 this.defaultText = (String ) defaultText; 72 } 73 74 78 public void init(Object columnName) 79 { 80 this.columnName = (String ) columnName; 81 } 82 83 86 public String getDefaultText() 87 { 88 return defaultText; 89 } 90 91 96 ValueNode getDefaultTree() 97 { 98 return defaultTree; 99 } 100 101 107 108 public String toString() 109 { 110 if (SanityManager.DEBUG) 111 { 112 return "defaultTree: " + defaultTree + "\n" + 113 "defaultText: " + defaultText + "\n" + 114 super.toString(); 115 } 116 else 117 { 118 return ""; 119 } 120 } 121 122 137 public ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, 138 Vector aggregateVector) 139 throws StandardException 140 { 141 ColumnDescriptor cd; 142 TableDescriptor td; 143 144 if (SanityManager.DEBUG) 145 { 146 SanityManager.ASSERT(fromList.size() != 0, 147 "fromList expected to be non-empty"); 148 if (! (fromList.elementAt(0) instanceof FromBaseTable)) 149 { 150 SanityManager.THROWASSERT( 151 "fromList.elementAt(0) expected to be instanceof FromBaseTable, not " + 152 fromList.elementAt(0).getClass().getName()); 153 } 154 155 } 156 td = ((FromBaseTable) fromList.elementAt(0)).getTableDescriptor(); 158 159 cd = td.getColumnDescriptor(columnName); 161 if (SanityManager.DEBUG) 162 { 163 SanityManager.ASSERT(cd != null, 164 "cd expected to be non-null"); 165 } 166 167 170 DefaultInfoImpl defaultInfo = (DefaultInfoImpl) cd.getDefaultInfo(); 171 if (defaultInfo != null) 172 { 173 String defaultText = defaultInfo.getDefaultText(); 174 ValueNode defaultTree = parseDefault(defaultText, getLanguageConnectionContext(), 175 getCompilerContext()); 176 177 178 DefaultDescriptor defaultDescriptor = cd.getDefaultDescriptor( 179 getDataDictionary()); 180 getCompilerContext().createDependency(defaultDescriptor); 181 182 return defaultTree.bindExpression( 183 fromList, 184 subqueryList, 185 aggregateVector); 186 } 187 else 188 { 189 ValueNode nullNode = (ValueNode) getNodeFactory().getNode( 191 C_NodeTypes.UNTYPED_NULL_CONSTANT_NODE, 192 getContextManager()); 193 return nullNode; 194 } 195 } 196 197 208 public static ValueNode parseDefault 209 ( 210 String defaultText, 211 LanguageConnectionContext lcc, 212 CompilerContext cc 213 ) 214 throws StandardException 215 { 216 Parser p; 217 ValueNode defaultTree; 218 219 220 221 224 String values = "VALUES " + defaultText; 225 226 231 CompilerContext newCC = lcc.pushCompilerContext(); 232 233 p = newCC.getParser(); 234 235 236 237 QueryTreeNode qt = p.parseStatement(values); 240 if (SanityManager.DEBUG) 241 { 242 if (! (qt instanceof CursorNode)) 243 { 244 SanityManager.THROWASSERT( 245 "qt expected to be instanceof CursorNode, not " + 246 qt.getClass().getName()); 247 } 248 CursorNode cn = (CursorNode) qt; 249 if (! (cn.getResultSetNode() instanceof RowResultSetNode)) 250 { 251 SanityManager.THROWASSERT( 252 "cn.getResultSetNode() expected to be instanceof RowResultSetNode, not " + 253 cn.getResultSetNode().getClass().getName()); 254 } 255 } 256 257 defaultTree = ((ResultColumn) 258 ((CursorNode) qt).getResultSetNode().getResultColumns().elementAt(0)). 259 getExpression(); 260 261 lcc.popCompilerContext(newCC); 262 263 return defaultTree; 264 } 265 266 269 public void generateExpression(ExpressionClassBuilder acb, 270 MethodBuilder mb) 271 throws StandardException 272 { 273 if (SanityManager.DEBUG) 274 { 275 SanityManager.THROWASSERT( 276 "generateExpression not expected to be called"); 277 } 278 } 279 280 283 protected boolean isEquivalent(ValueNode other) 284 { 285 return false; 286 } 287 } 288 | Popular Tags |