sql

package
v3.7.26 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: Apache-2.0 Imports: 26 Imported by: 1

Documentation

Index

Constants

View Source
const (
	BETWEEN            = "BETWEEN"
	NOTBETWEEN         = "NOT BETWEEN"
	IN                 = "IN"
	NOTIN              = "NOT IN"
	AND                = "AND"
	OR                 = "OR"
	ISNULL             = "IS NULL"
	ISNOTNULL          = "IS NOT NULL"
	EQUAL              = "="
	NOTEQUAL           = "!="
	GREATER            = ">"
	GREATEREQUAL       = ">="
	LESS               = "<"
	LESSEQUAL          = "<="
	LIKE               = "LIKE"
	JOIN               = "JOIN"
	INNERJOIN          = "INNER JOIN"
	LEFTJOIN           = "LEFT JOIN"
	RIGHTJOIN          = "RIGHT JOIN"
	UNION              = "UNION"
	UNIONALL           = "UNION ALL"
	DESC               = "DESC"
	ASC                = "ASC"
	LeftBracket        = "("
	RightBracket       = ")"
	And                = "AND"
	JsonContainsOne    = "JsonContainsOne"
	JsonContainsMany   = "JsonContainsMany"
	JsonExtract        = "JsonExtract"
	JsonContainsObject = "JsonContainsObject"
)

BETWEEN ...

Variables

This section is empty.

Functions

func Caller

func Caller(skip int) map[string]string

func Format

func Format(query string, args ...any) (string, error)

Format

func ResultAccept added in v3.5.6

func ResultAccept(err error) error

Types

type Builder

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

Builder 查询构造器

func NewBuilder

func NewBuilder() *Builder

NewBuilder 创建查询构造器

func (*Builder) And added in v3.6.5

func (builder *Builder) And() *Builder

IsNULL .

func (*Builder) Between

func (builder *Builder) Between(column string, value1 any, value2 any) *Builder

Between 构造Between

func (*Builder) Count

func (builder *Builder) Count() (sql string, args []any, err error)

Count ...

func (*Builder) Delete

func (builder *Builder) Delete() (sql string, args []any, err error)

Delete .

func (*Builder) Distinct

func (builder *Builder) Distinct() *Builder

Distinct .

func (*Builder) Equal

func (builder *Builder) Equal(column string, value any) *Builder

Equal 构造等于

func (*Builder) FirstLike added in v3.6.5

func (builder *Builder) FirstLike(column string, value any) *Builder

Like .

func (*Builder) Greater

func (builder *Builder) Greater(column string, value any) *Builder

Greater 构造大于

func (*Builder) GreaterEqual

func (builder *Builder) GreaterEqual(column string, value any) *Builder

Greater 构造大于等于

func (*Builder) GroupBy

func (builder *Builder) GroupBy(groups ...string) *Builder

GroupBy .

func (*Builder) In

func (builder *Builder) In(column string, value ...any) *Builder

In 构造 in语句

func (*Builder) InnerJoin

func (builder *Builder) InnerJoin(tablename string, on string) *Builder

InnerJoin ...

func (*Builder) Insert

func (builder *Builder) Insert(data any) (sql string, args []any, err error)

Insert 插入数据

func (*Builder) InsertUpdate

func (builder *Builder) InsertUpdate(insert any, update any) (sql string, args []any, err error)

InsertUpdate ...

func (*Builder) IsNULL

func (builder *Builder) IsNULL(column string) *Builder

IsNULL .

func (*Builder) IsNotNULL

func (builder *Builder) IsNotNULL(column string) *Builder

IsNotNULL .

func (*Builder) IsZero

func (builder *Builder) IsZero(v reflect.Value) bool

IsZero ...

func (*Builder) Join

func (builder *Builder) Join(tablename string, on string) *Builder

Join .

func (*Builder) JsonContainsMany added in v3.6.6

func (builder *Builder) JsonContainsMany(column string, value ...any) *Builder

JsonContainsMany .

func (*Builder) JsonContainsObject added in v3.6.7

func (builder *Builder) JsonContainsObject(column string, value ...any) *Builder

func (*Builder) JsonContainsOne added in v3.6.6

func (builder *Builder) JsonContainsOne(column string, value any) *Builder

