|
27 | 27 | import com.google.common.collect.ImmutableMap; |
28 | 28 | import com.google.common.collect.ImmutableSet; |
29 | 29 | import java.util.HashSet; |
| 30 | +import java.util.Locale; |
30 | 31 | import java.util.Map; |
31 | 32 | import java.util.Map.Entry; |
32 | 33 | import java.util.Set; |
@@ -66,6 +67,9 @@ public class PgCatalog { |
66 | 67 | .put( |
67 | 68 | new TableOrIndexName(null, "pg_sequences"), |
68 | 69 | new TableOrIndexName(null, "pg_sequences")) |
| 70 | + .put( |
| 71 | + new TableOrIndexName("information_schema", "sequences"), |
| 72 | + new TableOrIndexName(null, "pg_information_schema_sequences")) |
69 | 73 | .put( |
70 | 74 | new TableOrIndexName("pg_catalog", "pg_settings"), |
71 | 75 | new TableOrIndexName(null, "pg_settings")) |
@@ -95,7 +99,9 @@ public class PgCatalog { |
95 | 99 | new TableOrIndexName(null, "pg_range"), new PgRange(), |
96 | 100 | new TableOrIndexName(null, "pg_type"), new PgType(), |
97 | 101 | new TableOrIndexName(null, "pg_sequence"), new PgSequence(), |
98 | | - new TableOrIndexName(null, "pg_sequences"), new PgSequences()); |
| 102 | + new TableOrIndexName(null, "pg_sequences"), new PgSequences(), |
| 103 | + new TableOrIndexName(null, "pg_information_schema_sequences"), |
| 104 | + new InformationSchemaSequences()); |
99 | 105 | private final SessionState sessionState; |
100 | 106 |
|
101 | 107 | public PgCatalog(@Nonnull SessionState sessionState, @Nonnull WellKnownClient wellKnownClient) { |
@@ -130,7 +136,8 @@ public PgCatalog(@Nonnull SessionState sessionState, @Nonnull WellKnownClient we |
130 | 136 | /** Replace supported pg_catalog tables with Common Table Expressions. */ |
131 | 137 | public Statement replacePgCatalogTables(Statement statement) { |
132 | 138 | // Only replace tables if the statement contains at least one of the known prefixes. |
133 | | - if (checkPrefixes.stream().noneMatch(prefix -> statement.getSql().contains(prefix))) { |
| 139 | + String sql = statement.getSql().toLowerCase(Locale.ENGLISH); |
| 140 | + if (checkPrefixes.stream().noneMatch(sql::contains)) { |
134 | 141 | return statement; |
135 | 142 | } |
136 | 143 |
|
@@ -514,4 +521,22 @@ public String getTableExpression() { |
514 | 521 | return PG_SEQUENCE_CTE; |
515 | 522 | } |
516 | 523 | } |
| 524 | + |
| 525 | + private static class InformationSchemaSequences implements PgCatalogTable { |
| 526 | + // The name of this CTE is a little strange, but that is to make sure it does not accidentally |
| 527 | + // collide with any user-defined table or view. |
| 528 | + private static final String INFORMATION_SCHEMA_SEQUENCES_CTE = |
| 529 | + "pg_information_schema_sequences as (\n" |
| 530 | + + "select * from (" |
| 531 | + + "select ''::varchar as sequence_catalog, ''::varchar as sequence_schema, ''::varchar as sequence_name, " |
| 532 | + + "''::varchar as data_type, 0::bigint as numeric_precision, 0::bigint as numeric_precision_radix, " |
| 533 | + + "''::varchar as start_value, ''::varchar as minimum_value, ''::varchar as maximum_value, " |
| 534 | + + "''::varchar as increment, 'NO'::varchar as cycle_option\n" |
| 535 | + + ") seq where false)"; |
| 536 | + |
| 537 | + @Override |
| 538 | + public String getTableExpression() { |
| 539 | + return INFORMATION_SCHEMA_SEQUENCES_CTE; |
| 540 | + } |
| 541 | + } |
517 | 542 | } |
0 commit comments