Documentation
¶
Index ¶
- func Arg(args *[]interface{}, arg interface{}) string
- func Exec(db dbExecContext, query string, params NamedParams) (sql.Result, error)
- func ExecContext(ctx context.Context, db dbExecContext, query string, params NamedParams) (sql.Result, error)
- func Query(db dbQueryContext, query string, params NamedParams) (*sql.Rows, error)
- func QueryContext(ctx context.Context, db dbQueryContext, query string, params NamedParams) (*sql.Rows, error)
- type MissingParameterError
- type NamedParams
- type ParameterCountError
- type Row
- type SelectColumns
- type Stmt
- func (s *Stmt) Close() error
- func (s *Stmt) Exec(args NamedParams) (sql.Result, error)
- func (s *Stmt) ExecContext(ctx context.Context, named NamedParams) (sql.Result, error)
- func (s *Stmt) Query(args NamedParams) (*sql.Rows, error)
- func (s *Stmt) QueryContext(ctx context.Context, named NamedParams) (*sql.Rows, error)
- func (s *Stmt) QueryRow(args NamedParams) *Row
- func (s *Stmt) QueryRowContext(ctx context.Context, named NamedParams) *Row
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Arg ¶
func Arg(args *[]interface{}, arg interface{}) string
Arg adds a value to args, returning it's position
Example ¶
var db *sql.DB
var args []interface{}
query := `SELECT * FROM table WHERE a_column = ` + Arg(&args, `fubar`) + ` AND timestamp < ` + Arg(&args, time.Now())
_ = db.QueryRow(query, args...)
func ExecContext ¶
func QueryContext ¶
Types ¶
type MissingParameterError ¶
type MissingParameterError struct {
Missing string
Got NamedParams
}
func (MissingParameterError) Error ¶
func (e MissingParameterError) Error() string
type NamedParams ¶
type NamedParams map[string]interface{}
NamedParams enables the use of named parameters with any database by converting the query to use positional arguments
Example ¶
package main
import (
"database/sql"
"time"
"github.com/ferdypruis/sqeasy"
)
func main() {
var db *sql.DB
params := sqeasy.NamedParams{
"colA": "notthis",
"timestamp": time.Now(),
}
query := "SELECT * FROM table WHERE a_column != :colA AND timestamp < :timestamp"
_ = sqeasy.QueryRow(db, query, params)
}
Output:
func (NamedParams) Args ¶
func (np NamedParams) Args(params []string) ([]interface{}, error)
args returns the value for each parameter on the position as indicated in the param slice
type ParameterCountError ¶
type ParameterCountError struct {
Expected []string
Got NamedParams
}
func (ParameterCountError) Error ¶
func (e ParameterCountError) Error() string
type Row ¶
func QueryRow ¶
func QueryRow(db dbQueryRowContext, query string, params NamedParams) *Row
func QueryRowContext ¶
func QueryRowContext(ctx context.Context, db dbQueryRowContext, query string, params NamedParams) *Row
type SelectColumns ¶
type SelectColumns []struct {
Expr string
Dest interface{}
}
SelectColumns is a mapping of expression, like a column, to a destination variable
Example ¶
package main
import (
"database/sql"
"github.com/ferdypruis/sqeasy"
)
func main() {
var db *sql.DB
var (
colA string
timestamp sql.NullString
count int
)
columns := sqeasy.SelectColumns{
{`a_column`, &colA},
{`"timestamp"`, ×tamp},
{`COUNT(*)`, &count},
}
row := db.QueryRow("SELECT " + columns.ExprList() + " FROM table")
_ = columns.Scan(row)
}
Output:
func (SelectColumns) ExprList ¶
func (sc SelectColumns) ExprList() string
ExprList returns all the expressions separated by comma Use this in your SELECT statement
func (SelectColumns) Scan ¶
func (sc SelectColumns) Scan(row interface { Scan(dest ...interface{}) error }) error
Scan copies the columns from the row into the values
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
func PrepareContext ¶
func (*Stmt) ExecContext ¶
func (*Stmt) QueryContext ¶
func (*Stmt) QueryRow ¶
func (s *Stmt) QueryRow(args NamedParams) *Row
func (*Stmt) QueryRowContext ¶
func (s *Stmt) QueryRowContext(ctx context.Context, named NamedParams) *Row
Click to show internal directories.
Click to hide internal directories.