Skip to content

Commit

Permalink
Merge pull request #463 from dolthub/taylor/select-from
Browse files Browse the repository at this point in the history
Workaround for current_database and current_schema
  • Loading branch information
tbantle22 authored Jul 1, 2024
2 parents 0755b0c + e326552 commit 9c59678
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
25 changes: 25 additions & 0 deletions server/connection_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ func (h *ConnectionHandler) handleQuery(message messages.Query) error {
return err
}

// TODO: Remove this once we support `SELECT * FROM function()` syntax
// Github issue: https://github.com/dolthub/doltgresql/issues/464
handled, err = h.handledWorkbenchCommands(message.String)
if handled || err != nil {
return err
}

query, err := h.convertQuery(message.String)
if err != nil {
return err
Expand Down Expand Up @@ -836,6 +843,24 @@ func (h *ConnectionHandler) handledPSQLCommands(statement string) (bool, error)
return false, nil
}

// handledWorkbenchCommands handles commands used by some workbenches, such as dolt-workbench.
func (h *ConnectionHandler) handledWorkbenchCommands(statement string) (bool, error) {
lower := strings.ToLower(statement)
if lower == "select * from current_schema()" || lower == "select * from current_schema();" {
return true, h.query(ConvertedQuery{
String: `SELECT search_path AS 'current_schema';`,
StatementTag: "SELECT",
})
}
if lower == "select * from current_database()" || lower == "select * from current_database();" {
return true, h.query(ConvertedQuery{
String: `SELECT DATABASE() AS 'current_database';`,
StatementTag: "SELECT",
})
}
return false, nil
}

// endOfMessages should be called from HandleConnection or a function within HandleConnection. This represents the end
// of the message slice, which may occur naturally (all relevant response messages have been sent) or on error. Once
// endOfMessages has been called, no further messages should be sent, and the connection loop should wait for the next
Expand Down
2 changes: 1 addition & 1 deletion testing/bats/pgcatalog.bats
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ teardown() {
[[ "$output" =~ "attname" ]] || false
[ "${#lines[@]}" -eq 1 ]

run query_server_for_db newdb --csv -c "SELECT relname FROM pg_catalog.pg_class WHERE relname = 'test1';"
run query_server_for_db newdb --csv -c "SELECT relname FROM pg_catalog.pg_class WHERE relname = 'test1';"
[ "$status" -eq 0 ]
[[ "$output" =~ "relname" ]] || false
[ "${#lines[@]}" -eq 1 ]
Expand Down
45 changes: 45 additions & 0 deletions testing/bats/workbench-commands.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/setup/common.bash

setup() {
setup_common
start_sql_server
query_server <<SQL
CREATE TABLE test1 (pk BIGINT PRIMARY KEY, v1 SMALLINT);
CREATE TABLE test2 (pk BIGINT PRIMARY KEY, v1 INTEGER, v2 SMALLINT);
INSERT INTO test1 VALUES (1, 2), (6, 7);
INSERT INTO test2 VALUES (3, 4, 5), (8, 9, 0);
CREATE VIEW testview AS SELECT * FROM test1;
SQL
}

teardown() {
teardown_common
}

@test 'workbench-commands: current_schema' {
run query_server -c "SELECT * FROM current_schema()"
[ "$status" -eq 0 ]
[[ "$output" =~ "public" ]] || false

run query_server <<SQL
CREATE SCHEMA test_schema;
SET search_path TO test_schema;
SELECT * FROM current_schema();
SQL
[ "$status" -eq 0 ]
[[ "$output" =~ "test_schema" ]] || false
}

@test 'workbench-commands: current_database' {
run query_server -c "SELECT * FROM current_database();"
[ "$status" -eq 0 ]
[[ "$output" =~ "doltgres" ]] || false

run query_server -c "CREATE DATABASE newdb;"
[ "$status" -eq 0 ]

run query_server_for_db newdb -c "SELECT * FROM current_database()"
[ "$status" -eq 0 ]
[[ "$output" =~ "newdb" ]] || false
}

0 comments on commit 9c59678

Please sign in to comment.