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 RegisterDialect(dbName DbType, dialectFunc func() Dialect)
- func RegisterDriver(driverName string, driver Driver)
- func RegisteredDriverSize() int
- func SQLType2Type(st SQLType) reflect.Type
- func StructToSlice(query string, st interface{}) (string, []interface{}, error)
- type Base
- func (b *Base) AndStr() string
- func (db *Base) CreateIndexSql(tableName string, index *Index) string
- func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset string) string
- func (b *Base) DB() *DB
- func (b *Base) DBType() DbType
- func (b *Base) DataSourceName() string
- func (b *Base) DriverName() string
- func (db *Base) DropIndexSql(tableName string, index *Index) string
- func (db *Base) DropTableSql(tableName string) string
- func (b *Base) EqStr() string
- func (b *Base) ForUpdateSql(query string) string
- func (b *Base) FormatBytes(bs []byte) string
- func (db *Base) HasRecords(query string, args ...interface{}) (bool, error)
- func (b *Base) Init(db *DB, dialect Dialect, uri *Uri, drivername, dataSourceName string) error
- func (db *Base) IsColumnExist(tableName, colName string) (bool, error)
- func (b *Base) LogSQL(sql string, args []interface{})
- func (db *Base) ModifyColumnSql(tableName string, col *Column) string
- func (b *Base) OrStr() string
- func (db *Base) RollBackStr() string
- func (b *Base) SetLogger(logger ILogger)
- func (b *Base) SetParams(params map[string]string)
- func (b *Base) ShowCreateNull() bool
- func (db *Base) SupportDropIfExists() bool
- 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) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (db *DB) ExecMap(query string, mp interface{}) (sql.Result, error)
- func (db *DB) ExecMapContext(ctx context.Context, query string, mp interface{}) (sql.Result, error)
- func (db *DB) ExecStruct(query string, st interface{}) (sql.Result, error)
- func (db *DB) ExecStructContext(ctx context.Context, query string, st interface{}) (sql.Result, error)
- func (db *DB) Prepare(query string) (*Stmt, error)
- func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error)
- func (db *DB) Query(query string, args ...interface{}) (*Rows, error)
- func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
- func (db *DB) QueryMap(query string, mp interface{}) (*Rows, error)
- func (db *DB) QueryMapContext(ctx context.Context, query string, mp interface{}) (*Rows, error)
- func (db *DB) QueryRow(query string, args ...interface{}) *Row
- func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
- func (db *DB) QueryRowMap(query string, mp interface{}) *Row
- func (db *DB) QueryRowMapContext(ctx context.Context, query string, mp interface{}) *Row
- func (db *DB) QueryRowStruct(query string, st interface{}) *Row
- func (db *DB) QueryRowStructContext(ctx context.Context, query string, st interface{}) *Row
- func (db *DB) QueryStruct(query string, st interface{}) (*Rows, error)
- func (db *DB) QueryStructContext(ctx context.Context, query string, st interface{}) (*Rows, error)
- type DbType
- type Dialect
- type Driver
- type EmptyScanner
- type Filter
- type GonicMapper
- type ILogger
- type IMapper
- type IdFilter
- type Index
- type LogLevel
- type NullTime
- type PK
- type PrefixMapper
- type QuoteFilter
- type Quoter
- type Row
- func (row *Row) Columns() ([]string, error)
- func (row *Row) Scan(dest ...interface{}) error
- func (row *Row) ScanMap(dest interface{}) error
- func (row *Row) ScanSlice(dest interface{}) error
- func (row *Row) ScanStructByIndex(dest interface{}) error
- func (row *Row) ScanStructByName(dest interface{}) error
- func (row *Row) ToMapString() (map[string]string, error)
- type Rows
- type SQLType
- type SameMapper
- type SeqFilter
- type SnakeMapper
- type Stmt
- func (s *Stmt) ExecMap(mp interface{}) (sql.Result, error)
- func (s *Stmt) ExecMapContext(ctx context.Context, mp interface{}) (sql.Result, error)
- func (s *Stmt) ExecStruct(st interface{}) (sql.Result, error)
- func (s *Stmt) ExecStructContext(ctx context.Context, st interface{}) (sql.Result, error)
- func (s *Stmt) Query(args ...interface{}) (*Rows, error)
- func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error)
- func (s *Stmt) QueryMap(mp interface{}) (*Rows, error)
- func (s *Stmt) QueryMapContext(ctx context.Context, mp interface{}) (*Rows, error)
- func (s *Stmt) QueryRow(args ...interface{}) *Row
- func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row
- func (s *Stmt) QueryRowMap(mp interface{}) *Row
- func (s *Stmt) QueryRowMapContext(ctx context.Context, mp interface{}) *Row
- func (s *Stmt) QueryRowStruct(st interface{}) *Row
- func (s *Stmt) QueryRowStructContext(ctx context.Context, st interface{}) *Row
- func (s *Stmt) QueryStruct(st interface{}) (*Rows, error)
- func (s *Stmt) QueryStructContext(ctx context.Context, 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) ColumnType(name string) reflect.Type
- func (table *Table) Columns() []*Column
- func (table *Table) ColumnsSeq() []string
- func (table *Table) DeletedColumn() *Column
- func (table *Table) GetColumn(name string) *Column
- func (table *Table) GetColumnIdx(name string, idx int) *Column
- func (table *Table) PKColumns() []*Column
- func (table *Table) UpdatedColumn() *Column
- func (table *Table) VersionColumn() *Column
- type Tx
- func (tx *Tx) ExecMap(query string, mp interface{}) (sql.Result, error)
- func (tx *Tx) ExecMapContext(ctx context.Context, query string, mp interface{}) (sql.Result, error)
- func (tx *Tx) ExecStruct(query string, st interface{}) (sql.Result, error)
- func (tx *Tx) ExecStructContext(ctx context.Context, query string, st interface{}) (sql.Result, error)
- func (tx *Tx) Prepare(query string) (*Stmt, error)
- func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error)
- func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error)
- func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
- func (tx *Tx) QueryMap(query string, mp interface{}) (*Rows, error)
- func (tx *Tx) QueryMapContext(ctx context.Context, query string, mp interface{}) (*Rows, error)
- func (tx *Tx) QueryRow(query string, args ...interface{}) *Row
- func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
- func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row
- func (tx *Tx) QueryRowMapContext(ctx context.Context, query string, mp interface{}) *Row
- func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row
- func (tx *Tx) QueryRowStructContext(ctx context.Context, query string, st interface{}) *Row
- func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error)
- func (tx *Tx) QueryStructContext(ctx context.Context, query string, st interface{}) (*Rows, error)
- func (tx *Tx) Stmt(stmt *Stmt) *Stmt
- func (tx *Tx) StmtContext(ctx context.Context, 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" )
const ( UNKNOW_TYPE = iota TEXT_TYPE BLOB_TYPE TIME_TYPE NUMERIC_TYPE )
Variables ¶
var ( ErrCacheMiss = errors.New("xorm/cache: key not found.") ErrNotStored = errors.New("xorm/cache: not stored.") )
var ( ErrNoMapPointer = errors.New("mp should be a map's pointer") ErrNoStructPointer = errors.New("mp should be a struct's pointer") )
var ( Bit = "BIT" TinyInt = "TINYINT" SmallInt = "SMALLINT" MediumInt = "MEDIUMINT" Int = "INT" Integer = "INTEGER" BigInt = "BIGINT" Enum = "ENUM" Set = "SET" Char = "CHAR" Varchar = "VARCHAR" NChar = "NCHAR" NVarchar = "NVARCHAR" TinyText = "TINYTEXT" Text = "TEXT" NText = "NTEXT" Clob = "CLOB" MediumText = "MEDIUMTEXT" LongText = "LONGTEXT" Uuid = "UUID" UniqueIdentifier = "UNIQUEIDENTIFIER" SysName = "SYSNAME" Date = "DATE" DateTime = "DATETIME" SmallDateTime = "SMALLDATETIME" Time = "TIME" TimeStamp = "TIMESTAMP" TimeStampz = "TIMESTAMPZ" Decimal = "DECIMAL" Numeric = "NUMERIC" Money = "MONEY" SmallMoney = "SMALLMONEY" Real = "REAL" Float = "FLOAT" Double = "DOUBLE" Binary = "BINARY" VarBinary = "VARBINARY" TinyBlob = "TINYBLOB" Blob = "BLOB" MediumBlob = "MEDIUMBLOB" LongBlob = "LONGBLOB" Bytea = "BYTEA" Bool = "BOOL" Boolean = "BOOLEAN" Serial = "SERIAL" BigSerial = "BIGSERIAL" Json = "JSON" Jsonb = "JSONB" SqlTypes = map[string]int{ Bit: NUMERIC_TYPE, TinyInt: NUMERIC_TYPE, SmallInt: NUMERIC_TYPE, MediumInt: NUMERIC_TYPE, Int: NUMERIC_TYPE, Integer: NUMERIC_TYPE, BigInt: NUMERIC_TYPE, Enum: TEXT_TYPE, Set: TEXT_TYPE, Json: TEXT_TYPE, Jsonb: TEXT_TYPE, Char: TEXT_TYPE, NChar: TEXT_TYPE, Varchar: TEXT_TYPE, NVarchar: TEXT_TYPE, TinyText: TEXT_TYPE, Text: TEXT_TYPE, NText: TEXT_TYPE, MediumText: TEXT_TYPE, LongText: TEXT_TYPE, Uuid: TEXT_TYPE, Clob: TEXT_TYPE, SysName: TEXT_TYPE, Date: TIME_TYPE, DateTime: TIME_TYPE, Time: TIME_TYPE, TimeStamp: TIME_TYPE, TimeStampz: TIME_TYPE, SmallDateTime: TIME_TYPE, Decimal: NUMERIC_TYPE, Numeric: NUMERIC_TYPE, Real: NUMERIC_TYPE, Float: NUMERIC_TYPE, Double: NUMERIC_TYPE, Money: NUMERIC_TYPE, SmallMoney: NUMERIC_TYPE, Binary: BLOB_TYPE, VarBinary: BLOB_TYPE, TinyBlob: BLOB_TYPE, Blob: BLOB_TYPE, MediumBlob: BLOB_TYPE, LongBlob: BLOB_TYPE, Bytea: BLOB_TYPE, UniqueIdentifier: BLOB_TYPE, Bool: NUMERIC_TYPE, Serial: NUMERIC_TYPE, BigSerial: NUMERIC_TYPE, } )
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) BytesType = reflect.SliceOf(ByteType) 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) )
var (
DefaultCacheSize = 200
)
var LintGonicMapper = GonicMapper{ "API": true, "ASCII": true, "CPU": true, "CSS": true, "DNS": true, "EOF": true, "GUID": true, "HTML": true, "HTTP": true, "HTTPS": true, "ID": true, "IP": true, "JSON": true, "LHS": true, "QPS": true, "RAM": true, "RHS": true, "RPC": true, "SLA": true, "SMTP": true, "SSH": true, "TLS": true, "TTL": true, "UI": true, "UID": true, "UUID": true, "URI": true, "URL": true, "UTF8": true, "VM": true, "XML": true, "XSRF": true, "XSS": true, }
A GonicMapper that contains a list of common initialisms taken from golang/lint
Functions ¶
func MapToSlice ¶
func PutCacheSql ¶
func RegisterDialect ¶
RegisterDialect register database dialect
func RegisterDriver ¶
func RegisteredDriverSize ¶
func RegisteredDriverSize() int
func StructToSlice ¶
Types ¶
type Base ¶
type Base struct { *Uri // contains filtered or unexported fields }
func (*Base) CreateTableSql ¶
func (*Base) DataSourceName ¶
func (*Base) DriverName ¶
func (*Base) DropTableSql ¶
func (*Base) ForUpdateSql ¶
func (*Base) FormatBytes ¶
func (*Base) HasRecords ¶
func (*Base) ModifyColumnSql ¶
func (*Base) RollBackStr ¶
func (*Base) ShowCreateNull ¶
func (*Base) SupportDropIfExists ¶
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 // 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 TableName string FieldName string SQLType SQLType IsJSON bool Length int Length2 int Nullable bool Default string Indexes map[string]int IsPrimaryKey bool IsAutoIncrement bool MapType int IsCreated bool IsUpdated bool IsDeleted bool IsCascade bool IsVersion bool DefaultIsEmpty bool EnumOptions map[string]int SetOptions map[string]int DisableTimeZone bool TimeZone *time.Location // column specified time zone Comment string }
Column defines database column
func (*Column) StringNoPk ¶
StringNoPk generate column description string according dialect without primary keys
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 ¶
DB is a wrap of sql.DB with extra contents
func OpenDialect ¶
func (*DB) ExecMapContext ¶ added in v0.6.1
insert into (name) values (?) insert into (name) values (?name)
func (*DB) ExecStructContext ¶ added in v0.6.1
func (*DB) PrepareContext ¶ added in v0.6.1
func (*DB) QueryContext ¶ added in v0.6.1
QueryContext overwrites sql.DB.QueryContext
func (*DB) QueryMapContext ¶ added in v0.6.1
func (*DB) QueryRowContext ¶ added in v0.6.1
func (*DB) QueryRowMap ¶
func (*DB) QueryRowMapContext ¶ added in v0.6.1
func (*DB) QueryRowStruct ¶
func (*DB) QueryRowStructContext ¶ added in v0.6.1
type Dialect ¶
type Dialect interface { SetLogger(logger ILogger) Init(*DB, *Uri, string, string) error URI() *Uri DB() *DB DBType() DbType SqlType(*Column) string FormatBytes(b []byte) string DriverName() string DataSourceName() string QuoteStr() string IsReserved(string) bool Quote(string) string AndStr() string OrStr() string EqStr() string RollBackStr() string AutoIncrStr() string SupportInsertMany() bool SupportEngine() bool SupportCharset() bool SupportDropIfExists() bool IndexOnTable() bool ShowCreateNull() bool IndexCheckSql(tableName, idxName string) (string, []interface{}) TableCheckSql(tableName string) (string, []interface{}) IsColumnExist(tableName string, colName string) (bool, error) CreateTableSql(table *Table, tableName, storeEngine, charset string) string DropTableSql(tableName string) string CreateIndexSql(tableName string, index *Index) string DropIndexSql(tableName string, index *Index) string ModifyColumnSql(tableName string, col *Column) string ForUpdateSql(query string) string GetColumns(tableName string) ([]string, map[string]*Column, error) GetTables() ([]*Table, error) GetIndexes(tableName string) (map[string]*Index, error) Filters() []Filter SetParams(params map[string]string) }
a dialect is a driver's wrapper
func QueryDialect ¶
QueryDialect query if registed database dialect
type Driver ¶
func QueryDriver ¶
type EmptyScanner ¶
type EmptyScanner struct { }
func (EmptyScanner) Scan ¶
func (EmptyScanner) Scan(src interface{}) error
type GonicMapper ¶
GonicMapper implements IMapper. It will consider initialisms when mapping names. E.g. id -> ID, user -> User and to table names: UserID -> user_id, MyUID -> my_uid
func (GonicMapper) Obj2Table ¶
func (mapper GonicMapper) Obj2Table(name string) string
func (GonicMapper) Table2Obj ¶
func (mapper GonicMapper) Table2Obj(name string) string
type ILogger ¶
type ILogger interface { Debug(v ...interface{}) Debugf(format string, v ...interface{}) Error(v ...interface{}) Errorf(format string, v ...interface{}) Info(v ...interface{}) Infof(format string, v ...interface{}) Warn(v ...interface{}) Warnf(format string, v ...interface{}) Level() LogLevel SetLevel(l LogLevel) ShowSQL(show ...bool) IsShowSQL() bool }
logger interface
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 Row ¶
type Row struct {
// contains filtered or unexported fields
}
func (*Row) ScanSlice ¶ added in v0.4.5
scan data to a slice's pointer, slice's length should equal to columns' number
func (*Row) ScanStructByIndex ¶ added in v0.4.5
func (*Row) ScanStructByName ¶ added in v0.4.5
type Rows ¶
func (*Rows) ScanSlice ¶
scan data to a slice's pointer, slice's length should equal to columns' number
func (*Rows) ScanStructByIndex ¶
scan data to a struct's pointer according field index
func (*Rows) ScanStructByName ¶
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 Stmt ¶
func (*Stmt) ExecMapContext ¶ added in v0.6.1
func (*Stmt) ExecStructContext ¶ added in v0.6.1
func (*Stmt) QueryContext ¶ added in v0.6.1
func (*Stmt) QueryMapContext ¶ added in v0.6.1
func (*Stmt) QueryRowContext ¶ added in v0.6.1
func (*Stmt) QueryRowMap ¶
func (*Stmt) QueryRowMapContext ¶ added in v0.6.1
func (*Stmt) QueryRowStruct ¶
func (*Stmt) QueryRowStructContext ¶ added in v0.6.1
func (*Stmt) QueryStruct ¶
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 Deleted string Version string Cacher Cacher StoreEngine string Charset string Comment string // contains filtered or unexported fields }
database table
func NewEmptyTable ¶
func NewEmptyTable() *Table