1 21 22 package org.apache.derby.impl.jdbc; 23 24 import org.apache.derby.iapi.services.info.ProductVersionHolder; 25 26 import org.apache.derby.iapi.services.monitor.Monitor; 27 28 import org.apache.derby.iapi.services.sanity.SanityManager; 29 30 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 31 32 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 33 import org.apache.derby.iapi.sql.dictionary.SPSDescriptor; 34 35 import org.apache.derby.iapi.error.StandardException; 36 37 import org.apache.derby.impl.sql.execute.GenericConstantActionFactory; 38 import org.apache.derby.impl.sql.execute.GenericExecutionFactory; 39 40 import org.apache.derby.iapi.reference.Limits; 41 import org.apache.derby.iapi.reference.JDBC20Translation; 42 import org.apache.derby.iapi.reference.JDBC30Translation; 43 44 import java.util.Properties ; 45 46 import java.sql.DatabaseMetaData ; 47 import java.sql.Connection ; 48 import java.sql.PreparedStatement ; 49 import java.sql.SQLException ; 50 import java.sql.ResultSet ; 51 import java.sql.Types ; 52 53 import java.io.IOException ; 54 import java.io.InputStream ; 55 56 89 public class EmbedDatabaseMetaData extends ConnectionChild 90 implements DatabaseMetaData , java.security.PrivilegedAction { 91 92 96 97 private final String url; 98 99 102 103 private GenericConstantActionFactory constantActionFactory; 104 105 113 public EmbedDatabaseMetaData (EmbedConnection connection, String url) 114 throws SQLException { 115 116 super(connection); 117 this.url = url; 118 119 } 120 121 122 private static Properties queryDescriptions; 123 124 private static Properties queryDescriptions_net; 125 133 private Properties getQueryDescriptions(boolean net) { 134 Properties p = net ? queryDescriptions_net : queryDescriptions; 135 if (p != null) { 136 return p; 137 } 138 loadQueryDescriptions(); 139 return net ? queryDescriptions_net : queryDescriptions; 140 } 141 142 147 private void PBloadQueryDescriptions() { 148 String [] files = { 149 "metadata.properties", 150 "/org/apache/derby/impl/sql/catalog/metadata_net.properties" 151 }; 152 Properties [] props = new Properties [files.length]; 153 for (int i = 0; i < files.length; ++i) { 154 try { 155 props[i] = new Properties (); 156 InputStream is = getClass().getResourceAsStream(files[i]); 158 props[i].load(is); 159 is.close(); 160 } catch (IOException ioe) { 161 if (SanityManager.DEBUG) { 162 SanityManager.THROWASSERT("Error reading " + files[i], ioe); 163 } 164 } 165 } 166 queryDescriptions = props[0]; 167 queryDescriptions_net = props[1]; 168 } 169 170 176 179 185 public boolean allProceduresAreCallable() { 186 return true; 187 } 188 189 195 public boolean allTablesAreSelectable() { 196 return true; 197 } 198 199 204 public final String getURL() { 205 206 if (url == null) 207 return url; 208 int attributeStart = url.indexOf(';'); 209 if (attributeStart == -1) 210 return url; 211 else 212 return url.substring(0,attributeStart); 213 } 214 215 220 public String getUserName() { 221 return (getEmbedConnection().getTR().getUserName()); 222 } 223 224 229 public boolean isReadOnly() { 230 return getLanguageConnectionContext().getDatabase().isReadOnly(); 231 } 232 233 238 public boolean nullsAreSortedHigh() { 239 return true; 240 } 241 242 247 public boolean nullsAreSortedLow() { 248 return false; 249 } 250 251 256 public boolean nullsAreSortedAtStart() { 257 return false; 258 } 259 260 265 public boolean nullsAreSortedAtEnd() { 266 return false; 267 } 268 269 274 public String getDatabaseProductName() { 275 return Monitor.getMonitor().getEngineVersion().getProductName(); 276 } 277 278 283 public String getDatabaseProductVersion() { 284 ProductVersionHolder myPVH = Monitor.getMonitor().getEngineVersion(); 285 286 return myPVH.getVersionBuildString(true); 287 } 288 289 294 public String getDriverName() { 295 return "Apache Derby Embedded JDBC Driver"; 296 } 297 298 303 public String getDriverVersion() { 304 return getDatabaseProductVersion(); 305 } 306 307 312 public int getDriverMajorVersion() { 313 return getEmbedConnection().getLocalDriver().getMajorVersion(); 314 } 315 316 321 public int getDriverMinorVersion() { 322 return getEmbedConnection().getLocalDriver().getMinorVersion(); 323 } 324 325 330 public boolean usesLocalFiles() { 331 return true; 332 } 333 334 339 public boolean usesLocalFilePerTable() { 340 return true; 341 } 342 343 351 public boolean supportsMixedCaseIdentifiers() { 352 return false; 353 } 354 355 361 public boolean storesUpperCaseIdentifiers() { 362 return true; 363 } 364 365 371 public boolean storesLowerCaseIdentifiers() { 372 return false; 373 } 374 375 381 public boolean storesMixedCaseIdentifiers() { 382 return false; 383 } 384 385 393 public boolean supportsMixedCaseQuotedIdentifiers() { 394 return true; 395 } 396 397 403 public boolean storesUpperCaseQuotedIdentifiers() { 404 return false; 405 } 406 407 413 public boolean storesLowerCaseQuotedIdentifiers() { 414 return false; 415 } 416 417 423 public boolean storesMixedCaseQuotedIdentifiers() { 424 return true; 425 } 426 427 435 public String getIdentifierQuoteString() { 436 return "\""; 437 } 438 439 446 public String getSQLKeywords() { 447 return "ALIAS,BIGINT,BOOLEAN,CALL,CLASS,COPY,DB2J_DEBUG,EXECUTE,EXPLAIN,FILE,FILTER," 448 + "GETCURRENTCONNECTION,INDEX,INSTANCEOF,METHOD,NEW,OFF,PROPERTIES,PUBLICATION,RECOMPILE," 449 + "REFRESH,RENAME,RUNTIMESTATISTICS,STATEMENT,STATISTICS,TIMING,WAIT"; 450 } 451 452 457 public String getNumericFunctions() { 458 return "ABS,ACOS,ASIN,ATAN,CEILING,COS,DEGREES,EXP,FLOOR,LOG,LOG10,MOD,PI,RADIANS,SIN,SQRT,TAN"; 459 } 460 461 467 public String getStringFunctions() { 468 return "CONCAT,LENGTH,LCASE,LOCATE,LTRIM,RTRIM,SUBSTRING,UCASE"; 469 } 470 471 477 public String getSystemFunctions() { 478 return "USER"; 479 } 480 481 487 public String getTimeDateFunctions() { 488 return "CURDATE,CURTIME,HOUR,MINUTE,MONTH,SECOND,TIMESTAMPADD,TIMESTAMPDIFF,YEAR"; 489 } 490 491 500 public String getSearchStringEscape() { 501 return ""; 502 } 503 504 510 public String getExtraNameCharacters() { 511 return ""; 512 } 513 514 517 522 public boolean supportsAlterTableWithAddColumn() { 523 return true; 524 } 525 526 531 public boolean supportsAlterTableWithDropColumn() { 532 return true; 533 } 534 535 546 public boolean supportsColumnAliasing() { 547 return true; 548 } 549 550 557 public boolean nullPlusNonNullIsNull() { 558 return true; 559 } 560 561 566 public boolean supportsConvert() { 567 return true; 568 } 569 570 578 public boolean supportsConvert(int fromType, int toType) { 579 584 return false; 585 } 586 587 594 public boolean supportsTableCorrelationNames() { 595 return true; 596 } 597 598 604 public boolean supportsDifferentTableCorrelationNames() { 605 return true; 606 } 607 608 613 public boolean supportsExpressionsInOrderBy() { 614 return false; 615 } 616 617 622 public boolean supportsOrderByUnrelated() { 623 return false; 624 } 625 626 631 public boolean supportsGroupBy() { 632 return true; 633 } 634 635 640 public boolean supportsGroupByUnrelated() { 641 return true; 642 } 643 644 650 public boolean supportsGroupByBeyondSelect() { 651 return true; 652 } 653 654 661 public boolean supportsLikeEscapeClause() { 662 return true; 663 } 664 665 670 public boolean supportsMultipleResultSets() { 671 return true; 672 } 673 674 680 public boolean supportsMultipleTransactions() { 681 return true; 682 } 683 684 691 public boolean supportsNonNullableColumns() { 692 return true; 693 } 694 695 702 public boolean supportsMinimumSQLGrammar() { 703 return true; 704 } 705 706 711 public boolean supportsCoreSQLGrammar() { 712 return false; 713 } 714 715 720 public boolean supportsExtendedSQLGrammar() { 721 return false; 722 } 723 724 731 public boolean supportsANSI92EntryLevelSQL() { 732 return false; 733 } 734 735 741 public boolean supportsANSI92IntermediateSQL() { 742 return false; 743 } 744 745 751 public boolean supportsANSI92FullSQL() { 752 return false; 753 } 754 755 761 public boolean supportsIntegrityEnhancementFacility() { 762 return false; 763 } 764 765 771 public boolean supportsOuterJoins() { 772 return true; 773 } 774 775 781 public boolean supportsFullOuterJoins() { 782 return false; 783 } 784 785 792 public boolean supportsLimitedOuterJoins() { 793 return true; 794 } 795 796 802 public String getSchemaTerm() { 803 return "SCHEMA"; 804 } 805 806 812 public String getProcedureTerm() { 813 return "PROCEDURE"; 814 } 815 816 822 public String getCatalogTerm() { 823 return "CATALOG"; 824 } 825 826 833 public boolean isCatalogAtStart() { 834 return false; 835 } 836 837 843 public String getCatalogSeparator() { 844 return ""; 845 } 846 847 853 public boolean supportsSchemasInDataManipulation() { 854 return true; 855 } 856 857 863 public boolean supportsSchemasInProcedureCalls() { 864 return true; 865 } 866 867 873 public boolean supportsSchemasInTableDefinitions() { 874 return true; 875 } 876 877 882 public boolean supportsSchemasInIndexDefinitions() { 883 return true; 884 } 885 886 892 public boolean supportsSchemasInPrivilegeDefinitions() { 893 return true; 894 } 895 896 902 public boolean supportsCatalogsInDataManipulation() { 903 return false; 904 } 905 906 912 public boolean supportsCatalogsInProcedureCalls() { 913 return false; 914 } 915 916 922 public boolean supportsCatalogsInTableDefinitions() { 923 return false; 924 } 925 926 931 public boolean supportsCatalogsInIndexDefinitions() { 932 return false; 933 } 934 935 940 public boolean supportsCatalogsInPrivilegeDefinitions() { 941 return false; 942 } 943 944 945 950 public boolean supportsPositionedDelete() { 951 return true; 952 } 953 954 959 public boolean supportsPositionedUpdate() { 960 return true; 961 } 962 963 968 public boolean supportsSelectForUpdate() { 969 return true; 970 } 971 972 978 public boolean supportsStoredProcedures() { 979 return true; 980 } 981 982 989 public boolean supportsSubqueriesInComparisons() { 990 return true; 991 } 992 993 1000 public boolean supportsSubqueriesInExists() { 1001 return true; 1002 } 1003 1004 1011 public boolean supportsSubqueriesInIns() { 1012 return true; 1013 } 1014 1015 1022 public boolean supportsSubqueriesInQuantifieds() { 1023 return true; 1024 } 1025 1026 1033 public boolean supportsCorrelatedSubqueries() { 1034 return true; 1035 } 1036 1037 1042 public boolean supportsUnion() { 1043 return true; 1044 } 1045 1046 1051 public boolean supportsUnionAll() { 1052 return true; 1053 } 1054 1055 1060 public boolean supportsOpenCursorsAcrossCommit() { 1062 return false; 1063 } 1064 1065 1070 public boolean supportsOpenCursorsAcrossRollback() { 1071 return false; 1072 } 1073 1074 1079 public boolean supportsOpenStatementsAcrossCommit() { 1080 return true; 1081 } 1082 1083 1088 public boolean supportsOpenStatementsAcrossRollback() { 1089 return false; 1090 } 1091 1092 1093 1094 1100 1105 public int getMaxBinaryLiteralLength() { 1106 return 0; 1107 } 1108 1109 1114 public int getMaxCharLiteralLength() { 1115 return 0; 1116 } 1117 1118 1123 public int getMaxColumnNameLength() { 1124 return Limits.MAX_IDENTIFIER_LENGTH; 1125 } 1126 1127 1132 public int getMaxColumnsInGroupBy() { 1133 return 0; 1134 } 1135 1136 1141 public int getMaxColumnsInIndex() { 1142 return 0; 1143 } 1144 1145 1150 public int getMaxColumnsInOrderBy() { 1151 return 0; 1152 } 1153 1154 1161 public int getMaxColumnsInSelect() { 1162 return 0; 1163 } 1164 1165 1170 public int getMaxColumnsInTable() { 1171 return 0; 1172 } 1173 1174 1179 public int getMaxConnections() { 1180 return 0; 1181 } 1182 1183 1188 public int getMaxCursorNameLength() { 1189 return Limits.MAX_IDENTIFIER_LENGTH; 1190 } 1191 1192 1197 public int getMaxIndexLength() { 1198 return 0; 1199 } 1200 1201 1206 public int getMaxSchemaNameLength() { 1207 return Limits.MAX_IDENTIFIER_LENGTH; 1208 } 1209 1210 1215 public int getMaxProcedureNameLength() { 1216 return Limits.MAX_IDENTIFIER_LENGTH; 1217 } 1218 1219 1224 public int getMaxCatalogNameLength() { 1225 return 0; 1226 } 1227 1228 1233 public int getMaxRowSize() { 1234 return 0; 1235 } 1236 1237 1243 public boolean doesMaxRowSizeIncludeBlobs() { 1244 return true; 1245 } 1246 1247 1252 public int getMaxStatementLength() { 1253 return 0; 1254 } 1255 1256 1262 public int getMaxStatements() { 1263 return 0; 1264 } 1265 1266 1271 public int getMaxTableNameLength() { 1272 return Limits.MAX_IDENTIFIER_LENGTH; 1273 } 1274 1275 1280 public int getMaxTablesInSelect() { 1281 return 0; 1282 } 1283 1284 1289 public int getMaxUserNameLength() { 1290 return Limits.DB2_MAX_USERID_LENGTH; 1291 } 1292 1293 1295 1302 public int getDefaultTransactionIsolation() { 1303 return java.sql.Connection.TRANSACTION_READ_COMMITTED; 1304 } 1305 1306 1312 public boolean supportsTransactions() { 1313 return true; 1314 } 1315 1316 1327 public boolean supportsTransactionIsolationLevel(int level) 1328 { 1329 1344 1350 1351 return (level == Connection.TRANSACTION_SERIALIZABLE || 1352 level == Connection.TRANSACTION_REPEATABLE_READ || 1353 level == Connection.TRANSACTION_READ_COMMITTED || 1354 level == Connection.TRANSACTION_READ_UNCOMMITTED); 1355 } 1356 1357 1363 public boolean supportsDataDefinitionAndDataManipulationTransactions() { 1364 return true; 1365 } 1366 1372 public boolean supportsDataManipulationTransactionsOnly() 1373 { 1374 return false; 1375 } 1376 1383 public boolean dataDefinitionCausesTransactionCommit() { 1384 return false; 1385 } 1386 1392 public boolean dataDefinitionIgnoredInTransactions(){ 1393 return false; 1394 } 1395 1396 1397 1433 public ResultSet getProcedures(String catalog, String schemaPattern, 1434 String procedureNamePattern) throws SQLException { 1435 1436 return doGetProcs(catalog, schemaPattern, 1440 procedureNamePattern, "getProcedures40"); 1441 } 1442 1443 1448 public ResultSet getProceduresForODBC(String catalog, String schemaPattern, 1449 String procedureNamePattern) throws SQLException { 1450 1451 return doGetProcs(catalog, schemaPattern, 1454 procedureNamePattern, "odbc_getProcedures"); 1455 } 1456 1457 1477 public ResultSet getFunctions(java.lang.String catalog, 1478 java.lang.String schemaPattern, 1479 java.lang.String functionNamePattern) 1480 throws SQLException 1481 { 1482 return doGetProcs(catalog, schemaPattern, 1483 functionNamePattern, "getFunctions"); 1484 } 1485 1486 1494 private ResultSet doGetProcs(String catalog, String schemaPattern, 1495 String procedureNamePattern, String queryName) 1496 throws SQLException { 1497 1498 PreparedStatement s = getPreparedQuery(queryName); 1499 s.setString(1, swapNull(catalog)); 1500 s.setString(2, swapNull(schemaPattern)); 1501 s.setString(3, swapNull(procedureNamePattern)); 1502 return s.executeQuery(); 1503 } 1504 1505 1568 public ResultSet getProcedureColumns(String catalog, 1569 String schemaPattern, 1570 String procedureNamePattern, 1571 String columnNamePattern) throws SQLException { 1572 1573 return doGetProcCols(catalog, schemaPattern, 1577 procedureNamePattern, columnNamePattern, 1578 "getProcedureColumns40"); 1579 } 1580 1581 1586 public ResultSet getProcedureColumnsForODBC(String catalog, 1587 String schemaPattern, String procedureNamePattern, 1588 String columnNamePattern) throws SQLException { 1589 1590 return doGetProcCols(catalog, schemaPattern, 1593 procedureNamePattern, columnNamePattern, 1594 "odbc_getProcedureColumns"); 1595 } 1596 1597 1619 public ResultSet getFunctionColumns(String catalog, 1620 String schemaPattern, 1621 String functionNamePattern, 1622 String parameterNamePattern) 1623 throws SQLException { 1624 PreparedStatement s = getPreparedQuery("getFunctionColumns"); 1625 1626 s.setString(1, swapNull(schemaPattern)); 1633 s.setString(2, swapNull(functionNamePattern)); 1634 s.setString(3, swapNull(parameterNamePattern)); 1635 s.setString(4, swapNull(parameterNamePattern)); 1636 return s.executeQuery(); 1637 } 1638 1639 1647 private ResultSet doGetProcCols(String catalog, String schemaPattern, 1648 String procedureNamePattern, String columnNamePattern, 1649 String queryName) throws SQLException { 1650 1651 PreparedStatement s = getPreparedQuery(queryName); 1652 s.setString(1, swapNull(schemaPattern)); 1656 s.setString(2, swapNull(procedureNamePattern)); 1657 s.setString(3, swapNull(columnNamePattern)); 1658 return s.executeQuery(); 1659 } 1660 1661 1705 public ResultSet getTables(String catalog, String schemaPattern, 1706 String tableNamePattern, String types[]) throws SQLException { 1707 synchronized (getConnectionSynchronization()) { 1708 setupContextStack(); 1709 ResultSet rs = null; 1710 try { 1711 1712 String queryText = 1713 getQueryDescriptions(false).getProperty("getTables"); 1714 1715 1724 StringBuffer whereClauseTail = new StringBuffer (queryText); 1725 1726 if (types != null && types.length >= 1) { 1727 whereClauseTail.append(" AND TABLETYPE IN ('"); 1728 whereClauseTail.append(types[0].substring(0, 1)); 1729 1730 for (int i=1; i<types.length; i++) { 1731 whereClauseTail.append("','"); 1732 whereClauseTail.append(types[i].substring(0, 1)); 1733 } 1734 whereClauseTail.append("')"); 1735 } 1736 whereClauseTail.append( 1738 " ORDER BY TABLE_TYPE, TABLE_SCHEM, TABLE_NAME"); 1739 1740 PreparedStatement s = 1741 getEmbedConnection().prepareMetaDataStatement(whereClauseTail.toString()); 1742 1743 s.setString(1, swapNull(catalog)); 1744 s.setString(2, swapNull(schemaPattern)); 1745 s.setString(3, swapNull(tableNamePattern)); 1746 1747 rs = s.executeQuery(); 1748 } catch (Throwable t) { 1749 throw handleException(t); 1750 } finally { 1751 restoreContextStack(); 1752 } 1753 1754 return rs; 1755 } 1756 } 1757 1758 1772 public ResultSet getSchemas() throws SQLException { 1773 return getSchemas(null, null); 1774 } 1775 1776 1789 public ResultSet getCatalogs() throws SQLException { 1790 return getSimpleQuery("getCatalogs"); 1791 } 1792 1793 1808 public ResultSet getTableTypes() throws SQLException { 1809 return getSimpleQuery("getTableTypes"); 1810 } 1811 1812 1883 public ResultSet getColumns(String catalog, String schemaPattern, 1884 String tableNamePattern, String columnNamePattern) 1885 throws SQLException { 1886 1887 return doGetCols(catalog, schemaPattern, tableNamePattern, 1888 columnNamePattern, "getColumns"); 1889 } 1890 1891 1896 public ResultSet getColumnsForODBC(String catalog, String schemaPattern, 1897 String tableNamePattern, String columnNamePattern) 1898 throws SQLException { 1899 1900 return doGetCols(catalog, schemaPattern, tableNamePattern, 1901 columnNamePattern, "odbc_getColumns"); 1902 } 1903 1904 1911 private ResultSet doGetCols(String catalog, String schemaPattern, 1912 String tableNamePattern, String columnNamePattern, 1913 String queryName) throws SQLException { 1914 1915 PreparedStatement s = getPreparedQuery(queryName); 1916 s.setString(1, swapNull(catalog)); 1917 s.setString(2, swapNull(schemaPattern)); 1918 s.setString(3, swapNull(tableNamePattern)); 1919 s.setString(4, swapNull(columnNamePattern)); 1920 return s.executeQuery(); 1921 } 1922 1923 1952 public ResultSet getColumnPrivileges(String catalog, String schema, 1953 String table, String columnNamePattern) throws SQLException { 1954 PreparedStatement s = getPreparedQuery("getColumnPrivileges"); 1955 s.setString(1, swapNull(catalog)); 1956 s.setString(2, swapNull(schema)); 1957 s.setString(3, swapNull(table)); 1958 s.setString(4, swapNull(columnNamePattern)); 1959 return s.executeQuery(); 1960 } 1961 1962 1995 public ResultSet getTablePrivileges(String catalog, String schemaPattern, 1996 String tableNamePattern) throws SQLException { 1997 PreparedStatement s = getPreparedQuery("getTablePrivileges"); 1998 s.setString(1, swapNull(catalog)); 1999 s.setString(2, swapNull(schemaPattern)); 2000 s.setString(3, swapNull(tableNamePattern)); 2001 return s.executeQuery(); 2002 } 2003 2004 2040 public ResultSet getBestRowIdentifier 2041 ( 2042 String catalogPattern, 2043 String schemaPattern, 2044 String tablePattern, 2045 int scope, 2046 boolean nullable 2047 ) throws SQLException 2048 { 2049 return doGetBestRowId(catalogPattern, schemaPattern, tablePattern, 2050 scope, nullable, ""); 2051 } 2052 2053 2059 public ResultSet getBestRowIdentifierForODBC(String catalogPattern, 2060 String schemaPattern, String tablePattern, int scope, 2061 boolean nullable) throws SQLException { 2062 2063 return doGetBestRowId(catalogPattern, schemaPattern, tablePattern, 2064 scope, nullable, "odbc_"); 2065 } 2066 2067 2076 private ResultSet doGetBestRowId(String catalogPattern, 2077 String schemaPattern, String tablePattern, int scope, 2078 boolean nullable, String queryPrefix) throws SQLException { 2079 2080 int nullableInIntForm = 0; 2081 if (nullable) 2082 nullableInIntForm = 1; 2083 2084 if (catalogPattern == null) 2085 { 2086 catalogPattern = "%"; 2087 } 2088 if (schemaPattern == null) 2089 { 2090 schemaPattern = "%"; 2091 } 2092 if (tablePattern == null) 2093 { 2094 tablePattern = "%"; 2095 } 2096 2097 PreparedStatement ps; 2098 boolean done; 2099 2100 if (scope < 0 || scope > 2) { 2102 ps = getPreparedQuery("getBestRowIdentifierEmpty"); 2103 return ps.executeQuery(); 2104 } 2105 2106 ps = getPreparedQuery("getBestRowIdentifierPrimaryKey"); 2108 ps.setString(1,catalogPattern); 2109 ps.setString(2,schemaPattern); 2110 ps.setString(3,tablePattern); 2111 2112 ResultSet rs = ps.executeQuery(); 2113 done = rs.next(); 2114 String constraintId = ""; 2115 if (done) { 2116 constraintId = rs.getString(1); 2117 } 2118 2119 rs.close(); 2120 ps.close(); 2121 2122 if (done) 2123 { 2124 ps = getPreparedQuery(queryPrefix + "getBestRowIdentifierPrimaryKeyColumns"); 2128 ps.setString(1,constraintId); 2129 ps.setString(2,constraintId); 2130 return ps.executeQuery(); 2133 } 2134 2135 ps = getPreparedQuery("getBestRowIdentifierUniqueConstraint"); 2137 ps.setString(1,catalogPattern); 2138 ps.setString(2,schemaPattern); 2139 ps.setString(3,tablePattern); 2140 2141 rs = ps.executeQuery(); 2142 done = rs.next(); 2143 if (done) { 2144 constraintId = rs.getString(1); 2145 } 2146 2151 rs.close(); 2152 ps.close(); 2153 if (done) 2154 { 2155 ps = getPreparedQuery(queryPrefix + "getBestRowIdentifierUniqueKeyColumns"); 2157 ps.setString(1,constraintId); 2158 ps.setString(2,constraintId); 2159 ps.setInt(3,nullableInIntForm); 2160 return ps.executeQuery(); 2161 } 2162 2163 2164 ps = getPreparedQuery("getBestRowIdentifierUniqueIndex"); 2167 ps.setString(1,catalogPattern); 2168 ps.setString(2,schemaPattern); 2169 ps.setString(3,tablePattern); 2170 2171 rs = ps.executeQuery(); 2172 done = rs.next(); 2173 long indexNum = 0; 2174 if (done) { 2175 indexNum = rs.getLong(1); 2176 } 2177 2182 rs.close(); 2183 ps.close(); 2184 if (done) { 2185 ps = getPreparedQuery(queryPrefix + "getBestRowIdentifierUniqueIndexColumns"); 2187 ps.setLong(1,indexNum); 2188 ps.setInt(2,nullableInIntForm); 2189 return ps.executeQuery(); 2190 } 2191 2192 ps = getPreparedQuery(queryPrefix + "getBestRowIdentifierAllColumns"); 2195 ps.setString(1,catalogPattern); 2196 ps.setString(2,schemaPattern); 2197 ps.setString(3,tablePattern); 2198 ps.setInt(4,scope); 2199 ps.setInt(5,nullableInIntForm); 2200 return ps.executeQuery(); 2201 } 2202 2203 2233 public ResultSet getVersionColumns(String catalog, String schema, 2234 String table) throws SQLException { 2235 return doGetVersionCols(catalog, schema, table, "getVersionColumns"); 2236 } 2237 2238 2244 public ResultSet getVersionColumnsForODBC(String catalog, String schema, 2245 String table) throws SQLException { 2246 return doGetVersionCols(catalog, schema, table, "odbc_getVersionColumns"); 2247 } 2248 2249 2257 private ResultSet doGetVersionCols(String catalog, String schema, 2258 String table, String queryName) throws SQLException { 2259 2260 PreparedStatement s = getPreparedQuery(queryName); 2261 s.setString(1, swapNull(catalog)); 2262 s.setString(2, swapNull(schema)); 2263 s.setString(3, swapNull(table)); 2264 return s.executeQuery(); 2265 } 2266 2267 2280 private boolean notInSoftUpgradeMode() 2281 throws SQLException { 2282 if ( getEmbedConnection().isClosed()) 2283 throw Util.noCurrentConnection(); 2284 2285 boolean notInSoftUpgradeMode; 2286 try { 2287 notInSoftUpgradeMode = 2288 getLanguageConnectionContext().getDataDictionary().checkVersion( 2289 DataDictionary.DD_VERSION_CURRENT,null); 2290 } catch (Throwable t) { 2291 throw handleException(t); 2292 } 2293 return notInSoftUpgradeMode; 2294 } 2295 2296 2297 2319 public ResultSet getPrimaryKeys(String catalog, String schema, 2320 String table) throws SQLException { 2321 PreparedStatement s = getPreparedQuery("getPrimaryKeys"); 2322 s.setString(1, swapNull(catalog)); 2323 s.setString(2, swapNull(schema)); 2324 s.setString(3, swapNull(table)); 2325 return s.executeQuery(); 2326 } 2327 2328 2396 public ResultSet getImportedKeys(String catalog, String schema, 2397 String table) throws SQLException { 2398 PreparedStatement s = getPreparedQuery("getImportedKeys"); 2399 s.setString(1, swapNull(catalog)); 2400 s.setString(2, swapNull(schema)); 2401 s.setString(3, swapNull(table)); 2402 return s.executeQuery(); 2403 } 2404 2405 2406 2474 public ResultSet getExportedKeys(String catalog, String schema, 2475 String table) throws SQLException { 2476 PreparedStatement s = getPreparedQuery("getCrossReference"); 2477 s.setString(1, swapNull(catalog)); 2478 s.setString(2, swapNull(schema)); 2479 s.setString(3, swapNull(table)); 2480 s.setString(4, swapNull(null)); 2481 s.setString(5, swapNull(null)); 2482 s.setString(6, swapNull(null)); 2483 return s.executeQuery(); 2484 } 2485 2486 2562 public ResultSet getCrossReference( 2563 String primaryCatalog, String primarySchema, String primaryTable, 2564 String foreignCatalog, String foreignSchema, String foreignTable 2565 ) throws SQLException { 2566 PreparedStatement s = getPreparedQuery("getCrossReference"); 2567 s.setString(1, swapNull(primaryCatalog)); 2568 s.setString(2, swapNull(primarySchema)); 2569 s.setString(3, swapNull(primaryTable)); 2570 s.setString(4, swapNull(foreignCatalog)); 2571 s.setString(5, swapNull(foreignSchema)); 2572 s.setString(6, swapNull(foreignTable)); 2573 return s.executeQuery(); 2574 } 2575 2576 2622 public ResultSet getTypeInfo() throws SQLException { 2623 return getSimpleQuery("getTypeInfo"); 2624 } 2625 2626 2633 public ResultSet getTypeInfoForODBC() throws SQLException { 2634 return getSimpleQuery("odbc_getTypeInfo"); 2635 } 2636 2637 2689 public ResultSet getIndexInfo(String catalog, String schema, String table, 2690 boolean unique, boolean approximate) 2691 throws SQLException { 2692 return doGetIndexInfo(catalog, schema, table, unique, approximate, "getIndexInfo"); 2693 } 2694 2695 2701 public ResultSet getIndexInfoForODBC(String catalog, String schema, String table, 2702 boolean unique, boolean approximate) throws SQLException 2703 { 2704 return doGetIndexInfo(catalog, schema, table, unique, approximate, "odbc_getIndexInfo"); 2705 } 2706 2707 2715 private ResultSet doGetIndexInfo(String catalog, String schema, String table, 2716 boolean unique, boolean approximate, String queryName) 2717 throws SQLException { 2718 2719 int approximateInInt = 0; 2720 if (approximate) approximateInInt = 1; 2721 PreparedStatement s = getPreparedQuery(queryName); 2722 s.setString(1, swapNull(catalog)); 2723 s.setString(2, swapNull(schema)); 2724 s.setString(3, swapNull(table)); 2725 s.setBoolean(4, unique); 2726 s.setInt(5, approximateInInt); 2727 return s.executeQuery(); 2728 } 2729 2730 2736 2745 public boolean supportsResultSetType(int type) { 2746 if ((type == JDBC20Translation.TYPE_FORWARD_ONLY) || 2747 (type == JDBC20Translation.TYPE_SCROLL_INSENSITIVE)) { 2748 return true; 2749 } 2750 return false; 2752 } 2753 2754 2765 public boolean supportsResultSetConcurrency(int type, int concurrency) { 2766 if (type == JDBC20Translation.TYPE_SCROLL_SENSITIVE) { 2767 return false; 2769 } else { 2770 return true; 2775 } 2776 } 2777 2778 2786 public boolean ownUpdatesAreVisible(int type) { 2787 if (type == JDBC20Translation.TYPE_SCROLL_INSENSITIVE) { 2788 return true; 2789 } else { 2790 return false; 2791 } 2792 } 2793 2794 2802 public boolean ownDeletesAreVisible(int type) { 2803 if (type == JDBC20Translation.TYPE_SCROLL_INSENSITIVE) { 2804 return true; 2805 } else { 2806 return false; 2807 } 2808 } 2809 2810 2818 public boolean ownInsertsAreVisible(int type) { 2819 return false; 2820 } 2821 2822 2826 2834 public boolean othersUpdatesAreVisible(int type) { 2835 if (type == JDBC20Translation.TYPE_FORWARD_ONLY) 2836 return true; 2837 return false; 2838 } 2839 2840 2848 public boolean othersDeletesAreVisible(int type) { 2849 if (type == JDBC20Translation.TYPE_FORWARD_ONLY) 2850 return true; 2851 return false; 2852 } 2853 2854 2862 public boolean othersInsertsAreVisible(int type) { 2863 if (type == JDBC20Translation.TYPE_FORWARD_ONLY) 2864 return true; 2865 return false; 2866 } 2867 2868 2877 public boolean updatesAreDetected(int type) { 2878 if (type == JDBC20Translation.TYPE_SCROLL_INSENSITIVE) { 2879 return true; 2880 } else { 2881 return false; 2885 } 2886 } 2887 2888 2898 public boolean deletesAreDetected(int type) { 2899 if (type == JDBC20Translation.TYPE_SCROLL_INSENSITIVE) { 2900 return true; 2901 } else { 2902 return false; 2906 } 2907 } 2908 2909 2918 public boolean insertsAreDetected(int type) { 2919 return false; 2920 } 2921 2922 2928 public boolean supportsBatchUpdates() { 2929 return true; 2930 } 2931 2932 2976 public ResultSet getUDTs(String catalog, String schemaPattern, 2977 String typeNamePattern, int[] types) 2978 throws SQLException { 2979 synchronized (getConnectionSynchronization()) { 2984 setupContextStack(); 2985 ResultSet rs = null; 2986 int getClassTypes = 0; 2987 try { 2988 String queryText = getQueryDescriptions(false).getProperty("getUDTs"); 2989 2990 if (types != null && types.length >= 1) { 2991 for (int i=0; i<types.length; i++){ 2992 if (types[i] == java.sql.Types.JAVA_OBJECT) 2993 getClassTypes = 1; 2994 } 2995 } else 2996 getClassTypes = 1; 2997 2998 PreparedStatement s = 2999 getEmbedConnection().prepareMetaDataStatement(queryText); 3000 3001 s.setInt(1, java.sql.Types.JAVA_OBJECT); 3002 s.setString(2, catalog); 3003 s.setString(3, schemaPattern); 3004 s.setString(4, swapNull(typeNamePattern)); 3005 s.setInt(5, getClassTypes); 3006 3007 rs = s.executeQuery(); 3008 } finally { 3009 restoreContextStack(); 3010 } 3011 return rs; 3012 } 3013 } 3014 3015 3021 public Connection getConnection() { 3022 return getEmbedConnection().getApplicationConnection(); 3023 } 3024 3025 3032 3033 3039 3046 public boolean supportsStatementPooling() 3047 { 3048 return false; 3049 } 3050 3051 3058 public boolean supportsSavepoints() 3059 { 3060 return true; 3061 } 3062 3063 3070 public boolean supportsNamedParameters() 3071 { 3072 return false; 3073 } 3074 3075 3084 public boolean supportsMultipleOpenResults() 3085 { 3086 return true; 3087 } 3088 3089 3098 public boolean supportsGetGeneratedKeys() 3099 { 3100 3106 return false; 3107 } 3108 3109 3119 public boolean supportsResultSetHoldability(int holdability) 3120 { 3121 return true; 3122 } 3123 3124 3131 public int getResultSetHoldability() 3132 { 3133 return JDBC30Translation.HOLD_CURSORS_OVER_COMMIT; 3134 } 3135 3136 3143 public int getDatabaseMajorVersion() 3144 { 3145 ProductVersionHolder pvh = Monitor.getMonitor().getEngineVersion(); 3146 if (pvh == null) 3147 { 3148 return -1; 3149 } 3150 return pvh.getMajorVersion(); 3151 } 3152 3153 3160 public int getDatabaseMinorVersion() 3161 { 3162 ProductVersionHolder pvh = Monitor.getMonitor().getEngineVersion(); 3163 if (pvh == null) 3164 { 3165 return -1; 3166 } 3167 return pvh.getMinorVersion(); 3168 } 3169 3170 3177 public int getJDBCMajorVersion() 3178 { 3179 return 3; 3180 } 3181 3182 3189 public int getJDBCMinorVersion() 3190 { 3191 return 0; 3192 } 3193 3194 3202 public int getSQLStateType() 3203 { 3204 return JDBC30Translation.SQL_STATE_SQL99; 3205 } 3206 3207 3217 public boolean locatorsUpdateCopy() 3218 throws SQLException 3219 { 3220 return false; 3221 } 3222 3223 3237 public ResultSet getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) 3238 throws SQLException 3239 { 3240 return getSimpleQuery("getSuperTypes"); 3241 } 3242 3243 3256 public ResultSet getSuperTables(String catalog, String schemaPattern, String typeNamePattern) 3257 throws SQLException 3258 { 3259 return getSimpleQuery("getSuperTables"); 3260 } 3261 3262 3280 public ResultSet getAttributes(String catalog, String schemaPattern, 3281 String typeNamePattern, String attributeNamePattern) 3282 throws SQLException 3283 { 3284 return getSimpleQuery("getAttributes"); 3285 } 3286 3287 3293 3314 public ResultSet getClientInfoProperties() throws SQLException { 3315 return getSimpleQuery("getClientInfoProperties"); 3316 } 3317 3318 3340 public ResultSet getSchemas(String catalog, String schemaPattern) 3341 throws SQLException 3342 { 3343 PreparedStatement s = getPreparedQuery("getSchemas"); 3344 s.setString(1, swapNull(catalog)); 3345 s.setString(2, swapNull(schemaPattern)); 3346 return s.executeQuery(); 3347 } 3348 3349 3355 3362 public ResultSet getClientCachedMetaData() throws SQLException { 3363 return getSimpleQuery("METADATA", true); 3364 } 3365 3366 3369 3370 3382 private ResultSet getSimpleQuery(String nameKey, boolean net) 3383 throws SQLException 3384 { 3385 PreparedStatement ps = getPreparedQuery(nameKey, net); 3386 if (ps == null) 3387 return null; 3388 3389 return ps.executeQuery(); 3390 } 3391 3392 3400 protected ResultSet getSimpleQuery(String nameKey) throws SQLException { 3401 return getSimpleQuery(nameKey, false); 3402 } 3403 3404 3413 private PreparedStatement getPreparedQueryUsingSystemTables(String nameKey, 3414 boolean net) 3415 throws SQLException 3416 { 3417 synchronized (getConnectionSynchronization()) 3418 { 3419 setupContextStack(); 3420 PreparedStatement ps = null; 3421 3422 try 3423 { 3424 String queryText = 3425 getQueryDescriptions(net).getProperty(nameKey); 3426 if (queryText == null) 3427 { 3428 throw Util.notImplemented(nameKey); 3429 } 3430 3431 ps = prepareSPS(nameKey, queryText, net); 3432 } 3433 3434 catch (Throwable t) 3435 { 3436 throw handleException(t); 3437 } 3438 3439 finally 3440 { 3441 restoreContextStack(); 3442 } 3443 return ps; 3444 } 3445 } 3446 3447 3471 private PreparedStatement getPreparedQuery(String queryName, 3472 boolean net) 3473 throws SQLException { 3474 PreparedStatement s; 3475 if (notInSoftUpgradeMode()) 3479 s = getPreparedQueryUsingSystemTables(queryName, net); 3480 else { 3481 try { 3482 String queryText = getQueryFromDescription(queryName, net); 3486 s = getEmbedConnection().prepareMetaDataStatement(queryText); 3487 } catch (Throwable t) { 3488 throw handleException(t); 3489 } 3490 } 3491 return s; 3492 } 3493 3494 3501 protected PreparedStatement getPreparedQuery(String queryName) 3502 throws SQLException 3503 { 3504 return getPreparedQuery(queryName, false); 3505 } 3506 3507 3529 private String getQueryFromDescription(String queryName, boolean net) 3530 throws StandardException 3531 { 3532 DataDictionary dd = getLanguageConnectionContext().getDataDictionary(); 3533 3534 if (!dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_2, null)) 3538 { 3539 if (queryName.equals("getColumnPrivileges")) 3540 queryName = "getColumnPrivileges_10_1"; 3541 3542 if (queryName.equals("getTablePrivileges")) 3543 queryName = "getTablePrivileges_10_1"; 3544 } 3545 3546 return getQueryDescriptions(net).getProperty(queryName); 3547 } 3548 3549 3555 private PreparedStatement prepareSPS(String spsName, 3556 String spsText, 3557 boolean net) 3558 throws StandardException, SQLException 3559 { 3560 3561 LanguageConnectionContext lcc = getLanguageConnectionContext(); 3562 3563 3567 lcc.beginNestedTransaction(true); 3568 3569 DataDictionary dd = getLanguageConnectionContext().getDataDictionary(); 3570 SPSDescriptor spsd = dd.getSPSDescriptor( 3571 spsName, 3572 net ? dd.getSysIBMSchemaDescriptor() : 3573 dd.getSystemSchemaDescriptor()); 3574 lcc.commitNestedTransaction(); 3575 3576 if (spsd == null) 3577 { 3578 throw Util.notImplemented(spsName); 3579 } 3580 3581 3585 3591 String queryText = 3592 "EXECUTE STATEMENT " + (net ? "SYSIBM" : "SYS") + 3593 ".\"" + spsName + "\""; 3594 return getEmbedConnection().prepareMetaDataStatement(queryText); 3595 3596 } 3597 3598 static final protected String swapNull(String s) { 3599 return (s == null ? "%" : s); 3600 } 3601 3602 3609 private GenericConstantActionFactory getGenericConstantActionFactory() 3610 throws StandardException 3611 { 3612 if ( constantActionFactory == null ) 3613 { 3614 GenericExecutionFactory execFactory = (GenericExecutionFactory) 3615 getLanguageConnectionContext().getLanguageConnectionFactory().getExecutionFactory(); 3616 constantActionFactory = execFactory.getConstantActionFactory(); 3617 } 3618 3619 return constantActionFactory; 3620 } 3621 3622 3628 private LanguageConnectionContext getLanguageConnectionContext() 3629 { 3630 return getEmbedConnection().getLanguageConnection(); 3631 } 3632 3633 3636 3637 3642 private void loadQueryDescriptions() { 3643 java.security.AccessController.doPrivileged(this); 3644 } 3645 3646 3651 public final Object run() { 3652 PBloadQueryDescriptions(); 3654 return null; 3655 } 3656 3657} 3658 | Popular Tags |