TestMultiStatementBehavior tests that a single query string containing multiple semicolon-separated statements is handled correctly. This is distinct from TestProtocolMultipleStatements which sends separate query calls sequentially.
(t *testing.T)
| 937 | // semicolon-separated statements is handled correctly. This is distinct from |
| 938 | // TestProtocolMultipleStatements which sends separate query calls sequentially. |
| 939 | func TestMultiStatementBehavior(t *testing.T) { |
| 940 | t.Run("two_selects", func(t *testing.T) { |
| 941 | conn := openDuckgresConn(t) |
| 942 | defer func() { _ = conn.Close() }() |
| 943 | |
| 944 | // lib/pq sends this as a single simple query message. |
| 945 | // The server should handle it — either returning the last result |
| 946 | // or erroring gracefully. |
| 947 | rows, err := conn.Query("SELECT 1; SELECT 2") |
| 948 | if err != nil { |
| 949 | // Some PG-compatible servers reject multi-statement in simple query — acceptable |
| 950 | t.Logf("Multi-statement query returned error (acceptable): %v", err) |
| 951 | return |
| 952 | } |
| 953 | defer func() { _ = rows.Close() }() |
| 954 | |
| 955 | // If it succeeded, we should get at least one result |
| 956 | if !rows.Next() { |
| 957 | t.Error("Expected at least one row from multi-statement query") |
| 958 | } |
| 959 | }) |
| 960 | |
| 961 | t.Run("create_then_insert", func(t *testing.T) { |
| 962 | conn := openDuckgresConn(t) |
| 963 | defer func() { _ = conn.Close() }() |
| 964 | |
| 965 | // Try DDL + DML in one string |
| 966 | _, err := conn.Exec("CREATE TABLE multi_stmt_test (id INTEGER); INSERT INTO multi_stmt_test VALUES (1)") |
| 967 | if err != nil { |
| 968 | t.Logf("Multi-statement DDL+DML returned error (acceptable): %v", err) |
| 969 | // Clean up in case the CREATE succeeded |
| 970 | _, _ = conn.Exec("DROP TABLE IF EXISTS multi_stmt_test") |
| 971 | return |
| 972 | } |
| 973 | |
| 974 | // Verify if it worked |
| 975 | var count int |
| 976 | if err := conn.QueryRow("SELECT COUNT(*) FROM multi_stmt_test").Scan(&count); err != nil { |
| 977 | t.Fatalf("Query failed: %v", err) |
| 978 | } |
| 979 | if count != 1 { |
| 980 | t.Errorf("Expected 1 row, got %d", count) |
| 981 | } |
| 982 | |
| 983 | mustExec(t, conn, "DROP TABLE multi_stmt_test") |
| 984 | }) |
| 985 | |
| 986 | t.Run("empty_statements", func(t *testing.T) { |
| 987 | conn := openDuckgresConn(t) |
| 988 | defer func() { _ = conn.Close() }() |
| 989 | |
| 990 | // Trailing semicolons |
| 991 | _, err := conn.Exec("SELECT 1;") |
| 992 | if err != nil { |
| 993 | t.Logf("Trailing semicolon error (acceptable): %v", err) |
| 994 | } |
| 995 | |
| 996 | // Connection should still work |