1 30 31 32 package org.hsqldb.jdbc; 33 34 import java.sql.Connection ; 35 import java.sql.DatabaseMetaData ; 36 import java.sql.ResultSet ; 37 import java.sql.SQLException ; 38 39 import org.hsqldb.Column; 40 import org.hsqldb.Library; 41 import org.hsqldb.Trace; 42 import org.hsqldb.lib.StringUtil; 43 import org.hsqldb.persist.HsqlDatabaseProperties; 44 45 59 279 public class jdbcDatabaseMetaData implements DatabaseMetaData { 280 281 282 static final Integer INT_COLUMNS_NO_NULLS = new Integer (columnNoNulls); 283 284 288 292 private jdbcConnection connection; 293 294 297 private boolean useSchemaDefault; 298 299 305 private static final String BRI_SESSION_SCOPE_IN_LIST = "(" 306 + bestRowSession + ")"; 307 308 314 private static final String BRI_TEMPORARY_SCOPE_IN_LIST = "(" 315 + bestRowTemporary + "," + bestRowTransaction + "," + bestRowSession 316 + ")"; 317 318 324 private static final String BRI_TRANSACTION_SCOPE_IN_LIST = "(" 325 + bestRowTransaction + "," + bestRowSession + ")"; 326 327 335 private static final String selstar = "SELECT * FROM INFORMATION_SCHEMA."; 336 337 350 private static final String whereTrue = " WHERE 1=1"; 351 352 355 375 public boolean allProceduresAreCallable() throws SQLException { 376 return true; 377 } 378 379 400 public boolean allTablesAreSelectable() throws SQLException { 401 return true; 402 } 403 404 412 public String getURL() throws SQLException { 413 return connection.getURL(); 414 } 415 416 423 public String getUserName() throws SQLException { 424 425 ResultSet rs = execute("CALL USER()"); 426 427 rs.next(); 428 429 String result = rs.getString(1); 430 431 rs.close(); 432 433 return result; 434 } 435 436 452 public boolean isReadOnly() throws SQLException { 453 454 ResultSet rs = 455 execute("CALL \"org.hsqldb.Library.isReadOnlyDatabase\"()"); 456 457 rs.next(); 458 459 boolean result = rs.getBoolean(1); 460 461 rs.close(); 462 463 return result; 464 } 465 466 487 public boolean nullsAreSortedHigh() throws SQLException { 488 return false; 489 } 490 491 512 public boolean nullsAreSortedLow() throws SQLException { 513 return true; 514 } 515 516 532 public boolean nullsAreSortedAtStart() throws SQLException { 533 return false; 534 } 535 536 552 public boolean nullsAreSortedAtEnd() throws SQLException { 553 return false; 554 } 555 556 571 public String getDatabaseProductName() throws SQLException { 572 573 ResultSet rs = 574 execute("call \"org.hsqldb.Library.getDatabaseProductName\"()"); 575 576 rs.next(); 577 578 String result = rs.getString(1); 579 580 rs.close(); 581 582 return result; 583 } 584 585 600 public String getDatabaseProductVersion() throws SQLException { 601 602 ResultSet rs = execute( 603 "call \"org.hsqldb.Library.getDatabaseProductVersion\"()"); 604 605 rs.next(); 606 607 String result = rs.getString(1); 608 609 rs.close(); 610 611 return result; 612 } 613 614 620 public String getDriverName() throws SQLException { 621 return HsqlDatabaseProperties.PRODUCT_NAME + " Driver"; 622 } 623 624 630 public String getDriverVersion() throws SQLException { 631 return HsqlDatabaseProperties.THIS_VERSION; 632 } 633 634 639 public int getDriverMajorVersion() { 640 return HsqlDatabaseProperties.MAJOR; 641 } 642 643 648 public int getDriverMinorVersion() { 649 return HsqlDatabaseProperties.MINOR; 650 } 651 652 666 public boolean usesLocalFiles() throws SQLException { 667 return false; 668 } 669 670 685 public boolean usesLocalFilePerTable() throws SQLException { 686 return false; 687 } 688 689 707 public boolean supportsMixedCaseIdentifiers() throws SQLException { 708 return false; 709 } 710 711 729 public boolean storesUpperCaseIdentifiers() throws SQLException { 730 return true; 731 } 732 733 751 public boolean storesLowerCaseIdentifiers() throws SQLException { 752 return false; 753 } 754 755 773 public boolean storesMixedCaseIdentifiers() throws SQLException { 774 return false; 775 } 776 777 795 public boolean supportsMixedCaseQuotedIdentifiers() throws SQLException { 796 return true; 797 } 798 799 817 public boolean storesUpperCaseQuotedIdentifiers() throws SQLException { 818 return false; 819 } 820 821 837 public boolean storesLowerCaseQuotedIdentifiers() throws SQLException { 838 return false; 839 } 840 841 859 public boolean storesMixedCaseQuotedIdentifiers() throws SQLException { 860 return false; 861 } 862 863 878 public String getIdentifierQuoteString() throws SQLException { 879 return "\""; 880 } 881 882 885 904 public String getSQLKeywords() throws SQLException { 905 906 return "BEFORE,BIGINT,BINARY,CACHED,DATETIME," 907 + "LIMIT,LONGVARBINARY,LONGVARCHAR,OBJECT,OTHER,SAVEPOINT," 908 + "TEMP,TEXT,TOP,TRIGGER,TINYINT,VARBINARY,VARCHAR_IGNORECASE"; 909 } 910 911 918 public String getNumericFunctions() throws SQLException { 919 return StringUtil.getList(Library.sNumeric, ",", ""); 920 } 921 922 929 public String getStringFunctions() throws SQLException { 930 return StringUtil.getList(Library.sString, ",", ""); 931 } 932 933 940 public String getSystemFunctions() throws SQLException { 941 return StringUtil.getList(Library.sSystem, ",", ""); 942 } 943 944 950 public String getTimeDateFunctions() throws SQLException { 951 return StringUtil.getList(Library.sTimeDate, ",", ""); 952 } 953 954 976 public String getSearchStringEscape() throws SQLException { 977 return "\\"; 978 } 979 980 997 public String getExtraNameCharacters() throws SQLException { 998 return ""; 999 } 1000 1001 1004 1020 public boolean supportsAlterTableWithAddColumn() throws SQLException { 1021 return true; 1022 } 1023 1024 1040 public boolean supportsAlterTableWithDropColumn() throws SQLException { 1041 return true; 1042 } 1043 1044 1064 public boolean supportsColumnAliasing() throws SQLException { 1065 return true; 1066 } 1067 1068 1086 public boolean nullPlusNonNullIsNull() throws SQLException { 1087 return true; 1088 } 1089 1090 1107 public boolean supportsConvert() throws SQLException { 1108 return true; 1109 } 1110 1111 1118 1141 public boolean supportsConvert(int fromType, 1142 int toType) throws SQLException { 1143 return true; 1144 } 1145 1146 1162 public boolean supportsTableCorrelationNames() throws SQLException { 1163 return true; 1164 } 1165 1166 1183 public boolean supportsDifferentTableCorrelationNames() 1184 throws SQLException { 1185 return true; 1186 } 1187 1188 1205 public boolean supportsExpressionsInOrderBy() throws SQLException { 1206 return true; 1207 } 1208 1209 1228 public boolean supportsOrderByUnrelated() throws SQLException { 1229 return true; 1230 } 1231 1232 1249 public boolean supportsGroupBy() throws SQLException { 1250 return true; 1251 } 1252 1253 1273 public boolean supportsGroupByUnrelated() throws SQLException { 1274 return true; 1275 } 1276 1277 1299 public boolean supportsGroupByBeyondSelect() throws SQLException { 1300 return true; 1301 } 1302 1303 1321 public boolean supportsLikeEscapeClause() throws SQLException { 1322 return true; 1323 } 1324 1325 1345 public boolean supportsMultipleResultSets() throws SQLException { 1346 return false; 1347 } 1348 1349 1367 public boolean supportsMultipleTransactions() throws SQLException { 1368 return true; 1369 } 1370 1371 1388 public boolean supportsNonNullableColumns() throws SQLException { 1389 return true; 1390 } 1391 1392 1407 public boolean supportsMinimumSQLGrammar() throws SQLException { 1408 return false; 1409 } 1410 1411 1424 public boolean supportsCoreSQLGrammar() throws SQLException { 1425 return true; 1426 } 1427 1428 1443 public boolean supportsExtendedSQLGrammar() throws SQLException { 1444 return false; 1445 } 1446 1447 1463 public boolean supportsANSI92EntryLevelSQL() throws SQLException { 1464 return false; 1465 } 1466 1467 1483 public boolean supportsANSI92IntermediateSQL() throws SQLException { 1484 return false; 1485 } 1486 1487 1503 public boolean supportsANSI92FullSQL() throws SQLException { 1504 return false; 1505 } 1506 1507 1509 1524 public boolean supportsIntegrityEnhancementFacility() 1525 throws SQLException { 1526 return true; 1527 } 1528 1529 1545 public boolean supportsOuterJoins() throws SQLException { 1546 return true; 1547 } 1548 1549 1565 public boolean supportsFullOuterJoins() throws SQLException { 1566 return false; 1567 } 1568 1569 1585 public boolean supportsLimitedOuterJoins() throws SQLException { 1586 return true; 1587 } 1588 1589 1602 public String getSchemaTerm() throws SQLException { 1603 return "SCHEMA"; 1604 } 1605 1606 1623 public String getProcedureTerm() throws SQLException { 1624 return ""; 1625 } 1626 1627 1642 public String getCatalogTerm() throws SQLException { 1643 return ""; 1644 } 1645 1646 1662 public boolean isCatalogAtStart() throws SQLException { 1663 return false; 1664 } 1665 1666 1682 public String getCatalogSeparator() throws SQLException { 1683 return ""; 1684 } 1685 1686 1702 public boolean supportsSchemasInDataManipulation() throws SQLException { 1703 1704 return false; 1707 } 1708 1709 1724 public boolean supportsSchemasInProcedureCalls() throws SQLException { 1725 return false; 1726 } 1727 1728 1744 public boolean supportsSchemasInTableDefinitions() throws SQLException { 1745 return !useSchemaDefault; 1746 } 1747 1748 1764 public boolean supportsSchemasInIndexDefinitions() throws SQLException { 1765 return !useSchemaDefault; 1766 } 1767 1768 1784 public boolean supportsSchemasInPrivilegeDefinitions() 1785 throws SQLException { 1786 return !useSchemaDefault; 1787 } 1788 1789 1804 public boolean supportsCatalogsInDataManipulation() throws SQLException { 1805 return false; 1806 } 1807 1808 1823 public boolean supportsCatalogsInProcedureCalls() throws SQLException { 1824 return false; 1825 } 1826 1827 1842 public boolean supportsCatalogsInTableDefinitions() throws SQLException { 1843 return false; 1844 } 1845 1846 1861 public boolean supportsCatalogsInIndexDefinitions() throws SQLException { 1862 return false; 1863 } 1864 1865 1880 public boolean supportsCatalogsInPrivilegeDefinitions() 1881 throws SQLException { 1882 return false; 1883 } 1884 1885 1900 public boolean supportsPositionedDelete() throws SQLException { 1901 return false; 1902 } 1903 1904 1919 public boolean supportsPositionedUpdate() throws SQLException { 1920 return false; 1921 } 1922 1923 1938 public boolean supportsSelectForUpdate() throws SQLException { 1939 return false; 1940 } 1941 1942 1960 public boolean supportsStoredProcedures() throws SQLException { 1961 return true; 1962 } 1963 1964 1981 public boolean supportsSubqueriesInComparisons() throws SQLException { 1982 return true; 1983 } 1984 1985 2002 public boolean supportsSubqueriesInExists() throws SQLException { 2003 return true; 2004 } 2005 2006 2023 public boolean supportsSubqueriesInIns() throws SQLException { 2024 return true; 2025 } 2026 2027 2044 public boolean supportsSubqueriesInQuantifieds() throws SQLException { 2045 return true; 2046 } 2047 2048 2064 public boolean supportsCorrelatedSubqueries() throws SQLException { 2065 return true; 2066 } 2067 2068 2084 public boolean supportsUnion() throws SQLException { 2085 return true; 2086 } 2087 2088 2104 public boolean supportsUnionAll() throws SQLException { 2105 return true; 2106 } 2107 2108 2125 public boolean supportsOpenCursorsAcrossCommit() throws SQLException { 2126 return false; 2127 } 2128 2129 2146 public boolean supportsOpenCursorsAcrossRollback() throws SQLException { 2147 return false; 2148 } 2149 2150 2169 public boolean supportsOpenStatementsAcrossCommit() throws SQLException { 2170 return true; 2171 } 2172 2173 2192 public boolean supportsOpenStatementsAcrossRollback() 2193 throws SQLException { 2194 return true; 2195 } 2196 2197 2203 2223 public int getMaxBinaryLiteralLength() throws SQLException { 2224 2225 return 0; 2227 } 2228 2229 2248 public int getMaxCharLiteralLength() throws SQLException { 2249 2250 return 0; 2252 } 2253 2254 2273 public int getMaxColumnNameLength() throws SQLException { 2274 2275 return 0; 2277 } 2278 2279 2298 public int getMaxColumnsInGroupBy() throws SQLException { 2299 2300 return 0; 2302 } 2303 2304 2323 public int getMaxColumnsInIndex() throws SQLException { 2324 2325 return 0; 2327 } 2328 2329 2348 public int getMaxColumnsInOrderBy() throws SQLException { 2349 2350 return 0; 2352 } 2353 2354 2373 public int getMaxColumnsInSelect() throws SQLException { 2374 2375 return 0; 2377 } 2378 2379 2398 public int getMaxColumnsInTable() throws SQLException { 2399 2400 return 0; 2402 } 2403 2404 2423 public int getMaxConnections() throws SQLException { 2424 2425 return 0; 2427 } 2428 2429 2448 public int getMaxCursorNameLength() throws SQLException { 2449 return 0; 2450 } 2451 2452 2471 public int getMaxIndexLength() throws SQLException { 2472 return 0; 2473 } 2474 2475 2492 public int getMaxSchemaNameLength() throws SQLException { 2493 return 0; 2494 } 2495 2496 2515 public int getMaxProcedureNameLength() throws SQLException { 2516 return 0; 2517 } 2518 2519 2537 public int getMaxCatalogNameLength() throws SQLException { 2538 return 0; 2539 } 2540 2541 2558 public int getMaxRowSize() throws SQLException { 2559 return 0; 2560 } 2561 2562 2582 public boolean doesMaxRowSizeIncludeBlobs() throws SQLException { 2583 return true; 2584 } 2585 2586 2605 public int getMaxStatementLength() throws SQLException { 2606 return 0; 2607 } 2608 2609 2627 public int getMaxStatements() throws SQLException { 2628 return 0; 2629 } 2630 2631 2650 public int getMaxTableNameLength() throws SQLException { 2651 return 0; 2652 } 2653 2654 2673 public int getMaxTablesInSelect() throws SQLException { 2674 2675 return 0; 2680 } 2681 2682 2701 public int getMaxUserNameLength() throws SQLException { 2702 2703 return 0; 2705 } 2706 2707 2709 2726 public int getDefaultTransactionIsolation() throws SQLException { 2727 return Connection.TRANSACTION_READ_UNCOMMITTED; 2728 } 2729 2730 2747 public boolean supportsTransactions() throws SQLException { 2748 return true; 2749 } 2750 2751 2771 public boolean supportsTransactionIsolationLevel(int level) 2772 throws SQLException { 2773 2774 return level == Connection.TRANSACTION_READ_UNCOMMITTED 2775 || level == Connection.TRANSACTION_READ_COMMITTED 2776 || level == Connection.TRANSACTION_REPEATABLE_READ 2777 || level == Connection.TRANSACTION_SERIALIZABLE; 2778 } 2779 2780 2799 public boolean supportsDataDefinitionAndDataManipulationTransactions() 2800 throws SQLException { 2801 return false; 2802 } 2803 2804 2823 public boolean supportsDataManipulationTransactionsOnly() 2824 throws SQLException { 2825 return true; 2826 } 2827 2828 2845 public boolean dataDefinitionCausesTransactionCommit() 2846 throws SQLException { 2847 return true; 2848 } 2849 2850 2869 public boolean dataDefinitionIgnoredInTransactions() throws SQLException { 2870 return false; 2871 } 2872 2873 2932 public ResultSet getProcedures(String catalog, String schemaPattern, 2933 String procedureNamePattern) 2934 throws SQLException { 2935 2936 if (wantsIsNull(procedureNamePattern)) { 2937 return executeSelect("SYSTEM_PROCEDURES", "0=1"); 2938 } 2939 2940 schemaPattern = translateSchema(schemaPattern); 2941 2942 StringBuffer select = toQueryPrefix("SYSTEM_PROCEDURES").append( 2943 and("PROCEDURE_CAT", "=", catalog)).append( 2944 and("PROCEDURE_SCHEM", "LIKE", schemaPattern)).append( 2945 and("PROCEDURE_NAME", "LIKE", procedureNamePattern)); 2946 2947 return execute(select.toString()); 2950 } 2951 2952 3035 public ResultSet getProcedureColumns(String catalog, 3036 String schemaPattern, 3037 String procedureNamePattern, 3038 String columnNamePattern) 3039 throws SQLException { 3040 3041 if (wantsIsNull(procedureNamePattern) 3042 || wantsIsNull(columnNamePattern)) { 3043 return executeSelect("SYSTEM_PROCEDURECOLUMNS", "0=1"); 3044 } 3045 3046 schemaPattern = translateSchema(schemaPattern); 3047 3048 StringBuffer select = toQueryPrefix("SYSTEM_PROCEDURECOLUMNS").append( 3049 and("PROCEDURE_CAT", "=", catalog)).append( 3050 and("PROCEDURE_SCHEM", "LIKE", schemaPattern)).append( 3051 and("PROCEDURE_NAME", "LIKE", procedureNamePattern)).append( 3052 and("COLUMN_NAME", "LIKE", columnNamePattern)); 3053 3054 return execute(select.toString()); 3057 } 3058 3059 3130 public ResultSet getTables(String catalog, String schemaPattern, 3131 String tableNamePattern, 3132 String [] types) throws SQLException { 3133 3134 if (wantsIsNull(tableNamePattern) 3135 || (types != null && types.length == 0)) { 3136 return executeSelect("SYSTEM_TABLES", "0=1"); 3137 } 3138 3139 schemaPattern = translateSchema(schemaPattern); 3140 3141 StringBuffer select = 3142 toQueryPrefix("SYSTEM_TABLES").append(and("TABLE_CAT", "=", 3143 catalog)).append(and("TABLE_SCHEM", "LIKE", 3144 schemaPattern)).append(and("TABLE_NAME", 3145 "LIKE", tableNamePattern)); 3146 3147 if (types == null) { 3148 3149 } else { 3151 select.append(" AND TABLE_TYPE IN (").append( 3152 StringUtil.getList(types, ",", "'")).append(')'); 3153 } 3154 3155 return execute(select.toString()); 3158 } 3159 3160 3182 public ResultSet getSchemas() throws SQLException { 3183 3184 return executeSelect("SYSTEM_SCHEMAS", null); 3186 } 3187 3188 3213 public ResultSet getCatalogs() throws SQLException { 3214 return executeSelect("SYSTEM_CATALOGS", null); 3215 } 3216 3217 3246 public ResultSet getTableTypes() throws SQLException { 3247 3248 return executeSelect("SYSTEM_TABLETYPES", null); 3250 } 3251 3252 3345 public ResultSet getColumns(String catalog, String schemaPattern, 3346 String tableNamePattern, 3347 String columnNamePattern) 3348 throws SQLException { 3349 3350 if (wantsIsNull(tableNamePattern) || wantsIsNull(columnNamePattern)) { 3351 return executeSelect("SYSTEM_COLUMNS", "0=1"); 3352 } 3353 3354 schemaPattern = translateSchema(schemaPattern); 3355 3356 StringBuffer select = toQueryPrefix("SYSTEM_COLUMNS").append( 3357 and("TABLE_CAT", "=", catalog)).append( 3358 and("TABLE_SCHEM", "LIKE", schemaPattern)).append( 3359 and("TABLE_NAME", "LIKE", tableNamePattern)).append( 3360 and("COLUMN_NAME", "LIKE", columnNamePattern)); 3361 3362 return execute(select.toString()); 3365 } 3366 3367 3424 public ResultSet getColumnPrivileges(String catalog, String schema, 3425 String table, 3426 String columnNamePattern) 3427 throws SQLException { 3428 3429 if (table == null) { 3430 Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 3431 } 3432 3433 if (wantsIsNull(columnNamePattern)) { 3434 return executeSelect("SYSTEM_COLUMNPRIVILEGES", "0=1"); 3435 } 3436 3437 schema = translateSchema(schema); 3438 3439 StringBuffer select = toQueryPrefix("SYSTEM_COLUMNPRIVILEGES").append( 3440 and("TABLE_CAT", "=", catalog)).append( 3441 and("TABLE_SCHEM", "=", schema)).append( 3442 and("TABLE_NAME", "=", table)).append( 3443 and("COLUMN_NAME", "LIKE", columnNamePattern)); 3444 3445 return execute(select.toString()); 3448 } 3449 3450 3509 public ResultSet getTablePrivileges(String catalog, String schemaPattern, 3510 String tableNamePattern) 3511 throws SQLException { 3512 3513 if (wantsIsNull(tableNamePattern)) { 3514 return executeSelect("SYSTEM_TABLEPRIVILEGES", "0=1"); 3515 } 3516 3517 schemaPattern = translateSchema(schemaPattern); 3518 3519 StringBuffer select = toQueryPrefix("SYSTEM_TABLEPRIVILEGES").append( 3520 and("TABLE_CAT", "=", catalog)).append( 3521 and("TABLE_SCHEM", "LIKE", schemaPattern)).append( 3522 and("TABLE_NAME", "LIKE", tableNamePattern)); 3523 3524 return execute(select.toString()); 3527 } 3528 3529 3597 public ResultSet getBestRowIdentifier(String catalog, String schema, 3598 String table, int scope, 3599 boolean nullable) 3600 throws SQLException { 3601 3602 String scopeIn; 3603 3604 switch (scope) { 3605 3606 case bestRowTemporary : 3607 scopeIn = BRI_TEMPORARY_SCOPE_IN_LIST; 3608 break; 3609 3610 case bestRowTransaction : 3611 scopeIn = BRI_TRANSACTION_SCOPE_IN_LIST; 3612 break; 3613 3614 case bestRowSession : 3615 scopeIn = BRI_SESSION_SCOPE_IN_LIST; 3616 break; 3617 3618 default : 3619 throw Util.sqlException(Trace.ASSERT_FAILED, 3620 Trace.JDBC_INVALID_BRI_SCOPE, null); 3621 } 3622 3623 if (table == null) { 3624 Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 3625 } 3626 3627 schema = translateSchema(schema); 3628 3629 Integer Nullable = (nullable) ? null 3630 : INT_COLUMNS_NO_NULLS; 3631 StringBuffer select = toQueryPrefix( 3632 "SYSTEM_BESTROWIDENTIFIER").append( 3633 and("TABLE_CAT", "=", catalog)).append( 3634 and("TABLE_SCHEM", "=", schema)).append( 3635 and("TABLE_NAME", "=", table)).append( 3636 and("NULLABLE", "=", Nullable)).append( 3637 " AND SCOPE IN " + scopeIn); 3638 3639 return execute(select.toString()); 3646 } 3647 3648 3705 public ResultSet getVersionColumns(String catalog, String schema, 3706 String table) throws SQLException { 3707 3708 if (table == null) { 3709 Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 3710 } 3711 3712 schema = translateSchema(schema); 3713 3714 StringBuffer select = 3715 toQueryPrefix("SYSTEM_VERSIONCOLUMNS").append(and("TABLE_CAT", 3716 "=", catalog)).append(and("TABLE_SCHEM", "=", 3717 schema)).append(and("TABLE_NAME", 3718 "=", table)); 3719 3720 return execute(select.toString()); 3722 } 3723 3724 3774 3775 public ResultSet getPrimaryKeys(String catalog, String schema, 3777 String table) throws SQLException { 3778 3779 if (table == null) { 3780 Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 3781 } 3782 3783 schema = translateSchema(schema); 3784 3785 StringBuffer select = 3786 toQueryPrefix("SYSTEM_PRIMARYKEYS").append(and("TABLE_CAT", "=", 3787 catalog)).append(and("TABLE_SCHEM", "=", 3788 schema)).append(and("TABLE_NAME", "=", 3789 table)); 3790 3791 return execute(select.toString()); 3793 } 3794 3795 3891 public ResultSet getImportedKeys(String catalog, String schema, 3892 String table) throws SQLException { 3893 3894 if (table == null) { 3895 Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 3896 } 3897 3898 schema = translateSchema(schema); 3899 3900 StringBuffer select = toQueryPrefix("SYSTEM_CROSSREFERENCE").append( 3901 and("FKTABLE_CAT", "=", catalog)).append( 3902 and("FKTABLE_SCHEM", "=", schema)).append( 3903 and("FKTABLE_NAME", "=", table)).append( 3904 " ORDER BY PKTABLE_CAT, PKTABLE_SCHEM, PKTABLE_NAME, KEY_SEQ"); 3905 3906 return execute(select.toString()); 3907 } 3908 3909 4005 public ResultSet getExportedKeys(String catalog, String schema, 4006 String table) throws SQLException { 4007 4008 if (table == null) { 4009 Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 4010 } 4011 4012 schema = translateSchema(schema); 4013 4014 StringBuffer select = 4015 toQueryPrefix("SYSTEM_CROSSREFERENCE").append(and("PKTABLE_CAT", 4016 "=", catalog)).append(and("PKTABLE_SCHEM", "=", 4017 schema)).append(and("PKTABLE_NAME", 4018 "=", table)); 4019 4020 return execute(select.toString()); 4023 } 4024 4025 4132 public ResultSet getCrossReference(String primaryCatalog, 4133 String primarySchema, 4134 String primaryTable, 4135 String foreignCatalog, 4136 String foreignSchema, 4137 String foreignTable) 4138 throws SQLException { 4139 4140 if (primaryTable == null || foreignTable == null) { 4141 Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 4142 } 4143 4144 primarySchema = translateSchema(primarySchema); 4145 foreignSchema = translateSchema(foreignSchema); 4146 4147 StringBuffer select = toQueryPrefix("SYSTEM_CROSSREFERENCE").append( 4148 and("PKTABLE_CAT", "=", primaryCatalog)).append( 4149 and("PKTABLE_SCHEM", "=", primarySchema)).append( 4150 and("PKTABLE_NAME", "=", primaryTable)).append( 4151 and("FKTABLE_CAT", "=", foreignCatalog)).append( 4152 and("FKTABLE_SCHEM", "=", foreignSchema)).append( 4153 and("FKTABLE_NAME", "=", foreignTable)); 4154 4155 return execute(select.toString()); 4158 } 4159 4160 4219 public ResultSet getTypeInfo() throws SQLException { 4220 4221 return executeSelect("SYSTEM_TYPEINFO", null); 4223 } 4224 4225 4305 4306 public ResultSet getIndexInfo(String catalog, String schema, 4308 String table, boolean unique, 4309 boolean approximate) throws SQLException { 4310 4311 if (table == null) { 4312 Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 4313 } 4314 4315 schema = translateSchema(schema); 4316 4317 Boolean nu = (unique) ? Boolean.FALSE 4318 : null; 4319 StringBuffer select = 4320 toQueryPrefix("SYSTEM_INDEXINFO").append(and("TABLE_CAT", "=", 4321 catalog)).append(and("TABLE_SCHEM", "=", 4322 schema)).append(and("TABLE_NAME", "=", 4323 table)).append(and("NON_UNIQUE", 4324 "=", nu)); 4325 4326 return execute(select.toString()); 4329 } 4330 4331 4333 4343 public boolean supportsResultSetType(int type) throws SQLException { 4344 return (type == jdbcResultSet.TYPE_FORWARD_ONLY 4345 || type == jdbcResultSet.TYPE_SCROLL_INSENSITIVE); 4346 } 4347 4348 4360 public boolean supportsResultSetConcurrency(int type, 4361 int concurrency) throws SQLException { 4362 return supportsResultSetType(type) 4363 && concurrency == jdbcResultSet.CONCUR_READ_ONLY; 4364 } 4365 4366 4388 public boolean ownUpdatesAreVisible(int type) throws SQLException { 4389 return false; 4390 } 4391 4392 4413 public boolean ownDeletesAreVisible(int type) throws SQLException { 4414 return false; 4415 } 4416 4417 4438 public boolean ownInsertsAreVisible(int type) throws SQLException { 4439 return false; 4440 } 4441 4442 4464 public boolean othersUpdatesAreVisible(int type) throws SQLException { 4465 return false; 4466 } 4467 4468 4490 public boolean othersDeletesAreVisible(int type) throws SQLException { 4491 return false; 4492 } 4493 4494 4516 public boolean othersInsertsAreVisible(int type) throws SQLException { 4517 return false; 4518 } 4519 4520 4542 public boolean updatesAreDetected(int type) throws SQLException { 4543 return false; 4544 } 4545 4546 4572 public boolean deletesAreDetected(int type) throws SQLException { 4573 return false; 4574 } 4575 4576 4598 public boolean insertsAreDetected(int type) throws SQLException { 4599 return false; 4600 } 4601 4602 4619 public boolean supportsBatchUpdates() throws SQLException { 4620 return true; 4621 } 4622 4623 4693 public ResultSet getUDTs(String catalog, String schemaPattern, 4694 String typeNamePattern, 4695 int[] types) throws SQLException { 4696 4697 if (wantsIsNull(typeNamePattern) 4698 || (types != null && types.length == 0)) { 4699 executeSelect("SYSTEM_UDTS", "0=1"); 4700 } 4701 4702 schemaPattern = translateSchema(schemaPattern); 4703 4704 StringBuffer select = 4705 toQueryPrefix("SYSTEM_UDTS").append(and("TYPE_CAT", "=", 4706 catalog)).append(and("TYPE_SCHEM", "LIKE", 4707 schemaPattern)).append(and("TYPE_NAME", 4708 "LIKE", typeNamePattern)); 4709 4710 if (types == null) { 4711 4712 } else { 4714 select.append(" AND DATA_TYPE IN (").append( 4715 StringUtil.getList(types, ",", "'")).append(')'); 4716 } 4717 4718 return execute(select.toString()); 4721 } 4722 4723 4731 public Connection getConnection() throws SQLException { 4732 return connection; 4733 } 4734 4735 4739 4756 public boolean supportsSavepoints() throws SQLException { 4758 return true; 4759 } 4760 4761 4763 4781 public boolean supportsNamedParameters() throws SQLException { 4783 return true; 4784 } 4785 4786 4788 4809 public boolean supportsMultipleOpenResults() throws SQLException { 4811 return false; 4812 } 4813 4814 4816 4836 public boolean supportsGetGeneratedKeys() throws SQLException { 4838 return false; 4839 } 4840 4841 4843 4908 public ResultSet getSuperTypes(String catalog, String schemaPattern, 4910 String typeNamePattern) 4911 throws SQLException { 4912 4913 if (wantsIsNull(typeNamePattern)) { 4914 return executeSelect("SYSTEM_SUPERTYPES", "0=1"); 4915 } 4916 4917 schemaPattern = translateSchema(schemaPattern); 4918 4919 StringBuffer select = 4920 toQueryPrefix("SYSTEM_SUPERTYPES").append(and("TYPE_CAT", "=", 4921 catalog)).append(and("TYPE_SCHEM", "LIKE", 4922 schemaPattern)).append(and("TYPE_NAME", 4923 "LIKE", typeNamePattern)); 4924 4925 return execute(select.toString()); 4926 } 4927 4928 4930 4985 public ResultSet getSuperTables(String catalog, String schemaPattern, 4987 String tableNamePattern) 4988 throws SQLException { 4989 4990 if (wantsIsNull(tableNamePattern)) { 4991 return executeSelect("SYSTEM_SUPERTABLES", "0=1"); 4992 } 4993 4994 schemaPattern = translateSchema(schemaPattern); 4995 4996 StringBuffer select = 4997 toQueryPrefix("SYSTEM_SUPERTABLES").append(and("TABLE_CAT", "=", 4998 catalog)).append(and("TABLE_SCHEM", "LIKE", 4999 schemaPattern)).append(and("TABLE_NAME", 5000 "LIKE", tableNamePattern)); 5001 5002 return execute(select.toString()); 5003 } 5004 5005 5007 5102 public ResultSet getAttributes(String catalog, String schemaPattern, 5104 String typeNamePattern, 5105 String attributeNamePattern) 5106 throws SQLException { 5107 5108 if (wantsIsNull(typeNamePattern) 5109 || wantsIsNull(attributeNamePattern)) { 5110 return executeSelect("SYSTEM_UDTATTRIBUTES", "0=1"); 5111 } 5112 5113 schemaPattern = translateSchema(schemaPattern); 5114 5115 StringBuffer select = toQueryPrefix("SYSTEM_UDTATTRIBUTES").append( 5116 and("TYPE_CAT", "=", catalog)).append( 5117 and("TYPE_SCHEM", "LIKE", schemaPattern)).append( 5118 and("TYPE_NAME", "LIKE", typeNamePattern)).append( 5119 and("ATTR_NAME", "LIKE", attributeNamePattern)); 5120 5121 return execute(select.toString()); 5122 } 5123 5124 5126 5147 public boolean supportsResultSetHoldability(int holdability) 5149 throws SQLException { 5150 return holdability == jdbcResultSet.HOLD_CURSORS_OVER_COMMIT; 5151 } 5152 5153 5155 5178 public int getResultSetHoldability() throws SQLException { 5180 5181 return jdbcResultSet.HOLD_CURSORS_OVER_COMMIT; 5211 } 5212 5213 5215 5234 public int getDatabaseMajorVersion() throws SQLException { 5236 5237 ResultSet rs = 5238 execute("call \"org.hsqldb.Library.getDatabaseMajorVersion\"()"); 5239 5240 rs.next(); 5241 5242 int result = rs.getInt(1); 5243 5244 rs.close(); 5245 5246 return result; 5247 } 5248 5249 5251 5270 public int getDatabaseMinorVersion() throws SQLException { 5272 5273 ResultSet rs = 5274 execute("call \"org.hsqldb.Library.getDatabaseMinorVersion\"()"); 5275 5276 rs.next(); 5277 5278 int result = rs.getInt(1); 5279 5280 rs.close(); 5281 5282 return result; 5283 } 5284 5285 5287 5303 public int getJDBCMajorVersion() throws SQLException { 5305 return 3; 5306 } 5307 5308 5310 5325 public int getJDBCMinorVersion() throws SQLException { 5327 return 0; 5328 } 5329 5330 5332 5336 5337 5355 public int getSQLStateType() throws SQLException { 5357 return sqlStateSQL99; 5358 } 5359 5360 5362 5380 public boolean locatorsUpdateCopy() throws SQLException { 5382 return false; 5383 } 5384 5385 5387 5404 public boolean supportsStatementPooling() throws SQLException { 5406 return false; 5407 } 5408 5409 5412 5421 jdbcDatabaseMetaData(jdbcConnection c) throws SQLException { 5422 5423 connection = c; 5425 useSchemaDefault = c.connProperties.isPropertyTrue("default_schema"); 5426 } 5427 5428 5470 private static String and(String id, String op, Object val) { 5471 5472 if (val == null) { 5496 return ""; 5497 } 5498 5499 StringBuffer sb = new StringBuffer (); 5500 boolean isStr = (val instanceof String ); 5501 5502 if (isStr && ((String ) val).length() == 0) { 5503 return sb.append(" AND ").append(id).append( 5504 " IS NULL").toString(); 5505 } 5506 5507 String v = isStr ? Column.createSQLString((String ) val) 5508 : String.valueOf(val); 5509 5510 sb.append(" AND ").append(id).append(' '); 5511 5512 if (isStr && "LIKE".equalsIgnoreCase(op)) { 5514 if (v.indexOf('_') < 0 && v.indexOf('%') < 0) { 5515 5516 sb.append("=").append(' ').append(v); 5518 } else { 5519 sb.append("LIKE").append(' ').append(v); 5520 5521 if ((v.indexOf("\\_") >= 0) || (v.indexOf("\\%") >= 0)) { 5522 5523 sb.append(" ESCAPE '\\'"); 5525 } 5526 } 5527 } else { 5528 sb.append(op).append(' ').append(v); 5529 } 5530 5531 return sb.toString(); 5532 } 5533 5534 5546 private ResultSet execute(String sql) throws SQLException { 5547 5548 final int scroll = jdbcResultSet.TYPE_SCROLL_INSENSITIVE; 5559 final int concur = jdbcResultSet.CONCUR_READ_ONLY; 5560 ResultSet r = connection.createStatement(scroll, 5561 concur).executeQuery(sql); 5562 5563 ((jdbcResultSet) r).autoClose = true; 5564 5565 return r; 5566 } 5567 5568 5582 private ResultSet executeSelect(String table, 5583 String where) throws SQLException { 5584 5585 String select = selstar + table; 5586 5587 if (where != null) { 5588 select += " WHERE " + where; 5589 } 5590 5591 return execute(select); 5592 } 5593 5594 5606 private StringBuffer toQueryPrefix(String t) { 5607 5608 StringBuffer sb = new StringBuffer (255); 5609 5610 return sb.append(selstar).append(t).append(whereTrue); 5611 } 5612 5613 5622 private static boolean wantsIsNull(String s) { 5623 return (s != null && s.length() == 0); 5624 } 5625 5626 5631 private String translateSchema(String schemaName) throws SQLException { 5632 5633 if (useSchemaDefault && schemaName != null 5634 && schemaName.length() == 0) { 5635 ResultSet rs = executeSelect("SYSTEM_SCHEMAS", "IS_DEFAULT=TRUE"); 5636 5637 if (rs.next()) { 5638 return rs.getString(1); 5639 } 5640 5641 return schemaName; 5642 } 5643 5644 return schemaName; 5645 } 5646} 5647 | Popular Tags |