mirror of
https://github.com/jackc/pgx.git
synced 2025-05-28 02:02:52 +00:00
parent
cbf03821e1
commit
d306d42afb
31
conn.go
31
conn.go
@ -280,6 +280,35 @@ func (c *conn) sendSimpleQuery(sql string) (err error) {
|
||||
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
|
||||
// authentication or query response. The response to these messages
|
||||
// is the same regardless of when they occur.
|
||||
@ -290,6 +319,8 @@ func (c *conn) processContextFreeMsg(t byte, r *messageReader) (err error) {
|
||||
return nil
|
||||
case errorResponse:
|
||||
return c.rxErrorResponse(r)
|
||||
case noticeResponse:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("Received unknown message type: %c", t)
|
||||
}
|
||||
|
29
conn_test.go
29
conn_test.go
@ -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) {
|
||||
conn := getSharedConn()
|
||||
|
||||
|
@ -16,6 +16,7 @@ const (
|
||||
dataRow = 'D'
|
||||
commandComplete = 'C'
|
||||
errorResponse = 'E'
|
||||
noticeResponse = 'N'
|
||||
)
|
||||
|
||||
type startupMessage struct {
|
||||
|
Loading…
x
Reference in New Issue
Block a user