diff --git a/README.md b/README.md index 4f9e4d17..375bcc47 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ pgx === -Experimental PostgreSQL client library for Go \ No newline at end of file +Experimental PostgreSQL client library for Go + +Testing +------- + +To setup the test environment create a database named pgx_test. diff --git a/conn.go b/conn.go index 82e8c5f5..1b66efa2 100644 --- a/conn.go +++ b/conn.go @@ -17,6 +17,9 @@ type conn struct { txStatus byte } +// options: +// socket: path to unix domain socket +// database: name of database func Connect(options map[string]string) (c *conn, err error) { c = new(conn) @@ -35,7 +38,12 @@ func Connect(options map[string]string) (c *conn, err error) { // conn, err := net.Dial("tcp", "localhost:5432") + var database string + msg := newStartupMessage() + if database, present = options["database"]; present { + msg.options["database"] = database + } msg.options["user"] = "jack" c.txStartupMessage(msg) diff --git a/conn_test.go b/conn_test.go index 512297bb..d63b3039 100644 --- a/conn_test.go +++ b/conn_test.go @@ -5,7 +5,7 @@ import ( ) func TestConnect(t *testing.T) { - conn, err := Connect(map[string]string{"socket": "/private/tmp/.s.PGSQL.5432"}) + conn, err := Connect(map[string]string{"socket": "/private/tmp/.s.PGSQL.5432", "database": "pgx_test"}) if err != nil { t.Fatal("Unable to establish connection") } @@ -22,6 +22,12 @@ func TestConnect(t *testing.T) { t.Error("Backend secret key not stored") } + var rows []map[string]string + rows, err = conn.Query("select current_database()") + if err != nil || rows[0]["current_database"] != "pgx_test" { + t.Error("Did not connect to specified database (pgx_text)") + } + err = conn.Close() if err != nil { t.Fatal("Unable to close connection")