From b6ac3ef2b00adc7bd6ad038fbeda57588b0caf09 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 27 Jun 2014 14:56:27 -0500 Subject: [PATCH] Rename Execute to Exec --- README.md | 4 +-- bench_test.go | 18 +++++----- conn.go | 18 +++++----- conn_pool.go | 8 ++--- conn_pool_test.go | 18 +++++----- conn_test.go | 60 +++++++++++++++++----------------- data_row_reader.go | 4 +++ examples/url_shortener/main.go | 4 +-- helper_test.go | 6 ++-- stdlib/sql.go | 8 ++--- 10 files changed, 76 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index 80a83277..0487c4ec 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ if widgets, err := conn.SelectRows("select name, weight from widgets where type= Prepared statements are easy to use in pgx. Just call Prepare with the name of the statement and the SQL. To execute a prepared statement just pass the name -of the statement into a Select* or Execute command as the SQL text. It will +of the statement into a Select* or Exec command as the SQL text. It will automatically detect that it is the name of a prepared statement and execute it. @@ -61,7 +61,7 @@ being made available in the connection pool. This is especially useful to ensure all connections have the same prepared statements available or to change any other connection settings. -It also delegates Select* and Execute functions to an automatically checked +It also delegates Select* and Exec functions to an automatically checked out and released connection so you can avoid manually acquiring and releasing connections when you do not need that level of control. diff --git a/bench_test.go b/bench_test.go index c7225867..687c7650 100644 --- a/bench_test.go +++ b/bench_test.go @@ -8,7 +8,7 @@ import ( ) func createNarrowTestData(b *testing.B, conn *pgx.Conn) { - mustExecute(b, conn, ` + mustExec(b, conn, ` drop table if exists narrow; create table narrow( @@ -149,7 +149,7 @@ func BenchmarkSelectValueToPreparedNarrow(b *testing.B) { } func createJoinsTestData(b *testing.B, conn *pgx.Conn) { - mustExecute(b, conn, ` + mustExec(b, conn, ` drop table if exists product_component; drop table if exists component; drop table if exists product; @@ -246,7 +246,7 @@ func BenchmarkSelectRowsPreparedJoins(b *testing.B) { } func createInt2TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - mustExecute(b, conn, ` + mustExec(b, conn, ` drop table if exists t; create temporary table t( @@ -294,7 +294,7 @@ func BenchmarkInt2Binary(b *testing.B) { } func createInt4TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - mustExecute(b, conn, ` + mustExec(b, conn, ` drop table if exists t; create temporary table t( @@ -342,7 +342,7 @@ func BenchmarkInt4Binary(b *testing.B) { } func createInt8TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - mustExecute(b, conn, ` + mustExec(b, conn, ` drop table if exists t; create temporary table t( @@ -389,7 +389,7 @@ func BenchmarkInt8Binary(b *testing.B) { } func createFloat4TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - mustExecute(b, conn, ` + mustExec(b, conn, ` drop table if exists t; create temporary table t( @@ -436,7 +436,7 @@ func BenchmarkFloat4Binary(b *testing.B) { } func createFloat8TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - mustExecute(b, conn, ` + mustExec(b, conn, ` drop table if exists t; create temporary table t( @@ -483,7 +483,7 @@ func BenchmarkFloat8Binary(b *testing.B) { } func createBoolTextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - mustExecute(b, conn, ` + mustExec(b, conn, ` drop table if exists t; create temporary table t( @@ -530,7 +530,7 @@ func BenchmarkBoolBinary(b *testing.B) { } func createTimestampTzTextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - mustExecute(b, conn, ` + mustExec(b, conn, ` drop table if exists t; create temporary table t( diff --git a/conn.go b/conn.go index 836818d9..8dae0515 100644 --- a/conn.go +++ b/conn.go @@ -667,13 +667,13 @@ func (c *Conn) Prepare(name, sql string) (ps *PreparedStatement, err error) { // Deallocate released a prepared statement func (c *Conn) Deallocate(name string) (err error) { delete(c.preparedStatements, name) - _, err = c.Execute("deallocate " + c.QuoteIdentifier(name)) + _, err = c.Exec("deallocate " + c.QuoteIdentifier(name)) return } // Listen establishes a PostgreSQL listen/notify to channel func (c *Conn) Listen(channel string) (err error) { - _, err = c.Execute("listen " + channel) + _, err = c.Exec("listen " + channel) return } @@ -833,18 +833,18 @@ func (c *Conn) sendPreparedQuery(ps *PreparedStatement, arguments ...interface{} return err } -// Execute executes sql. sql can be either a prepared statement name or an SQL string. +// Exec 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 // should be referenced positionally from the sql string as $1, $2, etc. -func (c *Conn) Execute(sql string, arguments ...interface{}) (commandTag CommandTag, err error) { +func (c *Conn) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, err error) { startTime := time.Now() defer func() { if err == nil { endTime := time.Now() - c.logger.Info("Execute", "sql", sql, "args", arguments, "time", endTime.Sub(startTime)) + c.logger.Info("Exec", "sql", sql, "args", arguments, "time", endTime.Sub(startTime)) } else { - c.logger.Error("Execute", "sql", sql, "args", arguments, "error", err) + c.logger.Error("Exec", "sql", sql, "args", arguments, "error", err) } }() @@ -909,17 +909,17 @@ func (c *Conn) transaction(isoLevel string, f func() bool) (committed bool, err beginSql = fmt.Sprintf("begin isolation level %s", isoLevel) } - if _, err = c.Execute(beginSql); err != nil { + if _, err = c.Exec(beginSql); err != nil { return } defer func() { if committed && c.TxStatus == 'T' { - _, err = c.Execute("commit") + _, err = c.Exec("commit") if err != nil { committed = false } } else { - _, err = c.Execute("rollback") + _, err = c.Exec("rollback") committed = false } }() diff --git a/conn_pool.go b/conn_pool.go index 2ac877f0..751e6012 100644 --- a/conn_pool.go +++ b/conn_pool.go @@ -105,7 +105,7 @@ func (p *ConnPool) Acquire() (c *Conn, err error) { // Release gives up use of a connection. func (p *ConnPool) Release(conn *Conn) { if conn.TxStatus != 'I' { - conn.Execute("rollback") + conn.Exec("rollback") } p.cond.L.Lock() @@ -232,15 +232,15 @@ func (p *ConnPool) SelectValues(sql string, arguments ...interface{}) (values [] return c.SelectValues(sql, arguments...) } -// Execute acquires a connection, delegates the call to that connection, and releases the connection -func (p *ConnPool) Execute(sql string, arguments ...interface{}) (commandTag CommandTag, err error) { +// Exec acquires a connection, delegates the call to that connection, and releases the connection +func (p *ConnPool) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, err error) { var c *Conn if c, err = p.Acquire(); err != nil { return } defer p.Release(c) - return c.Execute(sql, arguments...) + return c.Exec(sql, arguments...) } // Transaction acquires a connection, delegates the call to that connection, diff --git a/conn_pool_test.go b/conn_pool_test.go index bf33d1a9..fcd8e9fb 100644 --- a/conn_pool_test.go +++ b/conn_pool_test.go @@ -104,8 +104,8 @@ func TestPoolAcquireAndReleaseCycle(t *testing.T) { allConnections := acquireAll() for _, c := range allConnections { - mustExecute(t, c, "create temporary table t(counter integer not null)") - mustExecute(t, c, "insert into t(counter) values(0);") + mustExec(t, c, "create temporary table t(counter integer not null)") + mustExec(t, c, "insert into t(counter) values(0);") } for _, c := range allConnections { @@ -120,7 +120,7 @@ func TestPoolAcquireAndReleaseCycle(t *testing.T) { defer pool.Release(conn) // Increment counter... - mustExecute(t, conn, "update t set counter = counter + 1") + mustExec(t, conn, "update t set counter = counter + 1") completeSync <- 0 } @@ -167,8 +167,8 @@ func TestPoolReleaseWithTransactions(t *testing.T) { if err != nil { t.Fatalf("Unable to acquire connection: %v", err) } - mustExecute(t, conn, "begin") - if _, err = conn.Execute("select"); err == nil { + mustExec(t, conn, "begin") + if _, err = conn.Exec("select"); err == nil { t.Fatal("Did not receive expected error") } if conn.TxStatus != 'E' { @@ -185,7 +185,7 @@ func TestPoolReleaseWithTransactions(t *testing.T) { if err != nil { t.Fatalf("Unable to acquire connection: %v", err) } - mustExecute(t, conn, "begin") + mustExec(t, conn, "begin") if conn.TxStatus != 'T' { t.Fatalf("Expected txStatus to be 'T', instead it was '%c'", conn.TxStatus) } @@ -267,7 +267,7 @@ func TestPoolReleaseDiscardsDeadConnections(t *testing.T) { } }() - if _, err = c2.Execute("select pg_terminate_backend($1)", c1.Pid); err != nil { + if _, err = c2.Exec("select pg_terminate_backend($1)", c1.Pid); err != nil { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } @@ -307,7 +307,7 @@ func TestPoolTransaction(t *testing.T) { defer pool.Close() committed, err := pool.Transaction(func(conn *pgx.Conn) bool { - mustExecute(t, conn, "create temporary table foo(id serial primary key)") + mustExec(t, conn, "create temporary table foo(id serial primary key)") return true }) if err != nil { @@ -323,7 +323,7 @@ func TestPoolTransaction(t *testing.T) { t.Fatalf("Did not receive expected value: %v", n) } - mustExecute(t, conn, "insert into foo(id) values(default)") + mustExec(t, conn, "insert into foo(id) values(default)") n = mustSelectValue(t, conn, "select count(*) from foo") if n.(int64) != 1 { diff --git a/conn_test.go b/conn_test.go index 66156710..570e7abe 100644 --- a/conn_test.go +++ b/conn_test.go @@ -233,48 +233,48 @@ func TestParseURI(t *testing.T) { } } -func TestExecute(t *testing.T) { +func TestExec(t *testing.T) { t.Parallel() conn := mustConnect(t, *defaultConnConfig) defer closeConn(t, conn) - if results := mustExecute(t, conn, "create temporary table foo(id integer primary key);"); results != "CREATE TABLE" { - t.Error("Unexpected results from Execute") + if results := mustExec(t, conn, "create temporary table foo(id integer primary key);"); results != "CREATE TABLE" { + t.Error("Unexpected results from Exec") } // Accept parameters - if results := mustExecute(t, conn, "insert into foo(id) values($1)", 1); results != "INSERT 0 1" { - t.Errorf("Unexpected results from Execute: %v", results) + if results := mustExec(t, conn, "insert into foo(id) values($1)", 1); results != "INSERT 0 1" { + t.Errorf("Unexpected results from Exec: %v", results) } - if results := mustExecute(t, conn, "drop table foo;"); results != "DROP TABLE" { - t.Error("Unexpected results from Execute") + if results := mustExec(t, conn, "drop table foo;"); results != "DROP TABLE" { + t.Error("Unexpected results from Exec") } // Multiple statements can be executed -- last command tag is returned - if results := mustExecute(t, conn, "create temporary table foo(id serial primary key); drop table foo;"); results != "DROP TABLE" { - t.Error("Unexpected results from Execute") + if results := mustExec(t, conn, "create temporary table foo(id serial primary key); drop table foo;"); results != "DROP TABLE" { + t.Error("Unexpected results from Exec") } // Can execute longer SQL strings than sharedBufferSize - if results := mustExecute(t, conn, strings.Repeat("select 42; ", 1000)); results != "SELECT 1" { - t.Errorf("Unexpected results from Execute: %v", results) + if results := mustExec(t, conn, strings.Repeat("select 42; ", 1000)); results != "SELECT 1" { + t.Errorf("Unexpected results from Exec: %v", results) } } -func TestExecuteFailure(t *testing.T) { +func TestExecFailure(t *testing.T) { t.Parallel() conn := mustConnect(t, *defaultConnConfig) defer closeConn(t, conn) - if _, err := conn.Execute("select;"); err == nil { + if _, err := conn.Exec("select;"); err == nil { t.Fatal("Expected SQL syntax error") } if _, err := conn.SelectValue("select 1"); err != nil { - t.Fatalf("Execute failure appears to have broken connection: %v", err) + t.Fatalf("Exec failure appears to have broken connection: %v", err) } } @@ -623,7 +623,7 @@ func TestPrepare(t *testing.T) { } } - mustExecute(t, conn, "create temporary table foo(id serial)") + mustExec(t, conn, "create temporary table foo(id serial)") if _, err = conn.Prepare("deleteFoo", "delete from foo"); err != nil { t.Fatalf("Unable to prepare delete: %v", err) } @@ -657,13 +657,13 @@ func TestTransaction(t *testing.T) { ); ` - if _, err := conn.Execute(createSql); err != nil { + if _, err := conn.Exec(createSql); err != nil { t.Fatalf("Failed to create table: %v", err) } // Transaction happy path -- it executes function and commits committed, err := conn.Transaction(func() bool { - mustExecute(t, conn, "insert into foo(id) values (1)") + mustExec(t, conn, "insert into foo(id) values (1)") return true }) if err != nil { @@ -679,11 +679,11 @@ func TestTransaction(t *testing.T) { t.Fatalf("Did not receive correct number of rows: %v", n) } - mustExecute(t, conn, "truncate foo") + mustExec(t, conn, "truncate foo") // It rolls back when passed function returns false committed, err = conn.Transaction(func() bool { - mustExecute(t, conn, "insert into foo(id) values (1)") + mustExec(t, conn, "insert into foo(id) values (1)") return false }) if err != nil { @@ -699,9 +699,9 @@ func TestTransaction(t *testing.T) { // it rolls back changes when connection is in error state committed, err = conn.Transaction(func() bool { - mustExecute(t, conn, "insert into foo(id) values (1)") - if _, err := conn.Execute("invalid"); err == nil { - t.Fatal("Execute was supposed to error but didn't") + mustExec(t, conn, "insert into foo(id) values (1)") + if _, err := conn.Exec("invalid"); err == nil { + t.Fatal("Exec was supposed to error but didn't") } return true }) @@ -718,8 +718,8 @@ func TestTransaction(t *testing.T) { // when commit fails committed, err = conn.Transaction(func() bool { - mustExecute(t, conn, "insert into foo(id) values (1)") - mustExecute(t, conn, "insert into foo(id) values (1)") + mustExec(t, conn, "insert into foo(id) values (1)") + mustExec(t, conn, "insert into foo(id) values (1)") return true }) if err == nil { @@ -741,7 +741,7 @@ func TestTransaction(t *testing.T) { }() committed, err = conn.Transaction(func() bool { - mustExecute(t, conn, "insert into foo(id) values (1)") + mustExec(t, conn, "insert into foo(id) values (1)") panic("stop!") }) @@ -785,7 +785,7 @@ func TestListenNotify(t *testing.T) { notifier := mustConnect(t, *defaultConnConfig) defer closeConn(t, notifier) - mustExecute(t, notifier, "notify chat") + mustExec(t, notifier, "notify chat") // when notification is waiting on the socket to be read notification, err := listener.WaitForNotification(time.Second) @@ -797,7 +797,7 @@ func TestListenNotify(t *testing.T) { } // when notification has already been read during previous query - mustExecute(t, notifier, "notify chat") + mustExec(t, notifier, "notify chat") mustSelectValue(t, listener, "select 1") notification, err = listener.WaitForNotification(0) if err != nil { @@ -817,7 +817,7 @@ func TestListenNotify(t *testing.T) { } // listener can listen again after a timeout - mustExecute(t, notifier, "notify chat") + mustExec(t, notifier, "notify chat") notification, err = listener.WaitForNotification(time.Second) if err != nil { t.Fatalf("Unexpected error on WaitForNotification: %v", err) @@ -849,7 +849,7 @@ func TestFatalRxError(t *testing.T) { } defer otherConn.Close() - if _, err := otherConn.Execute("select pg_terminate_backend($1)", conn.Pid); err != nil { + if _, err := otherConn.Exec("select pg_terminate_backend($1)", conn.Pid); err != nil { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } @@ -872,7 +872,7 @@ func TestFatalTxError(t *testing.T) { } defer otherConn.Close() - if _, err := otherConn.Execute("select pg_terminate_backend($1)", conn.Pid); err != nil { + if _, err := otherConn.Exec("select pg_terminate_backend($1)", conn.Pid); err != nil { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } diff --git a/data_row_reader.go b/data_row_reader.go index a7c25504..2fc77e3b 100644 --- a/data_row_reader.go +++ b/data_row_reader.go @@ -11,6 +11,10 @@ type DataRowReader struct { currentFieldIdx int } +func (r *DataRowReader) MessageReader() *MessageReader { + return r.mr +} + // ReadValue returns the next value from the current row. func (r *DataRowReader) ReadValue() interface{} { fieldDescription := r.FieldDescriptions[r.currentFieldIdx] diff --git a/examples/url_shortener/main.go b/examples/url_shortener/main.go index 8abfd861..0daf92f5 100644 --- a/examples/url_shortener/main.go +++ b/examples/url_shortener/main.go @@ -63,7 +63,7 @@ func putUrlHandler(w http.ResponseWriter, req *http.Request) { return } - if _, err := pool.Execute("putUrl", id, url); err == nil { + if _, err := pool.Exec("putUrl", id, url); err == nil { w.WriteHeader(http.StatusOK) } else { http.Error(w, "Internal server error", http.StatusInternalServerError) @@ -71,7 +71,7 @@ func putUrlHandler(w http.ResponseWriter, req *http.Request) { } func deleteUrlHandler(w http.ResponseWriter, req *http.Request) { - if _, err := pool.Execute("deleteUrl", req.URL.Path); err == nil { + if _, err := pool.Exec("deleteUrl", req.URL.Path); err == nil { w.WriteHeader(http.StatusOK) } else { http.Error(w, "Internal server error", http.StatusInternalServerError) diff --git a/helper_test.go b/helper_test.go index bbf9d0dc..821b5f98 100644 --- a/helper_test.go +++ b/helper_test.go @@ -27,10 +27,10 @@ func mustPrepare(t testing.TB, conn *pgx.Conn, name, sql string) { } } -func mustExecute(t testing.TB, conn *pgx.Conn, sql string, arguments ...interface{}) (commandTag pgx.CommandTag) { +func mustExec(t testing.TB, conn *pgx.Conn, sql string, arguments ...interface{}) (commandTag pgx.CommandTag) { var err error - if commandTag, err = conn.Execute(sql, arguments...); err != nil { - t.Fatalf("Execute unexpectedly failed with %v: %v", sql, err) + if commandTag, err = conn.Exec(sql, arguments...); err != nil { + t.Fatalf("Exec unexpectedly failed with %v: %v", sql, err) } return } diff --git a/stdlib/sql.go b/stdlib/sql.go index ab495a29..2e08f710 100644 --- a/stdlib/sql.go +++ b/stdlib/sql.go @@ -108,7 +108,7 @@ func (c *Conn) Begin() (driver.Tx, error) { return nil, driver.ErrBadConn } - _, err := c.conn.Execute("begin") + _, err := c.conn.Exec("begin") if err != nil { return nil, err } @@ -122,7 +122,7 @@ func (c *Conn) Exec(query string, argsV []driver.Value) (driver.Result, error) { } args := valueToInterface(argsV) - commandTag, err := c.conn.Execute(query, args...) + commandTag, err := c.conn.Exec(query, args...) return driver.RowsAffected(commandTag.RowsAffected()), err } @@ -233,11 +233,11 @@ type Tx struct { } func (t *Tx) Commit() error { - _, err := t.conn.Execute("commit") + _, err := t.conn.Exec("commit") return err } func (t *Tx) Rollback() error { - _, err := t.conn.Execute("rollback") + _, err := t.conn.Exec("rollback") return err }