Documentation
¶
Index ¶
- Constants
- Variables
- func GenSqlKey(sql string, args interface{}) string
- func MapToSlice(query string, mp interface{}) (string, []interface{}, error)
- func PutCacheSql(m Cacher, ids []PK, tableName, sql string, args interface{}) error
- func QueryDriver(driverName string) driver
- func ReflectNew(typ reflect.Type) reflect.Value
- func RegisterDialect(dbName dbType, dialect Dialect)
- func RegisterDriver(driverName string, driver driver)
- func SQLType2Type(st SQLType) reflect.Type
- func StructToSlice(query string, st interface{}) (string, []interface{}, error)
- type Base
- func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset string) string
- func (b *Base) DBType() dbType
- func (b *Base) DataSourceName() string
- func (b *Base) DriverName() string
- func (b *Base) Init(dialect Dialect, uri *Uri, drivername, dataSourceName string) error
- func (b *Base) Quote(c string) string
- func (b *Base) URI() *Uri
- type CacheMapper
- type CacheStore
- type Cacher
- type Column
- type Conversion
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) ExecMap(query string, mp interface{}) (sql.Result, error)
- func (db *DB) ExecStruct(query string, st interface{}) (sql.Result, error)
- func (db *DB) Prepare(query string) (*Stmt, error)
- func (db *DB) Query(query string, args ...interface{}) (*Rows, error)
- func (db *DB) QueryMap(query string, mp interface{}) (*Rows, error)
- func (db *DB) QueryRow(query string, args ...interface{}) *Row
- func (db *DB) QueryRowMap(query string, mp interface{}) *Row
- func (db *DB) QueryRowStruct(query string, st interface{}) *Row
- func (db *DB) QueryStruct(query string, st interface{}) (*Rows, error)
- type Dialect
- type EmptyScanner
- type Filter
- type IMapper
- type IdFilter
- type Index
- type PK
- type PrefixMapper
- type QuoteFilter
- type Quoter
- type Row
- type Rows
- type SQLType
- type SameMapper
- type SnakeMapper
- type Stmt
- func (s *Stmt) ExecMap(mp interface{}) (sql.Result, error)
- func (s *Stmt) ExecStruct(st interface{}) (sql.Result, error)
- func (s *Stmt) Query(args ...interface{}) (*Rows, error)
- func (s *Stmt) QueryMap(mp interface{}) (*Rows, error)
- func (s *Stmt) QueryRow(args ...interface{}) *Row
- func (s *Stmt) QueryRowMap(mp interface{}) *Row
- func (s *Stmt) QueryRowStruct(st interface{}) *Row
- func (s *Stmt) QueryStruct(st interface{}) (*Rows, error)
- type SuffixMapper
- type Table
- func (table *Table) AddColumn(col *Column)
- func (table *Table) AddIndex(index *Index)
- func (table *Table) AutoIncrColumn() *Column
- func (table *Table) Columns() map[string]*Column
- func (table *Table) ColumnsSeq() []string
- func (table *Table) GetColumn(name string) *Column
- func (table *Table) PKColumns() []*Column
- func (table *Table) VersionColumn() *Column
- type Tx
- func (tx *Tx) ExecMap(query string, mp interface{}) (sql.Result, error)
- func (tx *Tx) ExecStruct(query string, st interface{}) (sql.Result, error)
- func (tx *Tx) Prepare(query string) (*Stmt, error)
- func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error)
- func (tx *Tx) QueryMap(query string, mp interface{}) (*Rows, error)
- func (tx *Tx) QueryRow(query string, args ...interface{}) *Row
- func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row
- func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row
- func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error)
- func (tx *Tx) Stmt(stmt *Stmt) *Stmt
- type Uri
Constants ¶
const ( // default cache expired time CacheExpired = 60 * time.Minute // not use now CacheMaxMemory = 256 // evey ten minutes to clear all expired nodes CacheGcInterval = 10 * time.Minute // each time when gc to removed max nodes CacheGcMaxRemoved = 20 )
const ( TWOSIDES = iota + 1 ONLYTODB ONLYFROMDB )
const ( IndexType = iota + 1 UniqueType )
const ( POSTGRES = "postgres" SQLITE = "sqlite3" MYSQL = "mysql" MSSQL = "mssql" ORACLE = "oracle" )
Variables ¶
var ( ErrNoMapPointer = errors.New("mp should be a map's pointer") ErrNoStructPointer = errors.New("mp should be a map's pointer") )
var ( Bit = "BIT" TinyInt = "TINYINT" SmallInt = "SMALLINT" MediumInt = "MEDIUMINT" Int = "INT" Integer = "INTEGER" BigInt = "BIGINT" Char = "CHAR" Varchar = "VARCHAR" TinyText = "TINYTEXT" Text = "TEXT" MediumText = "MEDIUMTEXT" LongText = "LONGTEXT" Date = "DATE" DateTime = "DATETIME" Time = "TIME" TimeStamp = "TIMESTAMP" TimeStampz = "TIMESTAMPZ" Decimal = "DECIMAL" Numeric = "NUMERIC" Real = "REAL" Float = "FLOAT" Double = "DOUBLE" Binary = "BINARY" VarBinary = "VARBINARY" TinyBlob = "TINYBLOB" Blob = "BLOB" MediumBlob = "MEDIUMBLOB" LongBlob = "LONGBLOB" Bytea = "BYTEA" Bool = "BOOL" Serial = "SERIAL" BigSerial = "BIGSERIAL" SqlTypes = map[string]bool{ Bit: true, TinyInt: true, SmallInt: true, MediumInt: true, Int: true, Integer: true, BigInt: true, Char: true, Varchar: true, TinyText: true, Text: true, MediumText: true, LongText: true, Date: true, DateTime: true, Time: true, TimeStamp: true, TimeStampz: true, Decimal: true, Numeric: true, Binary: true, VarBinary: true, Real: true, Float: true, Double: true, TinyBlob: true, Blob: true, MediumBlob: true, LongBlob: true, Bytea: true, Bool: true, Serial: true, BigSerial: true, } )
var ( IntType = reflect.TypeOf(c_INT_DEFAULT) Int8Type = reflect.TypeOf(c_INT8_DEFAULT) Int16Type = reflect.TypeOf(c_INT16_DEFAULT) Int32Type = reflect.TypeOf(c_INT32_DEFAULT) Int64Type = reflect.TypeOf(c_INT64_DEFAULT) UintType = reflect.TypeOf(c_UINT_DEFAULT) Uint8Type = reflect.TypeOf(c_UINT8_DEFAULT) Uint16Type = reflect.TypeOf(c_UINT16_DEFAULT) Uint32Type = reflect.TypeOf(c_UINT32_DEFAULT) Uint64Type = reflect.TypeOf(c_UINT64_DEFAULT) Float32Type = reflect.TypeOf(c_FLOAT32_DEFAULT) Float64Type = reflect.TypeOf(c_FLOAT64_DEFAULT) Complex64Type = reflect.TypeOf(c_COMPLEX64_DEFAULT) Complex128Type = reflect.TypeOf(c_COMPLEX128_DEFAULT) StringType = reflect.TypeOf(c_EMPTY_STRING) BoolType = reflect.TypeOf(c_BOOL_DEFAULT) ByteType = reflect.TypeOf(c_BYTE_DEFAULT) TimeType = reflect.TypeOf(c_TIME_DEFAULT) )
var ( PtrIntType = reflect.PtrTo(IntType) PtrInt8Type = reflect.PtrTo(Int8Type) PtrInt16Type = reflect.PtrTo(Int16Type) PtrInt32Type = reflect.PtrTo(Int32Type) PtrInt64Type = reflect.PtrTo(Int64Type) PtrUintType = reflect.PtrTo(UintType) PtrUint8Type = reflect.PtrTo(Uint8Type) PtrUint16Type = reflect.PtrTo(Uint16Type) PtrUint32Type = reflect.PtrTo(Uint32Type) PtrUint64Type = reflect.PtrTo(Uint64Type) PtrFloat32Type = reflect.PtrTo(Float32Type) PtrFloat64Type = reflect.PtrTo(Float64Type) PtrComplex64Type = reflect.PtrTo(Complex64Type) PtrComplex128Type = reflect.PtrTo(Complex128Type) PtrStringType = reflect.PtrTo(StringType) PtrBoolType = reflect.PtrTo(BoolType) PtrByteType = reflect.PtrTo(ByteType) PtrTimeType = reflect.PtrTo(TimeType) )
Functions ¶
func MapToSlice ¶
func PutCacheSql ¶
func QueryDriver ¶
func QueryDriver(driverName string) driver
func RegisterDialect ¶
func RegisterDialect(dbName dbType, dialect Dialect)
func RegisterDriver ¶
func RegisterDriver(driverName string, driver driver)
func StructToSlice ¶
Types ¶
type Base ¶
type Base struct { *Uri // contains filtered or unexported fields }
func (*Base) CreateTableSql ¶
func (*Base) DataSourceName ¶
func (*Base) DriverName ¶
type CacheMapper ¶
type CacheMapper struct {
// contains filtered or unexported fields
}
func NewCacheMapper ¶
func NewCacheMapper(mapper IMapper) *CacheMapper
func (*CacheMapper) Obj2Table ¶
func (m *CacheMapper) Obj2Table(o string) string
func (*CacheMapper) Table2Obj ¶
func (m *CacheMapper) Table2Obj(t string) string
type CacheStore ¶
type CacheStore interface { // key is primary key or composite primary key or unique key's value // value is struct's pointer // key format : <tablename>-p-<pk1>-<pk2>... Put(key string, value interface{}) error Get(key string) (interface{}, error) Del(key string) error }
CacheStore is a interface to store cache
type Cacher ¶
type Cacher interface { GetIds(tableName, sql string) interface{} GetBean(tableName string, id string) interface{} PutIds(tableName, sql string, ids interface{}) PutBean(tableName string, id string, obj interface{}) DelIds(tableName, sql string) DelBean(tableName string, id string) ClearIds(tableName string) ClearBeans(tableName string) }
Cacher is an interface to provide cache id format : u-<pk1>-<pk2>...
type Column ¶
type Column struct { Name string FieldName string SQLType SQLType Length int Length2 int Nullable bool Default string Indexes map[string]bool IsPrimaryKey bool IsAutoIncrement bool MapType int IsCreated bool IsUpdated bool IsCascade bool IsVersion bool // contains filtered or unexported fields }
database column
func (*Column) StringNoPk ¶
type Conversion ¶
Conversion is an interface. A type implements Conversion will according the custom method to fill into database and retrieve from database.
type DB ¶
func (*DB) QueryRowMap ¶
func (*DB) QueryRowStruct ¶
type Dialect ¶
type Dialect interface { Init(*Uri, string, string) error URI() *Uri DBType() dbType SqlType(t *Column) string SupportInsertMany() bool QuoteStr() string AutoIncrStr() string SupportEngine() bool SupportCharset() bool IndexOnTable() bool IndexCheckSql(tableName, idxName string) (string, []interface{}) TableCheckSql(tableName string) (string, []interface{}) ColumnCheckSql(tableName, colName string) (string, []interface{}) GetColumns(tableName string) ([]string, map[string]*Column, error) GetTables() ([]*Table, error) GetIndexes(tableName string) (map[string]*Index, error) CreateTableSql(table *Table, tableName, storeEngine, charset string) string Filters() []Filter DriverName() string DataSourceName() string }
a dialect is a driver's wrapper
func QueryDialect ¶
func QueryDialect(dbName dbType) Dialect
type EmptyScanner ¶
type EmptyScanner struct { }
func (EmptyScanner) Scan ¶
func (EmptyScanner) Scan(src interface{}) error
type IdFilter ¶
type IdFilter struct { }
IdFilter filter SQL replace (id) to primary key column name
type PrefixMapper ¶
provide prefix table name support
func NewPrefixMapper ¶
func NewPrefixMapper(mapper IMapper, prefix string) PrefixMapper
func (PrefixMapper) Obj2Table ¶
func (mapper PrefixMapper) Obj2Table(name string) string
func (PrefixMapper) Table2Obj ¶
func (mapper PrefixMapper) Table2Obj(name string) string
type QuoteFilter ¶
type QuoteFilter struct { }
QuoteFilter filter SQL replace ` to database's own quote character
type Rows ¶
func (*Rows) ScanSlice ¶
scan data to a slice's pointer, slice's length should equal to columns' number
func (*Rows) ScanStruct ¶
scan data to a struct's pointer according field index
func (*Rows) ScanStruct2 ¶
scan data to a struct's pointer according field name
type SameMapper ¶
type SameMapper struct { }
SameMapper implements IMapper and provides same name between struct and database table
func (SameMapper) Obj2Table ¶
func (m SameMapper) Obj2Table(o string) string
func (SameMapper) Table2Obj ¶
func (m SameMapper) Table2Obj(t string) string
type SnakeMapper ¶
type SnakeMapper struct { }
SnakeMapper implements IMapper and provides name transaltion between struct and database table
func (SnakeMapper) Obj2Table ¶
func (mapper SnakeMapper) Obj2Table(name string) string
func (SnakeMapper) Table2Obj ¶
func (mapper SnakeMapper) Table2Obj(name string) string
type SuffixMapper ¶
provide suffix table name support
func NewSuffixMapper ¶
func NewSuffixMapper(mapper IMapper, suffix string) SuffixMapper
func (SuffixMapper) Obj2Table ¶
func (mapper SuffixMapper) Obj2Table(name string) string
func (SuffixMapper) Table2Obj ¶
func (mapper SuffixMapper) Table2Obj(name string) string
type Table ¶
type Table struct { Name string Type reflect.Type Indexes map[string]*Index PrimaryKeys []string AutoIncrement string Created map[string]bool Updated string Version string // contains filtered or unexported fields }
database table
func NewEmptyTable ¶
func NewEmptyTable() *Table