Search in sources :

Example 26 with MatcherStatement

use of com.google.cloud.spanner.pgadapter.statements.MatcherStatement in project pgadapter by GoogleCloudPlatform.

the class PSQLTest method testDescribeSelectedIndexMetadataHandlesBobbyTables.

@Test
public void testDescribeSelectedIndexMetadataHandlesBobbyTables() {
    // PSQL equivalent: \di <index>
    String sql = "SELECT n.nspname as \"Schema\",\n" + "  c.relname as \"Name\",\n" + "  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN" + " 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN" + " 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I'" + " THEN 'partitioned index' END as \"Type\",\n" + "  pg_catalog.pg_get_userbyid(c.relowner) as \"Owner\",\n" + " c2.relname as \"Table\"\n" + "FROM pg_catalog.pg_class c\n" + "     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n" + "     LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid\n" + "     LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid\n" + "WHERE c.relkind IN ('i','I','s','')\n" + "      AND n.nspname !~ '^pg_toast'\n" + "  AND c.relname OPERATOR(pg_catalog.~) '^(bobby'; DROP TABLE USERS; SELECT')$'\n" + "  AND pg_catalog.pg_table_is_visible(c.oid)\n" + "ORDER BY 1,2;";
    String expected = "SELECT table_catalog, table_schema, table_name, index_name, index_type, parent_table_name, is_unique, is_null_filtered, index_state, spanner_is_managed FROM information_schema.indexes WHERE LOWER(index_name) =" + " LOWER('bobby''; DROP TABLE USERS; SELECT''')";
    MatcherStatement matcherStatement = new MatcherStatement(options, parse(sql), connectionHandler);
    assertEquals(expected, matcherStatement.getSql());
}
Also used : MatcherStatement(com.google.cloud.spanner.pgadapter.statements.MatcherStatement) Test(org.junit.Test)

Example 27 with MatcherStatement

use of com.google.cloud.spanner.pgadapter.statements.MatcherStatement in project pgadapter by GoogleCloudPlatform.

the class PSQLTest method testTableSelectAutocomplete.

@Test
public void testTableSelectAutocomplete() {
    // PSQL equivalent: SELECT <table>
    String sql = "SELECT pg_catalog.quote_ident(c.relname) FROM pg_catalog.pg_class c WHERE c.relkind IN" + " ('r', 'S', 'v', 'm', 'f', 'p') AND" + " substring(pg_catalog.quote_ident(c.relname),1,0)='user' AND" + " pg_catalog.pg_table_is_visible(c.oid) AND c.relnamespace <> (SELECT oid FROM" + " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')\n" + "UNION\n" + "SELECT pg_catalog.quote_ident(n.nspname) || '.' FROM pg_catalog.pg_namespace n WHERE" + " substring(pg_catalog.quote_ident(n.nspname) || '.',1,0)='' AND (SELECT" + " pg_catalog.count(*) FROM pg_catalog.pg_namespace WHERE" + " substring(pg_catalog.quote_ident(nspname) || '.',1,0) =" + " substring('',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1\n" + "UNION\n" + "SELECT pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname)" + " FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n WHERE c.relnamespace = n.oid" + " AND c.relkind IN ('r', 'S', 'v', 'm', 'f', 'p') AND" + " substring(pg_catalog.quote_ident(n.nspname) || '.' ||" + " pg_catalog.quote_ident(c.relname),1,0)='' AND" + " substring(pg_catalog.quote_ident(n.nspname) || '.',1,0) =" + " substring('',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1) AND (SELECT" + " pg_catalog.count(*) FROM pg_catalog.pg_namespace WHERE" + " substring(pg_catalog.quote_ident(nspname) || '.',1,0) =" + " substring('',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1\n" + "LIMIT 1000";
    String expected = "SELECT table_name AS quote_ident FROM information_schema.tables WHERE" + " table_schema = 'public' and STARTS_WITH(LOWER(table_name)," + " LOWER('user')) LIMIT 1000";
    MatcherStatement matcherStatement = new MatcherStatement(options, parse(sql), connectionHandler);
    assertEquals(expected, matcherStatement.getSql());
}
Also used : MatcherStatement(com.google.cloud.spanner.pgadapter.statements.MatcherStatement) Test(org.junit.Test)

