Documentation ¶
Overview ¶
Package ef is an execution facade layer.
Fetched or not:
type table1 struct { Field1 string //=> bound to column "Field1" or "FIELD1" or "field1" field2 string //=> this field is private, so no value is stored Field3 string `db:"field2"` //=> bound to column "field2" Field4 string `db:"-"` //=> ignored to be bound } // build a query and args query, args = cue.Select("name").From("users").BuildSQL() // ^ Of course, you can build both variable manually. var t1 table1 if err = ef.FetchOne(db, &t1, query, args...); err == nil { // Now, t1 is &table1{Field1: "value of field1", field2: "", Field3: "value of field2", Field4: ""} } var t2 []table1 // slice if err = ef.FetchAll(db, &t2, query, args...); err == nil { // Now, t2 is []&table1{{...}, {...}, ...} }
Index ¶
- func Create(execer QueryExecer, table string, ifnotexists bool, tgt interface{}, ...) (sql.Result, error)
- func CreateStatement(table string, ifnotexists bool, tgt interface{}, optDialect ...dialect.Dialect) (*cue.CreateStatement, error)
- func EnumerateStructFields(st reflect.Value, handler StructFieldHandler)
- func Exec(execer QueryExecer, query string, args ...interface{}) (sql.Result, error)
- func Exists(execer QueryExecer, query string, args ...interface{}) (bool, error)
- func FetchAll(execer QueryExecer, dest interface{}, query string, args ...interface{}) error
- func FetchOne(execer QueryExecer, dest interface{}, query string, args ...interface{}) error
- func Insert(execer QueryExecer, table string, tgt interface{}, ...) (sql.Result, error)
- func InsertStatement(table string, tgt interface{}, optDialect ...dialect.Dialect) (*cue.InsertStatement, error)
- func ToString(value interface{}) string
- func Update(execer QueryExecer, table string, tgt interface{}, ...) (sql.Result, error)
- func UpdateStatement(table string, tgt interface{}, optDialect ...dialect.Dialect) (*cue.UpdateStatement, error)
- type ColumnSpec
- type QueryExecer
- type Rows
- type StructFieldHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Create ¶
func Create(execer QueryExecer, table string, ifnotexists bool, tgt interface{}, optDialect ...dialect.Dialect) (sql.Result, error)
Create creates table by field definitions of tgt struct.
func CreateStatement ¶
func CreateStatement(table string, ifnotexists bool, tgt interface{}, optDialect ...dialect.Dialect) (*cue.CreateStatement, error)
CreateStatement builds CREATE statement into *cue.CreateStatement.
func EnumerateStructFields ¶
func EnumerateStructFields(st reflect.Value, handler StructFieldHandler)
EnumerateStructFields enumerates fields in st struct and calls handler for each field.
func Exec ¶
func Exec(execer QueryExecer, query string, args ...interface{}) (sql.Result, error)
Exec wraps db.Exec.
func Exists ¶
func Exists(execer QueryExecer, query string, args ...interface{}) (bool, error)
Exists checks if given query has non-empty result set.
func FetchAll ¶
func FetchAll(execer QueryExecer, dest interface{}, query string, args ...interface{}) error
FetchAll fetches all records into dest(assumed as slice).
dest is (1)&[]struct, (2)&[]map
func FetchOne ¶
func FetchOne(execer QueryExecer, dest interface{}, query string, args ...interface{}) error
FetchOne fetches at most one record.
execer may be *sql.DB or *sql.Tx. dest is (1)&struct, (2)&map, (3)&var or (4)&interface{} query and args are same as for database/sql
func Insert ¶
func Insert(execer QueryExecer, table string, tgt interface{}, optDialect ...dialect.Dialect) (sql.Result, error)
Insert inserts a record from tgt form into table.
tgt is (!)&struct or (2)&map If field value in struct is nil and column spec has default, columns is set to its default, not null. Serial-type fields are not set. They are treated by the DBMS.
func InsertStatement ¶
func InsertStatement(table string, tgt interface{}, optDialect ...dialect.Dialect) (*cue.InsertStatement, error)
InsertStatement builds INSERT statement into *cue.InsertStatement.
tgt is (!)&struct or (2)&map If field value in struct is nil and column spec has default, columns is set to its default, not null. Serial-type fields are not set. They are treated by the DBMS.
func ToString ¶
func ToString(value interface{}) string
ToString is a helper for []utf8->string conversion.
func Update ¶
func Update(execer QueryExecer, table string, tgt interface{}, optDialect ...dialect.Dialect) (sql.Result, error)
Update updates a record.
Important: PKs in tgt structure is used to identify a target record.
struct { ID `db:"id; type=integer; pk"` // AT LEAST ONE PKs ARE REQUIRED TO UPDATE CORRECTLY. Name `db:"name;type=char; size=10"` }
func UpdateStatement ¶
func UpdateStatement(table string, tgt interface{}, optDialect ...dialect.Dialect) (*cue.UpdateStatement, error)
UpdateStatement builds UPDATE statement into *cue.UpdateStatement.
Important: PKs in tgt structure is used to identify a target record.
struct { ID `db:"id; type=integer; pk"` // AT LEAST ONE PKs ARE REQUIRED TO UPDATE CORRECTLY. Name `db:"name;type=char; size=10"` }
Types ¶
type ColumnSpec ¶
type ColumnSpec struct { DBName string Type string Size string IsSerial bool Default string IsPK bool IsNullable bool }
ColumnSpec has field infomation of `db` tag.
func CollectColumnSpecs ¶
func CollectColumnSpecs(st reflect.Value) []*ColumnSpec
CollectColumnSpecs collects specs of fields.
func CollectColumnSpecsAndPointers ¶
func CollectColumnSpecsAndPointers(st reflect.Value) (specs []*ColumnSpec, addrs []interface{})
CollectColumnSpecsAndPointers collects specs and addresses of fields.
Addresses should be used only to update A template struct. And later, the struct will be copied to another struct variable to return to user.
func GetColumnSpec ¶
func GetColumnSpec(field reflect.StructField) *ColumnSpec
GetColumnSpec reads the field info and returns ColumnSpec.
func (*ColumnSpec) ToString ¶
func (c *ColumnSpec) ToString() string
ToString returns a DB-column-specification string.
type QueryExecer ¶
type QueryExecer interface { Exec(query string, args ...interface{}) (sql.Result, error) Query(query string, args ...interface{}) (*sql.Rows, error) QueryRow(query string, args ...interface{}) *sql.Row }
QueryExecer interface is fulfilled by *sql.DB and *sql.Tx.
type Rows ¶
Rows is returned by FetchEach.
It has sampleType used when rows.Scan.
func FetchEach ¶
func FetchEach(execer QueryExecer, sampleOfDest interface{}, query string, args ...interface{}) (*Rows, error)
FetchEach starts fetching.
Use rows.Next() to test fetch, and use rows.Scan(dest) to fetch.
sampleOfDest must have same type as dest in rows.Scan(dest)
var user User rows, err := FetchEach(db, user, query, args...) if rows.Next() { err = rows.Scan(&user) }
type StructFieldHandler ¶
type StructFieldHandler func(i int, typ reflect.StructField, value reflect.Value)
StructFieldHandler handles EnumerateStructFields traversing.
i : n'th field typ : type of field value : value of field