@@ -280,6 +280,35 @@ func (c *conn) sendSimpleQuery(sql string) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *conn) Execute(sql string) (commandTag string, err error) {
|
||||||
|
if err = c.sendSimpleQuery(sql); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
var t byte
|
||||||
|
var r *messageReader
|
||||||
|
if t, r, err = c.rxMsg(); err == nil {
|
||||||
|
switch t {
|
||||||
|
case readyForQuery:
|
||||||
|
return
|
||||||
|
case rowDescription:
|
||||||
|
case dataRow:
|
||||||
|
case commandComplete:
|
||||||
|
commandTag = r.readString()
|
||||||
|
default:
|
||||||
|
if err = c.processContextFreeMsg(t, r); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
panic("Unreachable")
|
||||||
|
}
|
||||||
|
|
||||||
// Processes messages that are not exclusive to one context such as
|
// Processes messages that are not exclusive to one context such as
|
||||||
// authentication or query response. The response to these messages
|
// authentication or query response. The response to these messages
|
||||||
// is the same regardless of when they occur.
|
// is the same regardless of when they occur.
|
||||||
@@ -290,6 +319,8 @@ func (c *conn) processContextFreeMsg(t byte, r *messageReader) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
case errorResponse:
|
case errorResponse:
|
||||||
return c.rxErrorResponse(r)
|
return c.rxErrorResponse(r)
|
||||||
|
case noticeResponse:
|
||||||
|
return nil
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Received unknown message type: %c", t)
|
return fmt.Errorf("Received unknown message type: %c", t)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,6 +86,35 @@ func TestConnectWithMD5Password(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExecute(t *testing.T) {
|
||||||
|
conn := getSharedConn()
|
||||||
|
|
||||||
|
results, err := conn.Execute("create temporary table foo(id serial primary key);")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Execute failed: " + err.Error())
|
||||||
|
}
|
||||||
|
if results != "CREATE TABLE" {
|
||||||
|
t.Error("Unexpected results from Execute")
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err = conn.Execute("drop table foo;")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Execute failed: " + err.Error())
|
||||||
|
}
|
||||||
|
if results != "DROP TABLE" {
|
||||||
|
t.Error("Unexpected results from Execute")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multiple statements can be executed -- last command tag is returned
|
||||||
|
results, err = conn.Execute("create temporary table foo(id serial primary key); drop table foo;")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Execute failed: " + err.Error())
|
||||||
|
}
|
||||||
|
if results != "DROP TABLE" {
|
||||||
|
t.Error("Unexpected results from Execute")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestQuery(t *testing.T) {
|
func TestQuery(t *testing.T) {
|
||||||
conn := getSharedConn()
|
conn := getSharedConn()
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ const (
|
|||||||
dataRow = 'D'
|
dataRow = 'D'
|
||||||
commandComplete = 'C'
|
commandComplete = 'C'
|
||||||
errorResponse = 'E'
|
errorResponse = 'E'
|
||||||
|
noticeResponse = 'N'
|
||||||
)
|
)
|
||||||
|
|
||||||
type startupMessage struct {
|
type startupMessage struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user