From 4eb597d20bca231a1694b06470c9e329a9783ef7 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 17 May 2014 13:03:51 -0500 Subject: [PATCH] Rename pgx.Connection to pgx.Conn --- bench_test.go | 20 +++---- connection.go => conn.go | 90 +++++++++++++++--------------- connection_test.go => conn_test.go | 0 connection_pool.go | 46 +++++++-------- connection_pool_test.go | 18 +++--- examples/url_shortener/main.go | 2 +- helper_test.go | 16 +++--- sanitize.go | 6 +- 8 files changed, 99 insertions(+), 99 deletions(-) rename connection.go => conn.go (88%) rename connection_test.go => conn_test.go (100%) diff --git a/bench_test.go b/bench_test.go index 98c4238c..ddda2ec5 100644 --- a/bench_test.go +++ b/bench_test.go @@ -17,7 +17,7 @@ var float8TextVsBinaryTestDataLoaded bool var boolTextVsBinaryTestDataLoaded bool var timestampTzTextVsBinaryTestDataLoaded bool -func createNarrowTestData(b *testing.B, conn *pgx.Connection) { +func createNarrowTestData(b *testing.B, conn *pgx.Conn) { if narrowTestDataLoaded { return } @@ -158,7 +158,7 @@ func BenchmarkSelectValueToPreparedNarrow(b *testing.B) { } } -func createJoinsTestData(b *testing.B, conn *pgx.Connection) { +func createJoinsTestData(b *testing.B, conn *pgx.Conn) { if testJoinsDataLoaded { return } @@ -259,7 +259,7 @@ func BenchmarkSelectRowsPreparedJoins(b *testing.B) { } } -func createInt2TextVsBinaryTestData(b *testing.B, conn *pgx.Connection) { +func createInt2TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { if int2TextVsBinaryTestDataLoaded { return } @@ -312,7 +312,7 @@ func BenchmarkInt2Binary(b *testing.B) { } } -func createInt4TextVsBinaryTestData(b *testing.B, conn *pgx.Connection) { +func createInt4TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { if int4TextVsBinaryTestDataLoaded { return } @@ -365,7 +365,7 @@ func BenchmarkInt4Binary(b *testing.B) { } } -func createInt8TextVsBinaryTestData(b *testing.B, conn *pgx.Connection) { +func createInt8TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { if int8TextVsBinaryTestDataLoaded { return } @@ -418,7 +418,7 @@ func BenchmarkInt8Binary(b *testing.B) { } } -func createFloat4TextVsBinaryTestData(b *testing.B, conn *pgx.Connection) { +func createFloat4TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { if float4TextVsBinaryTestDataLoaded { return } @@ -471,7 +471,7 @@ func BenchmarkFloat4Binary(b *testing.B) { } } -func createFloat8TextVsBinaryTestData(b *testing.B, conn *pgx.Connection) { +func createFloat8TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { if float8TextVsBinaryTestDataLoaded { return } @@ -524,7 +524,7 @@ func BenchmarkFloat8Binary(b *testing.B) { } } -func createBoolTextVsBinaryTestData(b *testing.B, conn *pgx.Connection) { +func createBoolTextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { if boolTextVsBinaryTestDataLoaded { return } @@ -577,7 +577,7 @@ func BenchmarkBoolBinary(b *testing.B) { } } -func createTimestampTzTextVsBinaryTestData(b *testing.B, conn *pgx.Connection) { +func createTimestampTzTextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { if timestampTzTextVsBinaryTestDataLoaded { return } @@ -644,7 +644,7 @@ func BenchmarkConnectionPool(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - var conn *pgx.Connection + var conn *pgx.Conn if conn, err = pool.Acquire(); err != nil { b.Fatalf("Unable to acquire connection: %v", err) } diff --git a/connection.go b/conn.go similarity index 88% rename from connection.go rename to conn.go index 65ee971e..f952d240 100644 --- a/connection.go +++ b/conn.go @@ -36,10 +36,10 @@ type ConnectionParameters struct { Logger Logger } -// Connection is a PostgreSQL connection handle. It is not safe for concurrent usage. +// Conn is a PostgreSQL connection handle. It is not safe for concurrent usage. // Use ConnectionPool to manage access to multiple database connections from multiple // goroutines. -type Connection struct { +type Conn struct { conn net.Conn // the underlying TCP or unix domain socket connection reader *bufio.Reader // buffered reader to improve read performance writer *bufio.Writer // buffered writer to avoid sending tiny packets @@ -101,8 +101,8 @@ var NotificationTimeoutError = errors.New("Notification Timeout") // Connect establishes a connection with a PostgreSQL server using parameters. One // of parameters.Socket or parameters.Host must be specified. parameters.User // will default to the OS user name. Other parameters fields are optional. -func Connect(parameters ConnectionParameters) (c *Connection, err error) { - c = new(Connection) +func Connect(parameters ConnectionParameters) (c *Conn, err error) { + c = new(Conn) c.parameters = parameters if c.parameters.Logger != nil { @@ -211,7 +211,7 @@ func Connect(parameters ConnectionParameters) (c *Connection, err error) { } } -func (c *Connection) Close() (err error) { +func (c *Conn) Close() (err error) { err = c.txMsg('X', c.getBuf(), true) c.die(errors.New("Closed")) c.logger.Info("Closed connection") @@ -255,7 +255,7 @@ func ParseURI(uri string) (ConnectionParameters, error) { // need to simultaneously store the entire result set in memory. It also means that // it is possible to process some rows and then for an error to occur. Callers // should be aware of this possibility. -func (c *Connection) SelectFunc(sql string, onDataRow func(*DataRowReader) error, arguments ...interface{}) (err error) { +func (c *Conn) SelectFunc(sql string, onDataRow func(*DataRowReader) error, arguments ...interface{}) (err error) { defer func() { if err != nil { c.logger.Error(fmt.Sprintf("SelectFunc `%s` with %v failed: %v", sql, arguments, err)) @@ -307,7 +307,7 @@ func (c *Connection) SelectFunc(sql string, onDataRow func(*DataRowReader) error // 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 *Connection) SelectRows(sql string, arguments ...interface{}) (rows []map[string]interface{}, err error) { +func (c *Conn) SelectRows(sql string, arguments ...interface{}) (rows []map[string]interface{}, err error) { rows = make([]map[string]interface{}, 0, 8) onDataRow := func(r *DataRowReader) error { rows = append(rows, c.rxDataRow(r)) @@ -323,7 +323,7 @@ func (c *Connection) SelectRows(sql string, arguments ...interface{}) (rows []ma // positionally from the sql string as $1, $2, etc. // // Returns a NotSingleRowError if exactly one row is not found -func (c *Connection) SelectRow(sql string, arguments ...interface{}) (row map[string]interface{}, err error) { +func (c *Conn) SelectRow(sql string, arguments ...interface{}) (row map[string]interface{}, err error) { var numRowsFound int64 onDataRow := func(r *DataRowReader) error { @@ -345,7 +345,7 @@ func (c *Connection) SelectRow(sql string, arguments ...interface{}) (row map[st // // Returns a UnexpectedColumnCountError if exactly one column is not found // Returns a NotSingleRowError if exactly one row is not found -func (c *Connection) SelectValue(sql string, arguments ...interface{}) (v interface{}, err error) { +func (c *Conn) SelectValue(sql string, arguments ...interface{}) (v interface{}, err error) { var numRowsFound int64 onDataRow := func(r *DataRowReader) error { @@ -376,7 +376,7 @@ func (c *Connection) SelectValue(sql string, arguments ...interface{}) (v interf // // Returns a UnexpectedColumnCountError if exactly one column is not found // Returns a NotSingleRowError if exactly one row is not found -func (c *Connection) SelectValueTo(w io.Writer, sql string, arguments ...interface{}) (err error) { +func (c *Conn) SelectValueTo(w io.Writer, sql string, arguments ...interface{}) (err error) { defer func() { if err != nil { c.logger.Error(fmt.Sprintf("SelectValueTo `%s` with %v failed: %v", sql, arguments, err)) @@ -431,7 +431,7 @@ func (c *Connection) SelectValueTo(w io.Writer, sql string, arguments ...interfa return } -func (c *Connection) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) { +func (c *Conn) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) { var columnCount int16 err = binary.Read(c.reader, binary.BigEndian, &columnCount) if err != nil { @@ -475,7 +475,7 @@ func (c *Connection) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) { // the sql string as $1, $2, etc. // // Returns a UnexpectedColumnCountError if exactly one column is not found -func (c *Connection) SelectValues(sql string, arguments ...interface{}) (values []interface{}, err error) { +func (c *Conn) SelectValues(sql string, arguments ...interface{}) (values []interface{}, err error) { values = make([]interface{}, 0, 8) onDataRow := func(r *DataRowReader) error { if len(r.fields) != 1 { @@ -491,7 +491,7 @@ func (c *Connection) SelectValues(sql string, arguments ...interface{}) (values // Prepare creates a prepared statement with name and sql. sql can contain placeholders // for bound parameters. These placeholders are referenced positional as $1, $2, etc. -func (c *Connection) Prepare(name, sql string) (err error) { +func (c *Conn) Prepare(name, sql string) (err error) { defer func() { if err != nil { c.logger.Error(fmt.Sprintf("Prepare `%s` as `%s` failed: %v", name, sql, err)) @@ -565,21 +565,21 @@ func (c *Connection) Prepare(name, sql string) (err error) { } // Deallocate released a prepared statement -func (c *Connection) Deallocate(name string) (err error) { +func (c *Conn) Deallocate(name string) (err error) { delete(c.preparedStatements, name) _, err = c.Execute("deallocate " + c.QuoteIdentifier(name)) return } // Listen establishes a PostgreSQL listen/notify to channel -func (c *Connection) Listen(channel string) (err error) { +func (c *Conn) Listen(channel string) (err error) { _, err = c.Execute("listen " + channel) return } // WaitForNotification waits for a PostgreSQL notification for up to timeout. // If the timeout occurs it returns pgx.NotificationTimeoutError -func (c *Connection) WaitForNotification(timeout time.Duration) (*Notification, error) { +func (c *Conn) WaitForNotification(timeout time.Duration) (*Notification, error) { if len(c.notifications) > 0 { notification := c.notifications[0] c.notifications = c.notifications[1:] @@ -636,15 +636,15 @@ func (c *Connection) WaitForNotification(timeout time.Duration) (*Notification, } } -func (c *Connection) IsAlive() bool { +func (c *Conn) IsAlive() bool { return c.alive } -func (c *Connection) CauseOfDeath() error { +func (c *Conn) CauseOfDeath() error { return c.causeOfDeath } -func (c *Connection) sendQuery(sql string, arguments ...interface{}) (err error) { +func (c *Conn) sendQuery(sql string, arguments ...interface{}) (err error) { if ps, present := c.preparedStatements[sql]; present { return c.sendPreparedQuery(ps, arguments...) } else { @@ -652,7 +652,7 @@ func (c *Connection) sendQuery(sql string, arguments ...interface{}) (err error) } } -func (c *Connection) sendSimpleQuery(sql string, arguments ...interface{}) (err error) { +func (c *Conn) sendSimpleQuery(sql string, arguments ...interface{}) (err error) { if len(arguments) > 0 { sql, err = c.SanitizeSql(sql, arguments...) if err != nil { @@ -674,7 +674,7 @@ func (c *Connection) sendSimpleQuery(sql string, arguments ...interface{}) (err return c.txMsg('Q', buf, true) } -func (c *Connection) sendPreparedQuery(ps *preparedStatement, arguments ...interface{}) (err error) { +func (c *Conn) sendPreparedQuery(ps *preparedStatement, arguments ...interface{}) (err error) { if len(ps.ParameterOids) != len(arguments) { return fmt.Errorf("Prepared statement \"%v\" requires %d parameters, but %d were provided", ps.Name, len(ps.ParameterOids), len(arguments)) } @@ -752,7 +752,7 @@ func (c *Connection) sendPreparedQuery(ps *preparedStatement, arguments ...inter // 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 // should be referenced positionally from the sql string as $1, $2, etc. -func (c *Connection) Execute(sql string, arguments ...interface{}) (commandTag string, err error) { +func (c *Conn) Execute(sql string, arguments ...interface{}) (commandTag string, err error) { defer func() { if err != nil { c.logger.Error(fmt.Sprintf("Execute `%s` with %v failed: %v", sql, arguments, err)) @@ -791,7 +791,7 @@ func (c *Connection) Execute(sql string, arguments ...interface{}) (commandTag s // from err as an explicit rollback is not an error. Transaction will use the default // isolation level for the current connection. To use a specific isolation level see // TransactionIso -func (c *Connection) Transaction(f func() bool) (committed bool, err error) { +func (c *Conn) Transaction(f func() bool) (committed bool, err error) { return c.transaction("", f) } @@ -803,11 +803,11 @@ func (c *Connection) Transaction(f func() bool) (committed bool, err error) { // repeatable read // read committed // read uncommitted -func (c *Connection) TransactionIso(isoLevel string, f func() bool) (committed bool, err error) { +func (c *Conn) TransactionIso(isoLevel string, f func() bool) (committed bool, err error) { return c.transaction(isoLevel, f) } -func (c *Connection) transaction(isoLevel string, f func() bool) (committed bool, err error) { +func (c *Conn) transaction(isoLevel string, f func() bool) (committed bool, err error) { var beginSql string if isoLevel == "" { beginSql = "begin" @@ -837,7 +837,7 @@ func (c *Connection) transaction(isoLevel string, f func() bool) (committed bool // 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. -func (c *Connection) processContextFreeMsg(t byte, r *MessageReader) (err error) { +func (c *Conn) processContextFreeMsg(t byte, r *MessageReader) (err error) { switch t { case 'S': c.rxParameterStatus(r) @@ -853,7 +853,7 @@ func (c *Connection) processContextFreeMsg(t byte, r *MessageReader) (err error) } } -func (c *Connection) rxMsg() (t byte, r *MessageReader, err error) { +func (c *Conn) rxMsg() (t byte, r *MessageReader, err error) { var bodySize int32 t, bodySize, err = c.rxMsgHeader() if err != nil { @@ -869,7 +869,7 @@ func (c *Connection) rxMsg() (t byte, r *MessageReader, err error) { return } -func (c *Connection) rxMsgHeader() (t byte, bodySize int32, err error) { +func (c *Conn) rxMsgHeader() (t byte, bodySize int32, err error) { if !c.alive { err = errors.New("Connection is dead") return @@ -890,7 +890,7 @@ func (c *Connection) rxMsgHeader() (t byte, bodySize int32, err error) { return } -func (c *Connection) rxMsgBody(bodySize int32) (*bytes.Buffer, error) { +func (c *Conn) rxMsgBody(bodySize int32) (*bytes.Buffer, error) { if !c.alive { return nil, errors.New("Connection is dead") } @@ -905,7 +905,7 @@ func (c *Connection) rxMsgBody(bodySize int32) (*bytes.Buffer, error) { return buf, nil } -func (c *Connection) rxAuthenticationX(r *MessageReader) (err error) { +func (c *Conn) rxAuthenticationX(r *MessageReader) (err error) { code := r.ReadInt32() switch code { case 0: // AuthenticationOk @@ -928,13 +928,13 @@ func hexMD5(s string) string { return hex.EncodeToString(hash.Sum(nil)) } -func (c *Connection) rxParameterStatus(r *MessageReader) { +func (c *Conn) rxParameterStatus(r *MessageReader) { key := r.ReadCString() value := r.ReadCString() c.RuntimeParams[key] = value } -func (c *Connection) rxErrorResponse(r *MessageReader) (err PgError) { +func (c *Conn) rxErrorResponse(r *MessageReader) (err PgError) { for { switch r.ReadByte() { case 'S': @@ -951,16 +951,16 @@ func (c *Connection) rxErrorResponse(r *MessageReader) (err PgError) { } } -func (c *Connection) rxBackendKeyData(r *MessageReader) { +func (c *Conn) rxBackendKeyData(r *MessageReader) { c.Pid = r.ReadInt32() c.SecretKey = r.ReadInt32() } -func (c *Connection) rxReadyForQuery(r *MessageReader) { +func (c *Conn) rxReadyForQuery(r *MessageReader) { c.TxStatus = r.ReadByte() } -func (c *Connection) rxRowDescription(r *MessageReader) (fields []FieldDescription) { +func (c *Conn) rxRowDescription(r *MessageReader) (fields []FieldDescription) { fieldCount := r.ReadInt16() fields = make([]FieldDescription, fieldCount) for i := int16(0); i < fieldCount; i++ { @@ -976,7 +976,7 @@ func (c *Connection) rxRowDescription(r *MessageReader) (fields []FieldDescripti return } -func (c *Connection) rxParameterDescription(r *MessageReader) (parameters []Oid) { +func (c *Conn) rxParameterDescription(r *MessageReader) (parameters []Oid) { parameterCount := r.ReadInt16() parameters = make([]Oid, 0, parameterCount) for i := int16(0); i < parameterCount; i++ { @@ -985,7 +985,7 @@ func (c *Connection) rxParameterDescription(r *MessageReader) (parameters []Oid) return } -func (c *Connection) rxDataRow(r *DataRowReader) (row map[string]interface{}) { +func (c *Conn) rxDataRow(r *DataRowReader) (row map[string]interface{}) { fieldCount := len(r.fields) row = make(map[string]interface{}, fieldCount) @@ -995,11 +995,11 @@ func (c *Connection) rxDataRow(r *DataRowReader) (row map[string]interface{}) { return } -func (c *Connection) rxCommandComplete(r *MessageReader) string { +func (c *Conn) rxCommandComplete(r *MessageReader) string { return r.ReadCString() } -func (c *Connection) rxNotificationResponse(r *MessageReader) (err error) { +func (c *Conn) rxNotificationResponse(r *MessageReader) (err error) { n := new(Notification) n.Pid = r.ReadInt32() n.Channel = r.ReadCString() @@ -1008,7 +1008,7 @@ func (c *Connection) rxNotificationResponse(r *MessageReader) (err error) { return } -func (c *Connection) startTLS() (err error) { +func (c *Conn) startTLS() (err error) { err = binary.Write(c.conn, binary.BigEndian, []int32{8, 80877103}) if err != nil { return @@ -1029,7 +1029,7 @@ func (c *Connection) startTLS() (err error) { return nil } -func (c *Connection) txStartupMessage(msg *startupMessage) (err error) { +func (c *Conn) txStartupMessage(msg *startupMessage) (err error) { _, err = c.writer.Write(msg.Bytes()) if err != nil { return @@ -1038,7 +1038,7 @@ func (c *Connection) txStartupMessage(msg *startupMessage) (err error) { return } -func (c *Connection) txMsg(identifier byte, buf *bytes.Buffer, flush bool) (err error) { +func (c *Conn) txMsg(identifier byte, buf *bytes.Buffer, flush bool) (err error) { if !c.alive { return errors.New("Connection is dead") } @@ -1071,7 +1071,7 @@ func (c *Connection) txMsg(identifier byte, buf *bytes.Buffer, flush bool) (err return } -func (c *Connection) txPasswordMessage(password string) (err error) { +func (c *Conn) txPasswordMessage(password string) (err error) { buf := c.getBuf() _, err = buf.WriteString(password) @@ -1089,7 +1089,7 @@ func (c *Connection) txPasswordMessage(password string) (err error) { // Gets the shared connection buffer. Since bytes.Buffer never releases memory from // its internal byte array, check on the size and create a new bytes.Buffer so the // old one can get GC'ed -func (c *Connection) getBuf() *bytes.Buffer { +func (c *Conn) getBuf() *bytes.Buffer { c.buf.Reset() if cap(c.buf.Bytes()) > c.bufSize { c.logger.Debug(fmt.Sprintf("c.buf (%d) is larger than c.bufSize (%d) -- resetting", cap(c.buf.Bytes()), c.bufSize)) @@ -1098,7 +1098,7 @@ func (c *Connection) getBuf() *bytes.Buffer { return c.buf } -func (c *Connection) die(err error) { +func (c *Conn) die(err error) { c.alive = false c.causeOfDeath = err } diff --git a/connection_test.go b/conn_test.go similarity index 100% rename from connection_test.go rename to conn_test.go diff --git a/connection_pool.go b/connection_pool.go index 7401804c..ba795fbb 100644 --- a/connection_pool.go +++ b/connection_pool.go @@ -7,17 +7,17 @@ import ( type ConnectionPoolOptions struct { MaxConnections int // max simultaneous connections to use - AfterConnect func(*Connection) error + AfterConnect func(*Conn) error Logger Logger } type ConnectionPool struct { - allConnections []*Connection - availableConnections []*Connection + allConnections []*Conn + availableConnections []*Conn cond *sync.Cond parameters ConnectionParameters // parameters used when establishing connection maxConnections int - afterConnect func(*Connection) error + afterConnect func(*Conn) error logger Logger } @@ -40,12 +40,12 @@ func NewConnectionPool(parameters ConnectionParameters, options ConnectionPoolOp p.logger = nullLogger("null") } - p.allConnections = make([]*Connection, 0, p.maxConnections) - p.availableConnections = make([]*Connection, 0, p.maxConnections) + p.allConnections = make([]*Conn, 0, p.maxConnections) + p.availableConnections = make([]*Conn, 0, p.maxConnections) p.cond = sync.NewCond(new(sync.Mutex)) // Initially establish one connection - var c *Connection + var c *Conn c, err = p.createConnection() if err != nil { return @@ -57,7 +57,7 @@ func NewConnectionPool(parameters ConnectionParameters, options ConnectionPoolOp } // Acquire takes exclusive use of a connection until it is released. -func (p *ConnectionPool) Acquire() (c *Connection, err error) { +func (p *ConnectionPool) Acquire() (c *Conn, err error) { p.cond.L.Lock() defer p.cond.L.Unlock() @@ -93,7 +93,7 @@ func (p *ConnectionPool) Acquire() (c *Connection, err error) { } // Release gives up use of a connection. -func (p *ConnectionPool) Release(conn *Connection) { +func (p *ConnectionPool) Release(conn *Conn) { if conn.TxStatus != 'I' { conn.Execute("rollback") } @@ -142,7 +142,7 @@ func (p *ConnectionPool) CurrentConnectionCount() int { return p.maxConnections } -func (p *ConnectionPool) createConnection() (c *Connection, err error) { +func (p *ConnectionPool) createConnection() (c *Conn, err error) { c, err = Connect(p.parameters) if err != nil { return @@ -158,7 +158,7 @@ func (p *ConnectionPool) createConnection() (c *Connection, err error) { // SelectFunc acquires a connection, delegates the call to that connection, and releases the connection func (p *ConnectionPool) SelectFunc(sql string, onDataRow func(*DataRowReader) error, arguments ...interface{}) (err error) { - var c *Connection + var c *Conn if c, err = p.Acquire(); err != nil { return } @@ -169,7 +169,7 @@ func (p *ConnectionPool) SelectFunc(sql string, onDataRow func(*DataRowReader) e // SelectRows acquires a connection, delegates the call to that connection, and releases the connection func (p *ConnectionPool) SelectRows(sql string, arguments ...interface{}) (rows []map[string]interface{}, err error) { - var c *Connection + var c *Conn if c, err = p.Acquire(); err != nil { return } @@ -180,7 +180,7 @@ func (p *ConnectionPool) SelectRows(sql string, arguments ...interface{}) (rows // SelectRow acquires a connection, delegates the call to that connection, and releases the connection func (p *ConnectionPool) SelectRow(sql string, arguments ...interface{}) (row map[string]interface{}, err error) { - var c *Connection + var c *Conn if c, err = p.Acquire(); err != nil { return } @@ -191,7 +191,7 @@ func (p *ConnectionPool) SelectRow(sql string, arguments ...interface{}) (row ma // SelectValue acquires a connection, delegates the call to that connection, and releases the connection func (p *ConnectionPool) SelectValue(sql string, arguments ...interface{}) (v interface{}, err error) { - var c *Connection + var c *Conn if c, err = p.Acquire(); err != nil { return } @@ -202,7 +202,7 @@ func (p *ConnectionPool) SelectValue(sql string, arguments ...interface{}) (v in // SelectValueTo acquires a connection, delegates the call to that connection, and releases the connection func (p *ConnectionPool) SelectValueTo(w io.Writer, sql string, arguments ...interface{}) (err error) { - var c *Connection + var c *Conn if c, err = p.Acquire(); err != nil { return } @@ -213,7 +213,7 @@ func (p *ConnectionPool) SelectValueTo(w io.Writer, sql string, arguments ...int // SelectValues acquires a connection, delegates the call to that connection, and releases the connection func (p *ConnectionPool) SelectValues(sql string, arguments ...interface{}) (values []interface{}, err error) { - var c *Connection + var c *Conn if c, err = p.Acquire(); err != nil { return } @@ -224,7 +224,7 @@ func (p *ConnectionPool) SelectValues(sql string, arguments ...interface{}) (val // Execute acquires a connection, delegates the call to that connection, and releases the connection func (p *ConnectionPool) Execute(sql string, arguments ...interface{}) (commandTag string, err error) { - var c *Connection + var c *Conn if c, err = p.Acquire(); err != nil { return } @@ -235,9 +235,9 @@ func (p *ConnectionPool) Execute(sql string, arguments ...interface{}) (commandT // Transaction acquires a connection, delegates the call to that connection, // and releases the connection. The call signature differs slightly from the -// underlying Transaction in that the callback function accepts a *Connection -func (p *ConnectionPool) Transaction(f func(conn *Connection) bool) (committed bool, err error) { - var c *Connection +// underlying Transaction in that the callback function accepts a *Conn +func (p *ConnectionPool) Transaction(f func(conn *Conn) bool) (committed bool, err error) { + var c *Conn if c, err = p.Acquire(); err != nil { return } @@ -250,9 +250,9 @@ func (p *ConnectionPool) Transaction(f func(conn *Connection) bool) (committed b // TransactionIso acquires a connection, delegates the call to that connection, // and releases the connection. The call signature differs slightly from the -// underlying TransactionIso in that the callback function accepts a *Connection -func (p *ConnectionPool) TransactionIso(isoLevel string, f func(conn *Connection) bool) (committed bool, err error) { - var c *Connection +// underlying TransactionIso in that the callback function accepts a *Conn +func (p *ConnectionPool) TransactionIso(isoLevel string, f func(conn *Conn) bool) (committed bool, err error) { + var c *Conn if c, err = p.Acquire(); err != nil { return } diff --git a/connection_pool_test.go b/connection_pool_test.go index ebf15f89..6cb95b04 100644 --- a/connection_pool_test.go +++ b/connection_pool_test.go @@ -19,7 +19,7 @@ func createConnectionPool(t *testing.T, maxConnections int) *pgx.ConnectionPool func TestNewConnectionPool(t *testing.T) { var numCallbacks int - afterConnect := func(c *pgx.Connection) error { + afterConnect := func(c *pgx.Conn) error { numCallbacks++ return nil } @@ -39,7 +39,7 @@ func TestNewConnectionPool(t *testing.T) { // Pool creation returns an error if any AfterConnect callback does errAfterConnect := errors.New("Some error") - afterConnect = func(c *pgx.Connection) error { + afterConnect = func(c *pgx.Conn) error { return errAfterConnect } @@ -57,8 +57,8 @@ func TestPoolAcquireAndReleaseCycle(t *testing.T) { pool := createConnectionPool(t, maxConnections) defer pool.Close() - acquireAll := func() (connections []*pgx.Connection) { - connections = make([]*pgx.Connection, maxConnections) + acquireAll := func() (connections []*pgx.Conn) { + connections = make([]*pgx.Conn, maxConnections) for i := 0; i < maxConnections; i++ { var err error if connections[i], err = pool.Acquire(); err != nil { @@ -206,7 +206,7 @@ func TestPoolReleaseDiscardsDeadConnections(t *testing.T) { pool := createConnectionPool(t, maxConnections) defer pool.Close() - var c1, c2 *pgx.Connection + var c1, c2 *pgx.Conn var err error var stat pgx.ConnectionPoolStat @@ -265,7 +265,7 @@ func TestPoolTransaction(t *testing.T) { pool := createConnectionPool(t, 1) defer pool.Close() - committed, err := pool.Transaction(func(conn *pgx.Connection) bool { + committed, err := pool.Transaction(func(conn *pgx.Conn) bool { mustExecute(t, conn, "create temporary table foo(id serial primary key)") return true }) @@ -276,7 +276,7 @@ func TestPoolTransaction(t *testing.T) { t.Fatal("Transaction was not committed when it should have been") } - committed, err = pool.Transaction(func(conn *pgx.Connection) bool { + committed, err = pool.Transaction(func(conn *pgx.Conn) bool { n := mustSelectValue(t, conn, "select count(*) from foo") if n.(int64) != 0 { t.Fatalf("Did not receive expected value: %v", n) @@ -298,7 +298,7 @@ func TestPoolTransaction(t *testing.T) { t.Fatal("Transaction was committed when it shouldn't have been") } - committed, err = pool.Transaction(func(conn *pgx.Connection) bool { + committed, err = pool.Transaction(func(conn *pgx.Conn) bool { n := mustSelectValue(t, conn, "select count(*) from foo") if n.(int64) != 0 { t.Fatalf("Did not receive expected value: %v", n) @@ -318,7 +318,7 @@ func TestPoolTransactionIso(t *testing.T) { pool := createConnectionPool(t, 1) defer pool.Close() - committed, err := pool.TransactionIso("serializable", func(conn *pgx.Connection) bool { + committed, err := pool.TransactionIso("serializable", func(conn *pgx.Conn) bool { if level := mustSelectValue(t, conn, "select current_setting('transaction_isolation')"); level != "serializable" { t.Errorf("Expected to be in isolation level %v but was %v", "serializable", level) } diff --git a/examples/url_shortener/main.go b/examples/url_shortener/main.go index 54df21ba..db21c8cc 100644 --- a/examples/url_shortener/main.go +++ b/examples/url_shortener/main.go @@ -11,7 +11,7 @@ import ( var pool *pgx.ConnectionPool // afterConnect creates the prepared statements that this application uses -func afterConnect(conn *pgx.Connection) (err error) { +func afterConnect(conn *pgx.Conn) (err error) { err = conn.Prepare("getUrl", ` select url from shortened_urls where id=$1 `) diff --git a/helper_test.go b/helper_test.go index aa8c12a6..b298fe30 100644 --- a/helper_test.go +++ b/helper_test.go @@ -6,9 +6,9 @@ import ( "testing" ) -var sharedConnection *pgx.Connection +var sharedConnection *pgx.Conn -func getSharedConnection(t testing.TB) (c *pgx.Connection) { +func getSharedConnection(t testing.TB) (c *pgx.Conn) { if sharedConnection == nil || !sharedConnection.IsAlive() { var err error sharedConnection, err = pgx.Connect(*defaultConnectionParameters) @@ -20,13 +20,13 @@ func getSharedConnection(t testing.TB) (c *pgx.Connection) { return sharedConnection } -func mustPrepare(t testing.TB, conn *pgx.Connection, name, sql string) { +func mustPrepare(t testing.TB, conn *pgx.Conn, name, sql string) { if err := conn.Prepare(name, sql); err != nil { t.Fatalf("Could not prepare %v: %v", name, err) } } -func mustExecute(t testing.TB, conn *pgx.Connection, sql string, arguments ...interface{}) (commandTag string) { +func mustExecute(t testing.TB, conn *pgx.Conn, sql string, arguments ...interface{}) (commandTag string) { var err error if commandTag, err = conn.Execute(sql, arguments...); err != nil { t.Fatalf("Execute unexpectedly failed with %v: %v", sql, err) @@ -34,7 +34,7 @@ func mustExecute(t testing.TB, conn *pgx.Connection, sql string, arguments ...in return } -func mustSelectRow(t testing.TB, conn *pgx.Connection, sql string, arguments ...interface{}) (row map[string]interface{}) { +func mustSelectRow(t testing.TB, conn *pgx.Conn, sql string, arguments ...interface{}) (row map[string]interface{}) { var err error if row, err = conn.SelectRow(sql, arguments...); err != nil { t.Fatalf("SelectRow unexpectedly failed with %v: %v", sql, err) @@ -42,7 +42,7 @@ func mustSelectRow(t testing.TB, conn *pgx.Connection, sql string, arguments ... return } -func mustSelectRows(t testing.TB, conn *pgx.Connection, sql string, arguments ...interface{}) (rows []map[string]interface{}) { +func mustSelectRows(t testing.TB, conn *pgx.Conn, sql string, arguments ...interface{}) (rows []map[string]interface{}) { var err error if rows, err = conn.SelectRows(sql, arguments...); err != nil { t.Fatalf("SelectRows unexpected failed with %v: %v", sql, err) @@ -50,7 +50,7 @@ func mustSelectRows(t testing.TB, conn *pgx.Connection, sql string, arguments .. return } -func mustSelectValue(t testing.TB, conn *pgx.Connection, sql string, arguments ...interface{}) (value interface{}) { +func mustSelectValue(t testing.TB, conn *pgx.Conn, sql string, arguments ...interface{}) (value interface{}) { var err error if value, err = conn.SelectValue(sql, arguments...); err != nil { t.Fatalf("SelectValue unexpectedly failed with %v: %v", sql, err) @@ -58,7 +58,7 @@ func mustSelectValue(t testing.TB, conn *pgx.Connection, sql string, arguments . return } -func mustSelectValueTo(t testing.TB, conn *pgx.Connection, w io.Writer, sql string, arguments ...interface{}) { +func mustSelectValueTo(t testing.TB, conn *pgx.Conn, w io.Writer, sql string, arguments ...interface{}) { if err := conn.SelectValueTo(w, sql, arguments...); err != nil { t.Fatalf("SelectValueTo unexpectedly failed with %v: %v", sql, err) } diff --git a/sanitize.go b/sanitize.go index db280d64..b7cc95f0 100644 --- a/sanitize.go +++ b/sanitize.go @@ -13,14 +13,14 @@ var literalPattern *regexp.Regexp = regexp.MustCompile(`\$\d+`) // QuoteString escapes and quotes a string making it safe for interpolation // into an SQL string. -func (c *Connection) QuoteString(input string) (output string) { +func (c *Conn) QuoteString(input string) (output string) { output = "'" + strings.Replace(input, "'", "''", -1) + "'" return } // QuoteIdentifier escapes and quotes an identifier making it safe for // interpolation into an SQL string -func (c *Connection) QuoteIdentifier(input string) (output string) { +func (c *Conn) QuoteIdentifier(input string) (output string) { output = `"` + strings.Replace(input, `"`, `""`, -1) + `"` return } @@ -28,7 +28,7 @@ func (c *Connection) QuoteIdentifier(input string) (output string) { // SanitizeSql substitutely args positionaly into sql. Placeholder values are // $ prefixed integers like $1, $2, $3, etc. args are sanitized and quoted as // appropriate. -func (c *Connection) SanitizeSql(sql string, args ...interface{}) (output string, err error) { +func (c *Conn) SanitizeSql(sql string, args ...interface{}) (output string, err error) { replacer := func(match string) (replacement string) { n, _ := strconv.ParseInt(match[1:], 10, 0) switch arg := args[n-1].(type) {