1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.services.compiler.MethodBuilder; 25 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 import org.apache.derby.iapi.services.context.ContextManager; 28 29 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 30 31 import org.apache.derby.iapi.types.DataTypeDescriptor; 32 import org.apache.derby.iapi.types.DataValueDescriptor; 33 import org.apache.derby.iapi.types.StringDataValue; 34 import org.apache.derby.iapi.sql.ResultColumnDescriptor; 35 import org.apache.derby.iapi.types.DataTypeDescriptor; 36 import org.apache.derby.iapi.types.DataValueDescriptor; 37 import org.apache.derby.iapi.types.TypeId; 38 import org.apache.derby.iapi.services.io.StoredFormatIds; 39 import org.apache.derby.iapi.types.DataValueFactory; 40 41 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 42 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 43 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 44 45 import org.apache.derby.iapi.sql.compile.CompilerContext; 46 import org.apache.derby.iapi.sql.compile.RowOrdering; 47 import org.apache.derby.iapi.sql.compile.Visitable; 48 import org.apache.derby.iapi.sql.compile.Visitor; 49 import org.apache.derby.iapi.sql.compile.C_NodeTypes; 50 51 import org.apache.derby.impl.sql.compile.ActivationClassBuilder; 52 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder; 53 54 import org.apache.derby.iapi.store.access.Qualifier; 55 56 import org.apache.derby.iapi.error.StandardException; 57 import org.apache.derby.iapi.reference.SQLState; 58 import org.apache.derby.iapi.reference.ClassName; 59 60 import org.apache.derby.iapi.util.JBitSet; 61 import org.apache.derby.iapi.util.StringUtil; 62 63 import java.sql.Types ; 64 65 import java.util.Vector ; 66 67 77 78 public class ResultColumn extends ValueNode 79 implements ResultColumnDescriptor, Comparable 80 { 81 85 String name; 86 String exposedName; 87 String tableName; 88 String sourceTableName; 89 String sourceSchemaName; 91 ValueNode expression; 92 ColumnDescriptor columnDescriptor; 93 boolean isGenerated; 94 boolean isGeneratedForUnmatchedColumnInInsert; 95 boolean isGroupingColumn; 96 boolean isReferenced; 97 boolean isRedundant; 98 boolean isNameGenerated; 99 boolean updated; 100 boolean updatableByCursor; 101 private boolean defaultColumn; 102 103 boolean autoincrementGenerated; 106 107 boolean autoincrement; 110 111 112 private int resultSetNumber = -1; 113 ColumnReference reference; 115 116 private int virtualColumnId; 117 118 154 public void init(Object arg1, Object arg2) throws StandardException 155 { 156 if ((arg1 instanceof String ) || (arg1 == null)) 160 { 161 this.name = (String ) arg1; 162 this.exposedName = this.name; 163 this.expression = (ValueNode) arg2; 164 } 165 else if (arg1 instanceof ColumnReference) 166 { 167 ColumnReference ref = (ColumnReference) arg1; 168 169 this.name = ref.getColumnName(); 170 this.exposedName = ref.getColumnName(); 171 175 this.reference = ref; 176 this.expression = (ValueNode) arg2; 177 } 178 else if (arg1 instanceof ColumnDescriptor) 179 { 180 ColumnDescriptor coldes = (ColumnDescriptor) arg1; 181 DataTypeDescriptor colType = coldes.getType(); 182 183 this.name = coldes.getColumnName(); 184 this.exposedName = name; 185 186 setType(new DataTypeDescriptor(colType, colType.isNullable())); 187 this.columnDescriptor = coldes; 188 this.expression = (ValueNode) arg2; 189 this.autoincrement = coldes.isAutoincrement(); 190 } 191 else 192 { 193 setType((DataTypeDescriptor) arg1); 194 this.expression = (ValueNode) arg2; 195 if (arg2 instanceof ColumnReference) 196 { 197 reference = (ColumnReference) arg2; 198 } 199 } 200 201 204 if (expression != null && 205 expression.isInstanceOf(C_NodeTypes.DEFAULT_NODE)) 206 defaultColumn = true; 207 } 208 209 213 public boolean isDefaultColumn() 214 { 215 return defaultColumn; 216 } 217 218 public void setDefaultColumn(boolean value) 219 { 220 defaultColumn = value; 221 } 222 223 227 228 public String getName() 229 { 230 return exposedName; 231 } 232 233 public String getSchemaName() throws StandardException 234 { 235 if ((columnDescriptor!=null) && 236 (columnDescriptor.getTableDescriptor() != null)) 237 return columnDescriptor.getTableDescriptor().getSchemaName(); 238 else 239 { 240 if (expression != null) 241 return expression.getSchemaName(); 243 else 244 return null; 245 } 246 } 247 248 public String getTableName() 249 { 250 if (tableName != null) 251 { 252 return tableName; 253 } 254 if ((columnDescriptor!=null) && 255 (columnDescriptor.getTableDescriptor() != null)) 256 { 257 return columnDescriptor.getTableDescriptor().getName(); 258 } 259 else 260 { 261 return expression.getTableName(); 262 } 263 } 264 265 268 public String getSourceTableName() 269 { 270 return sourceTableName; 271 } 272 273 276 public String getSourceSchemaName() 277 { 278 return sourceSchemaName; 279 } 280 281 285 public void clearTableName() 286 { 287 if (expression instanceof ColumnReference) 288 { 289 ((ColumnReference) expression).setTableNameNode((TableName) null); 290 } 291 } 292 293 public DataTypeDescriptor getType() 294 { 295 return dataTypeServices; 296 } 297 298 public DataTypeDescriptor getExpressionType() throws StandardException 299 { 300 return (expression == null) ? 301 dataTypeServices : 302 expression.getTypeServices(); 303 } 304 305 public int getColumnPosition() 306 { 307 if (columnDescriptor!=null) 308 return columnDescriptor.getPosition(); 309 else 310 return virtualColumnId; 311 312 } 313 314 323 324 public void setExpression(ValueNode expression) 325 { 326 this.expression = expression; 327 } 328 329 334 335 public ValueNode getExpression() 336 { 337 return expression; 338 } 339 340 346 void setExpressionToNullNode() 347 throws StandardException 348 { 349 expression = getNullNode(getTypeId(), 350 getContextManager()); 351 } 352 353 362 363 public void setName(String name) 364 { 365 if (this.name == null) 366 { 367 this.name = name; 368 } 369 else { 370 if (SanityManager.DEBUG) 371 SanityManager.ASSERT(reference == null || 372 name.equals(reference.getColumnName()), 373 "don't change name from reference name"); 374 } 375 376 this.exposedName = name; 377 } 378 379 382 public boolean isNameGenerated() 383 { 384 return isNameGenerated; 385 } 386 387 390 public void setNameGenerated(boolean value) 391 { 392 isNameGenerated = value; 393 } 394 395 403 public void setResultSetNumber(int resultSetNumber) 404 { 405 this.resultSetNumber = resultSetNumber; 406 } 407 408 413 public int getResultSetNumber() 414 { 415 return resultSetNumber; 416 } 417 418 423 public void setClause(int clause) 424 { 425 super.setClause(clause); 426 427 if (expression != null) 428 { 429 expression.setClause(clause); 430 } 431 else if (SanityManager.DEBUG) 432 { 433 SanityManager.ASSERT(this instanceof AllResultColumn, 434 "this expected to be instanceof AllResultColumn when expression is null"); 435 } 436 } 437 438 443 444 public void adjustVirtualColumnId(int adjust) 445 { 446 virtualColumnId += adjust; 447 } 448 449 454 455 public void setVirtualColumnId(int id) 456 { 457 virtualColumnId = id; 458 } 459 460 465 public int getVirtualColumnId() 466 { 467 return virtualColumnId; 468 } 469 470 476 public void guaranteeColumnName() throws StandardException 477 { 478 if (exposedName == null) 479 { 480 481 exposedName ="SQLCol" + getCompilerContext().getNextColumnNumber(); 482 isNameGenerated = true; 483 } 484 } 485 486 492 493 public String toString() 494 { 495 if (SanityManager.DEBUG) 496 { 497 return "exposedName: " + exposedName + "\n" + 498 "name: " + name + "\n" + 499 "tableName: " + tableName + "\n" + 500 "isNameGenerated: " + isNameGenerated + "\n" + 501 "sourceTableName: " + sourceTableName + "\n" + 502 "type: " + dataTypeServices + "\n" + 503 "columnDescriptor: " + columnDescriptor + "\n" + 504 "isGenerated: " + isGenerated + "\n" + 505 "isGeneratedForUnmatchedColumnInInsert: " + isGeneratedForUnmatchedColumnInInsert + "\n" + 506 "isGroupingColumn: " + isGroupingColumn + "\n" + 507 "isReferenced: " + isReferenced + "\n" + 508 "isRedundant: " + isRedundant + "\n" + 509 "virtualColumnId: " + virtualColumnId + "\n" + 510 "resultSetNumber: " + resultSetNumber + "\n" + 511 super.toString(); 512 } 513 else 514 { 515 return ""; 516 } 517 } 518 519 525 526 public void printSubNodes(int depth) 527 { 528 if (SanityManager.DEBUG) 529 { 530 super.printSubNodes(depth); 531 if (expression != null) 532 { 533 printLabel(depth, "expression: "); 534 expression.treePrint(depth + 1); 535 } 536 if (reference != null) 537 { 538 printLabel(depth, "reference: "); 539 reference.treePrint(depth + 1); 540 } 541 } 542 } 543 544 562 563 public ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, 564 Vector aggregateVector) 565 throws StandardException 566 { 567 572 if (expression.requiresTypeFromContext()) 573 { 574 if (getTypeServices() != null) 575 { 576 expression.setType(getTypeServices()); 577 } 578 } 579 580 expression = expression.bindExpression(fromList, subqueryList, 581 aggregateVector); 582 583 if (expression instanceof ColumnReference) 584 { 585 autoincrement = ((ColumnReference)expression).getSource().isAutoincrement(); 586 } 587 588 589 590 return this; 591 } 592 593 611 612 void bindResultColumnByPosition(TableDescriptor tableDescriptor, 613 int columnId) 614 throws StandardException 615 { 616 ColumnDescriptor columnDescriptor; 617 618 columnDescriptor = tableDescriptor.getColumnDescriptor(columnId); 619 620 if (columnDescriptor == null) 621 { 622 String errorString; 623 String schemaName; 624 625 errorString = ""; 626 schemaName = tableDescriptor.getSchemaName(); 627 if (schemaName != null) 628 errorString += schemaName + "."; 629 errorString += tableDescriptor.getName(); 630 631 throw StandardException.newException(SQLState.LANG_TOO_MANY_RESULT_COLUMNS, errorString); 632 } 633 634 setColumnDescriptor(tableDescriptor, columnDescriptor); 635 setVirtualColumnId(columnId); 636 } 637 638 657 658 public void bindResultColumnByName(TableDescriptor tableDescriptor, 659 int columnId) 660 throws StandardException 661 { 662 ColumnDescriptor columnDescriptor; 663 664 columnDescriptor = tableDescriptor.getColumnDescriptor(exposedName); 665 666 if (columnDescriptor == null) 667 { 668 String errorString; 669 String schemaName; 670 671 errorString = ""; 672 schemaName = tableDescriptor.getSchemaName(); 673 if (schemaName != null) 674 errorString += schemaName + "."; 675 errorString += tableDescriptor.getName(); 676 677 throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, exposedName, errorString); 678 } 679 680 setColumnDescriptor(tableDescriptor, columnDescriptor); 681 setVirtualColumnId(columnId); 682 if (isPrivilegeCollectionRequired()) 683 getCompilerContext().addRequiredColumnPriv( columnDescriptor); 684 } 685 686 691 public void typeUntypedNullExpression( ResultColumn bindingRC) 692 throws StandardException 693 { 694 TypeId typeId = bindingRC.getTypeId(); 695 698 if (typeId == null) 699 { 700 throw StandardException.newException(SQLState.LANG_NULL_IN_VALUES_CLAUSE); 701 } 702 703 if( expression instanceof UntypedNullConstantNode) 704 expression = getNullNode( typeId, getContextManager()); 705 else if( ( expression instanceof ColumnReference) && expression.getTypeServices() == null) 706 { 707 expression.setType( bindingRC.getType()); 709 } 710 } 711 712 728 void setColumnDescriptor(TableDescriptor tableDescriptor, 729 ColumnDescriptor columnDescriptor) throws StandardException 730 { 731 732 if (SanityManager.DEBUG) 733 SanityManager.ASSERT(columnDescriptor != null, 734 "Caller is responsible for verifying that column exists"); 735 736 setType(columnDescriptor.getType()); 737 this.columnDescriptor = columnDescriptor; 738 739 743 if (reference != null && reference.getTableName() != null) 744 { 745 if (! tableDescriptor.getName().equals( 746 reference.getTableName()) ) 747 { 748 753 String realName = tableDescriptor.getName(); 754 String refName = reference.getTableName(); 755 756 throw StandardException.newException(SQLState.LANG_TABLE_NAME_MISMATCH, 757 realName, refName); 758 } 759 } 760 } 761 762 770 public void bindResultColumnToExpression() 771 throws StandardException 772 { 773 778 setType(expression.getTypeServices()); 779 780 if (expression instanceof ColumnReference) 781 { 782 ColumnReference cr = (ColumnReference) expression; 783 tableName = cr.getTableName(); 784 sourceTableName = cr.getSourceTableName(); 785 sourceSchemaName = cr.getSourceSchemaName(); 786 } 787 } 788 789 804 public ValueNode preprocess(int numTables, 805 FromList outerFromList, 806 SubqueryList outerSubqueryList, 807 PredicateList outerPredicateList) 808 throws StandardException 809 { 810 if (expression == null) 811 return this; 812 expression = expression.preprocess(numTables, outerFromList, 813 outerSubqueryList, 814 outerPredicateList); 815 return this; 816 } 817 818 829 public void checkStorableExpression(ResultColumn toStore) 830 throws StandardException 831 { 832 TypeId columnTypeId, toStoreTypeId; 833 834 toStoreTypeId = toStore.getTypeId(); 835 if( toStoreTypeId == null) 836 return; 837 838 columnTypeId = getTypeId(); 839 840 if (! getTypeCompiler().storable(toStoreTypeId, getClassFactory())) 841 throw StandardException.newException(SQLState.LANG_NOT_STORABLE, 842 columnTypeId.getSQLTypeName(), 843 toStoreTypeId.getSQLTypeName() ); 844 } 845 846 857 public void checkStorableExpression() 858 throws StandardException 859 { 860 TypeId columnTypeId = getTypeId(); 861 TypeId toStoreTypeId = getExpressionType().getTypeId(); 862 863 if (! getTypeCompiler().storable(toStoreTypeId, getClassFactory())) 864 throw StandardException.newException(SQLState.LANG_NOT_STORABLE, 865 columnTypeId.getSQLTypeName(), 866 toStoreTypeId.getSQLTypeName() ); 867 } 868 869 879 880 public void generateExpression(ExpressionClassBuilder ecb, 881 MethodBuilder mb) 882 throws StandardException 883 { 884 expression.generateExpression(ecb, mb); 885 } 886 887 898 922 930 public void generateHolder(ExpressionClassBuilder acb, 931 MethodBuilder mb) 932 throws StandardException 933 { 934 937 acb.generateNull(mb, getTypeCompiler()); 938 mb.upCast(ClassName.DataValueDescriptor); 939 } 940 941 951 952 boolean columnTypeAndLengthMatch() 953 throws StandardException 954 { 955 DataTypeDescriptor resultColumnType; 956 DataTypeDescriptor expressionType = expression.getTypeServices(); 957 958 963 if (expression.requiresTypeFromContext()) 964 { 965 return false; 966 } 967 968 resultColumnType = getType(); 969 970 if (SanityManager.DEBUG) 971 { 972 if (! (resultColumnType != null)) 973 { 974 SanityManager.THROWASSERT("Type is null for column " + 975 this); 976 } 977 } 978 979 if (resultColumnType.getTypeId().isXMLTypeId()) 986 return false; 987 988 989 if ( ! resultColumnType.getTypeId().getSQLTypeName().equals( 990 expressionType.getTypeId().getSQLTypeName() 991 ) 992 ) 993 { 994 return false; 995 } 996 997 998 if (resultColumnType.getPrecision() != expressionType.getPrecision()) 999 { 1000 return false; 1001 } 1002 1003 1004 if (resultColumnType.getScale() != expressionType.getScale()) 1005 { 1006 return false; 1007 } 1008 1009 1010 if (resultColumnType.getMaximumWidth() != expressionType.getMaximumWidth()) 1011 { 1012 return false; 1013 } 1014 1015 1016 if ((! resultColumnType.isNullable()) && expressionType.isNullable()) 1017 { 1018 return false; 1019 } 1020 1021 return true; 1022 } 1023 1024 boolean columnTypeAndLengthMatch(ResultColumn otherColumn) 1025 throws StandardException 1026 { 1027 DataTypeDescriptor resultColumnType; 1028 DataTypeDescriptor otherResultColumnType; 1029 ValueNode otherExpression = otherColumn.getExpression(); 1030 1031 resultColumnType = getType(); 1032 otherResultColumnType = otherColumn.getType(); 1033 1034 if (SanityManager.DEBUG) 1035 { 1036 SanityManager.ASSERT(resultColumnType != null, 1037 "Type is null for column " + this); 1038 SanityManager.ASSERT(otherResultColumnType != null, 1039 "Type is null for column " + otherColumn); 1040 } 1041 1042 1047 if ((otherExpression != null) && (otherExpression.requiresTypeFromContext()) || 1048 (expression.requiresTypeFromContext())) 1049 { 1050 return false; 1051 } 1052 1053 if (resultColumnType.getTypeId().isXMLTypeId()) 1060 return false; 1061 1062 1063 if ( ! resultColumnType.getTypeId().equals( 1064 otherResultColumnType.getTypeId() 1065 ) 1066 ) 1067 { 1068 1081 if (otherExpression instanceof ConstantNode) 1082 { 1083 ConstantNode constant = (ConstantNode)otherColumn.getExpression(); 1084 DataValueDescriptor oldValue = constant.getValue(); 1085 1086 1087 DataValueDescriptor newValue = convertConstant( 1088 resultColumnType.getTypeId(), 1089 resultColumnType.getMaximumWidth(), oldValue); 1090 1091 if ((oldValue != newValue) && 1092 (oldValue instanceof StringDataValue == 1093 newValue instanceof StringDataValue)) 1094 { 1095 constant.setValue(newValue); 1096 constant.setType(getTypeServices()); 1097 otherColumn.bindResultColumnToExpression(); 1098 otherResultColumnType = otherColumn.getType(); 1099 } 1100 } 1101 if ( ! resultColumnType.getTypeId().equals( 1102 otherResultColumnType.getTypeId() 1103 ) 1104 ) 1105 { 1106 return false; 1107 } 1108 } 1109 1110 1111 if (resultColumnType.getPrecision() != 1112 otherResultColumnType.getPrecision()) 1113 { 1114 return false; 1115 } 1116 1117 1118 if (resultColumnType.getScale() != otherResultColumnType.getScale()) 1119 { 1120 return false; 1121 } 1122 1123 1124 if (resultColumnType.getMaximumWidth() != 1125 otherResultColumnType.getMaximumWidth()) 1126 { 1127 return false; 1128 } 1129 1130 1141 if ((! resultColumnType.isNullable()) && 1142 (otherResultColumnType.isNullable() || 1143 otherColumn.isGeneratedForUnmatchedColumnInInsert())) 1144 { 1145 return false; 1146 } 1147 1148 return true; 1149 } 1150 1151 1156 public boolean isGenerated() 1157 { 1158 return (isGenerated == true); 1159 } 1160 1161 1166 public boolean isGeneratedForUnmatchedColumnInInsert() 1167 { 1168 return (isGeneratedForUnmatchedColumnInInsert == true); 1169 } 1170 1171 1174 public void markGenerated() 1175 { 1176 isGenerated = true; 1177 1178 isReferenced = true; 1179 } 1180 1181 1184 public void markGeneratedForUnmatchedColumnInInsert() 1185 { 1186 isGeneratedForUnmatchedColumnInInsert = true; 1187 1188 isReferenced = true; 1189 } 1190 1191 1196 public boolean isReferenced() 1197 { 1198 return isReferenced; 1199 } 1200 1201 1204 public void setReferenced() 1205 { 1206 isReferenced = true; 1207 } 1208 1209 1213 void pullVirtualIsReferenced() 1214 { 1215 if( isReferenced()) 1216 return; 1217 1218 for( ValueNode expr = expression; expr != null && (expr instanceof VirtualColumnNode);) 1219 { 1220 VirtualColumnNode vcn = (VirtualColumnNode) expr; 1221 ResultColumn src = vcn.getSourceColumn(); 1222 if( src.isReferenced()) 1223 { 1224 setReferenced(); 1225 return; 1226 } 1227 expr = src.getExpression(); 1228 } 1229 } 1231 1234 public void setUnreferenced() 1235 { 1236 isReferenced = false; 1237 } 1238 1239 1243 void markAllRCsInChainReferenced() 1244 { 1245 setReferenced(); 1246 1247 ValueNode vn = expression; 1248 1249 while (vn instanceof VirtualColumnNode) 1250 { 1251 VirtualColumnNode vcn = (VirtualColumnNode) vn; 1252 ResultColumn rc = vcn.getSourceColumn(); 1253 rc.setReferenced(); 1254 vn = rc.getExpression(); 1255 } 1256 } 1257 1258 1263 public boolean isRedundant() 1264 { 1265 return isRedundant; 1266 } 1267 1268 1271 public void setRedundant() 1272 { 1273 isRedundant = true; 1274 } 1275 1276 1279 public void markAsGroupingColumn() 1280 { 1281 isGroupingColumn = true; 1282 } 1283 1284 1291 1292 void rejectParameter() throws StandardException 1293 { 1294 if ((expression != null) && (expression.isParameterNode())) 1295 throw StandardException.newException(SQLState.LANG_PARAM_IN_SELECT_LIST); 1296 if ((expression != null) && (expression instanceof UnaryOperatorNode) && 1297 ((UnaryOperatorNode)expression).isUnaryMinusOrPlusWithParameter()) 1298 throw StandardException.newException(SQLState.LANG_PARAM_IN_SELECT_LIST); 1299 } 1300 1301 1304 public int compareTo(Object other) 1305 { 1306 ResultColumn otherResultColumn = (ResultColumn) other; 1307 1308 return this.getColumnPosition() - otherResultColumn.getColumnPosition(); 1309 } 1310 1311 1314 void markUpdated() 1315 { 1316 updated = true; 1317 } 1318 1319 1323 void markUpdatableByCursor() 1324 { 1325 updatableByCursor = true; 1326 } 1327 1328 1333 boolean updated() 1334 { 1335 return updated; 1336 } 1337 1338 1343 public boolean updatableByCursor() 1344 { 1345 return updatableByCursor; 1346 } 1347 1348 1351 public void disablePrivilegeCollection() 1352 { 1353 super.disablePrivilegeCollection(); 1354 if (expression != null) 1355 expression.disablePrivilegeCollection(); 1356 } 1357 1358 1365 ResultColumn cloneMe() throws StandardException 1366 { 1367 ResultColumn newResultColumn; 1368 ValueNode cloneExpr; 1369 1370 1377 if (expression instanceof ColumnReference) 1378 { 1379 cloneExpr = ((ColumnReference) expression).getClone(); 1380 } 1381 else 1382 { 1383 cloneExpr = expression; 1384 } 1385 1386 1387 if (columnDescriptor != null) 1388 { 1389 newResultColumn = (ResultColumn) getNodeFactory().getNode( 1390 C_NodeTypes.RESULT_COLUMN, 1391 columnDescriptor, 1392 expression, 1393 getContextManager()); 1394 newResultColumn.setExpression(cloneExpr); 1395 } 1396 else 1397 { 1398 1399 newResultColumn = (ResultColumn) getNodeFactory().getNode( 1400 C_NodeTypes.RESULT_COLUMN, 1401 getName(), 1402 cloneExpr, 1403 getContextManager()); 1404 } 1405 1406 1407 newResultColumn.setVirtualColumnId(getVirtualColumnId()); 1408 1409 1410 newResultColumn.setName(getName()); 1411 newResultColumn.setType(getTypeServices()); 1412 newResultColumn.setNameGenerated(isNameGenerated()); 1413 1414 1416 if (isGeneratedForUnmatchedColumnInInsert()) 1417 newResultColumn.markGeneratedForUnmatchedColumnInInsert(); 1418 1419 1420 if (isReferenced()) 1421 newResultColumn.setReferenced(); 1422 1423 1424 if (updated()) 1425 newResultColumn.markUpdated(); 1426 1427 1428 if (updatableByCursor()) 1429 newResultColumn.markUpdatableByCursor(); 1430 1431 if (isAutoincrementGenerated()) 1432 newResultColumn.setAutoincrementGenerated(); 1433 1434 if (isAutoincrement()) 1435 newResultColumn.setAutoincrement(); 1436 if (isGroupingColumn()) 1437 newResultColumn.markAsGroupingColumn(); 1438 return newResultColumn; 1439 } 1440 1441 1446 public int getMaximumColumnSize() 1447 { 1448 return dataTypeServices.getTypeId() 1449 .getApproximateLengthInBytes(dataTypeServices); 1450 } 1451 1452 1465 protected int getOrderableVariantType() throws StandardException 1466 { 1467 1476 int expType = ((expression != null) ? 1477 expression.getOrderableVariantType() : 1478 ((isAutoincrementGenerated()) ? 1479 Qualifier.VARIANT : Qualifier.CONSTANT)); 1480 1481 switch (expType) 1482 { 1483 case Qualifier.VARIANT: 1484 return Qualifier.VARIANT; 1485 1486 case Qualifier.SCAN_INVARIANT: 1487 case Qualifier.QUERY_INVARIANT: 1488 return Qualifier.SCAN_INVARIANT; 1489 1490 default: 1491 return Qualifier.CONSTANT; 1492 } 1493 } 1494 1495 1503 public Visitable accept(Visitor v) 1504 throws StandardException 1505 { 1506 Visitable returnNode = v.visit(this); 1507 1508 if (v.skipChildren(this)) 1509 { 1510 return returnNode; 1511 } 1512 1513 if (expression != null && !v.stopTraversal()) 1514 { 1515 expression = (ValueNode)expression.accept(v); 1516 } 1517 return returnNode; 1518 } 1519 1520 1523 public void setNullability(boolean nullability) 1524 { 1525 dataTypeServices.setNullability(nullability); 1526 } 1527 1528 1535 public boolean foundInList(String [] list) 1536 { 1537 return foundString(list, name); 1538 } 1539 1540 1545 void verifyOrderable() throws StandardException 1546 { 1547 1554 if (!getTypeId().orderable(getClassFactory())) 1555 { 1556 throw StandardException.newException(SQLState.LANG_COLUMN_NOT_ORDERABLE_DURING_EXECUTION, 1557 getTypeId().getSQLTypeName()); 1558 } 1559 } 1560 1561 1566 ColumnDescriptor getTableColumnDescriptor() {return columnDescriptor;} 1567 1568 1572 public boolean isAutoincrementGenerated() 1573 { 1574 return autoincrementGenerated; 1575 } 1576 1577 public void setAutoincrementGenerated() 1578 { 1579 autoincrementGenerated = true; 1580 } 1581 1582 public void resetAutoincrementGenerated() 1583 { 1584 autoincrementGenerated = false; 1585 } 1586 1587 public boolean isAutoincrement() 1588 { 1589 return autoincrement; 1590 } 1591 1592 public void setAutoincrement() 1593 { 1594 autoincrement = true; 1595 } 1596 1597 public boolean isGroupingColumn() 1598 { 1599 return isGroupingColumn; 1600 } 1601 1602 1605 private DataValueDescriptor convertConstant(TypeId toTypeId, int maxWidth, DataValueDescriptor constantValue) 1606 throws StandardException 1607 { 1608 int formatId = toTypeId.getTypeFormatId(); 1609 DataValueFactory dvf = getDataValueFactory(); 1610 switch (formatId) 1611 { 1612 default: 1613 case StoredFormatIds.CHAR_TYPE_ID: 1614 return constantValue; 1615 1616 case StoredFormatIds.VARCHAR_TYPE_ID: 1617 case StoredFormatIds.NATIONAL_CHAR_TYPE_ID: 1618 case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID: 1619 String sourceValue = constantValue.getString(); 1620 int sourceWidth = sourceValue.length(); 1621 int posn; 1622 1623 1628 1629 if (sourceWidth <= maxWidth) 1630 { 1631 switch (formatId) 1632 { 1633 case StoredFormatIds.NATIONAL_CHAR_TYPE_ID: 1636 1637 if (sourceWidth < maxWidth) 1638 { 1639 StringBuffer stringBuffer = new StringBuffer (sourceValue); 1640 1641 int needed = maxWidth - sourceWidth; 1642 char blankArray[] = new char[needed]; 1643 for (int i = 0; i < needed; i++) 1644 blankArray[i] = ' '; 1645 stringBuffer.append(blankArray, 0, 1646 maxWidth - sourceWidth); 1647 sourceValue = stringBuffer.toString(); 1648 } 1649 return dvf.getNationalCharDataValue(sourceValue); 1650 1651 case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID: 1652 return dvf.getNationalVarcharDataValue(sourceValue); 1653 1654 case StoredFormatIds.VARCHAR_TYPE_ID: 1655 return dvf.getVarcharDataValue(sourceValue); 1656 } 1657 } 1658 1659 1662 for (posn = maxWidth; posn < sourceWidth; posn++) 1663 { 1664 if (sourceValue.charAt(posn) != ' ') 1665 { 1666 String typeName = null; 1667 switch (formatId) 1668 { 1669 case StoredFormatIds.NATIONAL_CHAR_TYPE_ID: 1670 typeName = TypeId.NATIONAL_CHAR_NAME; 1671 break; 1672 1673 case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID: 1674 typeName = TypeId.NATIONAL_VARCHAR_NAME; 1675 break; 1676 1677 case StoredFormatIds.VARCHAR_TYPE_ID: 1678 typeName = TypeId.VARCHAR_NAME; 1679 break; 1680 } 1681 throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, 1682 typeName, 1683 StringUtil.formatForPrint(sourceValue), 1684 String.valueOf(maxWidth)); 1685 } 1686 } 1687 1688 switch (formatId) 1689 { 1690 case StoredFormatIds.NATIONAL_CHAR_TYPE_ID: 1691 return dvf.getNationalCharDataValue(sourceValue.substring(0, maxWidth)); 1692 1693 case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID: 1694 return dvf.getNationalVarcharDataValue(sourceValue.substring(0, maxWidth)); 1695 1696 case StoredFormatIds.VARCHAR_TYPE_ID: 1697 return dvf.getVarcharDataValue(sourceValue.substring(0, maxWidth)); 1698 } 1699 1700 case StoredFormatIds.LONGVARCHAR_TYPE_ID: 1701 return dvf.getLongvarcharDataValue(constantValue.getString()); 1703 1704 case StoredFormatIds.NATIONAL_LONGVARCHAR_TYPE_ID: 1705 return dvf.getNationalLongvarcharDataValue(constantValue.getString()); 1707 1708 } 1709 } 1710 1711 1717 public TypeId getTypeId() throws StandardException 1718 { 1719 TypeId t = super.getTypeId(); 1720 if( t == null) 1721 { 1722 if( expression != null) 1723 { 1724 DataTypeDescriptor dtd = getTypeServices(); 1725 if( dtd != null) 1726 t = dtd.getTypeId(); 1727 } 1728 } 1729 return t; 1730 } 1732 1738 public DataTypeDescriptor getTypeServices() throws StandardException 1739 { 1740 DataTypeDescriptor dtd = super.getTypeServices(); 1741 if( dtd == null && expression != null) 1742 { 1743 dtd = expression.getTypeServices(); 1744 if( dtd != null) 1745 setType( dtd); 1746 } 1747 return dtd; 1748 } 1750 public TableName getTableNameObject() { 1751 return null; 1752 } 1753 1754 1755 public ColumnReference getReference() { return reference; } 1756 1757 1765 public BaseColumnNode getBaseColumnNode() { 1766 ValueNode vn = expression; 1767 while (true) { 1768 if (vn instanceof ResultColumn) { 1769 vn = ((ResultColumn) vn).expression; 1770 } else if (vn instanceof ColumnReference) { 1771 vn = ((ColumnReference) vn).getSource(); 1772 } else if (vn instanceof VirtualColumnNode) { 1773 vn = ((VirtualColumnNode) vn).getSourceColumn(); 1774 } else if (vn instanceof BaseColumnNode) { 1775 return (BaseColumnNode) vn; 1776 } else { 1777 return null; 1778 } 1779 } 1780 } 1781 1782 1800 public int getTableNumber() 1801 throws StandardException 1802 { 1803 if (expression instanceof ColumnReference) 1804 return ((ColumnReference)expression).getTableNumber(); 1805 else if (expression instanceof VirtualColumnNode) 1806 { 1807 VirtualColumnNode vcn = (VirtualColumnNode)expression; 1808 1809 if (vcn.getSourceResultSet() instanceof FromBaseTable) 1812 { 1813 return ((FromBaseTable)vcn.getSourceResultSet()). 1814 getTableNumber(); 1815 } 1816 1817 return vcn.getSourceColumn().getTableNumber(); 1819 } 1820 1821 return -1; 1825 } 1826 1827 public boolean isEquivalent(ValueNode o) throws StandardException 1828 { 1829 if (o.getNodeType() == getNodeType()) 1830 { 1831 ResultColumn other = (ResultColumn)o; 1832 if (expression != null) { 1833 return expression.isEquivalent(other.expression); 1834 } 1835 } 1836 return false; 1837 } 1838 1839} 1840 | Popular Tags |