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 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
|
/*******************************************************************************
* Copyright (c) 1998, 2015 Oracle and/or its affiliates, IBM Corporation. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Oracle - initial API and implementation from Oracle TopLink
* Vikram Bhatia - bug fix for releasing temporary LOBs after conversion
* 02/08/2012-2.4 Guy Pelletier
* - 350487: JPA 2.1 Specification defined support for Stored Procedure Calls
* 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/05/2012-2.5 Guy Pelletier
* - 350487: JPA 2.1 Specification defined support for Stored Procedure Calls
* 01/08/2012-2.5 Guy Pelletier
* - 389090: JPA 2.1 DDL Generation Support
* 02/19/2015 - Rick Curtis
* - 458877 : Add national character support
******************************************************************************/
package org.eclipse.persistence.internal.databaseaccess;
// javase imports
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.sql.Types;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;
// EclipseLink imports
import org.eclipse.persistence.queries.Call;
import org.eclipse.persistence.queries.DatabaseQuery;
import org.eclipse.persistence.sessions.DatabaseLogin;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.exceptions.QueryException;
import org.eclipse.persistence.internal.helper.ClassConstants;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.helper.LOBValueWriter;
import org.eclipse.persistence.internal.helper.NonSynchronizedVector;
import org.eclipse.persistence.internal.helper.ThreadCursoredList;
import org.eclipse.persistence.internal.localization.ExceptionLocalization;
import org.eclipse.persistence.internal.localization.ToStringLocalization;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.internal.sessions.ArrayRecord;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.sessions.Login;
import org.eclipse.persistence.sessions.SessionProfiler;
import org.eclipse.persistence.sessions.DatabaseRecord;
import org.eclipse.persistence.mappings.structures.ObjectRelationalDataTypeDescriptor;
import static org.eclipse.persistence.internal.helper.DatabaseField.NULL_SQL_TYPE;
/**
* INTERNAL:
* DatabaseAccessor is private to EclipseLink. It encapsulates low level database operations (such as executing
* SQL and reading data by row). Database accessor defines a protocol by which EclipseLink may invoke these
* operations. <p>
* DatabaseAccessor also defines a single reference through which all configuration dependent behavior may
* be invoked. <p>
*
* DabaseAccessor implements the following behavior. <ul>
* <li> Connect and disconnect from the database.
* <li> Execute SQL statements on the database, returning results.
* <li> Handle auto-commit and transactions.
* </ul>
* DatabaseAccessor dispatches the following protocols to its platform reference. <ul>
* <li> Provision of database platform specific type names.
* </ul>
* DatabaseAccessor dispatches the following protocols to the schema object. <ul>
* <li> Creation and deletion of schema objects.
* </ul>
* @see DatabasePlatform
* @since TOPLink/Java 1.0
*/
public class DatabaseAccessor extends DatasourceAccessor {
/** PERF: Backdoor to disabling dynamic statements. Reverts to old prepared statement usage if set. */
public static boolean shouldUseDynamicStatements = true;
/** Stores statement handles for common used prepared statements. */
protected Map<String, Statement> statementCache;
/** Cache of the connection's java.sql.DatabaseMetaData */
protected DatabaseMetaData metaData;
/** This attribute will be used to store the currently active Batch Mechanism */
protected BatchWritingMechanism activeBatchWritingMechanism;
/**
* These two attributes store the available BatchWritingMechanisms. We sacrifice a little space to
* prevent the work involved in recreating these objects each time a different type of SQL statement is
* executed. Depending on user behavior we may want to review this.
*/
protected DynamicSQLBatchWritingMechanism dynamicSQLMechanism;
protected ParameterizedSQLBatchWritingMechanism parameterizedMechanism;
// Bug 2804663 - Each DatabaseAccessor holds on to its own LOBValueWriter instance
protected LOBValueWriter lobWriter;
/** PERF: Cache the statement object for dynamic SQL execution. */
protected Statement dynamicStatement;
protected boolean isDynamicStatementInUse;
public DatabaseAccessor() {
super();
this.lobWriter = null;
this.isDynamicStatementInUse = false;
}
/**
* Create a database accessor with the given connection.
*/
public DatabaseAccessor(Object connection) {
this();
this.datasourceConnection = connection;
}
/**
* Lazy init the dynamic SQL mechanism.
*/
protected DynamicSQLBatchWritingMechanism getDynamicSQLMechanism() {
if (this.dynamicSQLMechanism == null) {
this.dynamicSQLMechanism = new DynamicSQLBatchWritingMechanism(this);
}
return this.dynamicSQLMechanism;
}
/**
* Lazy init the parameterized SQL mechanism.
*/
protected ParameterizedSQLBatchWritingMechanism getParameterizedMechanism() {
if (this.parameterizedMechanism == null) {
this.parameterizedMechanism = new ParameterizedSQLBatchWritingMechanism(this);
}
return this.parameterizedMechanism;
}
/**
* Execute any deferred select calls stored in the LOBValueWriter instance.
* This method will typically be called by the CallQueryMechanism object.
* Bug 2804663.
*
* @see org.eclipse.persistence.internal.helper.LOBValueWriter
* @see org.eclipse.persistence.internal.queries.CallQueryMechanism#insertObject()
*/
public void flushSelectCalls(AbstractSession session) {
if (lobWriter != null) {
lobWriter.buildAndExecuteSelectCalls(session);
}
}
/**
* Return the LOBValueWriter instance. Lazily initialize the instance.
* Bug 2804663.
*
* @see org.eclipse.persistence.internal.helper.LOBValueWriter
*/
public LOBValueWriter getLOBWriter() {
if (lobWriter == null) {
lobWriter = new LOBValueWriter(this);
}
return lobWriter;
}
/**
* Allocate a statement for dynamic SQL execution.
* Either return the cached dynamic statement, or a new statement.
* This statement must be released after execution.
*/
public synchronized Statement allocateDynamicStatement(Connection connection) throws SQLException {
if (dynamicStatement == null) {
dynamicStatement = connection.createStatement();
}
if (isDynamicStatementInUse()) {
return connection.createStatement();
}
setIsDynamicStatementInUse(true);
return dynamicStatement;
}
/**
* Return the cached statement for dynamic SQL execution is in use.
* Used to handle concurrency for the dynamic statement, this
* method must only be called from within a synchronized method/block.
*/
public boolean isDynamicStatementInUse() {
return isDynamicStatementInUse;
}
/**
* Set the platform.
* This should be set to the session's platform, not the connections
* which may not be configured correctly.
*/
public void setDatasourcePlatform(DatasourcePlatform platform) {
super.setDatasourcePlatform(platform);
// lobWriter may have been left from a different platform type.
this.lobWriter = null;
}
/**
* Set if the cached statement for dynamic SQL execution is in use.
* Used to handle concurrency for the dynamic statement.
*/
public synchronized void setIsDynamicStatementInUse(boolean isDynamicStatementInUse) {
this.isDynamicStatementInUse = isDynamicStatementInUse;
}
/**
* Begin a transaction on the database. This means toggling the auto-commit option.
*/
@Override
public void basicBeginTransaction(AbstractSession session) throws DatabaseException {
try {
if (getPlatform().supportsAutoCommit()) {
getConnection().setAutoCommit(false);
} else {
getPlatform().beginTransaction(this);
}
} catch (SQLException exception) {
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
}
/**
* If logging is turned on and the JDBC implementation supports meta data then display connection info.
*/
protected void buildConnectLog(AbstractSession session) {
try {
// Log connection information.
if (session.shouldLog(SessionLog.CONFIG, SessionLog.CONNECTION)) {// Avoid printing if no logging required.
DatabaseMetaData metaData = getConnectionMetaData();
Object[] args = { metaData.getURL(), metaData.getUserName(), metaData.getDatabaseProductName(), metaData.getDatabaseProductVersion(), metaData.getDriverName(), metaData.getDriverVersion(), Helper.cr() + "\t" };
session.log(SessionLog.CONFIG, SessionLog.CONNECTION, "connected_user_database_driver", args, this);
}
} catch (Exception exception) {
// Some databases do not support metadata, ignore exception.
session.warning("JDBC_driver_does_not_support_meta_data", SessionLog.CONNECTION);
}
}
/**
* Build a row from the output parameters of a sp call.
*/
public AbstractRecord buildOutputRow(CallableStatement statement, DatabaseCall call, AbstractSession session) throws DatabaseException {
try {
return call.buildOutputRow(statement, this, session);
} catch (SQLException exception) {
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
}
/**
* Return the field sorted in the correct order corresponding to the result set.
* This is used for cursored selects where custom sql was provided.
* If the fields passed in are null, this means that the field are not known and should be
* built from the column names. This case occurs for DataReadQuery's.
*/
public Vector buildSortedFields(Vector fields, ResultSet resultSet, AbstractSession session) throws DatabaseException {
Vector sortedFields;
try {
Vector columnNames = getColumnNames(resultSet, session);
if (fields == null) {// Means fields not known.
sortedFields = columnNames;
} else {
sortedFields = sortFields(fields, columnNames);
}
} catch (SQLException exception) {
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
return sortedFields;
}
/**
* Connect to the database.
* Exceptions are caught and re-thrown as EclipseLink exceptions.
* Must set the transaction isolation.
*/
@Override
protected void connectInternal(Login login, AbstractSession session) throws DatabaseException {
super.connectInternal(login, session);
checkTransactionIsolation(session);
try {
session.getPlatform().initializeConnectionData(getConnection());
} catch (java.sql.SQLException sqlEx) {
DatabaseException commException = processExceptionForCommError(session, sqlEx, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(sqlEx, this, session, false);
}
}
/**
* Check to see if the transaction isolation needs to
* be set for the newly created connection. This must
* be done outside of a transaction.
* Exceptions are caught and re-thrown as EclipseLink exceptions.
*/
protected void checkTransactionIsolation(AbstractSession session) throws DatabaseException {
if ((!this.isInTransaction) && (this.login != null) && (((DatabaseLogin)this.login).getTransactionIsolation() != -1)) {
try {
getConnection().setTransactionIsolation(((DatabaseLogin)this.login).getTransactionIsolation());
} catch (java.sql.SQLException sqlEx) {
DatabaseException commException = processExceptionForCommError(session, sqlEx, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(sqlEx, this, session, false);
}
}
}
/**
* Flush the statement cache.
* Each statement must first be closed.
*/
public void clearStatementCache(AbstractSession session) {
if (hasStatementCache()) {
for (Statement statement : getStatementCache().values()) {
try {
statement.close();
} catch (SQLException exception) {
// an exception can be raised if
// a statement is closed twice.
}
}
this.statementCache = null;
}
// Close cached dynamic statement.
if (this.dynamicStatement != null) {
try {
this.dynamicStatement.close();
} catch (SQLException exception) {
// an exception can be raised if
// a statement is closed twice.
}
this.dynamicStatement = null;
this.setIsDynamicStatementInUse(false);
}
}
/**
* Clone the accessor.
*/
@Override
public Object clone() {
DatabaseAccessor accessor = (DatabaseAccessor)super.clone();
accessor.dynamicSQLMechanism = null;
if (this.activeBatchWritingMechanism != null) {
accessor.activeBatchWritingMechanism = this.activeBatchWritingMechanism.clone();
}
accessor.parameterizedMechanism = null;
accessor.statementCache = null;
return accessor;
}
/**
* Close the result set of the cursored stream.
*/
public void closeCursor(ResultSet resultSet, AbstractSession session) throws DatabaseException {
try {
resultSet.close();
} catch (SQLException exception) {
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
}
/**
* INTERNAL:
* Closes a PreparedStatement (which is supposed to close it's current resultSet).
* Factored out to simplify coding and handle exceptions.
*/
public void closeStatement(Statement statement, AbstractSession session, DatabaseCall call) throws SQLException {
if (statement == null) {
decrementCallCount();
return;
}
DatabaseQuery query = ((call == null)? null : call.getQuery());
try {
session.startOperationProfile(SessionProfiler.StatementExecute, query, SessionProfiler.ALL);
statement.close();
} finally {
session.endOperationProfile(SessionProfiler.StatementExecute, query, SessionProfiler.ALL);
decrementCallCount();
// If this is the cached dynamic statement, release it.
if (statement == this.dynamicStatement) {
this.dynamicStatement = null;
// The dynamic statement is cached and only closed on disconnect.
setIsDynamicStatementInUse(false);
}
}
}
/**
* Commit a transaction on the database. First flush any batched statements.
*/
@Override
public void commitTransaction(AbstractSession session) throws DatabaseException {
this.writesCompleted(session);
super.commitTransaction(session);
}
/**
* Commit a transaction on the database. This means toggling the auto-commit option.
*/
@Override
public void basicCommitTransaction(AbstractSession session) throws DatabaseException {
try {
if (getPlatform().supportsAutoCommit()) {
getConnection().commit();
getConnection().setAutoCommit(true);
} else {
getPlatform().commitTransaction(this);
}
} catch (SQLException exception) {
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
}
/**
* Advance the result set and return a Record populated
* with values from the next valid row in the result set. Intended solely
* for cursored stream support.
*/
public AbstractRecord cursorRetrieveNextRow(Vector fields, ResultSet resultSet, AbstractSession session) throws DatabaseException {
try {
if (resultSet.next()) {
return fetchRow(fields, resultSet, resultSet.getMetaData(), session);
} else {
return null;
}
} catch (SQLException exception) {
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
}
/**
* Advance the result set and return a DatabaseRow populated
* with values from the next valid row in the result set. Intended solely
* for scrollable cursor support.
*/
public AbstractRecord cursorRetrievePreviousRow(Vector fields, ResultSet resultSet, AbstractSession session) throws DatabaseException {
try {
if (resultSet.previous()) {
return fetchRow(fields, resultSet, resultSet.getMetaData(), session);
} else {
return null;
}
} catch (SQLException exception) {
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
}
/**
* Close the connection.
*/
@Override
public void closeDatasourceConnection() throws DatabaseException {
try {
getConnection().close();
} catch (SQLException exception) {
throw DatabaseException.sqlException(exception, this, null, false);
}
}
/**
* Disconnect from the datasource.
* Added for bug 3046465 to ensure the statement cache is cleared.
*/
@Override
public void disconnect(AbstractSession session) throws DatabaseException {
clearStatementCache(session);
super.disconnect(session);
}
/**
* Close the accessor's connection.
* This is used only for external connection pooling
* when it is intended for the connection to be reconnected in the future.
*/
@Override
public void closeConnection() {
// Unfortunately do not have the session to pass, fortunately it is not used.
clearStatementCache(null);
super.closeConnection();
}
/**
* Execute the EclipseLink dynamically batched/concatenated statement.
*/
protected void executeBatchedStatement(PreparedStatement statement, AbstractSession session) throws DatabaseException {
try {
executeDirectNoSelect(statement, null, session);
} catch (RuntimeException exception) {
try {// Ensure that the statement is closed, but still ensure that the real exception is thrown.
closeStatement(statement, session, null);
} catch (SQLException closeException) {
}
throw exception;
}
// This is in a separate try block to ensure that the real exception is not masked by the close exception.
try {
closeStatement(statement, session, null);
} catch (SQLException exception) {
//With an external connection pool the connection may be null after this call, if it is we will
//be unable to determine if it is a connection based exception so treat it as if it wasn't.
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
}
/**
* Execute the call.
* The execution can differ slightly depending on the type of call.
* The call may be parameterized where the arguments are in the translation row.
* The row will be empty if there are no parameters.
* @return depending of the type either the row count, row or vector of rows.
*/
@Override
public Object executeCall(Call call, AbstractRecord translationRow, AbstractSession session) throws DatabaseException {
// Keep complete implementation.
return basicExecuteCall(call, translationRow, session, true);
}
/**
* Execute the call.
* The execution can differ slightly depending on the type of call.
* The call may be parameterized where the arguments are in the translation row.
* The row will be empty if there are no parameters.
* @return depending of the type either the row count, row or vector of rows.
*/
@Override
public Object basicExecuteCall(Call call, AbstractRecord translationRow, AbstractSession session) throws DatabaseException {
return basicExecuteCall(call, translationRow, session, true);
}
/**
* Execute the call.
* The execution can differ slightly depending on the type of call.
* The call may be parameterized where the arguments are in the translation row.
* The row will be empty if there are no parameters.
* @return depending of the type either the row count, row or vector of rows.
*/
public Object basicExecuteCall(Call call, AbstractRecord translationRow, AbstractSession session, boolean batch) throws DatabaseException {
Statement statement = null;
Object result = null;
DatabaseCall dbCall = null;
ResultSet resultSet = null;// only used if this is a read query
try {
dbCall = (DatabaseCall)call;
} catch (ClassCastException e) {
throw QueryException.invalidDatabaseCall(call);
}
// If the login is null, then this accessor has never been connected.
if (this.login == null) {
throw DatabaseException.databaseAccessorNotConnected();
}
if (batch && isInBatchWritingMode(session)) {
// if there is nothing returned and we are not using optimistic locking then batch
//if it is a StoredProcedure with in/out or out parameters then do not batch
//logic may be weird but we must not batch if we are not using JDBC batchwriting and we have parameters
// we may want to refactor this some day
if (dbCall.isBatchExecutionSupported()) {
// this will handle executing batched statements, or switching mechanisms if required
getActiveBatchWritingMechanism(session).appendCall(session, dbCall);
//bug 4241441: passing 1 back to avoid optimistic lock exceptions since there
// is no way to know if it succeeded on the DB at this point.
return Integer.valueOf(1);
} else {
getActiveBatchWritingMechanism(session).executeBatchedStatements(session);
}
}
try {
incrementCallCount(session);
if (session.shouldLog(SessionLog.FINE, SessionLog.SQL)) {// Avoid printing if no logging required.
session.log(SessionLog.FINE, SessionLog.SQL, dbCall.getLogString(this), (Object[])null, this, false);
}
session.startOperationProfile(SessionProfiler.SqlPrepare, dbCall.getQuery(), SessionProfiler.ALL);
try {
statement = dbCall.prepareStatement(this, translationRow, session);
} finally {
session.endOperationProfile(SessionProfiler.SqlPrepare, dbCall.getQuery(), SessionProfiler.ALL);
}
// effectively this means that someone is executing an update type query.
if (dbCall.isExecuteUpdate()) {
dbCall.setExecuteReturnValue(execute(dbCall, statement, session));
dbCall.setStatement(statement);
this.possibleFailure = false;
return dbCall;
} else if (dbCall.isNothingReturned()) {
result = executeNoSelect(dbCall, statement, session);
this.writeStatementsCount++;
if (dbCall.isLOBLocatorNeeded()) {
// add original (insert or update) call to the LOB locator
// Bug 2804663 - LOBValueWriter is no longer a singleton
getLOBWriter().addCall(dbCall);
}
} else if ((!dbCall.getReturnsResultSet() || (dbCall.getReturnsResultSet() && dbCall.shouldBuildOutputRow()))) {
result = session.getPlatform().executeStoredProcedure(dbCall, (PreparedStatement)statement, this, session);
this.storedProcedureStatementsCount++;
} else {
resultSet = executeSelect(dbCall, statement, session);
this.readStatementsCount++;
if (!dbCall.shouldIgnoreFirstRowSetting() && dbCall.getFirstResult() != 0) {
resultSet.absolute(dbCall.getFirstResult());
}
dbCall.matchFieldOrder(resultSet, this, session);
if (dbCall.isCursorReturned()) {
dbCall.setStatement(statement);
dbCall.setResult(resultSet);
this.possibleFailure = false;
return dbCall;
}
result = processResultSet(resultSet, dbCall, statement, session);
}
if (result instanceof ThreadCursoredList) {
this.possibleFailure = false;
return result;
}
// Log any warnings on finest.
if (session.shouldLog(SessionLog.FINEST, SessionLog.SQL)) {// Avoid printing if no logging required.
SQLWarning warning = statement.getWarnings();
while (warning != null) {
String message = warning.getMessage() + ":" + warning.getSQLState() + " - " + warning.getCause();
// 325605: This log will not be tracked by QuerySQLTracker
session.log(SessionLog.FINEST, SessionLog.SQL, message, (Object[])null, this, false);
warning = warning.getNextWarning();
}
}
} catch (SQLException exception) {
//If this is a connection from an external pool then closeStatement will close the connection.
//we must test the connection before that happens.
RuntimeException exceptionToThrow = processExceptionForCommError(session, exception, dbCall);
try {// Ensure that the statement is closed, but still ensure that the real exception is thrown.
closeStatement(statement, session, dbCall);
} catch (Exception closeException) {
}
if (exceptionToThrow == null){
//not a comm failure :
throw DatabaseException.sqlException(exception, dbCall, this, session, false);
}
throw exceptionToThrow;
} catch (RuntimeException exception) {
try {// Ensure that the statement is closed, but still ensure that the real exception is thrown.
closeStatement(statement, session, dbCall);
} catch (Exception closeException) {
}
if (exception instanceof DatabaseException) {
((DatabaseException)exception).setCall(dbCall);
if(((DatabaseException)exception).getAccessor() == null) {
((DatabaseException)exception).setAccessor(this);
}
}
throw exception;
}
// This is in a separate try block to ensure that the real exception is not masked by the close exception.
try {
// Allow for caching of statement, forced closes are not cache as they failed execution so are most likely bad.
releaseStatement(statement, dbCall.getSQLString(), dbCall, session);
} catch (SQLException exception) {
//With an external connection pool the connection may be null after this call, if it is we will
//be unable to determine if it is a connection based exception so treat it as if it wasn't.
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) {
throw commException;
}
throw DatabaseException.sqlException(exception, this, session, false);
}
this.possibleFailure = false;
return result;
}
/**
* Fetch all the rows from the result set.
*/
public Object processResultSet(ResultSet resultSet, DatabaseCall call, Statement statement, AbstractSession session) throws SQLException {
Object result = null;
ResultSetMetaData metaData = resultSet.getMetaData();
// If there are no columns (and only an update count) throw an exception.
if (metaData.getColumnCount() == 0 && statement.getUpdateCount() > -1) {
resultSet.close();
throw new IllegalStateException(ExceptionLocalization.buildMessage("jpa21_invalid_call_with_no_result_sets_returned"));
}
session.startOperationProfile(SessionProfiler.RowFetch, call.getQuery(), SessionProfiler.ALL);
try {
if (call.isOneRowReturned()) {
if (resultSet.next()) {
if (call.isLOBLocatorNeeded()) {
//if Oracle BLOB/CLOB field is being written, and the thin driver is used, the driver 4k
//limit bug prevent the call from directly writing to the table if the LOB value size exceeds 4k.
//Instead, a LOB locator is retrieved and value is then piped into the table through the locator.
// Bug 2804663 - LOBValueWriter is no longer a singleton
getLOBWriter().fetchLocatorAndWriteValue(call, resultSet);
} else {
result = fetchRow(call.getFields(), call.getFieldsArray(), resultSet, metaData, session);
}
if (resultSet.next()) {
// Raise more rows event, some apps may interpret as error or warning.
if (session.hasEventManager()) {
session.getEventManager().moreRowsDetected(call);
}
}
} else {
result = null;
}
} else {
boolean hasMultipleResultsSets = call.hasMultipleResultSets();
Vector results = null;
boolean hasMoreResultsSets = true;
while (hasMoreResultsSets) {
boolean hasNext = resultSet.next();
// PERF: Optimize out simple empty case.
if (hasNext) {
if (session.isConcurrent()) {
// If using threading return the cursored list,
// do not close the result or statement as the rows are being fetched by the thread.
return buildThreadCursoredResult(call, resultSet, statement, metaData, session);
} else {
results = new Vector(16);
while (hasNext) {
results.add(fetchRow(call.getFields(), call.getFieldsArray(), resultSet, metaData, session));
hasNext = resultSet.next();
}
}
} else {
results = new Vector(0);
}
if (result == null) {
if (call.returnMultipleResultSetCollections()) {
result = new Vector();
((List) result).add(results);
} else {
result = results;
}
} else {
if (call.returnMultipleResultSetCollections()) {
((List)result).add(results);
} else {
((List)result).addAll(results);
}
}
if (hasMultipleResultsSets) {
hasMoreResultsSets = statement.getMoreResults();
if (hasMoreResultsSets) {
resultSet = statement.getResultSet();
metaData = resultSet.getMetaData();
call.setFields(null);
call.matchFieldOrder(resultSet, this, session);
}
} else {
hasMoreResultsSets = false;
}
}
}
resultSet.close();// This must be closed in case the statement is cached and not closed.
} finally {
session.endOperationProfile(SessionProfiler.RowFetch, call.getQuery(), SessionProfiler.ALL);
}
return result;
}
/**
* This allows for the rows to be fetched concurrently to the objects being built.
* This code is not currently publicly supported.
*/
protected Vector buildThreadCursoredResult(final DatabaseCall dbCall, final ResultSet resultSet, final Statement statement, final ResultSetMetaData metaData, final AbstractSession session) {
final ThreadCursoredList results = new ThreadCursoredList(20);
Runnable runnable = new Runnable() {
public void run() {
try {
session.startOperationProfile(SessionProfiler.RowFetch, dbCall.getQuery(), SessionProfiler.ALL);
try {
// Initial next was already validated before this method is called.
boolean hasNext = true;
while (hasNext) {
results.add(fetchRow(dbCall.getFields(), dbCall.getFieldsArray(), resultSet, metaData, session));
hasNext = resultSet.next();
}
resultSet.close();// This must be closed in case the statement is cached and not closed.
} catch (SQLException exception) {
//If this is a connection from an external pool then closeStatement will close the connection.
//we must test the connection before that happens.
RuntimeException exceptionToThrow = processExceptionForCommError(session, exception, dbCall);
try {// Ensure that the statement is closed, but still ensure that the real exception is thrown.
closeStatement(statement, session, dbCall);
} catch (Exception closeException) {
}
if (exceptionToThrow == null){
results.throwException(DatabaseException.sqlException(exception, dbCall, DatabaseAccessor.this, session, false));
}
results.throwException(exceptionToThrow);
} catch (RuntimeException exception) {
try {// Ensure that the statement is closed, but still ensure that the real exception is thrown.
closeStatement(statement, session, dbCall);
} catch (Exception closeException) {
}
if (exception instanceof DatabaseException) {
((DatabaseException)exception).setCall(dbCall);
}
results.throwException(exception);
} finally {
session.endOperationProfile(SessionProfiler.RowFetch, dbCall.getQuery(), SessionProfiler.ALL);
}
// This is in a separate try block to ensure that the real exception is not masked by the close exception.
try {
// Allow for caching of statement, forced closes are not cache as they failed execution so are most likely bad.
DatabaseAccessor.this.releaseStatement(statement, dbCall.getSQLString(), dbCall, session);
} catch (SQLException exception) {
//With an external connection pool the connection may be null after this call, if it is we will
//be unable to determine if it is a connection based exception so treat it as if it wasn't.
DatabaseException commException = processExceptionForCommError(session, exception, dbCall);
if (commException != null) results.throwException(commException);
results.throwException(DatabaseException.sqlException(exception, DatabaseAccessor.this, session, false));
}
} finally {
results.setIsComplete(true);
session.releaseReadConnection(DatabaseAccessor.this);
}
}
};
dbCall.returnCursor();
session.getServerPlatform().launchContainerRunnable(runnable);
return results;
}
/**
* Execute the statement.
*/
public Integer executeDirectNoSelect(Statement statement, DatabaseCall call, AbstractSession session) throws DatabaseException {
int rowCount = 0;
try {
if (call != null) {
session.startOperationProfile(SessionProfiler.StatementExecute, call.getQuery(), SessionProfiler.ALL);
} else {
session.startOperationProfile(SessionProfiler.StatementExecute, null, SessionProfiler.ALL);
}
if ((call != null) && call.isDynamicCall(session)) {
rowCount = statement.executeUpdate(call.getSQLString());
} else {
rowCount = ((PreparedStatement)statement).executeUpdate();
}
if ((!getPlatform().supportsAutoCommit()) && (!this.isInTransaction)) {
getPlatform().autoCommit(this);
}
} catch (SQLException exception) {
if (!getPlatform().shouldIgnoreException(exception)) {
DatabaseException commException = processExceptionForCommError(session, exception, call);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
} finally {
if (call != null) {
session.endOperationProfile(SessionProfiler.StatementExecute, call.getQuery(), SessionProfiler.ALL);
} else {
session.endOperationProfile(SessionProfiler.StatementExecute, null, SessionProfiler.ALL);
}
}
return Integer.valueOf(rowCount);
}
/**
* Execute the batched statement through the JDBC2 API.
*/
protected int executeJDK12BatchStatement(Statement statement, DatabaseCall dbCall, AbstractSession session, boolean isStatementPrepared) throws DatabaseException {
int returnValue =0;
try {
//bug 4241441: executeBatch moved to the platform, and result returned to batch mechanism
returnValue = this.getPlatform().executeBatch(statement, isStatementPrepared);
} catch (SQLException exception) {
//If this is a connection from an external pool then closeStatement will close the connection.
//we must test the connection before that happens.
DatabaseException commException = processExceptionForCommError(session, exception, dbCall);
if (commException != null) throw commException;
try {// Ensure that the statement is closed, but still ensure that the real exception is thrown.
closeStatement(statement, session, dbCall);
} catch (SQLException closeException) {
}
throw DatabaseException.sqlException(exception, this, session, false);
} catch (RuntimeException exception) {
try {// Ensure that the statement is closed, but still ensure that the real exception is thrown.
closeStatement(statement, session, dbCall);
} catch (SQLException closeException) {
}
throw exception;
}
// This is in a separate try block to ensure that the real exception is not masked by the close exception.
try {
// if we are called from the ParameterizedBatchWritingMechanism then dbCall will not be null
//and we should try an release the statement
if (dbCall != null) {
releaseStatement(statement, dbCall.getSQLString(), dbCall, session);
} else {
closeStatement(statement, session, dbCall);
}
} catch (SQLException exception) {
DatabaseException commException = processExceptionForCommError(session, exception, dbCall);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
return returnValue;
}
/**
* Execute the statement.
*/
protected Integer executeNoSelect(DatabaseCall call, Statement statement, AbstractSession session) throws DatabaseException {
Integer rowCount = executeDirectNoSelect(statement, call, session);
// Allow for procs with outputs to be raised as events for error handling.
if (call.shouldBuildOutputRow()) {
AbstractRecord outputRow = buildOutputRow((CallableStatement)statement, call, session);
call.getQuery().setProperty("output", outputRow);
if (session.hasEventManager()) {
session.getEventManager().outputParametersDetected(outputRow, call);
}
}
return rowCount;
}
/**
* Execute the statement.
*/
public boolean execute(DatabaseCall call, Statement statement, AbstractSession session) throws SQLException {
boolean result;
session.startOperationProfile(SessionProfiler.StatementExecute, call.getQuery(), SessionProfiler.ALL);
try {
if (call.isDynamicCall(session)) {
result = statement.execute(call.getSQLString());
} else {
result = ((PreparedStatement)statement).execute();
}
} finally {
session.endOperationProfile(SessionProfiler.StatementExecute, call.getQuery(), SessionProfiler.ALL);
}
return result;
}
/**
* Execute the statement.
*/
public ResultSet executeSelect(DatabaseCall call, Statement statement, AbstractSession session) throws SQLException {
ResultSet resultSet;
session.startOperationProfile(SessionProfiler.StatementExecute, call.getQuery(), SessionProfiler.ALL);
try {
if (call.isDynamicCall(session)) {
resultSet = statement.executeQuery(call.getSQLString());
} else {
resultSet = ((PreparedStatement)statement).executeQuery();
}
} finally {
session.endOperationProfile(SessionProfiler.StatementExecute, call.getQuery(), SessionProfiler.ALL);
}
// Allow for procs with outputs to be raised as events for error handling.
if (call.shouldBuildOutputRow() && getPlatform().isOutputAllowWithResultSet()) {
AbstractRecord outputRow = buildOutputRow((CallableStatement)statement, call, session);
call.getQuery().setProperty("output", outputRow);
if (session.hasEventManager()) {
session.getEventManager().outputParametersDetected(outputRow, call);
}
}
return resultSet;
}
/**
* Return a new DatabaseRow.<p>
* Populate the row from the data in cursor. The fields representing the results
* and the order of the results are stored in fields.
* <p><b>NOTE</b>:
* Make sure that the field name is set. An empty field name placeholder is
* used in the sortFields() method when the number of fields defined does not
* match the number of column names available on the database.
* PERF: This method must be highly optimized.
*/
protected AbstractRecord fetchRow(Vector fields, ResultSet resultSet, ResultSetMetaData metaData, AbstractSession session) throws DatabaseException {
int size = fields.size();
Vector values = NonSynchronizedVector.newInstance(size);
// PERF: Pass platform and optimize data flag.
DatabasePlatform platform = getPlatform();
boolean optimizeData = platform.shouldOptimizeDataConversion();
for (int index = 0; index < size; index++) {
DatabaseField field = (DatabaseField)fields.elementAt(index);
// Field can be null for fetch groups.
if (field != null) {
values.add(getObject(resultSet, field, metaData, index + 1, platform, optimizeData, session));
} else {
values.add(null);
}
}
// Row creation is optimized through sharing the same fields for the entire result set.
return new DatabaseRecord(fields, values);
}
/**
* Return a new DatabaseRow.<p>
* Populate the row from the data in cursor. The fields representing the results
* and the order of the results are stored in fields.
* <p><b>NOTE</b>:
* Make sure that the field name is set. An empty field name placeholder is
* used in the sortFields() method when the number of fields defined does not
* match the number of column names available on the database.
* PERF: This method must be highly optimized.
*/
public AbstractRecord fetchRow(Vector fields, DatabaseField[] fieldsArray, ResultSet resultSet, ResultSetMetaData metaData, AbstractSession session) throws DatabaseException {
int size = fieldsArray.length;
Object[] values = new Object[size];
// PERF: Pass platform and optimize data flag.
DatabasePlatform platform = getPlatform();
boolean optimizeData = platform.shouldOptimizeDataConversion();
for (int index = 0; index < size; index++) {
DatabaseField field = fieldsArray[index];
// Field can be null for fetch groups.
if (field != null) {
values[index] = getObject(resultSet, field, metaData, index + 1, platform, optimizeData, session);
} else {
values[index] = null;
}
}
// Row creation is optimized through sharing the same fields for the entire result set.
return new ArrayRecord(fields, fieldsArray, values);
}
public void populateRow(DatabaseField[] fieldsArray, Object[] values, ResultSet resultSet, ResultSetMetaData metaData, AbstractSession session, int startIndex, int endIndex) throws DatabaseException {
// PERF: Pass platform and optimize data flag.
DatabasePlatform platform = getPlatform();
boolean optimizeData = platform.shouldOptimizeDataConversion();
for (int index = startIndex; index < endIndex; index++) {
DatabaseField field = fieldsArray[index];
// Field can be null for fetch groups.
if (field != null) {
values[index] = getObject(resultSet, field, metaData, index + 1, platform, optimizeData, session);
}
}
}
/**
* INTERNAL:
* This method is used internally to return the active batch writing mechanism to batch the statement
*/
public BatchWritingMechanism getActiveBatchWritingMechanism(AbstractSession session) {
if (this.activeBatchWritingMechanism == null) {
// If the platform defines a custom mechanism, then use it.
if (((DatabasePlatform)this.platform).getBatchWritingMechanism() != null) {
this.activeBatchWritingMechanism = ((DatabasePlatform)this.platform).getBatchWritingMechanism().clone();
this.activeBatchWritingMechanism.setAccessor(this, session);
} else {
this.activeBatchWritingMechanism = getParameterizedMechanism();
}
}
return this.activeBatchWritingMechanism;
}
/**
* Get a description of table columns available in a catalog.
*
* <P>Only column descriptions matching the catalog, schema, table
* and column name criteria are returned. They are ordered by
* TABLE_SCHEM, TABLE_NAME and ORDINAL_POSITION.
*
* <P>Each column description has the following columns:
* <OL>
* <LI><B>TABLE_CAT</B> String => table catalog (may be null)
* <LI><B>TABLE_SCHEM</B> String => table schema (may be null)
* <LI><B>TABLE_NAME</B> String => table name
* <LI><B>COLUMN_NAME</B> String => column name
* <LI><B>DATA_TYPE</B> short => SQL type from java.sql.Types
* <LI><B>TYPE_NAME</B> String => Data source dependent type name
* <LI><B>COLUMN_SIZE</B> int => column size. For char or date
* types this is the maximum number of characters, for numeric or
* decimal types this is precision.
* <LI><B>BUFFER_LENGTH</B> is not used.
* <LI><B>DECIMAL_DIGITS</B> int => the number of fractional digits
* <LI><B>NUM_PREC_RADIX</B> int => Radix (typically either 10 or 2)
* <LI><B>NULLABLE</B> int => is NULL allowed?
* <UL>
* <LI> columnNoNulls - might not allow NULL values
* <LI> columnNullable - definitely allows NULL values
* <LI> columnNullableUnknown - nullability unknown
* </UL>
* <LI><B>REMARKS</B> String => comment describing column (may be null)
* <LI><B>COLUMN_DEF</B> String => default value (may be null)
* <LI><B>SQL_DATA_TYPE</B> int => unused
* <LI><B>SQL_DATETIME_SUB</B> int => unused
* <LI><B>CHAR_OCTET_LENGTH</B> int => for char types the
* maximum number of bytes in the column
* <LI><B>ORDINAL_POSITION</B> int => index of column in table
* (starting at 1)
* <LI><B>IS_NULLABLE</B> String => "NO" means column definitely
* does not allow NULL values; "YES" means the column might
* allow NULL values. An empty string means nobody knows.
* </OL>
*
* @param catalog a catalog name; "" retrieves those without a
* catalog; null means drop catalog name from the selection criteria
* @param schemaPattern a schema name pattern; "" retrieves those
* without a schema
* @param tableNamePattern a table name pattern
* @param columnNamePattern a column name pattern
* @return a Vector of DatabaseRows.
*/
public Vector getColumnInfo(String catalog, String schema, String tableName, String columnName, AbstractSession session) throws DatabaseException {
if (session.shouldLog(SessionLog.FINEST, SessionLog.QUERY)) {// Avoid printing if no logging required.
Object[] args = { catalog, schema, tableName, columnName };
session.log(SessionLog.FINEST, SessionLog.QUERY, "query_column_meta_data_with_column", args, this);
}
Vector result = new Vector();
ResultSet resultSet = null;
try {
incrementCallCount(session);
resultSet = getConnectionMetaData().getColumns(catalog, schema, tableName, columnName);
Vector fields = buildSortedFields(null, resultSet, session);
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
result.addElement(fetchRow(fields, resultSet, metaData, session));
}
resultSet.close();
} catch (SQLException sqlException) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException closeException) {
}
DatabaseException commException = processExceptionForCommError(session, sqlException, null);
if (commException != null) throw commException;
// Ensure that real exception is thrown.
throw DatabaseException.sqlException(sqlException, this, session, false);
} finally {
decrementCallCount();
}
return result;
}
/**
* Return the column names from a result sets meta data as a vector of DatabaseFields.
* This is required for custom SQL execution only,
* as generated SQL already knows the fields returned.
*/
protected Vector getColumnNames(ResultSet resultSet, AbstractSession session) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
Vector columnNames = new Vector(metaData.getColumnCount());
for (int index = 0; index < metaData.getColumnCount(); index++) {
// Changed the following code to use metaData#getColumnLabel() instead of metaData.getColumnName()
// This is as required by JDBC spec to access metadata for queries using column aliases.
// Reconsider whether to migrate this change to other versions of Eclipselink with older native query support
String columnName = metaData.getColumnLabel(index + 1);
if ((columnName == null) || columnName.equals("")) {
columnName = "C" + (index + 1);// Some column may be unnamed.
}
DatabaseField column = new DatabaseField(columnName);
// Force field names to upper case is set.
if (getPlatform().shouldForceFieldNamesToUpperCase()) {
column.useUpperCaseForComparisons(true);
}
columnNames.addElement(column);
}
return columnNames;
}
/**
* Return the receiver's connection to its data source. A connection is used to execute queries on,
* and retrieve data from, a data source.
* @see java.sql.Connection
*/
public Connection getConnection() throws DatabaseException {
return (Connection)this.datasourceConnection;
}
/**
* Return the platform.
*/
public DatabasePlatform getPlatform() {
return (DatabasePlatform)platform;
}
/**
* return the cached metaData
*/
public DatabaseMetaData getConnectionMetaData() throws SQLException {
return getConnection().getMetaData();
}
/**
* Return an object retrieved from resultSet with the getObject() method.
* Optimize the get for certain type to avoid double conversion.
* <b>NOTE</b>: This method handles a virtual machine error thrown when retrieving times & dates from Oracle or Sybase.
*/
public Object getObject(ResultSet resultSet, DatabaseField field, ResultSetMetaData metaData, int columnNumber, DatabasePlatform platform, boolean optimizeData, AbstractSession session) throws DatabaseException {
Object value = null;
try {
// PERF: Cache the JDBC type in the field to avoid JDBC call.
int type = field.sqlType;
if (type == NULL_SQL_TYPE) {
type = metaData.getColumnType(columnNumber);
field.setSqlType(type);
}
if (optimizeData) {
try {
value = getObjectThroughOptimizedDataConversion(resultSet, field, type, columnNumber, platform, session);
// Since null cannot be distinguished from no optimization done, this is return for no-op.
if (value == null) {
return null;
}
if (value == this) {
value = null;
}
} catch (SQLException exception) {
// Log the exception and try non-optimized data conversion
if (session.shouldLog(SessionLog.WARNING, SessionLog.SQL)) {
session.logThrowable(SessionLog.WARNING, SessionLog.SQL, exception);
}
}
}
if (value == null) {
if ((type == Types.LONGVARBINARY) && platform.usesStreamsForBinding()) {
//can read large binary data as a stream
InputStream tempInputStream;
tempInputStream = resultSet.getBinaryStream(columnNumber);
if (tempInputStream != null) {
try {
ByteArrayOutputStream tempOutputStream = new ByteArrayOutputStream();
int tempInt = tempInputStream.read();
while (tempInt != -1) {
tempOutputStream.write(tempInt);
tempInt = tempInputStream.read();
}
value = tempOutputStream.toByteArray();
} catch (IOException exception) {
throw DatabaseException.errorReadingBlobData();
}
} else {
value = null;
}
} else {
value = platform.getObjectFromResultSet(resultSet, columnNumber, type, session);
// PERF: only perform blob check on non-optimized types.
// CR2943 - convert early if the type is a BLOB or a CLOB.
if (isBlob(type)) {
// EL Bug 294578 - Store previous value of BLOB so that temporary objects can be freed after conversion
Object originalValue = value;
value = platform.convertObject(value, ClassConstants.APBYTE);
platform.freeTemporaryObject(originalValue);
} else if (isClob(type)) {
// EL Bug 294578 - Store previous value of CLOB so that temporary objects can be freed after conversion
Object originalValue = value;
value = platform.convertObject(value, ClassConstants.STRING);
platform.freeTemporaryObject(originalValue);
} else if (isArray(type)){
//Bug6068155 convert early if type is Array and Structs.
value = ObjectRelationalDataTypeDescriptor.buildArrayObjectFromArray(value);
} else if (isStruct(type, value)){
//Bug6068155 convert early if type is Array and Structs.
value=ObjectRelationalDataTypeDescriptor.buildArrayObjectFromStruct(value);
}
}
}
// PERF: Avoid wasNull check, null is return from the get call for nullable classes.
if ((!optimizeData) && resultSet.wasNull()) {
value = null;
}
} catch (SQLException exception) {
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
return value;
}
/**
* Handle the conversion into java optimally through calling the direct type API.
* If the type is not one that can be optimized return null.
*/
protected Object getObjectThroughOptimizedDataConversion(ResultSet resultSet, DatabaseField field, int type, int columnNumber, DatabasePlatform platform, AbstractSession session) throws SQLException {
Object value = this;// Means no optimization, need to distinguish from null.
Class fieldType = field.type;
if (platform.shouldUseGetSetNString() && (type == Types.NVARCHAR || type == Types.NCHAR)) {
value = resultSet.getNString(columnNumber);
if (type == Types.NCHAR && value != null && platform.shouldTrimStrings()) {
value = Helper.rightTrimString((String) value);
}
return value;
}else if (type == Types.VARCHAR || type == Types.CHAR || type == Types.NVARCHAR || type == Types.NCHAR) {
// CUSTOM PATCH for oracle drivers because they don't respond to getObject() when using scrolling result sets.
// Chars may require blanks to be trimmed.
value = resultSet.getString(columnNumber);
if ((type == Types.CHAR || type == Types.NCHAR) && (value != null) && platform.shouldTrimStrings()) {
value = Helper.rightTrimString((String)value);
}
return value;
} else if (fieldType == null) {
return this;
}
boolean isPrimitive = false;
// Optimize numeric values to avoid conversion into big-dec and back to primitives.
if ((fieldType == ClassConstants.PLONG) || (fieldType == ClassConstants.LONG)) {
value = Long.valueOf(resultSet.getLong(columnNumber));
isPrimitive = ((Long)value).longValue() == 0l;
} else if ((fieldType == ClassConstants.INTEGER) || (fieldType == ClassConstants.PINT)) {
value = Integer.valueOf(resultSet.getInt(columnNumber));
isPrimitive = ((Integer)value).intValue() == 0;
} else if ((fieldType == ClassConstants.FLOAT) || (fieldType == ClassConstants.PFLOAT)) {
value = Float.valueOf(resultSet.getFloat(columnNumber));
isPrimitive = ((Float)value).floatValue() == 0f;
} else if ((fieldType == ClassConstants.DOUBLE) || (fieldType == ClassConstants.PDOUBLE)) {
value = Double.valueOf(resultSet.getDouble(columnNumber));
isPrimitive = ((Double)value).doubleValue() == 0d;
} else if ((fieldType == ClassConstants.SHORT) || (fieldType == ClassConstants.PSHORT)) {
value = Short.valueOf(resultSet.getShort(columnNumber));
isPrimitive = ((Short)value).shortValue() == 0;
} else if (Helper.shouldOptimizeDates && (fieldType != null) && ((type == Types.TIME) || (type == Types.DATE) || (type == Types.TIMESTAMP))) {
// Optimize dates by avoid conversion to timestamp then back to date or time or util.date.
String dateString = resultSet.getString(columnNumber);
value = platform.convertObject(dateString, fieldType);
} else if ((fieldType != null) && ((type == Types.TIME) || (type == Types.DATE) || (type == Types.TIMESTAMP))) {
// PERF: Optimize dates by calling direct get method if type is Date or Time,
// unfortunately the double conversion is unavoidable for Calendar and util.Date.
if (fieldType == ClassConstants.SQLDATE) {
value = resultSet.getDate(columnNumber);
} else if (fieldType == ClassConstants.TIME) {
value = resultSet.getTime(columnNumber);
} else if (fieldType == ClassConstants.TIMESTAMP) {
value = resultSet.getTimestamp(columnNumber);
}
} else if (fieldType == ClassConstants.BIGINTEGER) {
value = resultSet.getBigDecimal(columnNumber);
if (value != null) return ((BigDecimal)value).toBigInteger();
} else if (fieldType == ClassConstants.BIGDECIMAL) {
value = resultSet.getBigDecimal(columnNumber);
}
// PERF: Only check for null for primitives.
if (isPrimitive && resultSet.wasNull()) {
value = null;
}
return value;
}
/**
* Return if the accessor has any cached statements.
* This should be used to avoid lazy instantiation of the cache.
*/
protected boolean hasStatementCache() {
return (statementCache != null) && (!statementCache.isEmpty());
}
/**
* The statement cache stores a fixed sized number of prepared statements.
*/
protected synchronized Map<String, Statement> getStatementCache() {
if (statementCache == null) {
statementCache = new HashMap<String, Statement>(50);
}
return statementCache;
}
/**
* Get a description of tables available in a catalog.
*
* <P>Only table descriptions matching the catalog, schema, table
* name and type criteria are returned. They are ordered by
* TABLE_TYPE, TABLE_SCHEM and TABLE_NAME.
*
* <P>Each table description has the following columns:
* <OL>
* <LI><B>TABLE_CAT</B> String => table catalog (may be null)
* <LI><B>TABLE_SCHEM</B> String => table schema (may be null)
* <LI><B>TABLE_NAME</B> String => table name
* <LI><B>TABLE_TYPE</B> String => table type. Typical types are "TABLE",
* "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY",
* "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
* <LI><B>REMARKS</B> String => explanatory comment on the table
* </OL>
*
* <P><B>Note:</B> Some databases may not return information for
* all tables.
*
* @param catalog a catalog name; "" retrieves those without a
* catalog; null means drop catalog name from the selection criteria
* @param schemaPattern a schema name pattern; "" retrieves those
* without a schema
* @param tableNamePattern a table name pattern
* @param types a list of table types to include; null returns all types
* @return a Vector of DatabaseRows.
*/
public Vector getTableInfo(String catalog, String schema, String tableName, String[] types, AbstractSession session) throws DatabaseException {
if (session.shouldLog(SessionLog.FINEST, SessionLog.QUERY)) {// Avoid printing if no logging required.
Object[] args = { catalog, schema, tableName };
session.log(SessionLog.FINEST, SessionLog.QUERY, "query_column_meta_data", args, this);
}
Vector result = new Vector();
ResultSet resultSet = null;
try {
incrementCallCount(session);
resultSet = getConnectionMetaData().getTables(catalog, schema, tableName, types);
Vector fields = buildSortedFields(null, resultSet, session);
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
result.addElement(fetchRow(fields, resultSet, metaData, session));
}
resultSet.close();
} catch (SQLException sqlException) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException closeException) {
}
DatabaseException commException = processExceptionForCommError(session, sqlException, null);
if (commException != null) throw commException;
// Ensure that real exception is thrown.
throw DatabaseException.sqlException(sqlException, this, session, false);
} finally {
decrementCallCount();
}
return result;
}
/**
* Return true if the receiver is currently connected to a data source. Return false otherwise.
*/
public boolean isDatasourceConnected() {
try {
return !getConnection().isClosed();
} catch (SQLException exception) {
return false;
}
}
/**
* Return the batch writing mode.
*/
protected boolean isInBatchWritingMode(AbstractSession session) {
return getPlatform().usesBatchWriting() && this.isInTransaction;
}
/**
* Prepare the SQL statement for the call.
* First check if the statement is cached before building a new one.
* Currently the SQL string is used as the cache key, this may have to be switched if it becomes a performance problem.
*/
public Statement prepareStatement(DatabaseCall call, AbstractSession session) throws SQLException {
return prepareStatement(call, session,false);
}
/**
* Prepare the SQL statement for the call.
* First check if the statement is cached before building a new one.
* @param unwrapConnection boolean flag set to true to unwrap the connection before preparing the statement in the
* case of a parameterized call.
*/
public Statement prepareStatement(DatabaseCall call, AbstractSession session, boolean unwrapConnection) throws SQLException {
Statement statement = null;
if (call.usesBinding(session) && call.shouldCacheStatement(session)) {
// Check the cache by sql string, must synchronize check and removal.
Map statementCache = getStatementCache();
synchronized (statementCache) {
statement = (PreparedStatement)statementCache.get(call.getSQLString());
if (statement != null) {
// Need to remove to allow concurrent statement execution.
statementCache.remove(call.getSQLString());
}
}
}
if (statement == null) {
Connection nativeConnection = getConnection();
if (nativeConnection==null){
throw DatabaseException.databaseAccessorConnectionIsNull(this, session);
}
// Unwrap the connection if required.
// This needs to be done in some cases before the statement is created to ensure the statement
// and result set are not wrapped.
if (unwrapConnection || call.isNativeConnectionRequired()) {
nativeConnection = getPlatform().getConnection(session, nativeConnection);
}
if (call.isCallableStatementRequired()) {
// Callable statements are used for StoredProcedures and PLSQL blocks.
if (call.isResultSetScrollable()) {
statement = nativeConnection.prepareCall(call.getSQLString(), call.getResultSetType(), call.getResultSetConcurrency());
statement.setFetchSize(call.getResultSetFetchSize());
} else {
statement = nativeConnection.prepareCall(call.getSQLString());
}
} else if (call.isResultSetScrollable()) {
// Scrollable statements are used for ScrollableCursors.
statement = nativeConnection.prepareStatement(call.getSQLString(), call.getResultSetType(), call.getResultSetConcurrency());
statement.setFetchSize(call.getResultSetFetchSize());
} else if (call.isDynamicCall(session)) {
// PERF: Dynamic statements are used for dynamic SQL.
statement = allocateDynamicStatement(nativeConnection);
} else {
statement = nativeConnection.prepareStatement(call.getSQLString());
}
}
return statement;
}
/**
* Prepare the SQL statement for the call.
* First check if the statement is cached before building a new one.
*/
public PreparedStatement prepareStatement(String sql, AbstractSession session, boolean callable) throws SQLException {
PreparedStatement statement = null;
// Check the cache by sql string, must synchronize check and removal.
if (getPlatform().shouldCacheAllStatements()) {
Map statementCache = getStatementCache();
synchronized (statementCache) {
statement = (PreparedStatement)statementCache.get(sql);
if (statement != null) {
// Need to remove to allow concurrent statement execution.
statementCache.remove(sql);
}
}
}
if (statement == null) {
Connection nativeConnection = getConnection();
if (nativeConnection == null ) {
throw DatabaseException.databaseAccessorConnectionIsNull(this, session);
}
if (callable) {
// Callable statements are used for StoredProcedures and PLSQL blocks.
statement = nativeConnection.prepareCall(sql);
} else {
statement = nativeConnection.prepareStatement(sql);
}
}
return statement;
}
/**
* This method is used to process an SQL exception and determine if the exception
* should be passed on for further processing.
* If the Exception was communication based then a DatabaseException will be return.
* If the method did not process the message of it was not a comm failure then null
* will be returned.
*/
public DatabaseException processExceptionForCommError(AbstractSession session, SQLException exception, Call call) {
if (session.getLogin().isConnectionHealthValidatedOnError((DatabaseCall)call, this)
&& (getConnection() != null)
&& session.getServerPlatform().wasFailureCommunicationBased(exception, this, session)) {
setIsValid(false);
setPossibleFailure(false);
//store exception for later as we must close the statement.
return DatabaseException.sqlException(exception, call, this, session, true);
} else {
return null;
}
}
/**
* Attempt to save some of the cost associated with getting a fresh connection.
* Assume the DatabaseDriver has been cached, if appropriate.
* Note: Connections that are participating in transactions will not be refreshed.^M
* Added for bug 3046465 to ensure the statement cache is cleared
*/
protected void reconnect(AbstractSession session) {
clearStatementCache(session);
super.reconnect(session);
}
/**
* Release the statement through closing it or putting it back in the statement cache.
*/
public void releaseStatement(Statement statement, String sqlString, DatabaseCall call, AbstractSession session) throws SQLException {
if (((call == null) && getPlatform().shouldCacheAllStatements())
|| ((call != null) && call.usesBinding(session) && call.shouldCacheStatement(session))) {
Map<String, Statement> statementCache = getStatementCache();
synchronized (statementCache) {
PreparedStatement preparedStatement = (PreparedStatement)statement;
if (!statementCache.containsKey(sqlString)) {// May already be there by other thread.
preparedStatement.clearParameters();
// Bug 5709179 - reset statement settings on cached statements (dminsky) - inclusion of reset
if (call != null) {
resetStatementFromCall(preparedStatement, call);
}
if (statementCache.size() > getPlatform().getStatementCacheSize()) {
// Currently one is removed at random...
PreparedStatement removedStatement = (PreparedStatement)statementCache.remove(statementCache.keySet().iterator().next());
closeStatement(removedStatement, session, call);
} else {
decrementCallCount();
}
statementCache.put(sqlString, preparedStatement);
} else {
// CR... Must close the statement if not cached.
closeStatement(statement, session, call);
}
}
} else if (statement == this.dynamicStatement) {
// The dynamic statement is cached and only closed on disconnect.
// Bug 5709179 - reset statement settings on cached statements (dminsky) - moved to its own method
if (call != null) {
resetStatementFromCall(statement, call);
}
setIsDynamicStatementInUse(false);
decrementCallCount();
} else {
closeStatement(statement, session, call);
}
}
/**
* Reset the Query Timeout, Max Rows, Resultset fetch size on the Statement
* if the DatabaseCall has values which differ from the default settings.
* For Bug 5709179 - reset settings on cached statements
*/
protected void resetStatementFromCall(Statement statement, DatabaseCall call) throws SQLException {
if (call.getQueryTimeout() > 0) {
statement.setQueryTimeout(0);
}
if (call.getMaxRows() > 0) {
statement.setMaxRows(0);
}
if (call.getResultSetFetchSize() > 0) {
statement.setFetchSize(0);
}
}
/**
* Rollback a transaction on the database. This means toggling the auto-commit option.
*/
public void rollbackTransaction(AbstractSession session) throws DatabaseException {
getActiveBatchWritingMechanism(session).clear();
super.rollbackTransaction(session);
}
/**
* Rollback a transaction on the database. This means toggling the auto-commit option.
*/
public void basicRollbackTransaction(AbstractSession session) throws DatabaseException {
try {
if (getPlatform().supportsAutoCommit()) {
getConnection().rollback();
getConnection().setAutoCommit(true);
} else {
getPlatform().rollbackTransaction(this);
}
} catch (SQLException exception) {
DatabaseException commException = processExceptionForCommError(session, exception, null);
if (commException != null) throw commException;
throw DatabaseException.sqlException(exception, this, session, false);
}
}
/**
* INTERNAL:
* This method is used to set the active Batch Mechanism on the accessor.
*/
public void setActiveBatchWritingMechanismToParameterizedSQL() {
this.activeBatchWritingMechanism = getParameterizedMechanism();
//Bug#3214927 The size for ParameterizedBatchWriting represents the number of statements
//and the max size is only 100.
if (((DatabaseLogin)this.login).getMaxBatchWritingSize() == DatabasePlatform.DEFAULT_MAX_BATCH_WRITING_SIZE) {
((DatabaseLogin)this.login).setMaxBatchWritingSize(DatabasePlatform.DEFAULT_PARAMETERIZED_MAX_BATCH_WRITING_SIZE);
}
}
/**
* INTERNAL:
* This method is used to set the active Batch Mechanism on the accessor.
*/
public void setActiveBatchWritingMechanismToDynamicSQL() {
this.activeBatchWritingMechanism = getDynamicSQLMechanism();
// Bug#3214927-fix - Also the size must be switched back when switch back from param to dynamic.
if (((DatabaseLogin)this.login).getMaxBatchWritingSize() == DatabasePlatform.DEFAULT_PARAMETERIZED_MAX_BATCH_WRITING_SIZE) {
((DatabaseLogin)this.login).setMaxBatchWritingSize(DatabasePlatform.DEFAULT_MAX_BATCH_WRITING_SIZE);
}
}
/**
* INTERNAL:
* This method is used to set the active Batch Mechanism on the accessor.
*/
public void setActiveBatchWritingMechanism(BatchWritingMechanism mechanism) {
this.activeBatchWritingMechanism = mechanism;
}
/**
* The statement cache stores a fixed sized number of prepared statements.
*/
protected void setStatementCache(Hashtable statementCache) {
this.statementCache = statementCache;
}
/**
* This method will sort the fields in correct order based
* on the column names.
*/
protected Vector sortFields(Vector fields, Vector columnNames) {
Vector sortedFields = new Vector(columnNames.size());
Vector eligableFields = (Vector)fields.clone();// Must clone to allow removing to support the same field twice.
Enumeration columnNamesEnum = columnNames.elements();
boolean valueFound;
DatabaseField field;
DatabaseField column;//DatabaseField from the columnNames vector
while (columnNamesEnum.hasMoreElements()) {
field = null;
valueFound = false;
column = (DatabaseField)columnNamesEnum.nextElement();
Enumeration fieldEnum = eligableFields.elements();
while (fieldEnum.hasMoreElements()) {
field = (DatabaseField)fieldEnum.nextElement();
if(field != null && field.equals(column)){
valueFound = true;
sortedFields.addElement(field);
break;
}
}
if (valueFound) {
// The eligible fields must be maintained as two field can have the same name, but different tables.
eligableFields.removeElement(field);
} else {
// Need to add a place holder in case the column is not in the fields vector
sortedFields.addElement(new DatabaseField());
}
}
return sortedFields;
}
public String toString() {
StringWriter writer = new StringWriter();
writer.write("DatabaseAccessor(");
if (isConnected()) {
writer.write(ToStringLocalization.buildMessage("connected", (Object[])null));
} else {
writer.write(ToStringLocalization.buildMessage("disconnected", (Object[])null));
}
writer.write(")");
return writer.toString();
}
/**
* Return if the JDBC type is a ARRAY type.
*/
private boolean isArray(int type) {
return (type == Types.ARRAY);
}
/**
* Return if the JDBC type is a binary type such as blob.
*/
public static boolean isBlob(int type) {
return (type == Types.BLOB) || (type == Types.LONGVARBINARY);
}
/**
* Return if the JDBC type is a large character type such as clob.
*/
public static boolean isClob(int type) {
return (type == Types.CLOB) || (type == Types.LONGVARCHAR) || (type == DatabasePlatform.Types_NCLOB) || (type == Types.LONGNVARCHAR);
}
/**
* Return if the JDBC type is a STRUCT type.
*/
private boolean isStruct(int type, Object value) {
return (type == Types.STRUCT && (value instanceof java.sql.Struct));
}
/**
* This method will be called after a series of writes have been issued to
* mark where a particular set of writes has completed. It will be called
* from commitTransaction and may be called from writeChanges. Its main
* purpose is to ensure that the batched statements have been executed
*/
public void writesCompleted(AbstractSession session) {
if (isConnected && isInBatchWritingMode(session)) {
getActiveBatchWritingMechanism(session).executeBatchedStatements(session);
}
}
}
|