Search in sources :

Example 16 with MatcherStatement

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

the class PSQLTest method testTableAttributesAutocomplete.

@Test
public void testTableAttributesAutocomplete() {
    // PSQL equivalent: INSERT INTO table_name (<attribute>)
    // PSQL equivalent: SELECT * FROM table_name WHERE <attribute>
    String sql = "SELECT pg_catalog.quote_ident(attname)   FROM pg_catalog.pg_attribute a," + " pg_catalog.pg_class c  WHERE c.oid = a.attrelid    AND a.attnum > 0    AND NOT" + " a.attisdropped    AND substring(pg_catalog.quote_ident(attname),1,3)='age'    AND" + " (pg_catalog.quote_ident(relname)='user'         OR '\"' || relname || '\"'='user') " + "   AND pg_catalog.pg_table_is_visible(c.oid)\n" + "LIMIT 1000";
    String expected = "SELECT column_name AS quote_ident FROM information_schema.columns WHERE" + " table_name = 'user' AND STARTS_WITH(LOWER(COLUMN_NAME), LOWER('age')) 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 17 with MatcherStatement

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

the class PSQLTest method testDescribeTableCatalogTranslates.

@Test
public void testDescribeTableCatalogTranslates() {
    // PSQL equivalent: \d <table> (2)
    String sql = "SELECT relchecks, relkind, relhasindex, relhasrules, reltriggers <> 0, false, false," + " relhasoids, false as relispartition, '', ''\n" + "FROM pg_catalog.pg_class WHERE oid = '-2264987671676060158';";
    String expected = "SELECT" + " 0 as relcheck," + " 'r' as relkind," + " false as relhasindex," + " false as relhasrules," + " false as reltriggers," + " false as bool1," + " false as bool2," + " false as relhasoids," + " '' as str1," + " '' as str2";
    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 18 with MatcherStatement

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

the class PSQLTest method testDescribeSelectedSchemaMetadataHandlesBobbyTables.

@Test
public void testDescribeSelectedSchemaMetadataHandlesBobbyTables() {
    // PSQL equivalent: \dn <schema>
    String sql = "SELECT n.nspname AS \"Name\",\n" + "  pg_catalog.pg_get_userbyid(n.nspowner) AS \"Owner\"\n" + "FROM pg_catalog.pg_namespace n\n" + "WHERE n.nspname OPERATOR(pg_catalog.~) '^(bobby'; DROP TABLE USERS; SELECT')$'\n" + "ORDER BY 1;";
    String expected = "SELECT * FROM information_schema.schemata WHERE LOWER(schema_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 19 with MatcherStatement

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

the class PSQLTest method testDescribeTableMetadataTranslates.

@Test
public void testDescribeTableMetadataTranslates() {
    // PSQL equivalent: \d <table> (3)
    String sql = "SELECT a.attname,\n" + "  pg_catalog.format_type(a.atttypid, a.atttypmod),\n" + "  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid, true) for 128)\n" + "   FROM pg_catalog.pg_attrdef d\n" + "   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),\n" + "  a.attnotnull,\n" + "  NULL AS attcollation,\n" + "  ''::pg_catalog.char AS attidentity,\n" + "  ''::pg_catalog.char AS attgenerated\n" + "FROM pg_catalog.pg_attribute a\n" + "WHERE a.attrelid = '-1' AND a.attnum > 0 AND NOT a.attisdropped\n" + "ORDER BY a.attnum;";
    String expected = "SELECT" + " t.column_name as attname," + " t.data_type as format_type," + " '' as substring," + " t.is_nullable = 'NO' as attnotnull," + " null::INTEGER as attcollation," + " null::INTEGER as indexdef," + " null::INTEGER as attfdwoptions" + " FROM" + " information_schema.columns AS t" + " WHERE" + " t.table_schema='public'" + " AND t.table_name = '-1'";
    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 20 with MatcherStatement

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

the class PSQLTest method testListCommandTranslates.

@Test
public void testListCommandTranslates() {
    // PSQL equivalent: \l
    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" + "ORDER BY 1;";
    String expected = "SELECT '' AS Name";
    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