Documentation ¶
Overview ¶
`sqle` package is an extension of the standard `database/sql` package
Features
- fully implements the `database/sql` interface.
- it is fast, sometimes very fast (has a minimum overhead).
- `Scan` method can take composite types as arguments, such as structures (including nested ones), maps and slices.
- `Columns` and `ColumnTypes` methods cache the returned result.
Installation
go get -u github.com/lazada/sqle
Usage import sql "github.com/lazada/sqle"
type User struct { Id int32 `sql:"id"` Name string `sql:"name"` Email string `sql:"email"` Created time.Time `sql:"created"` Updated time.Time `sql:"updated"` } db, err := sql.Open(`sqlite3`, `testdata/testdata.db`) if err != nil { log.Fatalln(err) } user := new(User) db.QueryRowContext(ctx, `SELECT * FROM users WHERE id = ?`, userId).Scan(user)
Index ¶
- Variables
- func InStrictMode(opt *dbOptions) error
- func Wrap(object interface{}, options ...DBOption) (_ interface{}, err error)
- type Aliases
- type CachedConvention
- type CamelConvention
- type Conn
- func (c *Conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error)
- func (c *Conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
- func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (db *DB) Conn(ctx context.Context) (*Conn, error)
- func (db *DB) Prepare(query string) (*Stmt, error)
- func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error)
- func (db *DB) Query(query string, args ...interface{}) (*Rows, error)
- func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
- func (db *DB) QueryRow(query string, args ...interface{}) *Row
- func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
- type DBOption
- type LowerConvention
- type Mapper
- type NamingConvention
- type NoopConvention
- type Numerable
- type Pointers
- type Row
- type Rows
- type SnakeConvention
- type Stmt
- type Tx
- func (tx *Tx) Prepare(query string) (*Stmt, error)
- func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error)
- func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error)
- func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
- func (tx *Tx) QueryRow(query string, args ...interface{}) *Row
- func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
- func (tx *Tx) Stmt(stmt *sql.Stmt) *Stmt
- func (tx *Tx) StmtContext(ctx context.Context, stmt *sql.Stmt) *Stmt
- type UpperConvention
- type Values
Constants ¶
This section is empty.
Variables ¶
var ( ErrSrcNotPtr = errors.New(`sqle: source is not a pointer`) ErrSrcNotPtrTo = errors.New(`sqle: source is not a pointer to a structure`) ErrNilCtor = errors.New(`sqle: constructor is nil`) ErrCtorRetNil = errors.New(`sqle: constructor returned nil pointer`) )
var (
ErrMiss = errors.New(`miss`)
)
Functions ¶
func InStrictMode ¶
func InStrictMode(opt *dbOptions) error
InStrictMode places the DB into strict mode.
Types ¶
type CachedConvention ¶
type CachedConvention struct {
// contains filtered or unexported fields
}
func NewCachedConvention ¶
func NewCachedConvention(conv NamingConvention) *CachedConvention
func (*CachedConvention) Name ¶
func (c *CachedConvention) Name(name string) string
func (*CachedConvention) Reset ¶
func (c *CachedConvention) Reset() error
type CamelConvention ¶
type CamelConvention struct{}
func (*CamelConvention) Name ¶
func (n *CamelConvention) Name(name string) string
func (*CamelConvention) Reset ¶
func (n *CamelConvention) Reset() error
type Conn ¶
Conn represents a single database session rather a pool of database sessions. Prefer running queries from DB unless there is a specific need for a continuous single database session.
A Conn must call Close to return the connection to the database pool and may do so concurrently with a running query.
After a call to Close, all operations on the connection fail with sql.ErrConnDone.
func (*Conn) BeginTx ¶
BeginTx starts a transaction.
The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.
The provided sql.TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.
func (*Conn) PrepareContext ¶
PrepareContext creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.
The provided context is used for the preparation of the statement, not for the execution of the statement.
func (*Conn) QueryContext ¶
QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*Conn) QueryRowContext ¶
QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return sql.ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
type DB ¶
DB is a database handle representing a pool of zero or more underlying connections. It's safe for concurrent use by multiple goroutines.
The sql package creates and frees connections automatically; it also maintains a free pool of idle connections. If the database has a concept of per-connection state, such state can only be reliably observed within a transaction. Once DB.Begin is called, the returned Tx is bound to a single connection. Once Commit or Rollback is called on the transaction, that transaction's connection is returned to DB's idle connection pool. The pool size can be controlled with SetMaxIdleConns.
func Open ¶
Open opens a database specified by its database driver name and a driver-specific data source name, usually consisting of at least a database name and connection information.
Most users will open a database via a driver-specific connection helper function that returns a *DB. No database drivers are included in the Go standard library. See https://golang.org/s/sqldrivers for a list of third-party drivers.
Open may just validate its arguments without creating a connection to the database. To verify that the data source name is valid, call Ping.
The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the Open function should be called just once. It is rarely necessary to close a DB.
func (*DB) Begin ¶
Begin starts a transaction. The default isolation level is dependent on the driver.
func (*DB) BeginTx ¶
BeginTx starts a transaction.
The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.
The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.
func (*DB) Conn ¶
Conn returns a single connection by either opening a new connection or returning an existing connection from the connection pool. Conn will block until either a connection is returned or ctx is canceled. Queries run on the same Conn will be run in the same database session.
Every Conn must be returned to the database pool after use by calling Conn.Close.
func (*DB) Prepare ¶
Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.
func (*DB) PrepareContext ¶
PrepareContext creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.
The provided context is used for the preparation of the statement, not for the execution of the statement.
func (*DB) Query ¶
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*DB) QueryContext ¶
QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
type LowerConvention ¶
type LowerConvention struct{}
func (*LowerConvention) Name ¶
func (n *LowerConvention) Name(name string) string
func (*LowerConvention) Reset ¶
func (n *LowerConvention) Reset() error
type Mapper ¶
type Mapper struct {
// contains filtered or unexported fields
}
func NewMapper ¶
func NewMapper(tag string, conv NamingConvention) *Mapper
type NamingConvention ¶
type NoopConvention ¶
type NoopConvention struct{}
func (*NoopConvention) Name ¶
func (n *NoopConvention) Name(name string) string
func (*NoopConvention) Reset ¶
func (n *NoopConvention) Reset() error
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row is the result of calling QueryRow to select a single row.
type Rows ¶
Rows is the result of a query. Its cursor starts before the first row of the result set.
func (*Rows) ColumnTypes ¶
func (r *Rows) ColumnTypes() ([]*sql.ColumnType, error)
ColumnTypes returns column information such as column type, length, and nullable. Some information may not be available from some drivers.
type SnakeConvention ¶
type SnakeConvention struct{}
func (*SnakeConvention) Name ¶
func (n *SnakeConvention) Name(name string) string
func (*SnakeConvention) Reset ¶
func (n *SnakeConvention) Reset() error
type Stmt ¶
Stmt is a prepared statement. A Stmt is safe for concurrent use by multiple goroutines.
func (*Stmt) Query ¶
Query executes a prepared query statement with given arguments and returns the query results as *Rows.
func (*Stmt) QueryContext ¶
QueryContext executes a prepared query statement with given arguments and returns the query results as *Rows.
func (*Stmt) QueryRow ¶
QueryRow executes a prepared query statement with the given arguments. If an error occurs during the execution of the statement, that error will be returned by a call to Scan on the returned *Row, which is always non-nil. If the query selects no rows, the *Row's Scan will return sql.ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
func (*Stmt) QueryRowContext ¶
QueryRowContext executes a prepared query statement with the given arguments. If an error occurs during the execution of the statement, that error will be returned by a call to Scan on the returned *Row, which is always non-nil. If the query selects no rows, the *Row's Scan will return sql.ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
type Tx ¶
Tx is an in-progress database transaction.
A transaction must end with a call to Commit or Rollback.
After a call to Commit or Rollback, all operations on the transaction fail with ErrTxDone.
The statements prepared for a transaction by calling the transaction's Prepare or Stmt methods are closed by the call to Commit or Rollback.
func (*Tx) Prepare ¶
Prepare creates a prepared statement for use within a transaction.
The returned statement operates within the transaction and can no longer be used once the transaction has been committed or rolled back.
To use an existing prepared statement on this transaction, see Tx.Stmt.
func (*Tx) PrepareContext ¶
Prepare creates a prepared statement for use within a transaction.
The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back.
To use an existing prepared statement on this transaction, see Tx.Stmt.
The provided context will be used for the preparation of the context, not for the execution of the returned statement. The returned statement will run in the transaction context.
func (*Tx) QueryContext ¶
QueryContext executes a query that returns rows, typically a SELECT.
func (*Tx) QueryRow ¶
QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until Row's Scan method is called.
func (*Tx) QueryRowContext ¶
QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called.
func (*Tx) Stmt ¶
Stmt returns a transaction-specific prepared statement from an existing statement. The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back.
func (*Tx) StmtContext ¶
StmtContext returns a transaction-specific prepared statement from an existing statement. The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back.
type UpperConvention ¶
type UpperConvention struct{}
func (*UpperConvention) Name ¶
func (n *UpperConvention) Name(name string) string
func (*UpperConvention) Reset ¶
func (n *UpperConvention) Reset() error