Documentation ¶
Overview ¶
Package gomodel is a library help for interact with database efficiently
Index ¶
- Constants
- Variables
- func CloseExec(stmt Stmt, err error, typ ResultType, args ...interface{}) (int64, error)
- func CloseUpdate(stmt Stmt, err error, args ...interface{}) (int64, error)
- func Exec(stmt Stmt, err error, typ ResultType, args ...interface{}) (int64, error)
- func FieldPtrs(model Model, fields uint64, args ...interface{}) []interface{}
- func FieldVals(model Model, fields uint64, args ...interface{}) []interface{}
- func FieldsExcp(numField, fields uint64) uint64
- func FieldsIdentity(sqlType SQLType, numField, fields, whereFields uint64) uint64
- func NewSqlId(create func(Executor) string) (id uint64)
- func ResolveResult(res sql.Result, err error, typ ResultType) (int64, error)
- func SQLPrint(enable bool, printer func(formart string, v ...interface{}))
- func Update(stmt Stmt, err error, args ...interface{}) (int64, error)
- type BoolStore
- type Cols
- type Columner
- type DB
- func (db *DB) All(store Store, model Model, fields, whereFields uint64) error
- func (db *DB) ArgsAll(store Store, model Model, fields, whereFields uint64, args ...interface{}) error
- func (db *DB) ArgsCount(model Model, whereFields uint64, args ...interface{}) (count int64, err error)
- func (db *DB) ArgsDelete(model Model, whereFields uint64, args ...interface{}) (int64, error)
- func (db *DB) ArgsIncrBy(model Model, field, whereFields uint64, args ...interface{}) (int64, error)
- func (db *DB) ArgsInsert(model Model, fields uint64, resType ResultType, args ...interface{}) (int64, error)
- func (db *DB) ArgsLimit(store Store, model Model, fields, whereFields uint64, args ...interface{}) error
- func (db *DB) ArgsOne(model Model, fields, whereFields uint64, args []interface{}, ...) error
- func (db *DB) ArgsUpdate(model Model, fields, whereFields uint64, args ...interface{}) (int64, error)
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) Connect(driver Driver, dsn string, maxIdle, maxOpen int) error
- func (db *DB) Count(model Model, whereFields uint64) (count int64, err error)
- func (db *DB) Delete(model Model, whereFields uint64) (int64, error)
- func (db *DB) Driver() Driver
- func (db *DB) Exec(sql string, resType ResultType, args ...interface{}) (int64, error)
- func (db *DB) ExecById(sqlid uint64, resTyp ResultType, args ...interface{}) (int64, error)
- func (db *DB) ExecUpdate(sql string, args ...interface{}) (int64, error)
- func (db *DB) IncrBy(model Model, field, whereFields uint64, count int) (int64, error)
- func (db *DB) Insert(model Model, fields uint64, resType ResultType) (int64, error)
- func (db *DB) Limit(store Store, model Model, fields, whereFields uint64, start, count int64) error
- func (db *DB) One(model Model, fields, whereFields uint64) error
- func (db *DB) QueryById(sqlid uint64, args ...interface{}) Scanner
- func (db *DB) StmtById(sqlid uint64) (Stmt, error)
- func (db *DB) Table(model Model) *Table
- func (db *DB) TxDo(do func(*Tx) error) error
- func (db *DB) Update(model Model, fields, whereFields uint64) (int64, error)
- func (db *DB) UpdateById(sqlid uint64, args ...interface{}) (int64, error)
- type Driver
- type Executor
- type IntStore
- type Model
- type Nocacher
- type NopCloseStmt
- type ResultType
- type SQLBuilder
- type SQLPrinter
- type SQLType
- type Scanner
- type Stmt
- type Store
- type StringStore
- type Table
- func (t *Table) Col(field uint64) string
- func (t *Table) Cols(fields uint64) Cols
- func (t *Table) Prepare(exec Executor, sqlType SQLType, fields, whereFields uint64, build SQLBuilder) (Stmt, error)
- func (t *Table) PrepareAll(exec Executor, fields, whereFields uint64) (Stmt, error)
- func (t *Table) PrepareCount(exec Executor, whereFields uint64) (Stmt, error)
- func (t *Table) PrepareDelete(exec Executor, whereFields uint64) (Stmt, error)
- func (t *Table) PrepareIncrBy(exec Executor, field, whereFields uint64) (Stmt, error)
- func (t *Table) PrepareInsert(exec Executor, fields uint64) (Stmt, error)
- func (t *Table) PrepareLimit(exec Executor, fields, whereFields uint64) (Stmt, error)
- func (t *Table) PrepareOne(exec Executor, fields, whereFields uint64) (Stmt, error)
- func (t *Table) PrepareUpdate(exec Executor, fields, whereFields uint64) (Stmt, error)
- func (t *Table) SQLAll(_ Driver, fields, whereFields uint64) string
- func (t *Table) SQLCount(_ Driver, _, whereFields uint64) string
- func (t *Table) SQLDelete(_ Driver, _, whereFields uint64) string
- func (t *Table) SQLIncrBy(_ Driver, field, whereFields uint64) string
- func (t *Table) SQLInsert(_ Driver, fields, _ uint64) string
- func (t *Table) SQLLimit(driver Driver, fields, whereFields uint64) string
- func (t *Table) SQLOne(_ Driver, fields, whereFields uint64) string
- func (t *Table) SQLUpdate(_ Driver, fields, whereFields uint64) string
- func (t *Table) Stmt(exec Executor, sqlType SQLType, fields, whereFields uint64, build SQLBuilder) (Stmt, error)
- func (t *Table) StmtAll(exec Executor, fields, whereFields uint64) (Stmt, error)
- func (t *Table) StmtCount(exec Executor, whereFields uint64) (Stmt, error)
- func (t *Table) StmtDelete(exec Executor, whereFields uint64) (Stmt, error)
- func (t *Table) StmtIncrBy(exec Executor, field, whereFields uint64) (Stmt, error)
- func (t *Table) StmtInsert(exec Executor, fields uint64) (Stmt, error)
- func (t *Table) StmtLimit(exec Executor, fields, whereFields uint64) (Stmt, error)
- func (t *Table) StmtOne(exec Executor, fields, whereFields uint64) (Stmt, error)
- func (t *Table) StmtUpdate(exec Executor, fields, whereFields uint64) (Stmt, error)
- func (t *Table) TabCol(field uint64) string
- func (t *Table) TabCols(fields uint64) Cols
- func (t *Table) TabWhere(fields uint64) string
- func (t *Table) Where(fields uint64) string
- type Tx
- func (tx *Tx) All(store Store, model Model, fields, whereFields uint64) error
- func (tx *Tx) ArgsAll(store Store, model Model, fields, whereFields uint64, args ...interface{}) error
- func (tx *Tx) ArgsCount(model Model, whereFields uint64, args ...interface{}) (count int64, err error)
- func (tx *Tx) ArgsDelete(model Model, whereFields uint64, args ...interface{}) (int64, error)
- func (tx *Tx) ArgsIncrBy(model Model, field, whereFields uint64, args ...interface{}) (int64, error)
- func (tx *Tx) ArgsInsert(model Model, fields uint64, resType ResultType, args ...interface{}) (int64, error)
- func (tx *Tx) ArgsLimit(store Store, model Model, fields, whereFields uint64, args ...interface{}) error
- func (tx *Tx) ArgsOne(model Model, fields, whereFields uint64, args []interface{}, ...) error
- func (tx *Tx) ArgsUpdate(model Model, fields, whereFields uint64, args ...interface{}) (int64, error)
- func (tx *Tx) Close() error
- func (tx *Tx) Count(model Model, whereFields uint64) (count int64, err error)
- func (tx *Tx) Delete(model Model, whereFields uint64) (int64, error)
- func (tx *Tx) Driver() Driver
- func (tx *Tx) Exec(sql string, resType ResultType, args ...interface{}) (int64, error)
- func (tx *Tx) ExecById(sqlid uint64, resType ResultType, args ...interface{}) (int64, error)
- func (tx *Tx) ExecUpdate(sql string, args ...interface{}) (int64, error)
- func (tx *Tx) IncrBy(model Model, field, whereFields uint64, count int) (int64, error)
- func (tx *Tx) Insert(model Model, fields uint64, resType ResultType) (int64, error)
- func (tx *Tx) Limit(store Store, model Model, fields, whereFields uint64, start, count int64) error
- func (tx *Tx) One(model Model, fields, whereFields uint64) error
- func (tx *Tx) PrepareById(sqlid uint64) (Stmt, error)
- func (tx *Tx) QueryById(sqlid uint64, args ...interface{}) Scanner
- func (tx *Tx) Success(success bool)
- func (tx *Tx) Table(model Model) *Table
- func (tx *Tx) Update(model Model, fields, whereFields uint64) (int64, error)
- func (tx *Tx) UpdateById(sqlid uint64, args ...interface{}) (int64, error)
Constants ¶
const ( STMT_CLOSEABLE = true STMT_NOPCLOSE = false )
const (
MAX_NUMFIELDS = 30
)
Variables ¶
var ( // InitialSQLCount is the initial capacity of sql storage, // it should bee changed before NewDB InitialSQLCount uint64 = 256 )
var ( // NumFIelds return fields count NumFields = bitset.BitCount )
Functions ¶
func CloseExec ¶
func CloseExec(stmt Stmt, err error, typ ResultType, args ...interface{}) (int64, error)
Exec execute stmt with given arguments and resolve the result if error is nil
func CloseUpdate ¶
Update always returl the count of affected rows
func Exec ¶
func Exec(stmt Stmt, err error, typ ResultType, args ...interface{}) (int64, error)
Exec execute stmt with given arguments and resolve the result if error is nil
func FieldPtrs ¶
FieldPtrs is similar to FieldVals, but for field pointers. FieldPtrs only used for query operations such as One, Limit, All.
func FieldVals ¶
FieldVals will extract values of fields from model, and concat with remains arguments
func FieldsExcp ¶
FieldsExcp create fieldset except given fields
func FieldsIdentity ¶
FieldsIdentity create signature from fields
func ResolveResult ¶
ResolveResult resolve sql result, if need id, return last insert id else return affected rows count
Types ¶
type Cols ¶
type Cols interface { // String return columns string join with ",", // result like "foo, bar" String() string // Paramed return columns string joind with "=?,", last "," was trimed, // result like "foo=?, bar=?" Paramed() string // OnlyParam return columns placeholdered string, // each column was replaced with "?" // result like "?, ?, ?, ?", count of "?" is colums length OnlyParam() string // Join append suffix string to each columns then join them with the seperator Join(suffix, sep string) string // Length return columns count Length() int }
type Columner ¶
type Columner interface { // Columns return all column names for this Model Columns() []string }
Columner is a optional interface for Model, if Model implements this interface, it's no need to parse Model info with reflection
type DB ¶
type DB struct { *sql.DB // initial models count for select 'All', default 20 InitialModels int // contains filtered or unexported fields }
DB holds database connections, store all tables
func (*DB) ArgsCount ¶
func (db *DB) ArgsCount(model Model, whereFields uint64, args ...interface{}) (count int64, err error)
ArgsCount return count of rows for model use custome arguments
func (*DB) ArgsDelete ¶
func (*DB) ArgsIncrBy ¶
func (*DB) ArgsInsert ¶
func (*DB) ArgsLimit ¶
func (db *DB) ArgsLimit(store Store, model Model, fields, whereFields uint64, args ...interface{}) error
The last two arguments must be "start" and "count" of limition with type "int"
func (*DB) ArgsUpdate ¶
func (*DB) Exec ¶
func (db *DB) Exec(sql string, resType ResultType, args ...interface{}) (int64, error)
Exec execute a update operation, return resolved result
func (*DB) ExecById ¶
func (db *DB) ExecById(sqlid uint64, resTyp ResultType, args ...interface{}) (int64, error)
func (*DB) ExecUpdate ¶
ExecUpdate execute a update operation, return resolved result
type Driver ¶ added in v0.6.0
type Driver interface { String() string DSN(host, port, username, password, dbname string, cfg map[string]string) string // Prepare should replace the standard placeholder '?' with driver specific placeholder, // for postgresql it's '$n' Prepare(sql string) string SQLLimit() string ParamLimit(start, count int64) (int64, int64) PrimaryKey() string DuplicateKey(err error) string ForeignKey(err error) string }
type Executor ¶ added in v0.6.0
type Executor interface { Driver() Driver Table(model Model) *Table Prepare(sql string) (*sql.Stmt, error) Insert(model Model, fields uint64, resType ResultType) (int64, error) ArgsInsert(model Model, fields uint64, resType ResultType, args ...interface{}) (int64, error) Update(model Model, fields, whereFields uint64) (int64, error) ArgsUpdate(model Model, fields, whereFields uint64, args ...interface{}) (int64, error) Delete(model Model, whereFields uint64) (int64, error) ArgsDelete(model Model, whereFields uint64, args ...interface{}) (int64, error) One(model Model, fields, whereFields uint64) error ArgsOne(model Model, fields, whereFields uint64, args []interface{}, ptrs ...interface{}) error Limit(store Store, model Model, fields, whereFields uint64, start, count int64) error ArgsLimit(store Store, model Model, fields, whereFields uint64, args ...interface{}) error All(store Store, model Model, fields, whereFields uint64) error ArgsAll(store Store, model Model, fields, whereFields uint64, args ...interface{}) error Count(model Model, whereFields uint64) (count int64, err error) ArgsCount(model Model, whereFields uint64, args ...interface{}) (count int64, err error) IncrBy(model Model, field, whereFields uint64, count int) (int64, error) ArgsIncrBy(model Model, field, whereFields uint64, args ...interface{}) (int64, error) ExecUpdate(sql string, args ...interface{}) (int64, error) Exec(sql string, resType ResultType, args ...interface{}) (int64, error) ExecById(sqlid uint64, resTyp ResultType, args ...interface{}) (int64, error) UpdateById(sqlid uint64, args ...interface{}) (int64, error) QueryById(sqlid uint64, args ...interface{}) Scanner }
type Model ¶
type Model interface { // Table return database table name that the model mapped Table() string // Vals store values of fields to given slice, the slice has length, // just setup value by index. The value order MUST same as fields defined // in strcuture Vals(fields uint64, vals []interface{}) // Ptrs is similar to Vals, but for field pointers Ptrs(fields uint64, ptrs []interface{}) }
Model represent a database model mapping to a table
type Nocacher ¶
type Nocacher interface {
Nocache() bool
}
Nocacher is a optional interface for Model, if Model implements this interface, and NoCache method return true, it will not allocate memory to store sql, stmt, columns for this Model, all sqls, stmts must be stored in DB instance.
If Nocache, the only methods to get Stmt are DB.StmtById and Tx.PrepareById, implements it only when you actually know what are you do.
type NopCloseStmt ¶
func (NopCloseStmt) Close ¶
func (s NopCloseStmt) Close() error
type ResultType ¶
type ResultType int
const ( RES_NO ResultType = iota // don't resolve sql.Result RES_ID // Result.LastInsertID RES_ROWS // Result.RowsAffected )
type SQLBuilder ¶
SQLBuilder build sql for model using fields and where fields
type SQLPrinter ¶
type SQLPrinter func(string, ...interface{})
func (SQLPrinter) Print ¶
func (p SQLPrinter) Print(fromcache bool, sql string)
type SQLType ¶
type SQLType uint64
const ( // These are several predefined sql types INSERT SQLType = iota + 1<<(MAX_NUMFIELDS*2) DELETE UPDATE INCRBY LIMIT ONE ALL )
type Scanner ¶
Scanner scan database rows to data Store when Error is nil, if the Rows is empty, sql.ErrNoRows was returned, the Rows will always be be Closed
func CloseQuery ¶
Query execute the query stmt, error stored in Scanner
type Stmt ¶
type Store ¶
type Store interface { // Init will be called twice, first to allocate initial data space, second to specified // the final row count // Init initial the data store with size rows, if size is not enough, // Realloc will be called Init(size int) // Final indicate the last found rows Final(size int) // Ptrs should store pointers of data store at given index to the ptr parameter Ptrs(index int, ptrs []interface{}) // Realloc will occurred if the initial size is not enough, only occured // when call the All method of Scanner. // The return value shold be the new size of Store. // If don't want to continue, just return a non-positive number. Realloc(currSize int) (latest int) }
Store defines the interface to store data from databqase rows
type StringStore ¶
type StringStore struct {
Values []string
}
func (*StringStore) Clear ¶
func (s *StringStore) Clear()
func (*StringStore) Final ¶
func (s *StringStore) Final(size int)
func (*StringStore) Init ¶
func (s *StringStore) Init(size int)
func (*StringStore) Ptrs ¶
func (s *StringStore) Ptrs(index int, ptrs []interface{})
func (*StringStore) Realloc ¶
func (s *StringStore) Realloc(count int) int
type Table ¶
Table represent information of type contains field count, table name, field names, field offsets
because count sql and limit select sql is both stored in the same cache, you should not use a empty fields for limit select, that will conflict with count sql and get the wrong sql statement.
func (*Table) Cols ¶
Cols return column names for given fields if fields is only one, return single column else return column slice
func (*Table) Prepare ¶
func (t *Table) Prepare(exec Executor, sqlType SQLType, fields, whereFields uint64, build SQLBuilder) (Stmt, error)
Stmt get sql from cache container, if cache not exist, then create new
func (*Table) PrepareAll ¶
func (*Table) PrepareCount ¶
func (*Table) PrepareDelete ¶
func (*Table) PrepareIncrBy ¶
func (*Table) PrepareInsert ¶
func (*Table) PrepareLimit ¶
func (*Table) PrepareOne ¶
func (*Table) PrepareUpdate ¶
func (*Table) Stmt ¶
func (t *Table) Stmt(exec Executor, sqlType SQLType, fields, whereFields uint64, build SQLBuilder) (Stmt, error)
Stmt get sql from cache container, if cache not exist, then create new
func (*Table) StmtDelete ¶
func (*Table) StmtIncrBy ¶
func (*Table) StmtUpdate ¶
func (*Table) TabCols ¶
TabCols return column names for given fields with type's table name as prefix like table.column
type Tx ¶
func (*Tx) ArgsAll ¶
func (tx *Tx) ArgsAll(store Store, model Model, fields, whereFields uint64, args ...interface{}) error
ArgsAll select all rows, the last two argument must be "start" and "count"
func (*Tx) ArgsCount ¶
func (tx *Tx) ArgsCount(model Model, whereFields uint64, args ...interface{}) (count int64, err error)
ArgsCount return count of rows for model use custome arguments
func (*Tx) ArgsDelete ¶
func (*Tx) ArgsIncrBy ¶
func (*Tx) ArgsInsert ¶
func (*Tx) ArgsLimit ¶
func (tx *Tx) ArgsLimit(store Store, model Model, fields, whereFields uint64, args ...interface{}) error
The last two arguments must be "start" and "count" of limition with type "int"
func (*Tx) ArgsUpdate ¶
func (*Tx) Close ¶ added in v0.6.1
Done check if error is nil then commit transaction, otherwise rollback. Done should be called only once, otherwise it will panic. Done should be called in deferred function to avoid uncommitted/unrollbacked transaction caused by panic.
Example:
func operation() (err error) { tx, err := db.Begin() if err != nil { return err } defer tx.Close() // do something tx.Success(true) return // err must be a named return value, otherwise, error in deferred isSuccessfunction will be lost }
func (*Tx) Exec ¶
func (tx *Tx) Exec(sql string, resType ResultType, args ...interface{}) (int64, error)
Exec execute a update operation, return resolved result
func (*Tx) ExecById ¶
func (tx *Tx) ExecById(sqlid uint64, resType ResultType, args ...interface{}) (int64, error)
ExecById execute a update operation, return rows affected
func (*Tx) ExecUpdate ¶
ExecUpdate execute a update operation, return resolved result
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
Package dberrs help processing database errors
|
Package dberrs help processing database errors |
example
|
|