1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package driver defines interfaces to be implemented by database
6 // drivers as used by package sql.
8 // Most code should use package sql.
17 // Value is a value that drivers must be able to handle.
18 // It is either nil, a type handled by a database driver's NamedValueChecker
19 // interface, or an instance of one of these types:
27 type Value
interface{}
29 // NamedValue holds both the value name and value.
30 type NamedValue
struct {
31 // If the Name is not empty it should be used for the parameter identifier and
32 // not the ordinal position.
34 // Name will not have a symbol prefix.
37 // Ordinal position of the parameter starting from one and is always set.
40 // Value is the parameter value.
44 // Driver is the interface that must be implemented by a database
47 // Database drivers may implement DriverContext for access
48 // to contexts and to parse the name only once for a pool of connections,
49 // instead of once per connection.
50 type Driver
interface {
51 // Open returns a new connection to the database.
52 // The name is a string in a driver-specific format.
54 // Open may return a cached connection (one previously
55 // closed), but doing so is unnecessary; the sql package
56 // maintains a pool of idle connections for efficient re-use.
58 // The returned connection is only used by one goroutine at a
60 Open(name
string) (Conn
, error
)
63 // If a Driver implements DriverContext, then sql.DB will call
64 // OpenConnector to obtain a Connector and then invoke
65 // that Connector's Conn method to obtain each needed connection,
66 // instead of invoking the Driver's Open method for each connection.
67 // The two-step sequence allows drivers to parse the name just once
68 // and also provides access to per-Conn contexts.
69 type DriverContext
interface {
70 // OpenConnector must parse the name in the same format that Driver.Open
71 // parses the name parameter.
72 OpenConnector(name
string) (Connector
, error
)
75 // A Connector represents a driver in a fixed configuration
76 // and can create any number of equivalent Conns for use
77 // by multiple goroutines.
79 // A Connector can be passed to sql.OpenDB, to allow drivers
80 // to implement their own sql.DB constructors, or returned by
81 // DriverContext's OpenConnector method, to allow drivers
82 // access to context and to avoid repeated parsing of driver
84 type Connector
interface {
85 // Connect returns a connection to the database.
86 // Connect may return a cached connection (one previously
87 // closed), but doing so is unnecessary; the sql package
88 // maintains a pool of idle connections for efficient re-use.
90 // The provided context.Context is for dialing purposes only
91 // (see net.DialContext) and should not be stored or used for
94 // The returned connection is only used by one goroutine at a
96 Connect(context
.Context
) (Conn
, error
)
98 // Driver returns the underlying Driver of the Connector,
99 // mainly to maintain compatibility with the Driver method
104 // ErrSkip may be returned by some optional interfaces' methods to
105 // indicate at runtime that the fast path is unavailable and the sql
106 // package should continue as if the optional interface was not
107 // implemented. ErrSkip is only supported where explicitly
109 var ErrSkip
= errors
.New("driver: skip fast-path; continue as if unimplemented")
111 // ErrBadConn should be returned by a driver to signal to the sql
112 // package that a driver.Conn is in a bad state (such as the server
113 // having earlier closed the connection) and the sql package should
114 // retry on a new connection.
116 // To prevent duplicate operations, ErrBadConn should NOT be returned
117 // if there's a possibility that the database server might have
118 // performed the operation. Even if the server sends back an error,
119 // you shouldn't return ErrBadConn.
120 var ErrBadConn
= errors
.New("driver: bad connection")
122 // Pinger is an optional interface that may be implemented by a Conn.
124 // If a Conn does not implement Pinger, the sql package's DB.Ping and
125 // DB.PingContext will check if there is at least one Conn available.
127 // If Conn.Ping returns ErrBadConn, DB.Ping and DB.PingContext will remove
128 // the Conn from pool.
129 type Pinger
interface {
130 Ping(ctx context
.Context
) error
133 // Execer is an optional interface that may be implemented by a Conn.
135 // If a Conn implements neither ExecerContext nor Execer Execer,
136 // the sql package's DB.Exec will first prepare a query, execute the statement,
137 // and then close the statement.
139 // Exec may return ErrSkip.
141 // Deprecated: Drivers should implement ExecerContext instead.
142 type Execer
interface {
143 Exec(query
string, args
[]Value
) (Result
, error
)
146 // ExecerContext is an optional interface that may be implemented by a Conn.
148 // If a Conn does not implement ExecerContext, the sql package's DB.Exec
149 // will fall back to Execer; if the Conn does not implement Execer either,
150 // DB.Exec will first prepare a query, execute the statement, and then
151 // close the statement.
153 // ExecerContext may return ErrSkip.
155 // ExecerContext must honor the context timeout and return when the context is canceled.
156 type ExecerContext
interface {
157 ExecContext(ctx context
.Context
, query
string, args
[]NamedValue
) (Result
, error
)
160 // Queryer is an optional interface that may be implemented by a Conn.
162 // If a Conn implements neither QueryerContext nor Queryer,
163 // the sql package's DB.Query will first prepare a query, execute the statement,
164 // and then close the statement.
166 // Query may return ErrSkip.
168 // Deprecated: Drivers should implement QueryerContext instead.
169 type Queryer
interface {
170 Query(query
string, args
[]Value
) (Rows
, error
)
173 // QueryerContext is an optional interface that may be implemented by a Conn.
175 // If a Conn does not implement QueryerContext, the sql package's DB.Query
176 // will fall back to Queryer; if the Conn does not implement Queryer either,
177 // DB.Query will first prepare a query, execute the statement, and then
178 // close the statement.
180 // QueryerContext may return ErrSkip.
182 // QueryerContext must honor the context timeout and return when the context is canceled.
183 type QueryerContext
interface {
184 QueryContext(ctx context
.Context
, query
string, args
[]NamedValue
) (Rows
, error
)
187 // Conn is a connection to a database. It is not used concurrently
188 // by multiple goroutines.
190 // Conn is assumed to be stateful.
191 type Conn
interface {
192 // Prepare returns a prepared statement, bound to this connection.
193 Prepare(query
string) (Stmt
, error
)
195 // Close invalidates and potentially stops any current
196 // prepared statements and transactions, marking this
197 // connection as no longer in use.
199 // Because the sql package maintains a free pool of
200 // connections and only calls Close when there's a surplus of
201 // idle connections, it shouldn't be necessary for drivers to
202 // do their own connection caching.
205 // Begin starts and returns a new transaction.
207 // Deprecated: Drivers should implement ConnBeginTx instead (or additionally).
211 // ConnPrepareContext enhances the Conn interface with context.
212 type ConnPrepareContext
interface {
213 // PrepareContext returns a prepared statement, bound to this connection.
214 // context is for the preparation of the statement,
215 // it must not store the context within the statement itself.
216 PrepareContext(ctx context
.Context
, query
string) (Stmt
, error
)
219 // IsolationLevel is the transaction isolation level stored in TxOptions.
221 // This type should be considered identical to sql.IsolationLevel along
222 // with any values defined on it.
223 type IsolationLevel
int
225 // TxOptions holds the transaction options.
227 // This type should be considered identical to sql.TxOptions.
228 type TxOptions
struct {
229 Isolation IsolationLevel
233 // ConnBeginTx enhances the Conn interface with context and TxOptions.
234 type ConnBeginTx
interface {
235 // BeginTx starts and returns a new transaction.
236 // If the context is canceled by the user the sql package will
237 // call Tx.Rollback before discarding and closing the connection.
239 // This must check opts.Isolation to determine if there is a set
240 // isolation level. If the driver does not support a non-default
241 // level and one is set or if there is a non-default isolation level
242 // that is not supported, an error must be returned.
244 // This must also check opts.ReadOnly to determine if the read-only
245 // value is true to either set the read-only transaction property if supported
246 // or return an error if it is not supported.
247 BeginTx(ctx context
.Context
, opts TxOptions
) (Tx
, error
)
250 // SessionResetter may be implemented by Conn to allow drivers to reset the
251 // session state associated with the connection and to signal a bad connection.
252 type SessionResetter
interface {
253 // ResetSession is called while a connection is in the connection
254 // pool. No queries will run on this connection until this method returns.
256 // If the connection is bad this should return driver.ErrBadConn to prevent
257 // the connection from being returned to the connection pool. Any other
258 // error will be discarded.
259 ResetSession(ctx context
.Context
) error
262 // Result is the result of a query execution.
263 type Result
interface {
264 // LastInsertId returns the database's auto-generated ID
265 // after, for example, an INSERT into a table with primary
267 LastInsertId() (int64, error
)
269 // RowsAffected returns the number of rows affected by the
271 RowsAffected() (int64, error
)
274 // Stmt is a prepared statement. It is bound to a Conn and not
275 // used by multiple goroutines concurrently.
276 type Stmt
interface {
277 // Close closes the statement.
279 // As of Go 1.1, a Stmt will not be closed if it's in use
283 // NumInput returns the number of placeholder parameters.
285 // If NumInput returns >= 0, the sql package will sanity check
286 // argument counts from callers and return errors to the caller
287 // before the statement's Exec or Query methods are called.
289 // NumInput may also return -1, if the driver doesn't know
290 // its number of placeholders. In that case, the sql package
291 // will not sanity check Exec or Query argument counts.
294 // Exec executes a query that doesn't return rows, such
295 // as an INSERT or UPDATE.
297 // Deprecated: Drivers should implement StmtExecContext instead (or additionally).
298 Exec(args
[]Value
) (Result
, error
)
300 // Query executes a query that may return rows, such as a
303 // Deprecated: Drivers should implement StmtQueryContext instead (or additionally).
304 Query(args
[]Value
) (Rows
, error
)
307 // StmtExecContext enhances the Stmt interface by providing Exec with context.
308 type StmtExecContext
interface {
309 // ExecContext executes a query that doesn't return rows, such
310 // as an INSERT or UPDATE.
312 // ExecContext must honor the context timeout and return when it is canceled.
313 ExecContext(ctx context
.Context
, args
[]NamedValue
) (Result
, error
)
316 // StmtQueryContext enhances the Stmt interface by providing Query with context.
317 type StmtQueryContext
interface {
318 // QueryContext executes a query that may return rows, such as a
321 // QueryContext must honor the context timeout and return when it is canceled.
322 QueryContext(ctx context
.Context
, args
[]NamedValue
) (Rows
, error
)
325 // ErrRemoveArgument may be returned from NamedValueChecker to instruct the
326 // sql package to not pass the argument to the driver query interface.
327 // Return when accepting query specific options or structures that aren't
328 // SQL query arguments.
329 var ErrRemoveArgument
= errors
.New("driver: remove argument from query")
331 // NamedValueChecker may be optionally implemented by Conn or Stmt. It provides
332 // the driver more control to handle Go and database types beyond the default
333 // Values types allowed.
335 // The sql package checks for value checkers in the following order,
336 // stopping at the first found match: Stmt.NamedValueChecker, Conn.NamedValueChecker,
337 // Stmt.ColumnConverter, DefaultParameterConverter.
339 // If CheckNamedValue returns ErrRemoveArgument, the NamedValue will not be included in
340 // the final query arguments. This may be used to pass special options to
343 // If ErrSkip is returned the column converter error checking
344 // path is used for the argument. Drivers may wish to return ErrSkip after
345 // they have exhausted their own special cases.
346 type NamedValueChecker
interface {
347 // CheckNamedValue is called before passing arguments to the driver
348 // and is called in place of any ColumnConverter. CheckNamedValue must do type
349 // validation and conversion as appropriate for the driver.
350 CheckNamedValue(*NamedValue
) error
353 // ColumnConverter may be optionally implemented by Stmt if the
354 // statement is aware of its own columns' types and can convert from
355 // any type to a driver Value.
357 // Deprecated: Drivers should implement NamedValueChecker.
358 type ColumnConverter
interface {
359 // ColumnConverter returns a ValueConverter for the provided
360 // column index. If the type of a specific column isn't known
361 // or shouldn't be handled specially, DefaultValueConverter
363 ColumnConverter(idx
int) ValueConverter
366 // Rows is an iterator over an executed query's results.
367 type Rows
interface {
368 // Columns returns the names of the columns. The number of
369 // columns of the result is inferred from the length of the
370 // slice. If a particular column name isn't known, an empty
371 // string should be returned for that entry.
374 // Close closes the rows iterator.
377 // Next is called to populate the next row of data into
378 // the provided slice. The provided slice will be the same
379 // size as the Columns() are wide.
381 // Next should return io.EOF when there are no more rows.
383 // The dest should not be written to outside of Next. Care
384 // should be taken when closing Rows not to modify
385 // a buffer held in dest.
386 Next(dest
[]Value
) error
389 // RowsNextResultSet extends the Rows interface by providing a way to signal
390 // the driver to advance to the next result set.
391 type RowsNextResultSet
interface {
394 // HasNextResultSet is called at the end of the current result set and
395 // reports whether there is another result set after the current one.
396 HasNextResultSet() bool
398 // NextResultSet advances the driver to the next result set even
399 // if there are remaining rows in the current result set.
401 // NextResultSet should return io.EOF when there are no more result sets.
402 NextResultSet() error
405 // RowsColumnTypeScanType may be implemented by Rows. It should return
406 // the value type that can be used to scan types into. For example, the database
407 // column type "bigint" this should return "reflect.TypeOf(int64(0))".
408 type RowsColumnTypeScanType
interface {
410 ColumnTypeScanType(index
int) reflect
.Type
413 // RowsColumnTypeDatabaseTypeName may be implemented by Rows. It should return the
414 // database system type name without the length. Type names should be uppercase.
415 // Examples of returned types: "VARCHAR", "NVARCHAR", "VARCHAR2", "CHAR", "TEXT",
416 // "DECIMAL", "SMALLINT", "INT", "BIGINT", "BOOL", "[]BIGINT", "JSONB", "XML",
418 type RowsColumnTypeDatabaseTypeName
interface {
420 ColumnTypeDatabaseTypeName(index
int) string
423 // RowsColumnTypeLength may be implemented by Rows. It should return the length
424 // of the column type if the column is a variable length type. If the column is
425 // not a variable length type ok should return false.
426 // If length is not limited other than system limits, it should return math.MaxInt64.
427 // The following are examples of returned values for various types:
428 // TEXT (math.MaxInt64, true)
429 // varchar(10) (10, true)
430 // nvarchar(10) (10, true)
431 // decimal (0, false)
433 // bytea(30) (30, true)
434 type RowsColumnTypeLength
interface {
436 ColumnTypeLength(index
int) (length
int64, ok
bool)
439 // RowsColumnTypeNullable may be implemented by Rows. The nullable value should
440 // be true if it is known the column may be null, or false if the column is known
441 // to be not nullable.
442 // If the column nullability is unknown, ok should be false.
443 type RowsColumnTypeNullable
interface {
445 ColumnTypeNullable(index
int) (nullable
, ok
bool)
448 // RowsColumnTypePrecisionScale may be implemented by Rows. It should return
449 // the precision and scale for decimal types. If not applicable, ok should be false.
450 // The following are examples of returned values for various types:
451 // decimal(38, 4) (38, 4, true)
453 // decimal (math.MaxInt64, math.MaxInt64, true)
454 type RowsColumnTypePrecisionScale
interface {
456 ColumnTypePrecisionScale(index
int) (precision
, scale
int64, ok
bool)
459 // Tx is a transaction.
465 // RowsAffected implements Result for an INSERT or UPDATE operation
466 // which mutates a number of rows.
467 type RowsAffected
int64
469 var _ Result
= RowsAffected(0)
471 func (RowsAffected
) LastInsertId() (int64, error
) {
472 return 0, errors
.New("no LastInsertId available")
475 func (v RowsAffected
) RowsAffected() (int64, error
) {
479 // ResultNoRows is a pre-defined Result for drivers to return when a DDL
480 // command (such as a CREATE TABLE) succeeds. It returns an error for both
481 // LastInsertId and RowsAffected.
482 var ResultNoRows noRows
486 var _ Result
= noRows
{}
488 func (noRows
) LastInsertId() (int64, error
) {
489 return 0, errors
.New("no LastInsertId available after DDL statement")
492 func (noRows
) RowsAffected() (int64, error
) {
493 return 0, errors
.New("no RowsAffected available after DDL statement")