Documentation
¶
Index ¶
- Constants
- Variables
- func ConvertPtr[T any](v T) *T
- func ConvertVal[T any](v *T) (ret T)
- func NewContext(ctx context.Context, e *Engine) context.Context
- func NewExpr(query string, args ...any) *types.Expr
- func NewSqlClause(fmts string, args ...any) *types.SqlClauseValue
- func Url2MySql(strUrl string) (string, error)
- type AfterCreateInterface
- type AfterDeleteInterface
- type AfterQueryInterface
- type AfterUpdateInterface
- type BeforeCreateInterface
- type BeforeDeleteInterface
- type BeforeQueryInterface
- type BeforeUpdateInterface
- type CaseWhen
- type Decimal
- type Engine
- func (e *Engine) And(query any, args ...any) *Engine
- func (e *Engine) Asc(columns ...string) *Engine
- func (e *Engine) Attach(strDatabaseName string, db *sqlx.DB) *Engine
- func (e *Engine) AutoRollback() *Engine
- func (e *Engine) Avg(strColumn string, as ...string) *Engine
- func (e *Engine) Case(strThen string, strWhen string, args ...any) *CaseWhen
- func (e *Engine) Close() *Engine
- func (e *Engine) Count(strColumn string, as ...string) *Engine
- func (e *Engine) CountRows() (count int64, err error)
- func (e *Engine) Counter() *counter
- func (e *Engine) Debug(ok bool)
- func (e *Engine) Delete() (rowsAffected int64, err error)
- func (e *Engine) Desc(columns ...string) *Engine
- func (e *Engine) Distinct() *Engine
- func (e *Engine) Eq(strColumn string, value any) *Engine
- func (e *Engine) Equal(strColumn string, value any) *Engine
- func (e *Engine) Exclude(columns ...string) *Engine
- func (e *Engine) ExecRaw(query string, args ...any) (rowsAffected, lastInsertId int64, err error)
- func (e *Engine) ForUpdate() *Engine
- func (e *Engine) Force() *Engine
- func (e *Engine) From(exprs ...string) *Engine
- func (e *Engine) GeoHash(lng, lat float64, precision int) (strGeoHash string, strNeighbors []string)
- func (e *Engine) GetAdapter() types.AdapterType
- func (e *Engine) GetPkName() string
- func (e *Engine) GreaterEqual(strColumn string, value any) *Engine
- func (e *Engine) GreaterThan(strColumn string, value any) *Engine
- func (e *Engine) GroupBy(columns ...string) *Engine
- func (e *Engine) Gt(strColumn string, value any) *Engine
- func (e *Engine) Gte(strColumn string, value any) *Engine
- func (e *Engine) GteLte(strColumn string, value1, value2 any) *Engine
- func (e *Engine) Having(strFmt string, args ...any) *Engine
- func (e *Engine) Id(value any) *Engine
- func (e *Engine) Ignore() *Engine
- func (e *Engine) In(strColumn string, args ...any) *Engine
- func (e *Engine) InnerJoin(strTableName string) *Join
- func (e *Engine) Insert() (lastInsertId, rowsAffected int64, err error)
- func (e *Engine) IsNULL(strColumn string) *Engine
- func (e *Engine) IsNull(strColumn string) *Engine
- func (e *Engine) JsonEqual(strColumn, strPath string, value any) *Engine
- func (e *Engine) JsonGreater(strColumn, strPath string, value any) *Engine
- func (e *Engine) JsonGreaterEqual(strColumn, strPath string, value any) *Engine
- func (e *Engine) JsonLess(strColumn, strPath string, value any) *Engine
- func (e *Engine) JsonLessEqual(strColumn, strPath string, value any) *Engine
- func (e *Engine) JsonMarshal(v any) (strJson string)
- func (e *Engine) JsonUnmarshal(strJson string, v any) (err error)
- func (e *Engine) LeftJoin(strTableName string) *Join
- func (e *Engine) LessEqual(strColumn string, value any) *Engine
- func (e *Engine) LessThan(strColumn string, value any) *Engine
- func (e *Engine) Like(strColumn, keyword string) *Engine
- func (e *Engine) Likes(kvs map[string]any) *Engine
- func (e *Engine) Limit(args ...int) *Engine
- func (e *Engine) Lock(key string, expiry time.Duration) (unlock func() error, err error)
- func (e *Engine) LockShareMode() *Engine
- func (e *Engine) Lt(strColumn string, value any) *Engine
- func (e *Engine) Lte(strColumn string, value any) *Engine
- func (e *Engine) Max(strColumn string, as ...string) *Engine
- func (e *Engine) Min(strColumn string, as ...string) *Engine
- func (e *Engine) Model(args ...interface{}) *Engine
- func (e *Engine) MustFind() (rowsAffected int64, err error)
- func (e *Engine) Ne(strColumn string, value any) *Engine
- func (e *Engine) NearBy(strLngCol, strLatCol, strAS string, lng, lat, distance float64) *Engine
- func (e *Engine) NewContext(ctx context.Context) context.Context
- func (e *Engine) NewID() ID
- func (e *Engine) NoVerbose() *Engine
- func (e *Engine) NotIn(strColumn string, args ...any) *Engine
- func (e *Engine) NotNULL(strColumn string) *Engine
- func (e *Engine) Offset(offset int) *Engine
- func (e *Engine) Omit(columns ...string) *Engine
- func (e *Engine) OnConflict(columns ...string) *Engine
- func (e *Engine) Or(query any, args ...any) *Engine
- func (e *Engine) OrderBy(orders ...string) *Engine
- func (e *Engine) Page(pageNo, pageSize int) *Engine
- func (e *Engine) Ping() (err error)
- func (e *Engine) Query() (rowsAffected int64, err error)
- func (e *Engine) QueryEx() (rowsAffected, total int64, err error)
- func (e *Engine) QueryJson() (s string, err error)
- func (e *Engine) QueryMap(query string, args ...any) (rowsAffected int64, err error)
- func (e *Engine) QueryRaw(query string, args ...any) (rowsAffected int64, err error)
- func (e *Engine) RightJoin(strTableName string) *Join
- func (e *Engine) Round(strColumn string, round int, as ...string) *Engine
- func (e *Engine) Select(columns ...string) *Engine
- func (e *Engine) SetCustomTag(tagNames ...string) *Engine
- func (e *Engine) SetLogFile(strPath string)
- func (e *Engine) SetPkName(strName string) *Engine
- func (e *Engine) SetReadOnly(columns ...string)
- func (e *Engine) SlowQuery(on bool, ms int)
- func (e *Engine) Sum(strColumn string, as ...string) *Engine
- func (e *Engine) Table(exprs ...string) *Engine
- func (e *Engine) ToSQL(operType types.OperType) (strSql string)
- func (e *Engine) TryLock(key string, expiry, timeout time.Duration) (unlock func() error, err error)
- func (e *Engine) TxBegin() (*Engine, error)
- func (e *Engine) TxCommit() error
- func (e *Engine) TxExec(query string, args ...any) (lastInsertId, rowsAffected int64, err error)
- func (e *Engine) TxFunc(fn func(tx *Engine) error) (err error)
- func (e *Engine) TxFuncContext(ctx context.Context, fn func(ctx context.Context, tx *Engine) error) (err error)
- func (e *Engine) TxGet(dest any, strSql string, args ...any) (count int64, err error)
- func (e *Engine) TxHandle(handler TxHandler) (err error)
- func (e *Engine) TxRollback() error
- func (e *Engine) Update() (rowsAffected int64, err error)
- func (e *Engine) Upsert(strCustomizeUpdates ...string) (lastInsertId int64, err error)
- func (e *Engine) Use(strDatabaseName string) (*Engine, error)
- func (e *Engine) Where(query any, args ...any) *Engine
- type Fetcher
- type ID
- type Id
- type Join
- type JoinType
- type ModelReflector
- type Option
- type Point
- type RedisConfig
- type SSH
- type SnowFlake
- type StringBuilder
- type TxHandler
- type UrlInfo
Constants ¶
const ( DefaultConnMax = 150 DefaultConnIdle = 5 )
const ( JoinType_Inner = 0 //inner join JoinType_Left = 1 //left join JoinType_Right = 2 //right join )
Variables ¶
var (
ErrRecordNotFound = errors.New("record not found")
)
Functions ¶
func ConvertVal ¶ added in v3.5.3
func ConvertVal[T any](v *T) (ret T)
ConvertVal 返回指针指向的值,如果指针为nil则返回类型的零值
func NewSqlClause ¶
func NewSqlClause(fmts string, args ...any) *types.SqlClauseValue
Types ¶
type AfterCreateInterface ¶
type AfterDeleteInterface ¶
type AfterQueryInterface ¶
type AfterUpdateInterface ¶
type BeforeCreateInterface ¶
type BeforeDeleteInterface ¶
type BeforeQueryInterface ¶
type BeforeUpdateInterface ¶
type CaseWhen ¶
type CaseWhen struct {
// contains filtered or unexported fields
}
type Decimal ¶
func NewDecimal ¶
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
func FromContext ¶ added in v3.1.0
func NewEngine ¶
// [mysql] "mysql://root:123456@127.0.0.1:3306/test?charset=utf8mb4" // [postgres] "postgres://root:123456@127.0.0.1:5432/test?sslmode=disable&search_path=public" // [opengauss] "opengauss://root:123456@127.0.0.1:5432/test?sslmode=disable&search_path=public" // [mssql] "mssql://sa:123456@127.0.0.1:1433/mydb?instance=SQLExpress&windows=false" // [sqlite] "sqlite:///var/lib/test.db"
func (*Engine) AutoRollback ¶
func (*Engine) ExecRaw ¶
ExecRaw 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) GeoHash ¶
func (e *Engine) GeoHash(lng, lat float64, precision int) (strGeoHash string, strNeighbors []string)
GeoHash encode geo hash string (precision 1~8)
returns geo hash and neighbors areas
func (*Engine) GetAdapter ¶
func (e *Engine) GetAdapter() types.AdapterType
func (*Engine) Insert ¶
Insert 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) JsonGreater ¶
func (*Engine) JsonGreaterEqual ¶
func (*Engine) JsonLessEqual ¶
func (*Engine) JsonMarshal ¶
func (*Engine) LockShareMode ¶
func (*Engine) Model ¶
Model 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) NearBy ¶
NearBy -- select geo point as distance where distance <= n km (float64) SELECT
a.*, ( 6371 * ACOS ( COS( RADIANS( a.lat ) ) * COS( RADIANS( 28.8039097230 ) ) * COS( RADIANS( 121.5619236231 ) - RADIANS( a.lng ) ) + SIN( RADIANS( a.lat ) ) * SIN( RADIANS( 28.8039097230 ) ) ) ) AS distance
FROM
t_address a
HAVING distance <= 200 -- less than or equal 200km ORDER BY
distance LIMIT 10
func (*Engine) NewContext ¶ added in v3.1.0
func (*Engine) OnConflict ¶
OnConflict set the conflict columns for upsert only for postgresql
func (*Engine) Page ¶
Page page query
SELECT ... FROM ... WHERE ... LIMIT (pageNo*pageSize), pageSize
func (*Engine) Query ¶
Query 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 if slave == true, try query from a slave connection, if not exist query from master
func (*Engine) QueryEx ¶
QueryEx orm query with total count 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 if slave == true, try query from a slave connection, if not exist query from master
func (*Engine) QueryMap ¶
QueryMap 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 ¶
QueryRaw 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) SetCustomTag ¶
SetCustomTag set your customer tag for db query/insert/update (eg. go structure generated by protobuf not contain 'db' tag) this function must calls before Model()
func (*Engine) SetReadOnly ¶
SetReadOnly set read only columns
func (*Engine) SlowQuery ¶
SlowQuery slow query alert on or off
on -> true/false ms -> milliseconds (can be 0 if on is false)
func (*Engine) Table ¶
Table set orm query table name(s) expression when your struct type name is not a table name
func (*Engine) TxFunc ¶
TxFunc execute transaction by customize function
auto rollback when function return error
func (*Engine) TxFuncContext ¶
func (e *Engine) TxFuncContext(ctx context.Context, fn func(ctx context.Context, tx *Engine) error) (err error)
TxFuncContext execute transaction by customize function with context
auto rollback when function return error
func (*Engine) TxHandle ¶
TxHandle execute transaction by customize handler auto rollback when handler return error
func (*Engine) TxRollback ¶
func (*Engine) Update ¶
Update orm update from model columns... 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 ¶
Upsert 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 updates -> customize updates condition when key(s) conflict [MySQL] INSERT INTO messages(id, message_type, unread_count) VALUES('10000', '2', '1', '3') ON DUPLICATE KEY UPDATE message_type=values(message_type), unread_count=unread_count+values(unread_count) --------------------------------------------------------------------------------------------------------------------------------------- e.Model(&do).Table("messages").Upsert("message_type=values(message_type)", "unread_count=unread_count+values(unread_count)") ---------------------------------------------------------------------------------------------------------------------------------------
type ModelReflector ¶
type ModelReflector struct {
Dict map[string]interface{} //dictionary of structure tag and value
Columns []string //column names
// contains filtered or unexported fields
}
func (*ModelReflector) ParseModel ¶
func (s *ModelReflector) ParseModel(tagNames ...string) *ModelReflector
parse struct tag and value to map
type Option ¶ added in v3.4.0
type Option func(*dialOption)
func WithDefaultLimit ¶ added in v3.4.0
func WithDisableOffset ¶ added in v3.4.0
func WithDisableOffset() Option
func WithIdleConn ¶ added in v3.4.0
func WithMaxConn ¶ added in v3.4.0
func WithRedisConfig ¶ added in v3.7.0
func WithRedisConfig(rc *RedisConfig) Option
func WithSnowFlake ¶ added in v3.4.0
type Point ¶ added in v3.2.1
Point 表示数据库中的point类型
func (Point) MarshalJSON ¶ added in v3.2.1
MarshalJSON 实现json.Marshaler接口
func (*Point) Scan ¶ added in v3.2.1
Scan 实现sql.Scanner接口,用于解析MySQL的POINT类型数据(0x000000000101000000E5D022DBF98E5B40F6285C8FC27524C0 => POINT(110.234 -10.23))
func (*Point) UnmarshalJSON ¶ added in v3.2.1
UnmarshalJSON 实现json.Unmarshaler接口
type RedisConfig ¶ added in v3.7.0
type RedisConfig struct {
Address string // redis address, eg. "127.0.0.1:6379"
Password string // redis password, default empty
DB int // db index, default 0
MaxActive int // max active connections
MaxIdle int // max idle connections
ConnTimeout *time.Duration // connection timeout
// Close connections older than this duration. If the value is zero, then
// the pool does not close connections based on age.
MaxConnLifetime time.Duration
ClientName string
UseTLS bool
SkipVerify bool
TlsConfig *tls.Config
}
type SSH ¶
type SSH struct {
User string //SSH tunnel server login account
Password string //SSH tunnel server login password
PrivateKey string //SSH tunnel server private key, eg. "/home/test/.ssh/private-key.pem"
Host string //SSH tunnel server host [ip or domain], default port 22 if not specified
// contains filtered or unexported fields
}
type StringBuilder ¶
type StringBuilder struct {
// contains filtered or unexported fields
}
func NewStringBuilder ¶
func NewStringBuilder() *StringBuilder
func (*StringBuilder) Append ¶
func (s *StringBuilder) Append(query string, args ...any) *StringBuilder
func (*StringBuilder) Args ¶
func (s *StringBuilder) Args() []interface{}
func (*StringBuilder) String ¶
func (s *StringBuilder) String() string