-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcf3fbd
commit 6e9fa42
Showing
6 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "debug", | ||
"program": "${fileDirname}" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"go.testEnvVars": { | ||
"PGX_TEST_DATABASE":"host=127.0.0.1 user=gamerhound password=gamerhound dbname=gamerhound" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"reflect" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
) | ||
|
||
func main() { | ||
pool, err := pgxpool.New(context.Background(), "postgres://gamerhound:gamerhound@localhost:5432/gamerhound") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer pool.Close() | ||
|
||
// Create the enum type. | ||
_, err = pool.Exec(context.Background(), `DROP TYPE IF EXISTS test_enum_type`) | ||
if err != nil { | ||
log.Print(err) | ||
return | ||
} | ||
_, err = pool.Exec(context.Background(), `CREATE TYPE test_enum_type AS ENUM ('a', 'b')`) | ||
if err != nil { | ||
log.Print(err) | ||
return | ||
} | ||
|
||
err = testQuery(pool, "SELECT 'a'", "a") | ||
if err != nil { | ||
log.Printf("test TEXT error: %s\n", err) | ||
} | ||
|
||
err = testQuery(pool, "SELECT 'a'::test_enum_type", "a") | ||
if err != nil { | ||
log.Printf("test ENUM error: %s\n", err) | ||
} | ||
|
||
err = testQuery(pool, "SELECT '{}'::jsonb", "{}") | ||
if err != nil { | ||
log.Printf("test JSONB error: %s\n", err) | ||
} | ||
} | ||
|
||
// T implements the sql.Scanner interface. | ||
type T struct { | ||
v *any | ||
} | ||
|
||
func (t T) Scan(v any) error { | ||
*t.v = v | ||
return nil | ||
} | ||
|
||
// testQuery executes the query and checks if the scanned value matches | ||
// the expected result. | ||
func testQuery(pool *pgxpool.Pool, query string, expected any) error { | ||
rows, err := pool.Query(context.Background(), query) | ||
if err != nil { | ||
return err | ||
} | ||
// defer rows.Close() | ||
|
||
var got any | ||
t := T{v: &got} | ||
for rows.Next() { | ||
if err := rows.Scan(t); err != nil { | ||
return err | ||
} | ||
} | ||
if err = rows.Err(); err != nil { | ||
return err | ||
} | ||
if !reflect.DeepEqual(got, expected) { | ||
return fmt.Errorf("expected %#v; got %#v", expected, got) | ||
} | ||
return nil | ||
} |