diff --git a/conn.go b/conn.go new file mode 100644 index 00000000..4169218e --- /dev/null +++ b/conn.go @@ -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 +} \ No newline at end of file diff --git a/conn_test.go b/conn_test.go index 164fbc17..ef2baa1f 100644 --- a/conn_test.go +++ b/conn_test.go @@ -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) } diff --git a/messages.go b/messages.go index 840dcc93..ebb848eb 100644 --- a/messages.go +++ b/messages.go @@ -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 { +} + + +