Extracting connection

pgx-vs-pq
Jack Christensen 2013-03-30 17:56:31 -05:00
parent 6fb92c12b8
commit 5563e0c825
3 changed files with 49 additions and 20 deletions

40
conn.go Normal file
View File

@ -0,0 +1,40 @@
package pqx
import (
"encoding/binary"
"net"
)
type conn struct {
conn net.Conn
}
func Connect(options map[string] string) (c *conn, err error) {
c = new(conn)
var present bool
var socket string
if socket, present = options["socket"]; present {
c.conn, err = net.Dial("unix", socket)
if err != nil { return nil, err }
}
// conn, err := net.Dial("tcp", "localhost:5432")
msg := newStartupMessage()
msg.options["user"] = "jack"
msg.WriteTo(c.conn)
buf := make([]byte, 512)
num, _ := c.conn.Read(buf)
println(string(buf[0:1]))
println(binary.BigEndian.Uint32(buf[1:5]))
println(binary.BigEndian.Uint32(buf[5:9]))
println(num)
return c, nil
}

View File

@ -1,29 +1,12 @@
package pqx
import (
"encoding/binary"
"net"
"testing"
)
func TestXxx(t *testing.T) {
conn, err := net.Dial("unix", "/private/tmp/.s.PGSQL.5432")
// conn, err := net.Dial("tcp", "localhost:5432")
func TestConnect(t *testing.T) {
_, err := Connect(map[string] string { "socket": "/private/tmp/.s.PGSQL.5432" })
if err != nil {
// handle error
t.Fatal("Unable to establish connection")
}
msg := newStartupMessage()
msg.options["user"] = "jack"
msg.WriteTo(conn)
buf := make([]byte, 512)
num, _ := conn.Read(buf)
println(string(buf[0:1]))
println(binary.BigEndian.Uint32(buf[1:5]))
println(binary.BigEndian.Uint32(buf[5:9]))
println(num)
}

View File

@ -33,3 +33,9 @@ func (self *startupMessage) WriteTo(w io.Writer) (n int64, err error) {
n32, err = w.Write(buf)
return int64(n32), err
}
type authenticationOk struct {
}