core

package
v0.0.0-...-554892d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
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
)
View Source
const (
	TWOSIDES = iota + 1
	ONLYTODB
	ONLYFROMDB
)
View Source
const (
	IndexType = iota + 1
	UniqueType
)
View Source
const (
	POSTGRES = "postgres"
	SQLITE   = "sqlite3"
	MYSQL    = "mysql"
	MSSQL    = "mssql"
	ORACLE   = "oracle"
)

Variables

View Source
var (
	ErrNoMapPointer    = errors.New("mp should be a map's pointer")
	ErrNoStructPointer = errors.New("mp should be a map's pointer")
)
View Source
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,
	}
)
View Source
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)
)
View Source
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 GenSqlKey

func GenSqlKey(sql string, args interface{}) string

func MapToSlice

func MapToSlice(query string, mp interface{}) (string, []interface{}, error)

func PutCacheSql

func PutCacheSql(m Cacher, ids []PK, tableName, sql string, args interface{}) error

func QueryDriver

func QueryDriver(driverName string) driver

func ReflectNew

func ReflectNew(typ reflect.Type) reflect.Value

func RegisterDialect

func RegisterDialect(dbName dbType, dialect Dialect)

func RegisterDriver

func RegisterDriver(driverName string, driver driver)

func SQLType2Type

func SQLType2Type(st SQLType) reflect.Type

default sql type change to go types

func StructToSlice

func StructToSlice(query string, st interface{}) (string, []interface{}, error)

Types

type Base

type Base struct {
	*Uri
	// contains filtered or unexported fields
}

func (*Base) CreateTableSql

func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset string) string

func (*Base) DBType

func (b *Base) DBType() dbType

func (*Base) DataSourceName

func (b *Base) DataSourceName() string

func (*Base) DriverName

func (b *Base) DriverName() string

func (*Base) Init

func (b *Base) Init(dialect Dialect, uri *Uri, drivername, dataSourceName string) error

func (*Base) Quote

func (b *Base) Quote(c string) string

func (*Base) URI

func (b *Base) URI() *Uri

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 NewColumn

func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column

func (*Column) String

func (col *Column) String(d Dialect) string

generate column description string according dialect

func (*Column) StringNoPk

func (col *Column) StringNoPk(d Dialect) string

func (*Column) ValueOf

func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error)

return col's filed of struct's value

func (*Column) ValueOfV

func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error)

type Conversion

type Conversion interface {
	FromDB([]byte) error
	ToDB() ([]byte, error)
}

Conversion is an interface. A type implements Conversion will according the custom method to fill into database and retrieve from database.

type DB

type DB struct {
	*sql.DB
	Mapper IMapper
}

func Open

func Open(driverName, dataSourceName string) (*DB, error)

func (*DB) Begin

func (db *DB) Begin() (*Tx, error)

func (*DB) ExecMap

func (db *DB) ExecMap(query string, mp interface{}) (sql.Result, error)

insert into (name) values (?) insert into (name) values (?name)

func (*DB) ExecStruct

func (db *DB) ExecStruct(query string, st interface{}) (sql.Result, error)

func (*DB) Prepare

func (db *DB) Prepare(query string) (*Stmt, error)

func (*DB) Query

func (db *DB) Query(query string, args ...interface{}) (*Rows, error)

func (*DB) QueryMap

func (db *DB) QueryMap(query string, mp interface{}) (*Rows, error)

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...interface{}) *Row

func (*DB) QueryRowMap

func (db *DB) QueryRowMap(query string, mp interface{}) *Row

func (*DB) QueryRowStruct

func (db *DB) QueryRowStruct(query string, st interface{}) *Row

func (*DB) QueryStruct

func (db *DB) QueryStruct(query string, st interface{}) (*Rows, error)

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 Filter

type Filter interface {
	Do(sql string, dialect Dialect, table *Table) string
}

Filter is an interface to filter SQL

type IMapper

type IMapper interface {
	Obj2Table(string) string
	Table2Obj(string) string
}

name translation between struct, fields names and table, column names

type IdFilter

type IdFilter struct {
}

IdFilter filter SQL replace (id) to primary key column name

func (*IdFilter) Do

func (i *IdFilter) Do(sql string, dialect Dialect, table *Table) string

type Index

type Index struct {
	Name string
	Type int
	Cols []string
}

database index

func NewIndex

func NewIndex(name string, indexType int) *Index

new an index

func (*Index) AddColumn

func (index *Index) AddColumn(cols ...string)

add columns which will be composite index

type PK

type PK []interface{}

func GetCacheSql

func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]PK, error)

func NewPK

func NewPK(pks ...interface{}) *PK

func (*PK) FromString

func (p *PK) FromString(content string) error