JsonContainsOne .

func (*Builder) JsonExtract added in v3.6.7

func (builder *Builder) JsonExtract(column string, value ...any) *Builder

func (*Builder) LeftBracket added in v3.6.5

func (builder *Builder) LeftBracket() *Builder

func (*Builder) LeftJoin

func (builder *Builder) LeftJoin(tablename string, on string) *Builder

LeftJoin .

func (*Builder) Less

func (builder *Builder) Less(column string, value any) *Builder

Greater 构造小于

func (*Builder) LessEqual

func (builder *Builder) LessEqual(column string, value any) *Builder

Greater 构造小于等于

func (*Builder) Like

func (builder *Builder) Like(column string, value any) *Builder

Like .

func (*Builder) Limit

func (builder *Builder) Limit(limit int64) *Builder

Limit .

func (*Builder) MultiInsert

func (builder *Builder) MultiInsert(data ...any) (sql string, args []any, err error)

MultiInsert 批量插入

func (*Builder) NotBetween

func (builder *Builder) NotBetween(column string, value1 any, value2 any) *Builder

NotBetween 构造不Not Between

func (*Builder) NotEqual

func (builder *Builder) NotEqual(column string, value any) *Builder

NotEqual 构造不等于

func (*Builder) NotIn

func (builder *Builder) NotIn(column string, value ...any) *Builder

NotIn .

func (*Builder) NotOrBetween

func (builder *Builder) NotOrBetween(column string, value1 any, value2 any) *Builder

NotOrBetween 构造 Not Between OR Not Between

func (*Builder) Offset

func (builder *Builder) Offset(offset int64) *Builder

Offset .

func (*Builder) OrBetween

func (builder *Builder) OrBetween(column string, value1 any, value2 any) *Builder

OrBetween 构造 或者 Between

func (*Builder) OrEqual

func (builder *Builder) OrEqual(column string, value any) *Builder

OrEqual 构造或者等于

func (*Builder) OrIn

func (builder *Builder) OrIn(column string, value ...any) *Builder

OrIn orin语句

func (*Builder) OrIsNULL

func (builder *Builder) OrIsNULL(column string) *Builder

OrIsNULL .

func (*Builder) OrIsNotNULL

func (builder *Builder) OrIsNotNULL(column string) *Builder

OrIsNotNULL .

func (*Builder) OrLike

func (builder *Builder) OrLike(column string, value any) *Builder

OrLike .

func (*Builder) OrNotEqual

func (builder *Builder) OrNotEqual(column string, value any) *Builder

OrNotEqual 构造或者不等于

func (*Builder) OrNotIn

func (builder *Builder) OrNotIn(column string, value ...any) *Builder

OrNotIn .

func (*Builder) OrWhere

func (builder *Builder) OrWhere(column string, value ...any) *Builder

OrWhere 构造OR条件

func (*Builder) OrderBy

func (builder *Builder) OrderBy(column string, direction string) *Builder

OrderBy .

func (*Builder) Replace

func (builder *Builder) Replace(data ...any) (sql string, args []any, err error)

Replace 替换

func (*Builder) RightBracket added in v3.6.5

func (builder *Builder) RightBracket() *Builder

func (*Builder) RightJoin

func (builder *Builder) RightJoin(tablename string, on string) *Builder

RightJoin .

func (*Builder) Row

func (builder *Builder) Row() (sql string, args []any, err error)

Row

func (*Builder) Rows

func (builder *Builder) Rows() (sql string, args []any, err error)

Rows

func (*Builder) Select

func (builder *Builder) Select(columns ...string) *Builder

Select 查询字段

func (*Builder) Skip

func (builder *Builder) Skip(offset int64) *Builder

Skip .

func (*Builder) Table

func (builder *Builder) Table(tableName ...string) *Builder

Table 设置操作的表名称

func (*Builder) ToSQL

func (builder *Builder) ToSQL(method string) string

ToSQL 输出SQL语句

func (*Builder) Union

func (builder *Builder) Union(unions ...Builder) *Builder

Union .

func (*Builder) UnionAll

func (builder *Builder) UnionAll(unions ...Builder) *Builder

UnionAll .

func (*Builder) UnionLimit

