Skip to content

Commit

Permalink
Merge pull request #169 from Mytherin/semicolon
Browse files Browse the repository at this point in the history
Fix #151 - strip trailing semicolon in postgres_query
  • Loading branch information
Mytherin authored Jan 18, 2024
2 parents 105e4dc + f626d32 commit e3e4f20
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/postgres_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ static unique_ptr<FunctionData> PGQueryBind(ClientContext &context, TableFunctio
vector<LogicalType> &return_types, vector<string> &names) {
auto result = make_uniq<PostgresBindData>();

if (input.inputs[0].IsNull() || input.inputs[1].IsNull()) {
throw BinderException("Parameters to postgres_query cannot be NULL");
}

// look up the database to query
auto db_name = input.inputs[0].GetValue<string>();
auto &db_manager = DatabaseManager::Get(context);
Expand All @@ -27,6 +31,12 @@ static unique_ptr<FunctionData> PGQueryBind(ClientContext &context, TableFunctio
auto &pg_catalog = catalog.Cast<PostgresCatalog>();
auto &transaction = Transaction::Get(context, catalog).Cast<PostgresTransaction>();
auto sql = input.inputs[1].GetValue<string>();
// strip any trailing semicolons
StringUtil::RTrim(sql);
while (!sql.empty() && sql.back() == ';') {
sql = sql.substr(0, sql.size() - 1);
StringUtil::RTrim(sql);
}

auto &con = transaction.GetConnection();
auto conn = con.GetConn();
Expand Down
36 changes: 36 additions & 0 deletions test/sql/scanner/postgres_query.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

require postgres_scanner

require-env POSTGRES_TEST_DATABASE_AVAILABLE

statement ok
ATTACH 'dbname=postgresscanner' AS s1 (TYPE POSTGRES)

Expand Down Expand Up @@ -73,17 +75,51 @@ SELECT TYPEOF(COLUMNS(*)) FROM postgres_query('s1', 'SELECT * FROM pg_datearrayt
----
DATE[] TIME[] TIME WITH TIME ZONE[] TIMESTAMP[] TIMESTAMP WITH TIME ZONE[]

# semicolons
query III
select * from postgres_query('s1', 'SELECT * FROM cars;') WHERE color='red';
----
ferari testarosa red

query III
select * from postgres_query('s1', 'SELECT * FROM cars; ; ') WHERE color='red';
----
ferari testarosa red

# incorrect usage
statement error
select * from postgres_query('s1', 'CREATE TABLE my_table(i INT)');
----
No fields returned by query

# database not found
statement error
select * from postgres_query('xx', 'SELECT * FROM cars');
----
Failed to find attached database

# empty statements
statement error
select * from postgres_query('s1', '') WHERE color='red';
----
must be a SELECT statement

statement error
select * from postgres_query('s1', ';;') WHERE color='red';
----
must be a SELECT statement

# NULL parameters
statement error
select * from postgres_query(NULL, 'SELECT 42');
----
cannot be NULL

statement error
select * from postgres_query('s1', NULL);
----
cannot be NULL

# what if the database is not a postgres database
statement ok
ATTACH ':memory:' AS ddb
Expand Down

0 comments on commit e3e4f20

Please sign in to comment.