func (*PK) ToString

func (p *PK) ToString() (string, error)

type PrefixMapper

type PrefixMapper struct {
	Mapper IMapper
	Prefix string
}

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

func (*QuoteFilter) Do

func (s *QuoteFilter) Do(sql string, dialect Dialect, table *Table) string

type Quoter

type Quoter struct {
	// contains filtered or unexported fields
}

func NewQuoter

func NewQuoter(dialect Dialect) *Quoter

func (*Quoter) Quote

func (q *Quoter) Quote(content string) string

type Row

type Row struct {
	*sql.Row

	Mapper IMapper
	// contains filtered or unexported fields
}

func (*Row) Scan

func (row *Row) Scan(dest ...interface{}) error

type Rows

type Rows struct {
	*sql.Rows
	Mapper IMapper
}

func (*Rows) ScanMap

func (rs *Rows) ScanMap(dest interface{}) error

scan data to a map's pointer

func (*Rows) ScanSlice

func (rs *Rows) ScanSlice(dest interface{}) error

scan data to a slice's pointer, slice's length should equal to columns' number

func (*Rows) ScanStruct

func (rs *Rows) ScanStruct(dest ...interface{}) error

scan data to a struct's pointer according field index

func (*Rows) ScanStruct2

func (rs *Rows) ScanStruct2(dest interface{}) error

scan data to a struct's pointer according field name

type SQLType

type SQLType struct {
	Name           string
	DefaultLength  int
	DefaultLength2 int
}

xorm SQL types

func Type2SQLType

func Type2SQLType(t reflect.Type) (st SQLType)

func (*SQLType) IsBlob

func (s *SQLType) IsBlob() bool

func (*SQLType) IsText

func (s *SQLType) IsText() bool

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

type Stmt struct {
	*sql.Stmt
	Mapper IMapper
	// contains filtered or unexported fields
}

func (*Stmt) ExecMap

func (s *Stmt) ExecMap(mp interface{}) (sql.Result, error)

func (*Stmt) ExecStruct

func (s *Stmt) ExecStruct(st interface{}) (sql.Result, error)

func (*Stmt) Query

func (s *Stmt) Query(args ...interface{}) (*Rows, error)

func (*Stmt) QueryMap

func (s *Stmt) QueryMap(mp interface{}) (*Rows, error)

func (*Stmt) QueryRow

func (s *Stmt) QueryRow(args ...interface{}) *Row

func (*Stmt) QueryRowMap

func (s *Stmt) QueryRowMap(mp interface{}) *Row

func (*Stmt) QueryRowStruct

func (s *Stmt) QueryRowStruct(st interface{}) *Row

func (*Stmt) QueryStruct

func (s *Stmt) QueryStruct(st interface{}) (*Rows, error)

type SuffixMapper

type SuffixMapper struct {
	Mapper IMapper
	Suffix string
}

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

func NewTable

func NewTable(name string, t reflect.Type) *Table

func (*Table) AddColumn

func (table *Table) AddColumn(col *Column)

add a column to table

func (*Table) AddIndex

func (table *Table) AddIndex(index *Index)

add an index or an unique to table

func (*Table) AutoIncrColumn

func (table *Table) AutoIncrColumn() *Column

func (*Table) Columns

func (table *Table) Columns() map[string]*Column

func (*Table) ColumnsSeq

func (table *Table) ColumnsSeq() []string

func (*Table) GetColumn

func (table *Table) GetColumn(name string) *Column

func (*Table) PKColumns

func (table *Table) PKColumns() []*Column

if has primary key, return column

func (*Table) VersionColumn

func (table *Table) VersionColumn() *Column

type Tx

type Tx struct {
	*sql.Tx
	Mapper IMapper
}

func (*Tx) ExecMap

func (tx *Tx) ExecMap(query string, mp interface{}) (sql.Result, error)

func (*Tx) ExecStruct

func (tx *Tx) ExecStruct(query string, st interface{}) (sql.Result, error)

func (*Tx) Prepare

func (tx *Tx) Prepare(query string) (*Stmt, error)

func (*Tx) Query

func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error)

func (*Tx) QueryMap

func (tx *Tx) QueryMap(query string, mp interface{}) (*Rows, error)

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...interface{}) *Row

func (*Tx) QueryRowMap

func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row

func (*Tx) QueryRowStruct

func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row

func (*Tx) QueryStruct

func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error)

func (*Tx) Stmt

func (tx *Tx) Stmt(stmt *Stmt) *Stmt

type Uri

type Uri struct {
	DbType  dbType
	Proto   string
	Host    string
	Port    string
	DbName  string
	User    string
	Passwd  string
	Charset string
	Laddr   string
	Raddr   string
	Timeout time.Duration
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL