Documentation
¶
Overview ¶
Package dali wraps the sql.DB and provides convenient API for building database driven applications. Its main goal is to create a unified way of handling placeholders among all drivers and to simplify some common, repetive queries.
There is no support for query builders (you have to write pure SQL queries). It focuses on the common queries (like writing INSERTs or UPDATEs) and on loading of results into structs, for which it provides easy-to-write alternatives.
Placeholders ¶
The following is the complete list of possible placeholders that can be used when writing a query using Query method.
? primitive value or a value implementing driver.Valuer ?... a slice of values which is going to be expanded (especially useful in IN clauses) ?values expects either Map, or a struct as an argument. It derives column names from map keys or struct fields and constructs a VALUES clause (e.g. INSERT INTO user ?values) ?set similar to ?values but used for SET clauses (e.g. UPDATE user SET ?set) ?values... expects a slice of structs as an argument which is expanded into multi INSERT clause ?ident used for identifiers (column or table name) ?ident... expands identifiers and separates them with a comma ?sql inserts the parameter, a string or Marshaler, as is (meant for SQL parts)
Prepared statements ¶
dali has also a support for prepared statements. However, it doesn't support certain placeholders. Only ?ident, ?ident..., and ?sql placeholders are allowed in the phase of the query building (befored the statement is prepared). The ? placeholder is the only one left for parameter binding. So working with prepared statements can look like this:
cols := []strings{"name", "group_id"} var ( name string groupID int64 ) stmt := db.Prepare(`SELECT ?ident... FROM [user] WHERE [id] = ?`, cols) // This prepares this statement: // SELECT `name`, `group_id` FROM `user` WHERE `id` = ? stmt.Bind(14).ScanRow(&name, &groupID) // Bind the statement with 14 value and scan the row into these variables.
Index ¶
- func LastInsertID(res sql.Result, err error) (int64, error)
- type DB
- type Execer
- type Map
- type Marshaler
- type Query
- func (q *Query) All(dest interface{}) error
- func (q *Query) Exec() (sql.Result, error)
- func (q *Query) One(dest interface{}) error
- func (q *Query) Rows() (*sql.Rows, error)
- func (q *Query) ScanAllRows(dests ...interface{}) error
- func (q *Query) ScanRow(dest ...interface{}) error
- func (q *Query) String() string
- type Stmt
- type Translator
- type Tx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LastInsertID ¶
LastInsertID is a helper that wraps a call to a function returning (res sql.Result, err error). It returns err if it is not nil, otherwise it returns res.LastInsertId(). It is intended for uses such as
q := db.Query(...) id, err := dali.LastInsertID(q.Exec())
Types ¶
type DB ¶
DB wraps the sql.DB and provides slightly different API for communication with the database. The primary method is Query which provides methods for executing queries or scanning results.
func Open ¶
Open opens a database by calling sql.Open. It returns a new DB and selects the appropriate dialect which is inferred from the driverName. It panics if the dialect is not supported by dali itself.
func (*DB) Ping ¶
Ping verifies a connection to the database is still alive, establishing a connection if necessary.
func (*DB) Prepare ¶
Prepare creates a prepared statement for later queries or executions. The caller must call the statement's Close method when the statement is no longer needed. Unlike the Prepare methods in database/sql this method also accepts args, which are meant only for query building. Therefore, only ?ident, ?ident..., ?sql are interpolated in this phase. Apart of that, ? is the only other placeholder allowed (this one will be transformed into a dialect specific one to allow the parameter binding.
func (*DB) Query ¶
Query is a fundamental method of DB. It returns a Query struct which is capable of executing the sql (given by the query and the args) or loading the result into structs or primitive values.
func (*DB) SetMiddlewareFunc ¶
SetMiddlewareFunc changes the DB middleware func. Default func passes the Execer unchanged. SetMiddlewareFunc allowes the user to set his own middleware to perform additional operations (e.g. profiling) when executing queries.
type Execer ¶
type Execer interface { Exec(query string, args ...interface{}) (sql.Result, error) Query(query string, args ...interface{}) (*sql.Rows, error) QueryRow(query string, args ...interface{}) *sql.Row }
Execer is an interface that Query works with.
type Marshaler ¶ added in v0.3.0
type Marshaler interface {
MarshalSQL(t Translator) (string, error)
}
Marshaler is the interface implemented by types that can marshal themselves into valid SQL. Any type that implements Marshaler can be used as an argument to the ?sql placeholder.
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query represents an arbitrary SQL statement. The SQL is preprocessed by Preprocessor before running.
func (*Query) All ¶
All executes the query that should return rows, and loads the resulting data into dest which must be a slice of structs. Only fields that match the column names (after filtering through the mapperFunc) are filled.
func (*Query) Exec ¶
Exec executes the query that shouldn't return rows. For example: INSERT or UPDATE.
func (*Query) One ¶
One executes the query that should return rows and loads the resulting data from the first row into dest which must be a struct. Only fields that match the column names (after filtering through the mapperFunc) are filled. One returns sql.ErrNoRows if there are no rows.
func (*Query) ScanAllRows ¶ added in v0.3.0
ScanAllRows executes the query that is expected to return rows. It copies the columns from the matched rows into the slices pointed at by dests.
func (*Query) ScanRow ¶
ScanRow executes the query that is expected to return at most one row. It copies the columns from the matched row into the values pointed at by dest. If more than one row matches the query, ScanRow uses the first row and discards the rest. If no row matches the query, ScanRow returns sql.ErrNoRows.
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
Stmt is a prepared statement.
type Translator ¶ added in v0.3.0
type Translator struct {
// contains filtered or unexported fields
}
A Translator translates SQL queries using a dialect.
type Tx ¶
Tx wraps the sql.Tx to provide the Query method instead of the sql.Tx's original methods for comunication with the database.
func (*Tx) Prepare ¶
Prepare creates a prepared statement for later queries or executions. The caller must call the statement's Close method when the statement is no longer needed. Unlike the Prepare methods in database/sql this method also accepts args, which are meant only for query building. Therefore, only ?ident, ?ident..., ?sql are interpolated in this phase. Apart of that, ? is the only other placeholder allowed (this one will be transformed into a dialect specific one to allow the parameter binding.