Documentation
¶
Index ¶
- Constants
- Variables
- func NewContext(ctx context.Context, e *Engine) context.Context
- 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
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(d2 interface{}) Decimal
- func (d Decimal) Amount2Btc() Decimal
- func (d Decimal) Amount2Coin(prec int) Decimal
- func (d Decimal) Amount2Ether() Decimal
- func (d Decimal) Amount2FIL() Decimal
- func (d Decimal) BigInt() (b *big.Int, ok bool)
- func (d Decimal) Btc2Amount() Decimal
- func (d Decimal) Cmp(d2 interface{}) int
- func (d Decimal) Coin2Amount(prec int) Decimal
- func (d Decimal) Cos() Decimal
- func (d Decimal) Div(d2 interface{}) Decimal
- func (d Decimal) Equal(d2 interface{}) bool
- func (d Decimal) Ether2Amount() Decimal
- func (d Decimal) FIL2Amount() Decimal
- func (d Decimal) Float64() (f float64)
- func (d *Decimal) FromFloat(v float64)
- func (d *Decimal) FromInt(v int64)
- func (d *Decimal) FromString(v string)
- func (d Decimal) GetDecimal() decimal.Decimal
- func (d Decimal) GreaterThan(d2 interface{}) bool
- func (d Decimal) GreaterThanOrEqual(d2 interface{}) bool
- func (d Decimal) IntPart() int64
- func (d Decimal) IsNegative() bool
- func (d Decimal) IsPositive() bool
- func (d Decimal) IsZero() bool
- func (d Decimal) LessThan(d2 interface{}) bool
- func (d Decimal) LessThanOrEqual(d2 interface{}) bool
- func (d Decimal) Marshal() ([]byte, error)
- func (d Decimal) MarshalBSON() ([]byte, error)
- func (d Decimal) MarshalBinary() (data []byte, err error)
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() (text []byte, err error)
- func (d Decimal) Max(rest ...Decimal) Decimal
- func (d Decimal) Min(rest ...Decimal) Decimal
- func (d Decimal) Mod(d2 interface{}) Decimal
- func (d Decimal) Mul(d2 interface{}) Decimal
- func (d Decimal) Neg() Decimal
- func (d Decimal) Pow(d2 interface{}) Decimal
- func (d Decimal) Round(places int32) Decimal
- func (d *Decimal) Scan(src interface{}) error
- func (d Decimal) Sign() int
- func (d Decimal) Sin() Decimal
- func (d Decimal) String() string
- func (d Decimal) StringFixed(places int32) string
- func (d Decimal) StringScaled(exp int32) string
- func (d Decimal) Sub(d2 interface{}) Decimal
- func (d Decimal) Sum(rest ...Decimal) Decimal
- func (d Decimal) Tan() Decimal
- func (d Decimal) Truncate(precision int32) Decimal
- func (d *Decimal) Unmarshal(data []byte) error
- func (d *Decimal) UnmarshalBSON(data []byte) error
- func (d *Decimal) UnmarshalBinary(data []byte) error
- func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error
- func (d *Decimal) UnmarshalText(text []byte) error
- func (d Decimal) Value() (driver.Value, error)
- 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(query string, args ...any) (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) In(strColumn string, args ...any) *Engine
- func (e *Engine) InnerJoin(strTableName string) *Join
- func (e *Engine) Insert() (lastInsertId 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) 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) 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 Options
- 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 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 ¶
type Decimal struct {
// contains filtered or unexported fields
}
func NewDecimal ¶
func NewDecimal(v interface{}) (d Decimal)
func (Decimal) Amount2Btc ¶
func (Decimal) Amount2Coin ¶
func (Decimal) Amount2Ether ¶
func (Decimal) Amount2FIL ¶
func (Decimal) Btc2Amount ¶
func (Decimal) Cmp ¶
Cmp compares the numbers represented by d and d2 and returns:
-1 if d < d2 0 if d == d2 +1 if d > d2
func (Decimal) Coin2Amount ¶
func (Decimal) Div ¶
Div returns d / d2. If it doesn't divide exactly, the result will have DivisionPrecision digits after the decimal point.
func (Decimal) Ether2Amount ¶
func (Decimal) FIL2Amount ¶
func (Decimal) Float64 ¶
Float64 returns the nearest float64 value for d and a bool indicating whether f represents d exactly.
func (*Decimal) FromString ¶
func (Decimal) GetDecimal ¶
GetDecimal returns the decimal.Decimal type
func (Decimal) GreaterThan ¶
GreaterThan (GT) returns true when d is greater than d2.
func (Decimal) GreaterThanOrEqual ¶
GreaterThanOrEqual (GTE) returns true when d is greater than or equal to d2.
func (Decimal) LessThanOrEqual ¶
LessThanOrEqual (LTE) returns true when d is less than or equal to d2.
func (Decimal) MarshalBSON ¶
MarshalBSON implements the bson.Marshaler interface.
func (Decimal) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (Decimal) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Decimal) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface for XML serialization.
func (Decimal) Max ¶
Max returns the largest Decimal that was passed in the arguments. To call this function with an array, you must do: This makes it harder to accidentally call Max with 0 arguments.
func (Decimal) Min ¶
Min returns the smallest Decimal that was passed in the arguments. To call this function with an array, you must do: This makes it harder to accidentally call Min with 0 arguments.
func (Decimal) Round ¶
Round rounds the decimal to places decimal places. If places < 0, it will round the integer part to the nearest 10^(-places).
Example:
NewFromFloat(5.45).Round(1).String() // output: "5.5" NewFromFloat(545).Round(-1).String() // output: "550"
func (Decimal) String ¶
String returns the string representation of the decimal with the fixed point.
Example:
d := New(-12345, -3) println(d.String())
Output:
-12.345
func (Decimal) StringFixed ¶
StringFixed returns a rounded fixed-point string with places digits after the decimal point.
Example:
NewFromFloat(0).StringFixed(2) // output: "0.00" NewFromFloat(0).StringFixed(0) // output: "0" NewFromFloat(5.45).StringFixed(0) // output: "5" NewFromFloat(5.45).StringFixed(1) // output: "5.5" NewFromFloat(5.45).StringFixed(2) // output: "5.45" NewFromFloat(5.45).StringFixed(3) // output: "5.450" NewFromFloat(545).StringFixed(-1) // output: "550"
func (Decimal) StringScaled ¶
StringScaled first scales the decimal then calls .String() on it. NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead.
func (Decimal) Truncate ¶
Truncate truncates off digits from the number, without rounding.
NOTE: precision is the last digit that will not be truncated (must be >= 0).
Example:
decimal.NewFromString("123.456").Truncate(2).String() // "123.45"
func (*Decimal) UnmarshalBSON ¶
func (*Decimal) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. As a string representation is already used when encoding to text, this method stores that string as []byte
func (*Decimal) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Decimal) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface for XML deserialization.
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 Options ¶
type Options struct {
Debug bool //enable debug mode
Max int //max active connections
Idle int //max idle connections
SSH *SSH //ssh tunnel server config
SnowFlake *SnowFlake //snowflake id config
DisableOffset bool //disable page offset for LIMIT (default page no is 1, if true then page no start from 0)
DefaultLimit int32 //limit default (0 means no limit)
}
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