sqlca

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2020 License: MIT Imports: 18 Imported by: 1

README

author

lory.li

email

civet148@126.com

QQ

93864947

sqlca

a enhancement database and cache tool based on sqlx and redigogo which based on redigo and go-redis-cluster

Documentation

Index

Constants

View Source
const (
	CACHE_INDEX_DEEP  = 1           // index deep in cache
	CACHE_REPLICATE   = "replicate" //replicate host [ip:port,...]
	CACHE_DB_INDEX    = "db"
	CAHCE_SQLX_PREFIX = "sqlx:cache"
)
View Source
const (
	ValueType_Data  valueType = 1 // data of table
	ValueType_Index valueType = 2 // index of data
)
View Source
const (
	TAG_NAME_DB          = "db"
	DRIVER_NAME_MYSQL    = "mysql"
	DRIVER_NAME_POSTGRES = "postgres"
	DRIVER_NAME_SQLITE   = "sqlite3"
	DRIVER_NAME_MSSQL    = "adodb"
	DRIVER_NAME_REDIS    = "redis"
)
View Source
const (
	ORDER_BY_ASC                 = "asc"
	ORDER_BY_DESC                = "desc"
	DEFAULT_CAHCE_EXPIRE_SECONDS = 60 * 60
	DEFAULT_PRIMARY_KEY_NAME     = "id"
	SQLX_IGNORE_CREATED_AT       = "created_at"
	SQLX_IGNORE_UPDATED_AT       = "updated_at"
)
View Source
const (
	ModelType_Struct   = 1
	ModelType_Slice    = 2
	ModelType_Map      = 3
	ModelType_BaseType = 4
)
View Source
const (
	URL_SCHEME_SEP = "://"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AdapterType

type AdapterType int
const (
	AdapterSqlx_MySQL    AdapterType = 1  //sqlx: mysql
	AdapterSqlx_Postgres AdapterType = 2  //sqlx: postgresql
	AdapterSqlx_Sqlite   AdapterType = 3  //sqlx: sqlite
	AdapterSqlx_Mssql    AdapterType = 4  //sqlx: mssql server
	AdapterCache_Redis   AdapterType = 11 //cache: redis
)

func (AdapterType) DriverName

func (a AdapterType) DriverName() string

func (AdapterType) GoString

func (a AdapterType) GoString() string

func (AdapterType) String

func (a AdapterType) String() string

type Engine

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

func NewEngine

func NewEngine(debug bool) *Engine

func (*Engine) Asc

func (e *Engine) Asc() *Engine

order by [field1,field2...] asc

func (*Engine) Debug

func (e *Engine) Debug(ok bool)

debug mode on or off if debug on, some method will panic if your condition illegal

func (*Engine) Desc

func (e *Engine) Desc() *Engine

order by [field1,field2...] desc

func (*Engine) ExecRaw

func (e *Engine) ExecRaw(strQuery string, args ...interface{}) (rowsAffected, lastInsertId int64, err error)

use raw sql to insert/update database, results can not be cached to redis/memcached/memory... return rows affected and error, if err is not nil must be something wrong

func (*Engine) GetPkName

func (e *Engine) GetPkName() string

func (*Engine) GroupBy

func (e *Engine) GroupBy(strColumns ...string) *Engine

group by [field1,field2...]

func (*Engine) Id

func (e *Engine) Id(value interface{}) *Engine

set orm primary key's value

func (*Engine) Index

func (e *Engine) Index(strColumn string, value interface{}) *Engine

index which select from cache or update to cache if your index is not a primary key, it will create a cache index and pointer to primary key data index or data in cache key is 'sqlx:cache:[table name]:[column name]:[column value]', eg. 'sqlx:cache:users:phone:8613055556666'

func (*Engine) Insert

func (e *Engine) Insert() (lastInsertId int64, err error)

orm insert return last insert id and error, if err is not nil must be something wrong NOTE: Model function is must be called before call this function

func (*Engine) Limit

func (e *Engine) Limit(args ...int) *Engine

query limit Limit(10) - query records limit 10 (mysql/postgres)

func (*Engine) Model

func (e *Engine) Model(args ...interface{}) *Engine

orm model use to get result set, support single struct object or slice [pointer type] notice: will clone a new engine object for orm operations(query/update/insert/upsert)

func (*Engine) Offset

func (e *Engine) Offset(offset int) *Engine

query offset (for mysql/postgres)

func (*Engine) OnConflict

func (e *Engine) OnConflict(strColumns ...string) *Engine

set the conflict columns for upsert only for postgresql

func (*Engine) Open

func (e *Engine) Open(adapterType AdapterType, strUrl string, expireSeconds ...int) *Engine

open a sqlx database or cache connection strUrl:

  1. data source name

    [mysql] Open(AdapterSqlx_MySQL, "mysql://root:123456@127.0.0.1:3306/mydb?charset=utf8mb4") [postgres] Open(AdapterSqlx_Postgres, "postgres://root:123456@127.0.0.1:5432/mydb?sslmode=disable") [sqlite] Open(AdapterSqlx_Sqlite, "sqlite:///var/lib/my.db") [mssql] Open(AdapterSqlx_Mssql, "mssql://sa:123456@127.0.0.1:1433/mydb?instance=&windows=false")

  2. cache config [redis-alone] Open(AdapterTypeCache_Redis, "redis://123456@127.0.0.1:6379/cluster?db=0") [redis-cluster] Open(AdapterTypeCache_Redis, "redis://123456@127.0.0.1:6379/cluster?db=0&replicate=127.0.0.1:6380,127.0.0.1:6381")

expireSeconds cache data expire seconds, just for AdapterTypeCache_XXX

func (*Engine) OrderBy

func (e *Engine) OrderBy(strColumns ...string) *Engine

order by [field1,field2...]

func (*Engine) Query

func (e *Engine) Query() (rowsAffected int64, err error)

orm query return rows affected and error, if err is not nil must be something wrong NOTE: Model function is must be called before call this function

func (*Engine) QueryMap

func (e *Engine) QueryMap(strQuery string, args ...interface{}) (rowsAffected int64, err error)

use raw sql to query results into a map slice (model type is []map[string]string) return results and error NOTE: Model function is must be called before call this function

func (*Engine) QueryRaw

func (e *Engine) QueryRaw(strQuery string, args ...interface{}) (rowsAffected int64, err error)

use raw sql to query results return rows affected and error, if err is not nil must be something wrong NOTE: Model function is must be called before call this function

func (*Engine) Select

func (e *Engine) Select(strColumns ...string) *Engine

orm select/update columns

func (*Engine) SetPkName

func (e *Engine) SetPkName(strName string) *Engine

set orm primary key's name, default named 'id'

func (*Engine) Table

func (e *Engine) Table(strName string) *Engine

set orm query table name when your struct type name is not a table name

func (*Engine) Update

func (e *Engine) Update() (rowsAffected int64, err error)

orm update from model strColumns... if set, columns will be updated, if none all columns in model will be updated except primary key return rows affected and error, if err is not nil must be something wrong NOTE: Model function is must be called before call this function

func (*Engine) Upsert

func (e *Engine) Upsert() (lastInsertId int64, err error)

orm insert or update if key(s) conflict return last insert id and error, if err is not nil must be something wrong, if your primary key is not a int/int64 type, maybe id return 0 NOTE: Model function is must be called before call this function and call OnConflict function when you are on postgresql

func (*Engine) UseCache

func (e *Engine) UseCache() *Engine

func (*Engine) Where

func (e *Engine) Where(strWhere string) *Engine

orm query return rows affected and error, if err is not nil must be something wrong Model function is must be called before call this function notice: use Where function, the records which be updated can not be refreshed to redis/memcached...

type Fetcher

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

type ModelReflector

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

func (*ModelReflector) ToMap

func (s *ModelReflector) ToMap(tagName string) map[string]interface{}

parse struct tag and value to map

type ModelType

type ModelType int

func (ModelType) GoString

func (m ModelType) GoString() string

func (ModelType) String

func (m ModelType) String() string

type OperType

type OperType int
const (
	OperType_Query    OperType = 1 // orm: query sql
	OperType_Update   OperType = 2 // orm: update sql
	OperType_Insert   OperType = 3 // orm: insert sql
	OperType_Upsert   OperType = 4 // orm: insert or update sql
	OperType_QueryRaw OperType = 5 // raw: query sql into model
	OperType_ExecRaw  OperType = 6 // raw: insert/update sql
	OperType_QueryMap OperType = 7 // raw: query sql into map
)

func (OperType) GoString

func (o OperType) GoString() string

func (OperType) String

func (o OperType) String() string

type UrlInfo

type UrlInfo struct {
	Scheme     string
	Host       string // host name and port like '127.0.0.1:3306'
	User       string
	Password   string
	Path       string
	Fragment   string
	Opaque     string
	ForceQuery bool
	Queries    map[string]string
}

func ParseUrl

func ParseUrl(strUrl string) (ui *UrlInfo)

URL have some special characters in password(支持URL中密码包含特殊字符)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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