func (builder *Builder) UnionLimit(limit int64) *Builder

UnionLimit .

func (*Builder) UnionOffset

func (builder *Builder) UnionOffset(offset int64) *Builder

UnionOffset .

func (*Builder) UnionOrderBy

func (builder *Builder) UnionOrderBy(column string, direction string) *Builder

UnionOrderBy .

func (*Builder) Update

func (builder *Builder) Update(data any) (sql string, args []any, err error)

Update 更新

func (*Builder) Where

func (builder *Builder) Where(column string, value ...any) *Builder

Where 构造条件语句

type Client

type Client struct {
	Username         string        // 账号 root
	Password         string        // 密码
	Host             string        // host localhost   mysql
	Port             string        // 端口 3306
	Charset          string        // 字符编码 utf8mb4
	Database         string        // 默认连接数据库
	TimeZone         string        // 数据库时区
	MaxOpenConns     int           // 连接池最多同时打开的连接数
	MaxIdleConns     int           // 连接池里最大空闲连接数。必须要比maxOpenConns小
	MaxLifetime      time.Duration // 连接池里面的连接最大存活时长
	MaxIdleTime      time.Duration // 连接池里面的连接最大空闲时长
	DriverName       string        // 驱动名称
	DisableMetric    bool          // 关闭指标采集
	DisableTrace     bool          // 关闭链路追踪
	DisablePrepare   bool          // 关闭预处理
	Addr             []string      // ip:port  clickhouse
	DialTimeout      string        // 200ms    clickhouse
	OpenStrategy     string        // random/in_order (default random)  clickhouse
	Compress         bool          // enable lz4 compression   clickhouse
	MaxExecutionTime string        // 执行超时时间 clickhouse
	DisableDebug     bool          // 关闭 debug模式  clickhouse
	ParseTime        bool          // 是否解析时间
}

func NewClient

func NewClient(opts ...Option) (*Client, error)

func (*Client) NewClickhouse

func (c *Client) NewClickhouse(opts ...SqlOption) SqlConn

NewClickhouse returns a clickhouse connection.

func (*Client) NewMysql

func (c *Client) NewMysql(opts ...SqlOption) SqlConn

NewMysql returns a mysql connection.

func (*Client) NewPostgres

func (c *Client) NewPostgres(opts ...SqlOption) SqlConn

NewPostgres returns a postgres connection.

func (*Client) NewSqlClient

func (c *Client) NewSqlClient(opts ...SqlOption) SqlConn

NewSqlClient 代理

type Epr

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

Epr ...

func NewEpr

func NewEpr(value string) Epr

NewEpr ...

func (Epr) ToString

func (e Epr) ToString() string

ToString ...

type Grammar

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

Grammar sql 语法

func (Grammar) Delete

func (g Grammar) Delete() string

Delete ...

func (Grammar) Insert

func (g Grammar) Insert() string

Insert ...

func (Grammar) InsertUpdate

func (g Grammar) InsertUpdate() string

InsertUpdate ...

func (Grammar) Replace

func (g Grammar) Replace() string

Replace ...

func (Grammar) Select

func (g Grammar) Select() string

Select 构造select

func (Grammar) ToSQL

func (g Grammar) ToSQL() string

ToSQL ...

func (Grammar) Update

func (g Grammar) Update() string

Update ...

type Option

type Option func(r *Client)

func WithAddr

func WithAddr(addr []string) Option

WithAddr 设置ip:port

func WithCharset

func WithCharset(charset string) Option

WithCharset 设置字符编码

func WithCompress

func WithCompress(compress bool) Option

WithCompress 设置enable lz4 compression

func WithDatabase

func WithDatabase(database string) Option

WithDatabase 设置默认连接数据库

func WithDialTimeout

func WithDialTimeout(dialTimeout string) Option

WithDialTimeout 设置200ms

func WithDisableDebug

func WithDisableDebug(disableDebug bool) Option

WithDisableDebug 关闭 debug模式

func WithDisableMetric

func WithDisableMetric(disableMetric bool) Option

WithDisableMetric 关闭指标采集

func WithDisablePrepare

func WithDisablePrepare(disablePrepare bool) Option

func WithDisableTrace

func WithDisableTrace(disableTrace bool) Option

