1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
|
/*
* Copyright (c) 1998, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
// 10/29/2010-2.2 Michael O'Brien
// - 325167: Make reserved # bind parameter char generic to enable native SQL pass through
// 05/24/2011-2.3 Guy Pelletier
// - 345962: Join fetch query when using tenant discriminator column fails.
// 07/13/2012-2.5 Guy Pelletier
// - 350487: JPA 2.1 Specification defined support for Stored Procedure Calls
// 08/24/2012-2.5 Guy Pelletier
// - 350487: JPA 2.1 Specification defined support for Stored Procedure Calls
// 11/10/2014-2.6 Dmitry Kornilov
// - 450818: Column names with hash mark => "java.sql.SQLException: Invalid column index"
package org.eclipse.persistence.internal.databaseaccess;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.internal.expressions.ParameterExpression;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.queries.DatabaseQueryMechanism;
import org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField;
import org.eclipse.persistence.queries.Call;
import org.eclipse.persistence.queries.DatabaseQuery;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* INTERNAL:
* <b>Purpose<b>: Used as an abstraction of a datasource invocation.
*
* @author James Sutherland
* @since OracleAS TopLink 10<i>g</i> (10.0.3)
*/
public abstract class DatasourceCall implements Call {
// Back reference to query, unfortunately required for events.
protected transient DatabaseQuery query;
// The parameters (values) are ordered as they appear in the call.
protected List<Object> parameters;
// The parameter types determine if the parameter is a modify, translation or literal type.
protected List<ParameterType> parameterTypes;
// The parameter binding determines if the specific parameter should be bound.
protected List<Boolean> parameterBindings;
/**
* The call may specify that all of its parameters should/shouldn't be bound.
* <p>
* Typically, this is set to false in the event that the DatabasePlatform marks the call
* as containing illegal binding behavior during JPQL parsing.
* <p>
* Defaults to null to indicate no preference and allows database platforms to determine
*/
protected Boolean usesBinding;
public enum ParameterType {
LITERAL(1), MODIFY(2), TRANSLATION(3), CUSTOM_MODIFY(4), OUT(5),
INOUT(6), IN(7), OUT_CURSOR(8), INLINE(9);
public int val;
ParameterType(int val) {
this.val = val;
}
public static ParameterType valueOf(int value) {
for(ParameterType v : values())
if(v.val == value) return v;
throw new IllegalArgumentException("Value (" + value + ") does not match a ParameterType");
}
}
// Store if the call has been prepared.
protected boolean isPrepared;
/** Allow connection unwrapping to be configured. */
protected boolean isNativeConnectionRequired;
//Eclipselink Bug 217745 indicates whether or not the token(#,?) needs to be processed if they are in the quotes.
protected boolean shouldProcessTokenInQuotes;
/**
* Keep a list of the output cursors.
*/
protected List<DatabaseField> outputCursors;
// Type of call.
protected int returnType;
protected static final int NO_RETURN = 1;
protected static final int RETURN_ONE_ROW = 2;
protected static final int RETURN_MANY_ROWS = 3;
protected static final int RETURN_CURSOR = 4;
protected static final int EXECUTE_UPDATE = 5;
public DatasourceCall() {
this.isPrepared = false;
this.shouldProcessTokenInQuotes = true;
this.usesBinding = null;
}
/**
* The parameters are the values in order of occurrence in the SQL statement.
* This is lazy initialized to conserve space on calls that have no parameters.
*/
public List getParameters() {
if (parameters == null) {
parameters = new ArrayList<Object>();
}
return parameters;
}
/**
* The parameter types determine if the parameter is a modify, translation or literal type.
*/
public List<ParameterType> getParameterTypes() {
if (parameterTypes == null) {
parameterTypes = new ArrayList<ParameterType>();
}
return parameterTypes;
}
/**
* The parameter binding determines if the specific parameter should be bound.
*/
public List<Boolean> getParameterBindings() {
if (parameterBindings == null) {
parameterBindings = new ArrayList<Boolean>();
}
return parameterBindings;
}
/**
* The parameters are the values in order of occurrence in the SQL statement.
*/
public void setParameters(List<Object> parameters) {
this.parameters = parameters;
}
/**
* The parameter types determine if the parameter is a modify, translation or literal type.
*/
public void setParameterTypes(List<ParameterType> parameterTypes) {
this.parameterTypes = parameterTypes;
}
/**
* The parameter binding determines if the specific parameter should be bound.
*/
public void setParameterBindings(List<Boolean> parameterBindings) {
this.parameterBindings = parameterBindings;
}
/**
* The parameters are the values in order of occurrence in call.
* This is lazy initialized to conserve space on calls that have no parameters.
*/
public boolean hasParameters() {
return (parameters != null) && (!getParameters().isEmpty());
}
/**
* INTERNAL:
* Return the output cursors for this stored procedure call.
*/
public List<DatabaseField> getOutputCursors() {
if (outputCursors == null) {
outputCursors = new ArrayList<DatabaseField>();
}
return outputCursors;
}
/**
* Return true if there are output cursors on this call.
*/
public boolean hasOutputCursors() {
return outputCursors != null && ! outputCursors.isEmpty();
}
/**
* The return type is one of, NoReturn, ReturnOneRow or ReturnManyRows.
*/
public boolean areManyRowsReturned() {
return this.returnType == RETURN_MANY_ROWS;
}
public static boolean isOutputParameterType(ParameterType parameterType) {
return (parameterType == ParameterType.OUT) || (parameterType == ParameterType.INOUT) || (parameterType == ParameterType.OUT_CURSOR);
}
/**
* Bound calls can have the SQL pre generated.
*/
public boolean isPrepared() {
return isPrepared;
}
/**
* Bound calls can have the SQL pre generated.
*/
public void setIsPrepared(boolean isPrepared) {
this.isPrepared = isPrepared;
}
/**
* Set that this call should or shouldn't bind all parameters
*/
public void setUsesBinding(boolean usesBinding) {
this.usesBinding = Boolean.valueOf(usesBinding);
}
/**
* Convenience method
* @see {@link #usesBinding(DatabasePlatform databasePlatform)}
*/
public boolean usesBinding(AbstractSession session) {
return usesBinding(session.getPlatform());
}
/**
* Determines if this call should bind all parameters.
* <p>
* Defaults behavior to the databasePlatform if this call does not have a preference; if
* {@link org.eclipse.persistence.internal.databaseaccess.DatasourceCall#usesBinding} is not set
* <p>
* @see org.eclipse.persistence.internal.databaseaccess.DatabasePlatform#shouldBindAllParameters()
*/
public boolean usesBinding(DatabasePlatform databasePlatform) {
if (this.usesBinding == null) {
return databasePlatform.shouldBindAllParameters();
} else {
return this.usesBinding.booleanValue();
}
}
/**
* INTERNAL
* Indicates whether usesBinding has been set.
*/
public Boolean usesBinding() {
return this.usesBinding;
}
/**
* INTERNAL
* Indicates whether usesBinding has been set.
*/
public boolean isUsesBindingSet() {
return this.usesBinding != null;
}
/**
* Return the appropriate mechanism,
* with the call added as necessary.
*/
public DatabaseQueryMechanism buildNewQueryMechanism(DatabaseQuery query) {
return new DatasourceCallQueryMechanism(query, this);
}
/**
* Return the appropriate mechanism,
* with the call added as necessary.
*/
public DatabaseQueryMechanism buildQueryMechanism(DatabaseQuery query, DatabaseQueryMechanism mechanism) {
if (mechanism.isCallQueryMechanism() && (mechanism instanceof DatasourceCallQueryMechanism)) {
// Must also add the call singleton...
DatasourceCallQueryMechanism callMechanism = ((DatasourceCallQueryMechanism)mechanism);
if (!callMechanism.hasMultipleCalls()) {
callMechanism.addCall(callMechanism.getCall());
callMechanism.setCall(null);
}
callMechanism.addCall(this);
return mechanism;
} else {
return buildNewQueryMechanism(query);
}
}
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException exception) {
;//Do nothing
}
return null;
}
/**
* Return the SQL string for logging purposes.
*/
public abstract String getLogString(Accessor accessor);
/**
* Back reference to query, unfortunately required for events.
*/
public DatabaseQuery getQuery() {
return query;
}
/**
* The return type is one of, NoReturn, ReturnOneRow or ReturnManyRows.
*/
public int getReturnType() {
return returnType;
}
/**
* The return type is one of, NoReturn, ReturnOneRow or ReturnManyRows.
*/
public boolean isCursorReturned() {
return this.returnType == RETURN_CURSOR;
}
/**
* Returns true if this call returns from a statement.execute call.
*/
public boolean isExecuteUpdate() {
return this.returnType == EXECUTE_UPDATE;
}
/**
* Return whether all the results of the call have been returned.
*/
public boolean isFinished() {
return !isCursorReturned() && !isExecuteUpdate();
}
/**
* The return type is one of, NoReturn, ReturnOneRow or ReturnManyRows.
*/
public boolean isNothingReturned() {
return this.returnType == NO_RETURN;
}
/**
* The return type is one of, NoReturn, ReturnOneRow or ReturnManyRows.
*/
public boolean isOneRowReturned() {
return this.returnType == RETURN_ONE_ROW;
}
public boolean isSQLCall() {
return false;
}
public boolean isStoredPLSQLFunctionCall() {
return false;
}
public boolean isStoredPLSQLProcedureCall() {
return false;
}
public boolean isStoredFunctionCall() {
return false;
}
public boolean isStoredProcedureCall() {
return false;
}
public boolean isJPQLCall() {
return false;
}
public boolean isEISInteraction() {
return false;
}
public boolean isQueryStringCall() {
return false;
}
/**
* Allow pre-printing of the query/SQL string for fully bound calls, to save from reprinting.
*/
public void prepare(AbstractSession session) {
setIsPrepared(true);
}
/**
* Cursor return is used for cursored streams.
*/
public void returnCursor() {
setReturnType(RETURN_CURSOR);
}
/**
* Indicates that this call will return a boolean value from an execute()
* call.
*/
public void setExecuteUpdate() {
setReturnType(EXECUTE_UPDATE);
}
/**
* Return if the call's return type has been set.
*/
public boolean isReturnSet() {
return this.returnType != 0;
}
/**
* Many rows are returned for read-all queries.
*/
public void returnManyRows() {
setReturnType(RETURN_MANY_ROWS);
}
/**
* No return is used for modify calls like insert / update / delete.
*/
public void returnNothing() {
setReturnType(NO_RETURN);
}
/**
* One row is returned for read-object queries.
*/
public void returnOneRow() {
setReturnType(RETURN_ONE_ROW);
}
/**
* Back reference to query, unfortunately required for events.
*/
public void setQuery(DatabaseQuery query) {
this.query = query;
}
/**
* The return type is one of, NoReturn, ReturnOneRow or ReturnManyRows.
*/
public void setReturnType(int returnType) {
this.returnType = returnType;
}
/**
* Allow the call to translate from the translation for predefined calls.
*/
public void translate(AbstractRecord translationRow, AbstractRecord modifyRow, AbstractSession session) {
//do nothing by default.
}
/**
* Return the query string of the call.
* This must be overwritten by subclasses that support query language translation (SQLCall, XQueryCall).
*/
public String getQueryString() {
return "";
}
/**
* Set the query string of the call.
* This must be overwritten by subclasses that support query language translation (SQLCall, XQueryCall).
*/
public void setQueryString(String queryString) {
// Nothing by default.
}
/**
* INTERNAL:
* Parse the query string for # markers for custom query based on a query language.
* This is used by SQLCall and XQuery call, but can be reused by other query languages.
*/
public void translateCustomQuery() {
if (this.shouldProcessTokenInQuotes) {
if (getQueryString().indexOf(this.query.getParameterDelimiter()) == -1) {
if (this.getQuery().shouldBindAllParameters() && getQueryString().indexOf("?") == -1) {
return;
}
translatePureSQLCustomQuery();
return;
}
} else {
if (!hasArgumentMark(getQueryString(), this.query.getParameterDelimiterChar(), '\'')
|| !hasArgumentMark(getQueryString(), this.query.getParameterDelimiterChar(), '\"')
|| !hasArgumentMark(getQueryString(), this.query.getParameterDelimiterChar(), '`')) {
if (this.getQuery().shouldBindAllParameters() && !hasArgumentMark(getQueryString(),'?', '\'')) {
return;
}
translatePureSQLCustomQuery();
return;
}
}
int lastIndex = 0;
String queryString = getQueryString();
Writer writer = new CharArrayWriter(queryString.length() + 50);
try {
// ** This method is heavily optimized do not touch anything unless you "know" what your doing.
while (lastIndex != -1) {
int poundIndex = queryString.indexOf(this.query.getParameterDelimiterChar(), lastIndex);
String token;
if (poundIndex == -1) {
token = queryString.substring(lastIndex, queryString.length());
lastIndex = -1;
} else {
if(this.shouldProcessTokenInQuotes){//Always process token no matter whether the quotes around it or not.
token = queryString.substring(lastIndex, poundIndex);
}else{
boolean hasPairedQuoteBeforePound = true;
int quotePairIndex=poundIndex;
do{
quotePairIndex=queryString.lastIndexOf('\'',quotePairIndex-1);
if(quotePairIndex!=-1 && quotePairIndex > lastIndex){
hasPairedQuoteBeforePound = !hasPairedQuoteBeforePound;
} else {
break;
}
}while(true);
int endQuoteIndex = -1;
if(!hasPairedQuoteBeforePound){//There is begin quote, so search end quote.
endQuoteIndex = queryString.indexOf('\'', poundIndex+1);
}
if(endQuoteIndex!=-1){//There is quote around pound.
token = queryString.substring(lastIndex, endQuoteIndex+1);
poundIndex=-1;
lastIndex = endQuoteIndex + 1;
} else { //No quote around pound,
token = queryString.substring(lastIndex, poundIndex);
lastIndex = poundIndex + 1;
}
}
}
writer.write(token);
if (poundIndex != -1) {
int wordEndIndex = poundIndex + 1;
while ((wordEndIndex < queryString.length()) && (whitespace().indexOf(queryString.charAt(wordEndIndex)) == -1)) {
wordEndIndex = wordEndIndex + 1;
}
// Check for ## which means field from modify row.
if (queryString.charAt(poundIndex + 1) == this.query.getParameterDelimiterChar()) {
// Check for ### which means OUT parameter type.
if (queryString.charAt(poundIndex + 2) == this.query.getParameterDelimiterChar()) {
// Check for #### which means INOUT parameter type.
if (queryString.charAt(poundIndex + 3) == this.query.getParameterDelimiterChar()) {
String fieldName = queryString.substring(poundIndex + 4, wordEndIndex);
DatabaseField field = createField(fieldName);
appendInOut(writer, field);
} else {
String fieldName = queryString.substring(poundIndex + 3, wordEndIndex);
DatabaseField field = createField(fieldName);
appendOut(writer, field);
}
} else {
String fieldName = queryString.substring(poundIndex + 2, wordEndIndex);
DatabaseField field = createField(fieldName);
appendModify(writer, field);
}
} else {
String fieldName = queryString.substring(poundIndex + 1, wordEndIndex);
DatabaseField field = createField(fieldName);
appendIn(writer, field);
}
lastIndex = wordEndIndex;
}
}
setQueryString(writer.toString());
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
}
/**
* INTERNAL:
* Parse the query string for ? markers for custom query based on a query language.
* This is used by SQLCall and XQuery call, but can be reused by other query languages.
*/
public void translatePureSQLCustomQuery() {
int lastIndex = 0;
String queryString = getQueryString();
int parameterIndex = 1; // this is the parameter index
Writer writer = new CharArrayWriter(queryString.length() + 50);
try {
// ** This method is heavily optimized do not touch anything unless you "know" what your doing.
while (lastIndex != -1) {
int markIndex = queryString.indexOf('?', lastIndex);
String token;
if (markIndex == -1) { // did not find question mark then we are done looking
token = queryString.substring(lastIndex, queryString.length()); //write rest of sql
lastIndex = -1;
} else {
if(this.shouldProcessTokenInQuotes){
token = queryString.substring(lastIndex, markIndex);
lastIndex = markIndex + 1;
}else{
boolean hasPairedQuoteBeforeMark = true;
int quotePairIndex=markIndex;
do{
quotePairIndex=queryString.lastIndexOf('\'',quotePairIndex-1);
if(quotePairIndex!=-1 && quotePairIndex > lastIndex){
hasPairedQuoteBeforeMark = !hasPairedQuoteBeforeMark;
} else {
break;
}
}while(true);
int endQuoteIndex = -1;
if(!hasPairedQuoteBeforeMark){//There is begin quote, so search end quote.
endQuoteIndex = queryString.indexOf('\'', markIndex+1);
}
if(endQuoteIndex!=-1){//There is quote around mark.
token = queryString.substring(lastIndex, endQuoteIndex+1);
markIndex=-1;
lastIndex = endQuoteIndex + 1;
}else{
//if no quote around the mark, write the rest of sql.
token = queryString.substring(lastIndex, markIndex);
lastIndex = markIndex + 1;
}
}
}
writer.write(token);
if (markIndex != -1) { // found the question mark now find the named token
int wordEndIndex = markIndex + 1;
while ((wordEndIndex < queryString.length()) && (whitespace().indexOf(queryString.charAt(wordEndIndex)) == -1)) {
wordEndIndex = wordEndIndex + 1;
}
if (wordEndIndex > markIndex + 1){ //found a 'name' for this token (may be positional)
String fieldName = queryString.substring(markIndex + 1, wordEndIndex);
DatabaseField field = createField(fieldName);
appendIn(writer, field);
lastIndex = wordEndIndex;
}else{
DatabaseField field = createField(String.valueOf(parameterIndex));
parameterIndex++;
appendIn(writer, field);
}
}
}
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
setQueryString(writer.toString());
}
/**
* INTERNAL:
* Create a new Database Field
* This method can be overridden by subclasses to return other field types
*/
protected DatabaseField createField(String fieldName) {
return new DatabaseField(fieldName);
}
/**
* INTERNAL:
* All values are printed as ? to allow for parameter binding or translation during the execute of the call.
*/
public void appendLiteral(Writer writer, Object literal) {
try {
writer.write(argumentMarker());
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
appendLiteral(literal);
}
/**
* INTERNAL:
* All values are printed as ? to allow for parameter binding or translation during the execute of the call.
*/
public void appendTranslation(Writer writer, DatabaseField modifyField) {
try {
writer.write(argumentMarker());
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
appendTranslation(modifyField);
}
/**
* INTERNAL:
* All values are printed as ? to allow for parameter binding or translation during the execute of the call.
*/
public void appendModify(Writer writer, DatabaseField modifyField) {
try {
writer.write(argumentMarker());
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
appendModify(modifyField);
}
/**
* INTERNAL:
* All values are printed as ? to allow for parameter binding or translation during the execute of the call.
*/
public void appendIn(Writer writer, DatabaseField field) {
try {
writer.write(argumentMarker());
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
appendIn(field);
}
/**
* INTERNAL:
* All values are printed as ? to allow for parameter binding or translation during the execute of the call.
*/
public void appendInOut(Writer writer, DatabaseField inoutField) {
try {
writer.write(argumentMarker());
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
appendInOut(inoutField);
}
/**
* INTERNAL:
* All values are printed as ? to allow for parameter binding or translation during the execute of the call.
*/
public void appendOut(Writer writer, DatabaseField outField) {
try {
writer.write(argumentMarker());
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
appendOut(outField);
}
/**
* INTERNAL:
*/
public void appendLiteral(Object literal) {
getParameters().add(literal);
getParameterTypes().add(ParameterType.LITERAL);
getParameterBindings().add(true);
}
/**
* INTERNAL:
*/
public void appendLiteral(Object literal, Boolean shouldBind) {
getParameters().add(literal);
getParameterTypes().add(ParameterType.LITERAL);
getParameterBindings().add(shouldBind);
}
/**
* INTERNAL:
*/
public void appendTranslation(DatabaseField modifyField) {
getParameters().add(modifyField);
getParameterTypes().add(ParameterType.TRANSLATION);
getParameterBindings().add(true);
}
/**
* INTERNAL:
*/
public void appendTranslation(DatabaseField modifyField, Boolean shouldBind) {
getParameters().add(modifyField);
getParameterTypes().add(ParameterType.TRANSLATION);
getParameterBindings().add(shouldBind);
}
/**
* INTERNAL:
*/
public void appendModify(DatabaseField modifyField) {
getParameters().add(modifyField);
getParameterTypes().add(ParameterType.MODIFY);
getParameterBindings().add(true);
}
/**
* INTERNAL:
*/
public void appendModify(DatabaseField modifyField, Boolean shouldBind) {
getParameters().add(modifyField);
getParameterTypes().add(ParameterType.MODIFY);
getParameterBindings().add(shouldBind);
}
/**
* INTERNAL:
*/
public void appendIn(Object inObject) {
getParameters().add(inObject);
getParameterTypes().add(ParameterType.IN);
getParameterBindings().add(true);
}
/**
* INTERNAL:
*/
public void appendIn(Object inObject, Boolean shouldBind) {
getParameters().add(inObject);
getParameterTypes().add(ParameterType.IN);
getParameterBindings().add(shouldBind);
}
/**
* INTERNAL:
*/
public void appendInOut(DatabaseField inoutField) {
Object[] inOut = { inoutField, inoutField };
getParameters().add(inOut);
getParameterTypes().add(ParameterType.INOUT);
getParameterBindings().add(true);
}
/**
* INTERNAL:
*/
public void appendInOut(DatabaseField inoutField, Boolean shouldBind) {
Object[] inOut = { inoutField, inoutField };
getParameters().add(inOut);
getParameterTypes().add(ParameterType.INOUT);
getParameterBindings().add(shouldBind);
}
/**
* INTERNAL:
*/
public void appendInOut(Object inValueOrField, DatabaseField outField) {
Object[] inOut = { inValueOrField, outField };
getParameters().add(inOut);
getParameterTypes().add(ParameterType.INOUT);
getParameterBindings().add(true);
}
/**
* INTERNAL:
*/
public void appendInOut(Object inValueOrField, DatabaseField outField, Boolean shouldBind) {
Object[] inOut = { inValueOrField, outField };
getParameters().add(inOut);
getParameterTypes().add(ParameterType.INOUT);
getParameterBindings().add(shouldBind);
}
/**
* INTERNAL:
*/
public void appendOut(DatabaseField outField) {
getParameters().add(outField);
getParameterTypes().add(ParameterType.OUT);
getParameterBindings().add(true);
}
/**
* INTERNAL:
*/
public void appendOut(DatabaseField outField, Boolean shouldBind) {
getParameters().add(outField);
getParameterTypes().add(ParameterType.OUT);
getParameterBindings().add(shouldBind);
}
/**
* INTERNAL:
*/
public void appendOutCursor(DatabaseField outField) {
getParameters().add(outField);
getParameterTypes().add(ParameterType.OUT_CURSOR);
getParameterBindings().add(true);
getOutputCursors().add(outField);
}
/**
* Add the parameter.
* If using binding bind the parameter otherwise let the platform print it.
* The platform may also decide to bind the value.
*/
public void appendOutCursor(DatabaseField outField, Boolean shouldBind) {
getParameters().add(outField);
getParameterTypes().add(ParameterType.OUT_CURSOR);
getParameterBindings().add(shouldBind);
getOutputCursors().add(outField);
}
/**
* Add the parameter using the DatasourcePlatform.
*/
public void appendParameter(Writer writer, Object parameter, boolean shouldBind, AbstractSession session) {
session.getDatasourcePlatform().appendParameter(this, writer, parameter);
}
/**
* INTERNAL:
* Return the character to use for the argument marker.
* ? is used in SQL, however other query languages such as XQuery need to use other markers.
*/
protected char argumentMarker() {
return '?';
}
/**
* INTERNAL:
* Return the characters that represent non-arguments names.
*/
protected String whitespace() {
return ",); \n\t:";
}
/**
* INTERNAL:
* Allow the call to translate from the translation for predefined calls.
*/
public void translateQueryString(AbstractRecord translationRow, AbstractRecord modifyRow, AbstractSession session) {
//has a '?'
if ((this.parameters == null) || getParameters().isEmpty()) {
//has no parameters
return;
}
if (getQueryString().indexOf(argumentMarker()) == -1) {
return;
}
int lastIndex = 0;
int parameterIndex = 0;
String queryString = getQueryString();
Writer writer = new CharArrayWriter(queryString.length() + 50);
try {
// PERF: This method is heavily optimized do not touch anything unless you know "very well" what your doing.
// Must translate field parameters and may get new bound parameters for large data.
List<Object> parameterFields = getParameters();
List<ParameterType> parameterTypes = getParameterTypes();
setParameters(new ArrayList<Object>(parameterFields.size()));
while (lastIndex != -1) {
int tokenIndex = queryString.indexOf(argumentMarker(), lastIndex);
String token;
if (tokenIndex == -1) {
token = queryString.substring(lastIndex, queryString.length());
lastIndex = -1;
} else {
if (this.shouldProcessTokenInQuotes) {
token = queryString.substring(lastIndex, tokenIndex);
} else {
boolean hasPairedQuoteBeforeMark = true;
int quotePairIndex = tokenIndex;
do {
quotePairIndex = queryString.lastIndexOf('\'', quotePairIndex - 1);
if (quotePairIndex != -1 && quotePairIndex > lastIndex){
hasPairedQuoteBeforeMark = !hasPairedQuoteBeforeMark;
} else {
break;
}
} while (true);
int endQuoteIndex = -1;
if (!hasPairedQuoteBeforeMark) { // there is a begin quote, so search for end quote.
endQuoteIndex = queryString.indexOf('\'', tokenIndex + 1);
}
if (endQuoteIndex != -1) { // there is a quote around the mark.
token = queryString.substring(lastIndex, endQuoteIndex + 1);
tokenIndex = -1;
lastIndex = endQuoteIndex + 1;
} else {
// if no quote around the mark, write the rest of sql.
token = queryString.substring(lastIndex, tokenIndex);
lastIndex = tokenIndex + 1;
}
}
}
writer.write(token);
if (tokenIndex != -1) {
// Process next parameter.
DatabaseField field = null;
Object value = null;
ParameterType parameterType = parameterTypes.get(parameterIndex);
Object parameter = parameterFields.get(parameterIndex);
switch(parameterType) {
case MODIFY:
field = (DatabaseField)parameter;
value = modifyRow.get(field);
appendParameter(writer, value, false, session);
break;
case CUSTOM_MODIFY:
field = (DatabaseField)parameter;
value = modifyRow.get(field);
if (value != null) {
value = session.getDatasourcePlatform().getCustomModifyValueForCall(this, value, field, false);
//Bug#5200826 needs use unwrapped connection.
if ((value instanceof BindCallCustomParameter) && ((BindCallCustomParameter)value).shouldUseUnwrappedConnection()){
this.isNativeConnectionRequired=true;
}
}
appendParameter(writer, value, false, session);
break;
case TRANSLATION:
value = null;
// Parameter expressions are used for nesting and correct mapping conversion of the value.
if (parameter instanceof ParameterExpression) {
value = ((ParameterExpression)parameter).getValue(translationRow, getQuery(), session);
} else {
field = (DatabaseField)parameter;
value = translationRow.get(field);
// Must check for the modify row as well for custom SQL compatibility as only one # is required.
if ((value == null) && (modifyRow != null)) {
value = modifyRow.get(field);
}
}
appendParameter(writer, value, false, session);
break;
case LITERAL:
if (parameter instanceof DatabaseField) {
parameter = null;
}
appendParameter(writer, parameter, false, session);
break;
case IN:
value = getValueForInParameter(parameter, translationRow, modifyRow, session, false);
appendParameter(writer, value, false, session);
break;
case INOUT:
value = getValueForInOutParameter(parameter, translationRow, modifyRow, session);
appendParameter(writer, value, false, session);
break;
case OUT:
case OUT_CURSOR:
if (parameter instanceof DatabaseField) {
parameter = null;
}
appendParameter(writer, parameter, false, session);
break;
}
lastIndex = tokenIndex + 1;
parameterIndex++;
}
}
setQueryString(writer.toString());
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
}
/**
* INTERNAL:
* Allow the call to translate from the translation for predefined calls.
*/
public void translateQueryStringAndBindParameters(AbstractRecord translationRow, AbstractRecord modifyRow, AbstractSession session) {
List<Object> parameters = getParameters();
// This call has no parameters
if ((parameters == null) || parameters.isEmpty()) {
return;
}
String marker = "" + argumentMarker();
StringBuilder queryString = new StringBuilder(getQueryString());
// The string has no argument markers
if (queryString.indexOf(marker) == -1) {
return;
}
int lastIndex = 0;
int tokenIndex = -1;
boolean hasParameterizedIN = false;
int size = parameters.size();
List<Object> translatedParametersValues = new ArrayList<Object>(size);
Writer writer = new CharArrayWriter(queryString.length() + 50);
try {
// PERF: This method is heavily optimized do not touch anything unless you know "very well" what your doing.
// Must translate field parameters and may get new bound parameters for large data.
List<Object> parameterFields = getParameters();
List<ParameterType> parameterTypes = getParameterTypes();
List<Boolean> canBindParameters = getParameterBindings();
// clear the parameters list
setParameters(new ArrayList<Object>(parameterFields.size()));
for (int parameterIndex = 0; parameterIndex < size; parameterIndex++) {
tokenIndex = queryString.indexOf(marker, tokenIndex + 1);
if (!this.shouldProcessTokenInQuotes) {
// Look for a parameter marker NOT inside quotes
do {
boolean hasPairedQuoteBeforeMark = true;
int quotePairIndex = tokenIndex;
// First, check if current mark is inside quotes
do {
quotePairIndex = queryString.lastIndexOf(String.valueOf('\''), quotePairIndex - 1);
if (quotePairIndex != -1 && quotePairIndex > lastIndex) {
hasPairedQuoteBeforeMark = !hasPairedQuoteBeforeMark;
} else {
break;
}
} while (true);
int endQuoteIndex = -1;
if (!hasPairedQuoteBeforeMark) {
// All the quotes in front of current mark are not paired, so we should be inside quotes
endQuoteIndex = queryString.indexOf(String.valueOf('\''), tokenIndex + 1);
}
if (endQuoteIndex != -1) {
// there is a quote around the mark, so find the next mark and try again
tokenIndex = queryString.indexOf(marker, tokenIndex + 1);
} else {
// we aren't inside quotes, so we're done
break;
}
} while (true);
}
DatabaseField field = null;
Object translatedValue = null;
Object parameterValue = parameterFields.get(parameterIndex);
ParameterType parameterType = parameterTypes.get(parameterIndex);
Boolean canBind = canBindParameters.get(parameterIndex);
switch(parameterType) {
case MODIFY:
field = (DatabaseField) parameterValue;
translatedValue = modifyRow.get(field);
// If the value is null, the field is passed as the value so the type can be obtained from the field.
if (translatedValue == null) {
// The field from the modify row is used, as the calls field may not have the type,
// but if the field is missing the calls field may also have the type.
translatedValue = modifyRow.getField(field);
if (translatedValue == null) {
translatedValue = field;
}
}
// If the parameter doesn't allow binding, we have to append this translated
// parameter value into the query string
if(Boolean.FALSE.equals(canBind)) {
String token = queryString.substring(lastIndex, tokenIndex);
writer.write(token);
lastIndex = tokenIndex + 1;
appendParameter(writer, translatedValue, false, session);
} else {
translatedParametersValues.add(translatedValue);
}
break;
case CUSTOM_MODIFY:
field = (DatabaseField) parameterValue;
translatedValue = modifyRow.get(field);
translatedValue = session.getPlatform().getCustomModifyValueForCall(this, translatedValue, field, true);
//Bug#8200836 needs use unwrapped connection
if ((translatedValue != null) && (translatedValue instanceof BindCallCustomParameter) && (((BindCallCustomParameter)translatedValue).shouldUseUnwrappedConnection())){
this.isNativeConnectionRequired=true;
}
// If the value is null, the field is passed as the value so the type can be obtained from the field.
if (translatedValue == null) {
// The field from the modify row is used, as the calls field may not have the type,
// but if the field is missing the calls field may also have the type.
translatedValue = modifyRow.getField(field);
if (translatedValue == null) {
translatedValue = field;
}
}
// If the parameter doesn't allow binding, we have to append this translated
// parameter value into the query string
if(Boolean.FALSE.equals(canBind)) {
String token = queryString.substring(lastIndex, tokenIndex);
writer.write(token);
lastIndex = tokenIndex + 1;
appendParameter(writer, translatedValue, false, session);
} else {
translatedParametersValues.add(translatedValue);
}
break;
case TRANSLATION:
if (parameterValue instanceof ParameterExpression) {
field = ((ParameterExpression) parameterValue).getField();
translatedValue = ((ParameterExpression) parameterValue).getValue(translationRow, query, session);
} else {
field = (DatabaseField)parameterValue;
translatedValue = translationRow.get(field);
if (translatedValue == null) {// Backward compatibility double check.
translatedValue = modifyRow.get(field);
}
}
if (translatedValue instanceof Collection && !Boolean.FALSE.equals(canBind)) {
// Must re-translate IN parameters.
hasParameterizedIN = true;
}
// If the value is null, the field is passed as the value so the type can be obtained from the field.
if ((translatedValue == null) && (field != null)) {
if (!this.query.hasNullableArguments() || !this.query.getNullableArguments().contains(field)) {
translatedValue = translationRow.getField(field);
// The field from the row is used, as the calls field may not have the type,
// but if the field is missing the calls field may also have the type.
if (translatedValue == null) {
translatedValue = field;
}
// If the parameter doesn't allow binding, we have to append this translated
// parameter value into the query string
if(Boolean.FALSE.equals(canBind)) {
String token = queryString.substring(lastIndex, tokenIndex);
writer.write(token);
lastIndex = tokenIndex + 1;
appendParameter(writer, translatedValue, false, session);
} else {
translatedParametersValues.add(translatedValue);
}
}
} else {
// If the parameter doesn't allow binding, we have to append this translated
// parameter value into the query string
if(Boolean.FALSE.equals(canBind)) {
String token = queryString.substring(lastIndex, tokenIndex);
writer.write(token);
lastIndex = tokenIndex + 1;
appendParameter(writer, translatedValue, false, session);
} else {
translatedParametersValues.add(translatedValue);
}
}
break;
case LITERAL:
translatedValue = parameterValue;
// If the parameter doesn't allow binding, we have to append this translated
// parameter value into the query string
if(Boolean.FALSE.equals(canBind)) {
String token = queryString.substring(lastIndex, tokenIndex);
writer.write(token);
lastIndex = tokenIndex + 1;
if (parameterValue instanceof DatabaseField) {
translatedValue = null;
}
appendParameter(writer, translatedValue, false, session);
} else {
translatedParametersValues.add(translatedValue);
}
break;
case IN:
translatedValue = getValueForInParameter(parameterValue, translationRow, modifyRow, session, true);
// Returning this means the parameter was optional and should not be included.
if (translatedValue != this) {
// If the parameter doesn't allow binding, we have to append this translated
// parameter value into the query string
if(Boolean.FALSE.equals(canBind)) {
String token = queryString.substring(lastIndex, tokenIndex);
writer.write(token);
lastIndex = tokenIndex + 1;
appendParameter(writer, translatedValue, false, session);
} else {
translatedParametersValues.add(translatedValue);
}
}
break;
case INOUT:
translatedValue = getValueForInOutParameter(parameterValue, translationRow, modifyRow, session);
// If the parameter doesn't allow binding, we have to append this translated
// parameter value into the query string
if(Boolean.FALSE.equals(canBind)) {
String token = queryString.substring(lastIndex, tokenIndex);
writer.write(token);
lastIndex = tokenIndex + 1;
appendParameter(writer, translatedValue, false, session);
} else {
translatedParametersValues.add(translatedValue);
}
break;
case OUT:
case OUT_CURSOR:
if (parameterValue != null && parameterValue instanceof OutputParameterForCallableStatement) {
((OutputParameterForCallableStatement) parameterValue).getOutputField().setIndex(parameterIndex);
}
// If the parameter doesn't allow binding, we have to append this translated
// parameter value into the query string
if(Boolean.FALSE.equals(canBind)) {
String token = queryString.substring(lastIndex, tokenIndex);
writer.write(token);
lastIndex = tokenIndex + 1;
appendParameter(writer, translatedValue, false, session);
} else {
translatedParametersValues.add(translatedValue);
}
break;
}
}
if(writer.toString().length() > 0) {
String token = queryString.substring(lastIndex);
writer.write(token);
setQueryString(writer.toString());
}
if(translatedParametersValues.size() > 0) {
setParameters(translatedParametersValues);
}
// If an IN parameter was found must translate SQL.
if (hasParameterizedIN) {
translateQueryStringForParameterizedIN(translationRow, modifyRow, session);
}
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
}
/**
* INTERNAL:
* Translate only IN() parameter values (List parameters).
*/
public void translateQueryStringForParameterizedIN(AbstractRecord translationRow, AbstractRecord modifyRow, AbstractSession session) {
int lastIndex = 0;
int parameterIndex = 0;
String queryString = getQueryString();
Writer writer = new CharArrayWriter(queryString.length() + 50);
try {
// PERF: This method is heavily optimized do not touch anything unless you know "very well" what your doing.
List parameters = getParameters();
List parametersValues = new ArrayList(parameters.size());
while (lastIndex != -1) {
int tokenIndex = queryString.indexOf(argumentMarker(), lastIndex);
String token;
if (tokenIndex == -1) {
token = queryString.substring(lastIndex, queryString.length());
lastIndex = -1;
} else {
token = queryString.substring(lastIndex, tokenIndex);
}
writer.write(token);
if (tokenIndex != -1) {
// Process next parameter.
Object parameter = parameters.get(parameterIndex);
// Parameter expressions are used for nesting and correct mapping conversion of the value.
if (parameter instanceof Collection) {
Collection values = (Collection)parameter;
writer.write("(");
if ((values.size() > 0) && (values.iterator().next() instanceof List)) {
// Support nested lists.
int size = values.size();
Iterator valuesIterator = values.iterator();
for (int index = 0; index < size; index++) {
List nestedValues = (List)valuesIterator.next();
parametersValues.addAll(nestedValues);
int nestedSize = nestedValues.size();
writer.write("(");
for (int nestedIndex = 0; nestedIndex < nestedSize; nestedIndex++) {
writer.write("?");
if ((nestedIndex + 1) < nestedSize) {
writer.write(",");
}
}
writer.write(")");
if ((index + 1) < size) {
writer.write(",");
}
}
} else {
parametersValues.addAll(values);
int size = values.size();
int limit = ((DatasourcePlatform)session.getDatasourcePlatform()).getINClauseLimit();
//The database platform has a limit for the IN clause so we need to reformat the clause
if(limit > 0) {
boolean not = token.endsWith(" NOT IN ");
String subToken = token.substring(0, token.length() - (not ? " NOT IN " : " IN ").length());
int spaceIndex = subToken.lastIndexOf(' ');
int braceIndex = subToken.lastIndexOf('(');
String fieldName = subToken.substring((spaceIndex > braceIndex ? spaceIndex : braceIndex) + 1);
String inToken = not ? ") AND " + fieldName + " NOT IN (" : ") OR " + fieldName + " IN (";
for (int index = 0; index < size; index++) {
writer.write("?");
if ((index + 1) < size) {
if (index > 0 && (index + 1) % limit == 0) {
writer.write(inToken);
} else {
writer.write(",");
}
}
}
} else {
for (int index = 0; index < size; index++) {
writer.write("?");
if ((index + 1) < size) {
writer.write(",");
}
}
}
}
writer.write(")");
} else {
parametersValues.add(parameter);
writer.write("?");
}
lastIndex = tokenIndex + 1;
parameterIndex++;
}
}
setParameters(parametersValues);
setQueryString(writer.toString());
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
}
/**
* INTERNAL:
* Returns value for IN parameter. Called by translate and translateSQLString methods.
* In case shouldBind==true tries to return a DatabaseField with type instead of null,
* returns null only in case no DatabaseField with type was found (case sensitive).
*/
protected Object getValueForInParameter(Object parameter, AbstractRecord translationRow, AbstractRecord modifyRow, AbstractSession session, boolean shouldBind) {
Object value = parameter;
DatabaseField field = null;
boolean isNull = false;
// Parameter expressions are used for nesting and correct mapping conversion of the value.
if (parameter instanceof ParameterExpression) {
value = ((ParameterExpression)parameter).getValue(translationRow, getQuery(), session);
field = ((ParameterExpression)parameter).getField();
} else if (parameter instanceof DatabaseField) {
field = (DatabaseField)parameter;
value = translationRow.get(field);
// Must check for the modify row as well for custom SQL compatibility as only one # is required.
if (modifyRow != null) {
if (value == null) {
value = modifyRow.get(field);
}
if (value != null) {
DatabaseField modifyField = modifyRow.getField(field);
if (modifyField != null) {
if (session.getDatasourcePlatform().shouldUseCustomModifyForCall(modifyField)) {
value = session.getDatasourcePlatform().getCustomModifyValueForCall(this, value, modifyField, shouldBind);
}
}
}
}
if (value == null && shouldBind) {
isNull = true;
if ((field.getType() != null) ||(field.getSqlType()!= DatabaseField.NULL_SQL_TYPE)){
value = field;
} else if (modifyRow != null) {
DatabaseField modifyField = modifyRow.getField(field);
if ((modifyField != null) && (modifyField.getType() != null)) {
value = modifyField;
}
}
if (value == null) {
DatabaseField translationField = translationRow.getField(field);
if (translationField == null){
session.log(SessionLog.WARNING, SessionLog.SQL, "named_argument_not_found_in_query_parameters", new Object[]{field});
}
if ((translationField != null) && (translationField.getType() != null)) {
value = translationField;
}
}
} else {
if (parameter instanceof ObjectRelationalDatabaseField){
value = new InParameterForCallableStatement(value, (DatabaseField)parameter);
}
}
}
if ((value == null || isNull) && this.query.hasNullableArguments() && this.query.getNullableArguments().contains(field)) {
return this;
}
return value;
}
/**
* INTERNAL:
* Returns value for INOUT parameter. Called by translate and translateSQLString methods.
*/
protected Object getValueForInOutParameter(Object parameter, AbstractRecord translationRow, AbstractRecord modifyRow, AbstractSession session) {
// parameter ts an array of two Objects: inParameter and outParameter
Object inParameter = ((Object[])parameter)[0];
Object inValue = getValueForInParameter(inParameter, translationRow, modifyRow, session, true);
Object outParameter = ((Object[])parameter)[1];
return createInOutParameter(inValue, outParameter, session);
}
/**
* INTERNAL:
* Returns INOUT parameter. Called by getValueForInOutParameter method.
* Descendants may override this method.
*/
protected Object createInOutParameter(Object inValue, Object outParameter, AbstractSession session) {
Object[] inOut = { inValue, outParameter };
return inOut;
}
/**
* Return true if the specific mark is existing and not quoted around.
*
* @param string string to search
* @param mark mark to find
* @param quote quote char (usually ' or ")
*/
private boolean hasArgumentMark(String string, char mark, char quote){
int quoteIndex = -1;
int lastEndQuoteIndex = -1;
do{
int markIndex=string.indexOf(mark,lastEndQuoteIndex+1);
if(markIndex==-1){
return false; //no mark at all.
}
quoteIndex = string.lastIndexOf(quote, markIndex);
if(quoteIndex==-1){//no quote before the mark
return true;
}else{//has quote before the mark
boolean hasPairedQuoteBeforeMark = false;
while(quoteIndex!=-1 && quoteIndex >= lastEndQuoteIndex){
if((quoteIndex=string.lastIndexOf(quote, quoteIndex-1))!=-1){
hasPairedQuoteBeforeMark = !hasPairedQuoteBeforeMark;
}
}
if(hasPairedQuoteBeforeMark){//if there is paired quotes before the mark.
return true;
}else{//might have quotes around the mark, need further check.
lastEndQuoteIndex = string.indexOf(quote, markIndex+1);
if(lastEndQuoteIndex==-1){
return true;//no end quote around the mark.
}
}
}
//Upon to here, the current mark is positioning between quotes
//we need search for the next mark.
}while(true);
}
/**
* Set if the call requires usage of a native (unwrapped) JDBC connection.
* This may be required for some Oracle JDBC support when a wrapping DataSource is used.
*/
public void setIsNativeConnectionRequired(boolean isNativeConnectionRequired) {
this.isNativeConnectionRequired = isNativeConnectionRequired;
}
/**
* Return if the call requires usage of a native (unwrapped) JDBC connection.
* This may be required for some Oracle JDBC support when a wrapping DataSource is used.
*/
public boolean isNativeConnectionRequired() {
return isNativeConnectionRequired;
}
/**
* INTERNAL:
* This method is used to correct parameterTypes which are compared to static values using == equality, which changes
* during serialization/deserialization.
* @param in
* @throws IOException
* @throws ClassNotFoundException
*/
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
if (parameterTypes !=null) {
List<ParameterType> newParameterTypes = new ArrayList<ParameterType>(parameterTypes.size());
for (ParameterType type: parameterTypes){
if (ParameterType.LITERAL.equals(type)) {
newParameterTypes.add(ParameterType.LITERAL);
} else if (ParameterType.MODIFY.equals(type)) {
newParameterTypes.add(ParameterType.MODIFY);
} else if (ParameterType.TRANSLATION.equals(type)) {
newParameterTypes.add(ParameterType.TRANSLATION);
} else if (ParameterType.CUSTOM_MODIFY.equals(type)) {
newParameterTypes.add(ParameterType.CUSTOM_MODIFY);
} else if (ParameterType.OUT.equals(type)) {
newParameterTypes.add(ParameterType.OUT);
} else if (ParameterType.INOUT.equals(type)) {
newParameterTypes.add(ParameterType.INOUT);
} else if (ParameterType.IN.equals(type)) {
newParameterTypes.add(ParameterType.IN);
} else if (ParameterType.OUT_CURSOR.equals(type)) {
newParameterTypes.add(ParameterType.OUT_CURSOR);
}
}
parameterTypes = newParameterTypes;
}
}
}
|