Example 28 with MatcherStatement

use of com.google.cloud.spanner.pgadapter.statements.MatcherStatement in project pgadapter by GoogleCloudPlatform.

the class PSQLTest method testDescribeIndexMetadataAutocomplete.

@Test
public void testDescribeIndexMetadataAutocomplete() {
    // PSQL equivalent: \\di <index>
    String sql = "SELECT pg_catalog.quote_ident(c.relname) FROM pg_catalog.pg_class c WHERE c.relkind IN" + " ('i', 'I') AND substring(pg_catalog.quote_ident(c.relname),1,5)='index' AND" + " pg_catalog.pg_table_is_visible(c.oid) AND c.relnamespace <> (SELECT oid FROM" + " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')\n" + "UNION\n" + "SELECT pg_catalog.quote_ident(n.nspname) || '.' FROM pg_catalog.pg_namespace n WHERE" + " substring(pg_catalog.quote_ident(n.nspname) || '.',1,5)='index' AND (SELECT" + " pg_catalog.count(*) FROM pg_catalog.pg_namespace WHERE" + " substring(pg_catalog.quote_ident(nspname) || '.',1,5) =" + " substring('index',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1\n" + "UNION\n" + "SELECT pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname)" + " FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n WHERE c.relnamespace = n.oid" + " AND c.relkind IN ('i', 'I') AND substring(pg_catalog.quote_ident(n.nspname) || '.'" + " || pg_catalog.quote_ident(c.relname),1,5)='index' AND" + " substring(pg_catalog.quote_ident(n.nspname) || '.',1,5) =" + " substring('index',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1) AND" + " (SELECT pg_catalog.count(*) FROM pg_catalog.pg_namespace WHERE" + " substring(pg_catalog.quote_ident(nspname) || '.',1,5) =" + " substring('index',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1\n" + "LIMIT 1000";
    String expected = "SELECT index_name AS quote_ident FROM INFORMATION_SCHEMA.INDEXES WHERE" + " STARTS_WITH(LOWER(index_name), LOWER('index')) LIMIT 1000";
    MatcherStatement matcherStatement = new MatcherStatement(options, parse(sql), connectionHandler);
    assertEquals(expected, matcherStatement.getSql());
}
Also used : MatcherStatement(com.google.cloud.spanner.pgadapter.statements.MatcherStatement) Test(org.junit.Test)

Example 29 with MatcherStatement

use of com.google.cloud.spanner.pgadapter.statements.MatcherStatement in project pgadapter by GoogleCloudPlatform.

the class PSQLTest method testListDatabaseCommandFailsButPrintsUnknown.

@Test
public void testListDatabaseCommandFailsButPrintsUnknown() {
    // PSQL equivalent: \l <table>
    String sql = "SELECT d.datname as \"Name\",\n" + "       pg_catalog.pg_get_userbyid(d.datdba) as \"Owner\",\n" + "       pg_catalog.pg_encoding_to_char(d.encoding) as \"Encoding\",\n" + "       pg_catalog.array_to_string(d.datacl, '\\n') AS \"Access privileges\"\n" + "FROM pg_catalog.pg_database d\n" + "WHERE d.datname OPERATOR(pg_catalog.~) '^(users)$'\n" + "ORDER BY 1;";
    String expected = "SELECT '' AS Name";
    // TODO: Add Connection#getDatabase() to Connection API and test here what happens if that
    // method throws an exception.
    MatcherStatement matcherStatement = new MatcherStatement(options, parse(sql), connectionHandler);
    assertEquals(expected, matcherStatement.getSql());
}
Also used : MatcherStatement(com.google.cloud.spanner.pgadapter.statements.MatcherStatement) Test(org.junit.Test)

Aggregations

MatcherStatement (com.google.cloud.spanner.pgadapter.statements.MatcherStatement)29 Test (org.junit.Test)29 JSONParser (org.json.simple.parser.JSONParser)2