Add CommandTag

scan-io
Jack Christensen 2014-05-19 09:32:31 -05:00
parent bc2a120301
commit f119d5221c
4 changed files with 40 additions and 4 deletions

18
conn.go
View File

@ -77,6 +77,20 @@ type Notification struct {
Payload string Payload string
} }
type CommandTag string
// RowsAffected returns the number of rows affected. If the CommandTag was not
// for a row affecting command (such as "CREATE TABLE") then it returns 0
func (ct CommandTag) RowsAffected() int64 {
words := strings.SplitN(string(ct), " ", 2)
if len(words) != 2 {
return 0
}
n, _ := strconv.ParseInt(words[1], 10, 64)
return n
}
// NotSingleRowError is returned when exactly 1 row is expected, but 0 or more than // NotSingleRowError is returned when exactly 1 row is expected, but 0 or more than
// 1 row is returned // 1 row is returned
type NotSingleRowError struct { type NotSingleRowError struct {
@ -760,7 +774,7 @@ func (c *Conn) sendPreparedQuery(ps *preparedStatement, arguments ...interface{}
// Execute executes sql. sql can be either a prepared statement name or an SQL string. // Execute executes sql. sql can be either a prepared statement name or an SQL string.
// arguments will be sanitized before being interpolated into sql strings. arguments // arguments will be sanitized before being interpolated into sql strings. arguments
// should be referenced positionally from the sql string as $1, $2, etc. // should be referenced positionally from the sql string as $1, $2, etc.
func (c *Conn) Execute(sql string, arguments ...interface{}) (commandTag string, err error) { func (c *Conn) Execute(sql string, arguments ...interface{}) (commandTag CommandTag, err error) {
defer func() { defer func() {
if err != nil { if err != nil {
c.logger.Error(fmt.Sprintf("Execute `%s` with %v failed: %v", sql, arguments, err)) c.logger.Error(fmt.Sprintf("Execute `%s` with %v failed: %v", sql, arguments, err))
@ -781,7 +795,7 @@ func (c *Conn) Execute(sql string, arguments ...interface{}) (commandTag string,
case dataRow: case dataRow:
case bindComplete: case bindComplete:
case commandComplete: case commandComplete:
commandTag = r.ReadCString() commandTag = CommandTag(r.ReadCString())
default: default:
if e := c.processContextFreeMsg(t, r); e != nil && err == nil { if e := c.processContextFreeMsg(t, r); e != nil && err == nil {
err = e err = e

View File

@ -223,7 +223,7 @@ func (p *ConnPool) SelectValues(sql string, arguments ...interface{}) (values []
} }
// Execute acquires a connection, delegates the call to that connection, and releases the connection // Execute acquires a connection, delegates the call to that connection, and releases the connection
func (p *ConnPool) Execute(sql string, arguments ...interface{}) (commandTag string, err error) { func (p *ConnPool) Execute(sql string, arguments ...interface{}) (commandTag CommandTag, err error) {
var c *Conn var c *Conn
if c, err = p.Acquire(); err != nil { if c, err = p.Acquire(); err != nil {
return return

View File

@ -854,3 +854,25 @@ func TestFatalTxError(t *testing.T) {
t.Fatal("Connection should not be live but was") t.Fatal("Connection should not be live but was")
} }
} }
func TestCommandTag(t *testing.T) {
var tests = []struct {
commandTag pgx.CommandTag
rowsAffected int64
}{
{commandTag: "UPDATE 0", rowsAffected: 0},
{commandTag: "UPDATE 1", rowsAffected: 1},
{commandTag: "DELETE 0", rowsAffected: 0},
{commandTag: "DELETE 1", rowsAffected: 1},
{commandTag: "CREATE TABLE", rowsAffected: 0},
{commandTag: "ALTER TABLE", rowsAffected: 0},
{commandTag: "DROP TABLE", rowsAffected: 0},
}
for i, tt := range tests {
actual := tt.commandTag.RowsAffected()
if tt.rowsAffected != actual {
t.Errorf(`%d. "%s" should have affected %d rows but it was %d`, i, tt.commandTag, tt.rowsAffected, actual)
}
}
}

View File

@ -26,7 +26,7 @@ func mustPrepare(t testing.TB, conn *pgx.Conn, name, sql string) {
} }
} }
func mustExecute(t testing.TB, conn *pgx.Conn, sql string, arguments ...interface{}) (commandTag string) { func mustExecute(t testing.TB, conn *pgx.Conn, sql string, arguments ...interface{}) (commandTag pgx.CommandTag) {
var err error var err error
if commandTag, err = conn.Execute(sql, arguments...); err != nil { if commandTag, err = conn.Execute(sql, arguments...); err != nil {
t.Fatalf("Execute unexpectedly failed with %v: %v", sql, err) t.Fatalf("Execute unexpectedly failed with %v: %v", sql, err)