Skip to content

Commit d6d6ffa

Browse files
- add non-context version of methods for Conn struct
1 parent 24c77b5 commit d6d6ffa

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

conn.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ func (c *Conn) Unleak() {
2929
c.connectionID = ""
3030
}
3131

32+
// Begin starts a transaction. The default isolation level is dependent on the driver.
33+
func (c *Conn) Begin(opts *stdSql.TxOptions) (*Tx, error) {
34+
return c.BeginTx(context.Background(), opts)
35+
}
36+
3237
// BeginTx starts a transaction.
3338
//
3439
// The provided context is used until the transaction is committed or rolled back.
@@ -63,6 +68,12 @@ func (c *Conn) Close() error {
6368
return nil
6469
}
6570

71+
// Exec executes a query without returning any rows.
72+
// The args are for any placeholder parameters in the query.
73+
func (c *Conn) Exec(query string, args ...interface{}) (stdSql.Result, error) {
74+
return c.ExecContext(context.Background(), query, args...)
75+
}
76+
6677
// ExecContext executes a query without returning any rows.
6778
// The args are for any placeholder parameters in the query.
6879
func (c *Conn) ExecContext(ctx context.Context, query string, args ...interface{}) (stdSql.Result, error) {
@@ -106,6 +117,11 @@ func (c *Conn) ExecContext(ctx context.Context, query string, args ...interface{
106117
}
107118
}
108119

120+
// Ping verifies a connection to the database is still alive.
121+
func (c *Conn) Ping() error {
122+
return c.PingContext(context.Background())
123+
}
124+
109125
// PingContext verifies the connection to the database is still alive.
110126
func (c *Conn) PingContext(ctx context.Context) error {
111127

@@ -141,6 +157,15 @@ func (c *Conn) PingContext(ctx context.Context) error {
141157
}
142158
}
143159

160+
// Prepare creates a prepared statement for later queries or executions.
161+
// Multiple queries or executions may be run concurrently from the
162+
// returned statement.
163+
// The caller must call the statement's Close method
164+
// when the statement is no longer needed.
165+
func (c *Conn) Prepare(query string) (*Stmt, error) {
166+
return c.PrepareContext(context.Background(), query)
167+
}
168+
144169
// PrepareContext creates a prepared statement for later queries or executions.
145170
// Multiple queries or executions may be run concurrently from the
146171
// returned statement.
@@ -191,6 +216,12 @@ func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error)
191216

192217
}
193218

219+
// Query executes a query that returns rows, typically a SELECT.
220+
// The args are for any placeholder parameters in the query.
221+
func (c *Conn) Query(query string, args ...interface{}) (*stdSql.Rows, error) {
222+
return c.QueryContext(context.Background(), query, args...)
223+
}
224+
194225
// QueryContext executes a query that returns rows, typically a SELECT.
195226
// The args are for any placeholder parameters in the query.
196227
func (c *Conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*stdSql.Rows, error) {
@@ -206,6 +237,14 @@ func (c *Conn) QueryContext(ctx context.Context, query string, args ...interface
206237
return c.conn.QueryContext(ctx, query, args...)
207238
}
208239

240+
// QueryRow executes a query that is expected to return at most one row.
241+
// QueryRow always returns a non-nil value. Errors are deferred until
242+
// Row's Scan method is called.
243+
// If the query selects no rows, the *Row's Scan will return ErrNoRows.
244+
// Otherwise, the *Row's Scan scans the first selected row and discards
245+
// the rest.
246+
func (c *Conn) QueryRow(query string, args ...interface{}) *stdSql.Row {
247+
return c.QueryRowContext(context.Background(), query, args...)
209248
}
210249

211250
// QueryRowContext executes a query that is expected to return at most one row.
@@ -223,6 +262,5 @@ func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...interf
223262
}
224263
}()
225264

226-
row := c.conn.QueryRowContext(ctx, query, args...)
227-
return row
265+
return c.conn.QueryRowContext(ctx, query, args...)
228266
}

db.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ func (db *DB) Conn(ctx context.Context) (*Conn, error) {
8888
return &Conn{conn, db.DB, connectionID}, nil
8989
}
9090
return &Conn{conn, db.KillerPool, connectionID}, nil
91-
9291
}
9392

9493
// Driver returns the database's underlying driver.

stmt.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,5 @@ func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *stdSql
132132
}
133133
}()
134134

135-
row := s.QueryRowContext(ctx, args...)
136-
return row
135+
return s.QueryRowContext(ctx, args...)
137136
}

tx.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interfa
213213
}
214214
}()
215215

216-
row := tx.tx.QueryRowContext(ctx, query, args...)
217-
return row
216+
return tx.tx.QueryRowContext(ctx, query, args...)
218217
}
219218

220219
// Rollback aborts the transaction.

0 commit comments

Comments
 (0)