WithDisableTrace 关闭链路追踪

func WithDriverName

func WithDriverName(driverName string) Option

WithDriverName 设置驱动名称

func WithHost

func WithHost(host string) Option

WithHost 设置host

func WithMaxExecutionTime

func WithMaxExecutionTime(maxExecutionTime string) Option

WithMaxExecutionTime 设置执行超时时间

func WithMaxIdleConns

func WithMaxIdleConns(maxIdleConns int) Option

WithMaxIdleConns 设置连接池里最大空闲连接数。必须要比maxOpenConns小

func WithMaxIdleTime

func WithMaxIdleTime(maxIdleTime time.Duration) Option

WithMaxIdleTime 设置连接池里面的连接最大空闲时长

func WithMaxLifetime

func WithMaxLifetime(maxLifetime time.Duration) Option

WithMaxLifetime 设置连接池里面的连接最大存活时长

func WithMaxOpenConns

func WithMaxOpenConns(maxOpenConns int) Option

WithMaxOpenConns 设置连接池最多同时打开的连接数

func WithOpenStrategy

func WithOpenStrategy(openStrategy string) Option

WithOpenStrategy 设置random/in_order (default random)

func WithParseTime

func WithParseTime(parseTime bool) Option

func WithPassword

func WithPassword(password string) Option

WithPassword 设置密码

func WithPort

func WithPort(port string) Option

WithPort 设置端口

func WithTimeZone

func WithTimeZone(timeZone string) Option

WithTimeZone 设置数据库时区

func WithUsername

func WithUsername(username string) Option

WithUsername 设置账号

type Pagination added in v3.7.25

type Pagination struct {
	Offset *int64
	Limit  *int64
}

func (*Pagination) GetLimit added in v3.7.25

func (obj *Pagination) GetLimit() int64

func (*Pagination) GetOffset added in v3.7.25

func (obj *Pagination) GetOffset() int64

type Row

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

Row 获取记录

func (*Row) ToAny

func (r *Row) ToAny() (result map[string]any, err error)

ToInterface ...

func (*Row) ToMap

func (r *Row) ToMap() (result map[string]string, err error)

ToMap get Map

func (*Row) ToStruct

func (r *Row) ToStruct(st any) error

ToStruct get Struct

type Rows

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

Rows

func (*Rows) ToAny

func (r *Rows) ToAny() (data []map[string]any, err error)

ToAny 用法

func (*Rows) ToMap

func (r *Rows) ToMap() (data []map[string]string, err error)

ToMap 用法

func (*Rows) ToStruct

func (r *Rows) ToStruct(st any) error

ToStruct 用法

type Session

type Session interface {
	MultiInsert(ctx context.Context, query string, args ...any) (int64, error)
	Replace(ctx context.Context, query string, args ...any) (int64, error)
	InsertUpdate(ctx context.Context, query string, args ...any) (int64, error)
	Insert(ctx context.Context, query string, args ...any) (int64, error)
	Update(ctx context.Context, query string, args ...any) (int64, error)
	Delete(ctx context.Context, query string, args ...any) (int64, error)
	Exec(ctx context.Context, query string, args ...any) (int64, error)
	Count(ctx context.Context, query string, args ...any) (int64, error)
	QueryRow(ctx context.Context, query string, args ...any) *Row
	QueryRows(ctx context.Context, query string, args ...any) *Rows
	ExecCtx(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryCtx(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}

Session stands for raw connections or transaction sessions

func NewSessionFromTx added in v3.6.18

func NewSessionFromTx(tx *sql.Tx) Session

NewSessionFromTx returns a Session with the given sql.Tx. Use it with caution, it's provided for other ORM to interact with.

type SqlConn

type SqlConn interface {
	Session
	Transact(ctx context.Context, fn func(context.Context, Session) error) error
}

SqlConn only stands for raw connections, so Transact method can be called.

func NewSqlConn

func NewSqlConn(driverName, dns string, pool *pool, opts ...SqlOption) SqlConn

NewSqlConn returns a SqlConn with given driver name and dns.

type SqlOption

type SqlOption func(*commonSqlConn)

SqlOption defines the method to customize a sql connection.

Jump to

Keyboard shortcuts

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