Documentation
¶
Index ¶
- Variables
- type Insert
- func (ins *Insert) Args() []interface{}
- func (ins *Insert) Columns() string
- func (ins *Insert) Insert(with InsertWith) (*sql.Stmt, error)
- func (ins *Insert) InsertContext(ctx context.Context, with InsertWith) (*sql.Stmt, error)
- func (ins *Insert) Params() string
- func (ins *Insert) SQL() string
- func (ins *Insert) Tokenize(tokenType TokenType) string
- type InsertWith
- type Inserter
- type TokenType
Constants ¶
This section is empty.
Variables ¶
var UseStructTag = `col`
UseStructTag specifies the struct tag key for the column name. Default is `col`.
var UseTokenType = QuestionMarkTokenType
UseTokenType specifies the token type to use for values. Default is the question mark (`?`).
Functions ¶
This section is empty.
Types ¶
type Insert ¶
type Insert struct {
Table string
Record interface{}
// contains filtered or unexported fields
}
Insert models data used to produce a valid SQL INSERT statement with bind args. Table is the table name. Record is the struct with column-name tagged fields and the data to be inserted. Private fields recordType and recordValue are used internally with reflection and interfaces to field values.
func NewInsert ¶
NewInsert builds a new Insert using a given table-name (string) and record data (struct). It is recommended that new NewInsert be used to build every new INSERT; however, if only Insert.Tokenize is needed, a "manually" build Insert will support tokenization as long as Insert.Table and Insert.Record are valid.
func (*Insert) Args ¶
func (ins *Insert) Args() []interface{}
Args returns the arguments to be bound in Insert() or the variadic Exec/ExecContext functions in database/sql.
func (*Insert) Columns ¶
Columns returns the comma-separated list of column names-as-tokens for the SQL INSERT statement.
func (*Insert) Insert ¶
func (ins *Insert) Insert(with InsertWith) (*sql.Stmt, error)
Insert prepares and executes a SQL INSERT statement on a *sql.DB, *sql.Tx, or other Inserter-compatible interface to Prepare and Exec.
func (*Insert) InsertContext ¶
InsertContext prepares and executes a SQL INSERT statement on a *sql.DB, *sql.Tx, *sql.Conn, or other Inserter-compatible interface to PrepareContext and ExecContext.
func (*Insert) Params ¶
Params returns the comma-separated list of bind param tokens for the SQL INSERT statement.
type InsertWith ¶
type InsertWith interface {
Prepare(query string) (*sql.Stmt, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
Exec(query string, args ...interface{}) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}
InsertWith models functionality needed to execute a SQL INSERT statement with database/sql via sql.DB or sql.Tx. Note: sql.Conn is also supported, however, for PrepareContext and ExecContext only.
type Inserter ¶
type Inserter interface {
Tokenize(tokenType TokenType) string
Columns() string
Params() string
SQL() string
Args() []interface{}
Insert(with InsertWith) (*sql.Stmt, error)
InsertContext(ctx context.Context, with InsertWith) (*sql.Stmt, error)
}
Inserter models functionality to produce a valid SQL INSERT statement with bind args.
type TokenType ¶
type TokenType int
TokenType represents a type of token in a SQL INSERT statement, whether column or value expression.
const ( // ColumnNameTokenType uses the column name from the struct tag specified by UseStructTag. // INSERT INTO tbl (foo, bar, ... baz) ColumnNameTokenType TokenType = 0 // QuestionMarkTokenType uses question marks as value-tokens. // VALUES (?, ?, ... ?) -- MySQL, SingleStore QuestionMarkTokenType TokenType = 1 // AtColumnNameTokenType uses @ followed by the column name from the struct tag specified by UseStructTag. // VALUES (@foo, @bar, ... @baz) -- MySQL, SingleStore AtColumnNameTokenType TokenType = 2 // OrdinalNumberTokenType uses % plus the value of an ordered sequence of integers starting at 1. // %1, %2, ... %n -- Postgres OrdinalNumberTokenType TokenType = 3 // ColonTokenType uses : followed by the column name from the struct tag specified by UseStructTag. // :foo, :bar, ... :baz -- Oracle ColonTokenType TokenType = 4 )