comparison src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java @ 323:8701024a9bb0

In DatabaseMetaData methods which accept name match patterns, optimise the SQL generation when the pattern equals a: % In those cases no additional AND name LIKE '%' condition is generated anymore.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Wed, 11 Sep 2019 19:22:40 +0200 (2019-09-11)
parents 7c79ef41b840
children 6ed8f5b1f9ed
comparison
equal deleted inserted replaced
322:0fcf338ce0b4 323:8701024a9bb0
18 import java.sql.Types; 18 import java.sql.Types;
19 19
20 /** 20 /**
21 * A DatabaseMetaData object suitable for the MonetDB database. 21 * A DatabaseMetaData object suitable for the MonetDB database.
22 * 22 *
23 * @author Fabian Groffen, Martin van Dinther 23 * @author Fabian Groffen
24 * @author Martin van Dinther
24 * @version 0.8 25 * @version 0.8
25 */ 26 */
26 public class MonetDatabaseMetaData 27 public class MonetDatabaseMetaData
27 extends MonetWrapper 28 extends MonetWrapper
28 implements DatabaseMetaData 29 implements DatabaseMetaData
1735 if (catalog != null && !catalog.isEmpty()) { 1736 if (catalog != null && !catalog.isEmpty()) {
1736 // non-empty catalog selection. 1737 // non-empty catalog selection.
1737 // as we do not support catalogs this always results in no rows returned 1738 // as we do not support catalogs this always results in no rows returned
1738 query.append(" AND 1 = 0"); 1739 query.append(" AND 1 = 0");
1739 } else { 1740 } else {
1740 if (schemaPattern != null) { 1741 if (schemaPattern != null && !schemaPattern.equals("%")) {
1741 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern)); 1742 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern));
1742 } 1743 }
1743 if (procedureNamePattern != null) { 1744 if (procedureNamePattern != null && !procedureNamePattern.equals("%")) {
1744 query.append(" AND f.\"name\" ").append(composeMatchPart(procedureNamePattern)); 1745 query.append(" AND f.\"name\" ").append(composeMatchPart(procedureNamePattern));
1745 } 1746 }
1746 } 1747 }
1747 1748
1748 query.append(" ORDER BY \"PROCEDURE_SCHEM\", \"PROCEDURE_NAME\", \"SPECIFIC_NAME\""); 1749 query.append(" ORDER BY \"PROCEDURE_SCHEM\", \"PROCEDURE_NAME\", \"SPECIFIC_NAME\"");
1860 if (catalog != null && !catalog.isEmpty()) { 1861 if (catalog != null && !catalog.isEmpty()) {
1861 // non-empty catalog selection. 1862 // non-empty catalog selection.
1862 // as we do not support catalogs this always results in no rows returned 1863 // as we do not support catalogs this always results in no rows returned
1863 query.append(" AND 1 = 0"); 1864 query.append(" AND 1 = 0");
1864 } else { 1865 } else {
1865 if (schemaPattern != null) { 1866 if (schemaPattern != null && !schemaPattern.equals("%")) {
1866 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern)); 1867 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern));
1867 } 1868 }
1868 if (procedureNamePattern != null) { 1869 if (procedureNamePattern != null && !procedureNamePattern.equals("%")) {
1869 query.append(" AND f.\"name\" ").append(composeMatchPart(procedureNamePattern)); 1870 query.append(" AND f.\"name\" ").append(composeMatchPart(procedureNamePattern));
1870 } 1871 }
1871 if (columnNamePattern != null) { 1872 if (columnNamePattern != null && !columnNamePattern.equals("%")) {
1872 query.append(" AND a.\"name\" ").append(composeMatchPart(columnNamePattern)); 1873 query.append(" AND a.\"name\" ").append(composeMatchPart(columnNamePattern));
1873 } 1874 }
1874 } 1875 }
1875 1876
1876 query.append(" ORDER BY \"PROCEDURE_SCHEM\", \"PROCEDURE_NAME\", \"SPECIFIC_NAME\", \"ORDINAL_POSITION\""); 1877 query.append(" ORDER BY \"PROCEDURE_SCHEM\", \"PROCEDURE_NAME\", \"SPECIFIC_NAME\", \"ORDINAL_POSITION\"");
1969 if (catalog != null && !catalog.isEmpty()) { 1970 if (catalog != null && !catalog.isEmpty()) {
1970 // non-empty catalog selection. 1971 // non-empty catalog selection.
1971 // as we do not support catalogs this always results in no rows returned 1972 // as we do not support catalogs this always results in no rows returned
1972 query.append(" AND 1 = 0"); 1973 query.append(" AND 1 = 0");
1973 } else { 1974 } else {
1974 if (schemaPattern != null) { 1975 if (schemaPattern != null && !schemaPattern.equals("%")) {
1975 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern)); 1976 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern));
1976 } 1977 }
1977 if (tableNamePattern != null) { 1978 if (tableNamePattern != null && !tableNamePattern.equals("%")) {
1978 query.append(" AND t.\"name\" ").append(composeMatchPart(tableNamePattern)); 1979 query.append(" AND t.\"name\" ").append(composeMatchPart(tableNamePattern));
1979 } 1980 }
1980 } 1981 }
1981 1982
1982 if (types != null && types.length > 0) { 1983 if (types != null && types.length > 0) {
2032 if (catalog != null && !catalog.isEmpty()) { 2033 if (catalog != null && !catalog.isEmpty()) {
2033 // non-empty catalog selection. 2034 // non-empty catalog selection.
2034 // as we do not support catalogs this always results in no rows returned 2035 // as we do not support catalogs this always results in no rows returned
2035 query.append(" WHERE 1 = 0"); 2036 query.append(" WHERE 1 = 0");
2036 } else { 2037 } else {
2037 if (schemaPattern != null) { 2038 if (schemaPattern != null && !schemaPattern.equals("%")) {
2038 query.append(" WHERE \"name\" ").append(composeMatchPart(schemaPattern)); 2039 query.append(" WHERE \"name\" ").append(composeMatchPart(schemaPattern));
2039 } 2040 }
2040 } 2041 }
2041 query.append(" ORDER BY \"TABLE_SCHEM\""); 2042 query.append(" ORDER BY \"TABLE_SCHEM\"");
2042 2043
2216 if (catalog != null && !catalog.isEmpty()) { 2217 if (catalog != null && !catalog.isEmpty()) {
2217 // non-empty catalog selection. 2218 // non-empty catalog selection.
2218 // as we do not support catalogs this always results in no rows returned 2219 // as we do not support catalogs this always results in no rows returned
2219 query.append(" AND 1 = 0"); 2220 query.append(" AND 1 = 0");
2220 } else { 2221 } else {
2221 if (schemaPattern != null) { 2222 if (schemaPattern != null && !schemaPattern.equals("%")) {
2222 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern)); 2223 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern));
2223 } 2224 }
2224 if (tableNamePattern != null) { 2225 if (tableNamePattern != null && !tableNamePattern.equals("%")) {
2225 query.append(" AND t.\"name\" ").append(composeMatchPart(tableNamePattern)); 2226 query.append(" AND t.\"name\" ").append(composeMatchPart(tableNamePattern));
2226 } 2227 }
2227 if (columnNamePattern != null) { 2228 if (columnNamePattern != null && !columnNamePattern.equals("%")) {
2228 query.append(" AND c.\"name\" ").append(composeMatchPart(columnNamePattern)); 2229 query.append(" AND c.\"name\" ").append(composeMatchPart(columnNamePattern));
2229 } 2230 }
2230 } 2231 }
2231 2232
2232 query.append(" ORDER BY \"TABLE_SCHEM\", \"TABLE_NAME\", \"ORDINAL_POSITION\""); 2233 query.append(" ORDER BY \"TABLE_SCHEM\", \"TABLE_NAME\", \"ORDINAL_POSITION\"");
2310 if (catalog != null && !catalog.isEmpty()) { 2311 if (catalog != null && !catalog.isEmpty()) {
2311 // non-empty catalog selection. 2312 // non-empty catalog selection.
2312 // as we do not support catalogs this always results in no rows returned 2313 // as we do not support catalogs this always results in no rows returned
2313 query.append(" AND 1 = 0"); 2314 query.append(" AND 1 = 0");
2314 } else { 2315 } else {
2315 if (schemaPattern != null) { 2316 if (schemaPattern != null && !schemaPattern.equals("%")) {
2316 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern)); 2317 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern));
2317 } 2318 }
2318 if (tableNamePattern != null) { 2319 if (tableNamePattern != null && !tableNamePattern.equals("%")) {
2319 query.append(" AND t.\"name\" ").append(composeMatchPart(tableNamePattern)); 2320 query.append(" AND t.\"name\" ").append(composeMatchPart(tableNamePattern));
2320 } 2321 }
2321 if (columnNamePattern != null) { 2322 if (columnNamePattern != null && !columnNamePattern.equals("%")) {
2322 query.append(" AND c.\"name\" ").append(composeMatchPart(columnNamePattern)); 2323 query.append(" AND c.\"name\" ").append(composeMatchPart(columnNamePattern));
2323 } 2324 }
2324 } 2325 }
2325 2326
2326 query.append(" ORDER BY \"TABLE_SCHEM\", \"TABLE_NAME\", \"COLUMN_NAME\", \"PRIVILEGE\""); 2327 query.append(" ORDER BY \"TABLE_SCHEM\", \"TABLE_NAME\", \"COLUMN_NAME\", \"PRIVILEGE\"");
2399 if (catalog != null && !catalog.isEmpty()) { 2400 if (catalog != null && !catalog.isEmpty()) {
2400 // non-empty catalog selection. 2401 // non-empty catalog selection.
2401 // as we do not support catalogs this always results in no rows returned 2402 // as we do not support catalogs this always results in no rows returned
2402 query.append(" AND 1 = 0"); 2403 query.append(" AND 1 = 0");
2403 } else { 2404 } else {
2404 if (schemaPattern != null) { 2405 if (schemaPattern != null && !schemaPattern.equals("%")) {
2405 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern)); 2406 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern));
2406 } 2407 }
2407 if (tableNamePattern != null) { 2408 if (tableNamePattern != null && !tableNamePattern.equals("%")) {
2408 query.append(" AND t.\"name\" ").append(composeMatchPart(tableNamePattern)); 2409 query.append(" AND t.\"name\" ").append(composeMatchPart(tableNamePattern));
2409 } 2410 }
2410 } 2411 }
2411 2412
2412 query.append(" ORDER BY \"TABLE_SCHEM\", \"TABLE_NAME\", \"PRIVILEGE\""); 2413 query.append(" ORDER BY \"TABLE_SCHEM\", \"TABLE_NAME\", \"PRIVILEGE\"");
2483 if (scope != DatabaseMetaData.bestRowSession 2484 if (scope != DatabaseMetaData.bestRowSession
2484 && scope != DatabaseMetaData.bestRowTransaction 2485 && scope != DatabaseMetaData.bestRowTransaction
2485 && scope != DatabaseMetaData.bestRowTemporary) { 2486 && scope != DatabaseMetaData.bestRowTemporary) {
2486 query.append(" AND 1 = 0"); 2487 query.append(" AND 1 = 0");
2487 } else { 2488 } else {
2488 if (schema != null) { 2489 if (schema != null && !schema.equals("%")) {
2489 query.append(" AND s.\"name\" ").append(composeMatchPart(schema)); 2490 query.append(" AND s.\"name\" ").append(composeMatchPart(schema));
2490 } 2491 }
2491 if (table != null) { 2492 if (table != null && !table.equals("%")) {
2492 query.append(" AND t.\"name\" ").append(composeMatchPart(table)); 2493 query.append(" AND t.\"name\" ").append(composeMatchPart(table));
2493 } 2494 }
2494 if (!nullable) { 2495 if (!nullable) {
2495 query.append(" AND c.\"null\" = false"); 2496 query.append(" AND c.\"null\" = false");
2496 } 2497 }
2596 if (catalog != null && !catalog.isEmpty()) { 2597 if (catalog != null && !catalog.isEmpty()) {
2597 // non-empty catalog selection. 2598 // non-empty catalog selection.
2598 // as we do not support catalogs this always results in no rows returned 2599 // as we do not support catalogs this always results in no rows returned
2599 query.append(" AND 1 = 0"); 2600 query.append(" AND 1 = 0");
2600 } else { 2601 } else {
2601 if (schema != null) { 2602 if (schema != null && !schema.equals("%")) {
2602 query.append(" AND s.\"name\" ").append(composeMatchPart(schema)); 2603 query.append(" AND s.\"name\" ").append(composeMatchPart(schema));
2603 } 2604 }
2604 if (table != null) { 2605 if (table != null && !table.equals("%")) {
2605 query.append(" AND t.\"name\" ").append(composeMatchPart(table)); 2606 query.append(" AND t.\"name\" ").append(composeMatchPart(table));
2606 } 2607 }
2607 } 2608 }
2608 2609
2609 query.append(" ORDER BY \"TABLE_SCHEM\", \"TABLE_NAME\", \"COLUMN_NAME\""); 2610 query.append(" ORDER BY \"TABLE_SCHEM\", \"TABLE_NAME\", \"COLUMN_NAME\"");
2711 if (catalog != null && !catalog.isEmpty()) { 2712 if (catalog != null && !catalog.isEmpty()) {
2712 // non-empty catalog selection. 2713 // non-empty catalog selection.
2713 // as we do not support catalogs this always results in no rows returned 2714 // as we do not support catalogs this always results in no rows returned
2714 query.append(" AND 1 = 0"); 2715 query.append(" AND 1 = 0");
2715 } else { 2716 } else {
2716 if (schema != null) { 2717 if (schema != null && !schema.equals("%")) {
2717 query.append(" AND fkschema.\"name\" ").append(composeMatchPart(schema)); 2718 query.append(" AND fkschema.\"name\" ").append(composeMatchPart(schema));
2718 } 2719 }
2719 if (table != null) { 2720 if (table != null && !table.equals("%")) {
2720 query.append(" AND fktable.\"name\" ").append(composeMatchPart(table)); 2721 query.append(" AND fktable.\"name\" ").append(composeMatchPart(table));
2721 } 2722 }
2722 } 2723 }
2723 2724
2724 query.append(" ORDER BY \"PKTABLE_SCHEM\", \"PKTABLE_NAME\", \"PK_NAME\", \"KEY_SEQ\""); 2725 query.append(" ORDER BY \"PKTABLE_SCHEM\", \"PKTABLE_NAME\", \"PK_NAME\", \"KEY_SEQ\"");
2799 if (catalog != null && !catalog.isEmpty()) { 2800 if (catalog != null && !catalog.isEmpty()) {
2800 // non-empty catalog selection. 2801 // non-empty catalog selection.
2801 // as we do not support catalogs this always results in no rows returned 2802 // as we do not support catalogs this always results in no rows returned
2802 query.append(" AND 1 = 0"); 2803 query.append(" AND 1 = 0");
2803 } else { 2804 } else {
2804 if (schema != null) { 2805 if (schema != null && !schema.equals("%")) {
2805 query.append(" AND pkschema.\"name\" ").append(composeMatchPart(schema)); 2806 query.append(" AND pkschema.\"name\" ").append(composeMatchPart(schema));
2806 } 2807 }
2807 if (table != null) { 2808 if (table != null && !table.equals("%")) {
2808 query.append(" AND pktable.\"name\" ").append(composeMatchPart(table)); 2809 query.append(" AND pktable.\"name\" ").append(composeMatchPart(table));
2809 } 2810 }
2810 } 2811 }
2811 2812
2812 query.append(" ORDER BY \"FKTABLE_SCHEM\", \"FKTABLE_NAME\", \"FK_NAME\", \"KEY_SEQ\""); 2813 query.append(" ORDER BY \"FKTABLE_SCHEM\", \"FKTABLE_NAME\", \"FK_NAME\", \"KEY_SEQ\"");
2898 || (fcatalog != null && !fcatalog.isEmpty())) { 2899 || (fcatalog != null && !fcatalog.isEmpty())) {
2899 // non-empty catalog selection. 2900 // non-empty catalog selection.
2900 // as we do not support catalogs this always results in no rows returned 2901 // as we do not support catalogs this always results in no rows returned
2901 query.append(" AND 1 = 0"); 2902 query.append(" AND 1 = 0");
2902 } else { 2903 } else {
2903 if (pschema != null) { 2904 if (pschema != null && !pschema.equals("%")) {
2904 query.append(" AND pkschema.\"name\" ").append(composeMatchPart(pschema)); 2905 query.append(" AND pkschema.\"name\" ").append(composeMatchPart(pschema));
2905 } 2906 }
2906 if (ptable != null) { 2907 if (ptable != null && !ptable.equals("%")) {
2907 query.append(" AND pktable.\"name\" ").append(composeMatchPart(ptable)); 2908 query.append(" AND pktable.\"name\" ").append(composeMatchPart(ptable));
2908 } 2909 }
2909 2910
2910 if (fschema != null) { 2911 if (fschema != null && !fschema.equals("%")) {
2911 query.append(" AND fkschema.\"name\" ").append(composeMatchPart(fschema)); 2912 query.append(" AND fkschema.\"name\" ").append(composeMatchPart(fschema));
2912 } 2913 }
2913 if (ftable != null) { 2914 if (ftable != null && !ftable.equals("%")) {
2914 query.append(" AND fktable.\"name\" ").append(composeMatchPart(ftable)); 2915 query.append(" AND fktable.\"name\" ").append(composeMatchPart(ftable));
2915 } 2916 }
2916 } 2917 }
2917 2918
2918 query.append(" ORDER BY \"FKTABLE_SCHEM\", \"FKTABLE_NAME\", \"FK_NAME\", \"KEY_SEQ\""); 2919 query.append(" ORDER BY \"FKTABLE_SCHEM\", \"FKTABLE_NAME\", \"FK_NAME\", \"KEY_SEQ\"");
3114 if (catalog != null && !catalog.isEmpty()) { 3115 if (catalog != null && !catalog.isEmpty()) {
3115 // non-empty catalog selection. 3116 // non-empty catalog selection.
3116 // as we do not support catalogs this always results in no rows returned 3117 // as we do not support catalogs this always results in no rows returned
3117 query.append(" AND 1 = 0"); 3118 query.append(" AND 1 = 0");
3118 } else { 3119 } else {
3119 if (schema != null) { 3120 if (schema != null && !schema.equals("%")) {
3120 query.append(" AND s.\"name\" ").append(composeMatchPart(schema)); 3121 query.append(" AND s.\"name\" ").append(composeMatchPart(schema));
3121 } 3122 }
3122 if (table != null) { 3123 if (table != null && !table.equals("%")) {
3123 query.append(" AND t.\"name\" ").append(composeMatchPart(table)); 3124 query.append(" AND t.\"name\" ").append(composeMatchPart(table));
3124 } 3125 }
3125 if (unique) { 3126 if (unique) {
3126 query.append(" AND k.\"name\" IS NOT NULL"); 3127 query.append(" AND k.\"name\" IS NOT NULL");
3127 } 3128 }
3281 if (catalog != null && !catalog.isEmpty()) { 3282 if (catalog != null && !catalog.isEmpty()) {
3282 // non-empty catalog selection. 3283 // non-empty catalog selection.
3283 // as we do not support catalogs this always results in no rows returned 3284 // as we do not support catalogs this always results in no rows returned
3284 query.append(" AND 1 = 0"); 3285 query.append(" AND 1 = 0");
3285 } else { 3286 } else {
3286 if (schemaPattern != null) { 3287 if (schemaPattern != null && !schemaPattern.equals("%")) {
3287 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern)); 3288 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern));
3288 } 3289 }
3289 if (typeNamePattern != null) { 3290 if (typeNamePattern != null && !typeNamePattern.equals("%")) {
3290 query.append(" AND t.\"sqlname\" ").append(composeMatchPart(typeNamePattern)); 3291 query.append(" AND t.\"sqlname\" ").append(composeMatchPart(typeNamePattern));
3291 } 3292 }
3292 } 3293 }
3293 3294
3294 if (types != null && types.length > 0) { 3295 if (types != null && types.length > 0) {
3879 if (catalog != null && !catalog.isEmpty()) { 3880 if (catalog != null && !catalog.isEmpty()) {
3880 // non-empty catalog selection. 3881 // non-empty catalog selection.
3881 // as we do not support catalogs this always results in no rows returned 3882 // as we do not support catalogs this always results in no rows returned
3882 query.append(" AND 1 = 0"); 3883 query.append(" AND 1 = 0");
3883 } else { 3884 } else {
3884 if (schemaPattern != null) { 3885 if (schemaPattern != null && !schemaPattern.equals("%")) {
3885 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern)); 3886 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern));
3886 } 3887 }
3887 if (functionNamePattern != null) { 3888 if (functionNamePattern != null && !functionNamePattern.equals("%")) {
3888 query.append(" AND f.\"name\" ").append(composeMatchPart(functionNamePattern)); 3889 query.append(" AND f.\"name\" ").append(composeMatchPart(functionNamePattern));
3889 } 3890 }
3890 } 3891 }
3891 3892
3892 query.append(" ORDER BY \"FUNCTION_SCHEM\", \"FUNCTION_NAME\", \"SPECIFIC_NAME\""); 3893 query.append(" ORDER BY \"FUNCTION_SCHEM\", \"FUNCTION_NAME\", \"SPECIFIC_NAME\"");
3997 if (catalog != null && !catalog.isEmpty()) { 3998 if (catalog != null && !catalog.isEmpty()) {
3998 // non-empty catalog selection. 3999 // non-empty catalog selection.
3999 // as we do not support catalogs this always results in no rows returned 4000 // as we do not support catalogs this always results in no rows returned
4000 query.append(" AND 1 = 0"); 4001 query.append(" AND 1 = 0");
4001 } else { 4002 } else {
4002 if (schemaPattern != null) { 4003 if (schemaPattern != null && !schemaPattern.equals("%")) {
4003 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern)); 4004 query.append(" AND s.\"name\" ").append(composeMatchPart(schemaPattern));
4004 } 4005 }
4005 if (functionNamePattern != null) { 4006 if (functionNamePattern != null && !functionNamePattern.equals("%")) {
4006 query.append(" AND f.\"name\" ").append(composeMatchPart(functionNamePattern)); 4007 query.append(" AND f.\"name\" ").append(composeMatchPart(functionNamePattern));
4007 } 4008 }
4008 if (columnNamePattern != null) { 4009 if (columnNamePattern != null && !columnNamePattern.equals("%")) {
4009 query.append(" AND a.\"name\" ").append(composeMatchPart(columnNamePattern)); 4010 query.append(" AND a.\"name\" ").append(composeMatchPart(columnNamePattern));
4010 } 4011 }
4011 } 4012 }
4012 4013
4013 query.append(" ORDER BY \"FUNCTION_SCHEM\", \"FUNCTION_NAME\", \"SPECIFIC_NAME\", \"ORDINAL_POSITION\""); 4014 query.append(" ORDER BY \"FUNCTION_SCHEM\", \"FUNCTION_NAME\", \"SPECIFIC_NAME\", \"ORDINAL_POSITION\"");