dbs

package module
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 License: MIT Imports: 14 Imported by: 17

README

DBS

SQL Builder 工具, 不是 ORM。

帮助

在集成的过程中有遇到问题,欢迎加 QQ 群 564704807 讨论。

安装

$ go get github.com/smartwalle/dbs

开始

package main

import (
	"fmt"
	"github.com/smartwalle/dbs"
)

func main() {
	var sb = dbs.NewSelectBuilder()
	sb.Selects("u.id", "u.name", "u.age")
	sb.From("user", "AS u")
	sb.Where("u.id = ?", 1)
	sb.Limit(1)

	sqlStr, args, _ := sb.SQL()
	fmt.Println("sqlStr:", sqlStr)
	fmt.Println("args:", args)
}

上述代码会输出如下内容:

sql: SELECT u.id, u.name, u.age FROM user AS u WHERE u.id = ? LIMIT ?
args: [10 1]
执行 SQL

此处使用 MySQL 作为演示,需要准备好一个测试用数据库及 user 表,user 表结构如下:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `human_id_uindex` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8

package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/smartwalle/dbs"
)

func main() {
	var sb = dbs.NewSelectBuilder()
	sb.Selects("u.id", "u.name", "u.age")
	sb.From("user", "AS u")
	sb.Where("u.id = ?", 1)
	sb.Limit(1)

	db, err := sql.Open("mysql", "数据库连接信息")
	if err != nil {
		fmt.Println("连接数据库出错:", err)
		return
	}
	defer db.Close()

	var user *User
	if err := sb.Scan(db, &user); err != nil {
		fmt.Println("Query 出错:", err)
		return
	}

	if user != nil {
		fmt.Println(user.Id, user.Name, user.Age)
	}
}

type User struct {
	Id   int64  `sql:"id"`
	Name string `sql:"name"`
	Age  int    `sql:"age"`
}

运行上述代码,如果数据库中存在 id 为 1 的数据,则会正常输出相关的信息。

Select
var sb = dbs.NewSelectBuilder()
sb.Selects("u.id", "u.name AS username", "u.age")
sb.Select(dbs.Alias("b.amount", "user_amount"))
sb.From("user", "AS u")
sb.LeftJoin("bank", "AS b ON b.user_id = u.id")
sb.Where("u.id = ?", 1)
fmt.Println(sb.SQL())

执行 Select 语句:

db, err := sql.Open("mysql", "数据库连接信息")
var user *User
sb.Scan(db, &user)
Insert
var ib = dbs.NewInsertBuilder()
ib.Table("user")
ib.Columns("name", "age")
ib.Values("用户1", 18)
ib.Values("用户2", 20)
fmt.Println(ib.SQL())

执行 Insert 语句:

db, err := sql.Open("mysql", "数据库连接信息")
ib.Exec(db)
Update
var ub = dbs.NewUpdateBuilder()
ub.Table("user")
ub.SET("name", "新的名字")
ub.Where("id = ? ", 1)
ub.Limit(1)
fmt.Println(ub.SQL())

执行 Update 语句:

db, err := sql.Open("mysql", "数据库连接信息")
ub.Exec(db)
Delete
var rb = dbs.NewDeleteBuilder()
rb.Table("user")
rb.Where("id = ?", 1)
rb.Limit(1)
fmt.Println(rb.SQL())

执行 Delete 语句:

db, err := sql.Open("mysql", "数据库连接信息")
rb.Exec(db)

更多内容请参考 test 文件。

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	QuestionPlaceholder = &question{}
	DollarPlaceholder   = &dollar{
		pool: sync.Pool{New: func() interface{} {
			return &bytes.Buffer{}
		}},
	}
)
View Source
var ErrNoRows = sql.ErrNoRows
View Source
var ErrStmtExists = errors.New("statement exists")
View Source
var ErrTxDone = sql.ErrTxDone

Functions

func AND

func AND(clauses ...SQLClause) *whereClause

func Alias

func Alias(clause interface{}, alias string) *aliasClause

func Case

func Case(what ...interface{}) *caseClause

func Exec added in v1.2.7

func Exec(ctx context.Context, session Session, query string, args ...interface{}) (result sql.Result, err error)

func OR

func OR(clauses ...SQLClause) *whereClause

func OnConflictKeyUpdate added in v1.1.7

func OnConflictKeyUpdate(columns ...string) *onConflictDoUpdateClause

func OnDuplicateKeyUpdate

func OnDuplicateKeyUpdate() *onDuplicateKeyUpdateClause

func Open added in v1.2.4

func Open(driver, url string, maxOpen, maxIdle int) (db *sql.DB, err error)

func Query added in v1.2.7

func Query[T any](ctx context.Context, session Session, query string, args ...interface{}) (dst T, err error)

func Scan

func Scan[T any](rows *sql.Rows) (dst T, err error)

func UsePlaceholder added in v1.2.4

func UsePlaceholder(d Placeholder)

Types

type Buffer added in v1.0.3

type Buffer struct {
	bytes.Buffer
	// contains filtered or unexported fields
}

func NewBuffer added in v1.0.1

func NewBuffer() *Buffer

func (*Buffer) Release added in v1.0.3

func (buffer *Buffer) Release()

func (*Buffer) Reset added in v1.0.3

func (buffer *Buffer) Reset()

func (*Buffer) Values added in v1.0.3

func (buffer *Buffer) Values() []interface{}

func (*Buffer) WriteArgs added in v1.0.3

func (buffer *Buffer) WriteArgs(args ...interface{})

type Builder

type Builder interface {
	Type() string
	SQL() (string, []interface{}, error)
}

type Clause added in v1.2.6

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

func NewClause added in v1.2.6

func NewClause(clause interface{}, args ...interface{}) *Clause

func SQL

func SQL(clause string, args ...interface{}) *Clause

func (*Clause) SQL added in v1.2.6

func (clause *Clause) SQL() (string, []interface{}, error)

func (*Clause) Write added in v1.2.6

func (clause *Clause) Write(w Writer) error

type Clauses added in v1.2.6

type Clauses []SQLClause

func (Clauses) SQL added in v1.2.6

func (clauses Clauses) SQL() (string, []interface{}, error)

func (Clauses) Write added in v1.2.6

func (clauses Clauses) Write(w Writer, sep string) error

type DB

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

func New added in v1.2.1

func New(db *sql.DB) *DB

func (*DB) Begin

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

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

func (*DB) Close added in v1.0.3

func (db *DB) Close() error

func (*DB) DB added in v1.2.4

func (db *DB) DB() *sql.DB

func (*DB) Exec added in v1.2.4

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

func (*DB) ExecContext added in v1.2.4

func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*DB) Ping

func (db *DB) Ping() error

func (*DB) PingContext

func (db *DB) PingContext(ctx context.Context) error

func (*DB) Prepare added in v1.2.4

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

Prepare 作用同 sql.DB 的 Prepare 方法。

本方法返回的 sql.Stmt 不会被缓存,不再使用之后需要调用其 Close 方法将其关闭。

func (*DB) PrepareContext added in v1.2.4

func (db *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext 作用同 sql.DB 的 PrepareContext 方法。

本方法返回的 sql.Stmt 不会被缓存,不再使用之后需要调用其 Close 方法将其关闭。

func (*DB) PrepareStatement added in v1.2.7

func (db *DB) PrepareStatement(ctx context.Context, key, query string) error

PrepareStatement 预先创建一个 sql.Stmt 并将其缓存,后续可以使用 key 获取该 sql.Stmt。

var db = dbs.New(...)

db.PrepareStatement(ctx, "key", "SELECT ...")

var stmt, _ = db.Statement(ctx, "key")

stmt.Query("参数1", "参数2")

或者

db.Query("key", "参数1", "参数2")

func (*DB) Query added in v1.2.4

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

func (*DB) QueryContext added in v1.2.4

func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*DB) QueryRow added in v1.2.4

func (db *DB) QueryRow(query string, args ...any) *sql.Row

func (*DB) QueryRowContext added in v1.2.4

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*DB) Statement added in v1.2.7

func (db *DB) Statement(ctx context.Context, query string) (*sql.Stmt, error)

Statement 根据 query 参数获取已经缓存的 sql.Stmt。

两种情况:

1、缓存中若存在,则直接返回;

2、缓存中不存在,则根据 query 参数创建一个 sql.Stmt 并将其缓存;

注意:一般不需要直接调用本方法获取 sql.Stmt, 本方法主要是供本结构体的 ExecContext 和 QueryContext 方法使用。 如果有从本方法获取 sql.Stmt,不再使用之后不能调用其 Close 方法。

type Database added in v1.2.4

type Database interface {
	Session

	Close() error

	Begin() (*Tx, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
}

type DeleteBuilder

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

func NewDeleteBuilder

func NewDeleteBuilder() *DeleteBuilder

func (*DeleteBuilder) Alias

func (db *DeleteBuilder) Alias(alias ...string) *DeleteBuilder

func (*DeleteBuilder) Exec

func (db *DeleteBuilder) Exec(s Session) (sql.Result, error)

func (*DeleteBuilder) ExecContext

func (db *DeleteBuilder) ExecContext(ctx context.Context, s Session) (result sql.Result, err error)

func (*DeleteBuilder) GetPlaceholder added in v1.2.4

func (b *DeleteBuilder) GetPlaceholder() Placeholder

func (*DeleteBuilder) Join

func (db *DeleteBuilder) Join(join, table, suffix string, args ...interface{}) *DeleteBuilder

func (*DeleteBuilder) LeftJoin

func (db *DeleteBuilder) LeftJoin(table, suffix string, args ...interface{}) *DeleteBuilder

func (*DeleteBuilder) Limit

func (db *DeleteBuilder) Limit(limit int64) *DeleteBuilder

func (*DeleteBuilder) Offset

func (db *DeleteBuilder) Offset(offset int64) *DeleteBuilder

func (*DeleteBuilder) Options

func (db *DeleteBuilder) Options(options ...string) *DeleteBuilder

func (*DeleteBuilder) OrderBy

func (db *DeleteBuilder) OrderBy(clause ...string) *DeleteBuilder

func (*DeleteBuilder) Prefix

func (db *DeleteBuilder) Prefix(clause string, args ...interface{}) *DeleteBuilder

func (*DeleteBuilder) RightJoin

func (db *DeleteBuilder) RightJoin(table, suffix string, args ...interface{}) *DeleteBuilder

func (*DeleteBuilder) SQL added in v1.2.1

func (db *DeleteBuilder) SQL() (string, []interface{}, error)

func (*DeleteBuilder) Suffix

func (db *DeleteBuilder) Suffix(clause interface{}, args ...interface{}) *DeleteBuilder

func (*DeleteBuilder) Table

func (db *DeleteBuilder) Table(table string, args ...string) *DeleteBuilder

func (*DeleteBuilder) Type

func (db *DeleteBuilder) Type() string

func (*DeleteBuilder) USING

func (db *DeleteBuilder) USING(clause string) *DeleteBuilder

func (*DeleteBuilder) UsePlaceholder added in v1.2.4

func (db *DeleteBuilder) UsePlaceholder(p Placeholder) *DeleteBuilder

func (*DeleteBuilder) Where

func (db *DeleteBuilder) Where(clause interface{}, args ...interface{}) *DeleteBuilder

func (*DeleteBuilder) Write added in v1.2.1

func (db *DeleteBuilder) Write(w Writer) (err error)

type Eq

type Eq map[string]interface{}

func (Eq) SQL added in v1.2.1

func (clause Eq) SQL() (string, []interface{}, error)

func (Eq) Write added in v1.2.1

func (clause Eq) Write(w Writer) error

type ExceptBuilder added in v1.2.6

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

func NewExceptBuilder added in v1.2.6

func NewExceptBuilder() *ExceptBuilder

func (*ExceptBuilder) Except added in v1.2.6

func (eb *ExceptBuilder) Except(clauses ...SQLClause) *ExceptBuilder

func (*ExceptBuilder) ExceptAll added in v1.2.6

func (eb *ExceptBuilder) ExceptAll(clauses ...SQLClause) *ExceptBuilder

func (*ExceptBuilder) GetPlaceholder added in v1.2.6

func (b *ExceptBuilder) GetPlaceholder() Placeholder

func (*ExceptBuilder) Limit added in v1.2.6

func (eb *ExceptBuilder) Limit(limit int64) *ExceptBuilder

func (*ExceptBuilder) Offset added in v1.2.6

func (eb *ExceptBuilder) Offset(offset int64) *ExceptBuilder

func (*ExceptBuilder) OrderBy added in v1.2.6

func (eb *ExceptBuilder) OrderBy(clause ...string) *ExceptBuilder

func (*ExceptBuilder) SQL added in v1.2.6

func (eb *ExceptBuilder) SQL() (string, []interface{}, error)

func (*ExceptBuilder) Type added in v1.2.6

func (eb *ExceptBuilder) Type() string

func (*ExceptBuilder) UsePlaceholder added in v1.2.6

func (eb *ExceptBuilder) UsePlaceholder(p Placeholder) *ExceptBuilder

func (*ExceptBuilder) Write added in v1.2.6

func (eb *ExceptBuilder) Write(w Writer) (err error)

type InsertBuilder

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

func Insert

func Insert(columns ...string) *InsertBuilder

func NewInsertBuilder

func NewInsertBuilder() *InsertBuilder

func (*InsertBuilder) Column

func (ib *InsertBuilder) Column(column string) *InsertBuilder

func (*InsertBuilder) Columns

func (ib *InsertBuilder) Columns(columns ...string) *InsertBuilder

func (*InsertBuilder) Exec

func (ib *InsertBuilder) Exec(s Session) (sql.Result, error)

func (*InsertBuilder) ExecContext

func (ib *InsertBuilder) ExecContext(ctx context.Context, s Session) (result sql.Result, err error)

func (*InsertBuilder) GetPlaceholder added in v1.2.4

func (b *InsertBuilder) GetPlaceholder() Placeholder

func (*InsertBuilder) Options

func (ib *InsertBuilder) Options(options ...string) *InsertBuilder

func (*InsertBuilder) Prefix

func (ib *InsertBuilder) Prefix(clause string, args ...interface{}) *InsertBuilder

func (*InsertBuilder) Returning added in v1.2.7

func (ib *InsertBuilder) Returning(columns ...string) *InsertBuilder

func (*InsertBuilder) SET

func (ib *InsertBuilder) SET(column string, value interface{}) *InsertBuilder

func (*InsertBuilder) SQL added in v1.2.1

func (ib *InsertBuilder) SQL() (string, []interface{}, error)

func (*InsertBuilder) Scan added in v1.2.7

func (ib *InsertBuilder) Scan(s Session, dst interface{}) (err error)

func (*InsertBuilder) ScanContext added in v1.2.7

func (ib *InsertBuilder) ScanContext(ctx context.Context, s Session, dst interface{}) (err error)

func (*InsertBuilder) ScanRow added in v1.2.7

func (ib *InsertBuilder) ScanRow(s Session, dst ...interface{}) (err error)

func (*InsertBuilder) ScanRowContext added in v1.2.7

func (ib *InsertBuilder) ScanRowContext(ctx context.Context, s Session, dst ...interface{}) (err error)

func (*InsertBuilder) Select

func (ib *InsertBuilder) Select(sb *SelectBuilder) *InsertBuilder

func (*InsertBuilder) Suffix

func (ib *InsertBuilder) Suffix(clause interface{}, args ...interface{}) *InsertBuilder

func (*InsertBuilder) Table

func (ib *InsertBuilder) Table(table string) *InsertBuilder

func (*InsertBuilder) Type

func (ib *InsertBuilder) Type() string

func (*InsertBuilder) UsePlaceholder added in v1.2.4

func (ib *InsertBuilder) UsePlaceholder(p Placeholder) *InsertBuilder

func (*InsertBuilder) Values

func (ib *InsertBuilder) Values(values ...interface{}) *InsertBuilder

func (*InsertBuilder) Write added in v1.2.1

func (ib *InsertBuilder) Write(w Writer) (err error)

type IntersectBuilder added in v1.2.6

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

func NewIntersectBuilder added in v1.2.6

func NewIntersectBuilder() *IntersectBuilder

func (*IntersectBuilder) GetPlaceholder added in v1.2.6

func (b *IntersectBuilder) GetPlaceholder() Placeholder

func (*IntersectBuilder) Intersect added in v1.2.6

func (ib *IntersectBuilder) Intersect(clauses ...SQLClause) *IntersectBuilder

func (*IntersectBuilder) IntersectAll added in v1.2.6

func (ib *IntersectBuilder) IntersectAll(clauses ...SQLClause) *IntersectBuilder

func (*IntersectBuilder) Limit added in v1.2.6

func (ib *IntersectBuilder) Limit(limit int64) *IntersectBuilder

func (*IntersectBuilder) Offset added in v1.2.6

func (ib *IntersectBuilder) Offset(offset int64) *IntersectBuilder

func (*IntersectBuilder) OrderBy added in v1.2.6

func (ib *IntersectBuilder) OrderBy(clause ...string) *IntersectBuilder

func (*IntersectBuilder) SQL added in v1.2.6

func (ib *IntersectBuilder) SQL() (string, []interface{}, error)

func (*IntersectBuilder) Type added in v1.2.6

func (ib *IntersectBuilder) Type() string

func (*IntersectBuilder) UsePlaceholder added in v1.2.6

func (ib *IntersectBuilder) UsePlaceholder(p Placeholder) *IntersectBuilder

func (*IntersectBuilder) Write added in v1.2.6

func (ib *IntersectBuilder) Write(w Writer) (err error)

type Mapper added in v1.2.4

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

func NewMapper added in v1.2.4

func NewMapper(tag string) *Mapper

func (*Mapper) Decode added in v1.2.7

func (mapper *Mapper) Decode(rows *sql.Rows, dst interface{}) error

type NotEq

type NotEq Eq

func (NotEq) SQL added in v1.2.1

func (clause NotEq) SQL() (string, []interface{}, error)

func (NotEq) Write added in v1.2.1

func (clause NotEq) Write(w Writer) error

type Placeholder added in v1.2.4

type Placeholder interface {
	Replace(clause string) (string, error)
}

func GetPlaceholder added in v1.2.4

func GetPlaceholder() Placeholder

type Preparer

type Preparer interface {
	Statement(ctx context.Context, query string) (*sql.Stmt, error)
}

type RawBuilder

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

RawBuilder 原始 SQL 语句构造器,不会自动添加任何的关键字,主要是为了便于 SQL 语句及参数的管理。

func NewBuilder

func NewBuilder(clause string, args ...interface{}) *RawBuilder

func (*RawBuilder) Append

func (rb *RawBuilder) Append(clause string, args ...interface{}) *RawBuilder

func (*RawBuilder) Exec

func (rb *RawBuilder) Exec(s Session) (sql.Result, error)

func (*RawBuilder) ExecContext

func (rb *RawBuilder) ExecContext(ctx context.Context, s Session) (result sql.Result, err error)

func (*RawBuilder) Format

func (rb *RawBuilder) Format(format string, args ...interface{}) *RawBuilder

func (*RawBuilder) GetPlaceholder added in v1.2.4

func (b *RawBuilder) GetPlaceholder() Placeholder

func (*RawBuilder) Params

func (rb *RawBuilder) Params(args ...interface{}) *RawBuilder

func (*RawBuilder) Query

func (rb *RawBuilder) Query(s Session) (*sql.Rows, error)

func (*RawBuilder) QueryContext

func (rb *RawBuilder) QueryContext(ctx context.Context, s Session) (*sql.Rows, error)

func (*RawBuilder) SQL added in v1.2.1

func (rb *RawBuilder) SQL() (string, []interface{}, error)

func (*RawBuilder) Scan

func (rb *RawBuilder) Scan(s Session, dst interface{}) (err error)

func (*RawBuilder) ScanContext

func (rb *RawBuilder) ScanContext(ctx context.Context, s Session, dst interface{}) (err error)

func (*RawBuilder) ScanRow

func (rb *RawBuilder) ScanRow(s Session, dst ...interface{}) (err error)

func (*RawBuilder) ScanRowContext

func (rb *RawBuilder) ScanRowContext(ctx context.Context, s Session, dst ...interface{}) (err error)

func (*RawBuilder) Type

func (rb *RawBuilder) Type() string

func (*RawBuilder) UsePlaceholder added in v1.2.4

func (rb *RawBuilder) UsePlaceholder(p Placeholder) *RawBuilder

func (*RawBuilder) Write added in v1.2.1

func (rb *RawBuilder) Write(w Writer) error

type SQLClause added in v1.2.6

type SQLClause interface {
	Write(w Writer) error
	SQL() (string, []interface{}, error)
}

func IN

func IN(clause string, args interface{}) SQLClause

func Like

func Like(clause string, args ...string) SQLClause

func NotIn

func NotIn(clause string, args interface{}) SQLClause

func NotLike

func NotLike(clause string, args ...string) SQLClause

type SQLValue

type SQLValue interface {
	SQLValue() string
}

type SelectBuilder

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

func NewSelectBuilder

func NewSelectBuilder() *SelectBuilder

func Select

func Select(columns ...string) *SelectBuilder

func (*SelectBuilder) Count

func (sb *SelectBuilder) Count(alias string) *SelectBuilder

func (*SelectBuilder) From

func (sb *SelectBuilder) From(table string, args ...string) *SelectBuilder

func (*SelectBuilder) FromClause added in v1.2.6

func (sb *SelectBuilder) FromClause(clause SQLClause) *SelectBuilder

func (*SelectBuilder) GetPlaceholder added in v1.2.4

func (b *SelectBuilder) GetPlaceholder() Placeholder

func (*SelectBuilder) GroupBy

func (sb *SelectBuilder) GroupBy(groupBys ...string) *SelectBuilder

func (*SelectBuilder) Having

func (sb *SelectBuilder) Having(clause interface{}, args ...interface{}) *SelectBuilder

func (*SelectBuilder) Join

func (sb *SelectBuilder) Join(join, table, suffix string, args ...interface{}) *SelectBuilder

func (*SelectBuilder) LeftJoin

func (sb *SelectBuilder) LeftJoin(table, suffix string, args ...interface{}) *SelectBuilder

func (*SelectBuilder) Limit

func (sb *SelectBuilder) Limit(limit int64) *SelectBuilder

func (*SelectBuilder) Offset

func (sb *SelectBuilder) Offset(offset int64) *SelectBuilder

func (*SelectBuilder) Options

func (sb *SelectBuilder) Options(options ...string) *SelectBuilder

func (*SelectBuilder) OrderBy

func (sb *SelectBuilder) OrderBy(clause ...string) *SelectBuilder

func (*SelectBuilder) Prefix

func (sb *SelectBuilder) Prefix(clause string, args ...interface{}) *SelectBuilder

func (*SelectBuilder) Query

func (sb *SelectBuilder) Query(s Session) (*sql.Rows, error)

func (*SelectBuilder) QueryContext

func (sb *SelectBuilder) QueryContext(ctx context.Context, s Session) (*sql.Rows, error)

func (*SelectBuilder) RightJoin

func (sb *SelectBuilder) RightJoin(table, suffix string, args ...interface{}) *SelectBuilder

func (*SelectBuilder) SQL added in v1.2.1

func (sb *SelectBuilder) SQL() (string, []interface{}, error)

func (*SelectBuilder) Scan

func (sb *SelectBuilder) Scan(s Session, dst interface{}) (err error)

Scan 读取数据到一个结构体中。

var user *User

var sb = dbs.NewSelectBuilder()

sb.Scan(db, &user)

func (*SelectBuilder) ScanContext

func (sb *SelectBuilder) ScanContext(ctx context.Context, s Session, dst interface{}) (err error)

ScanContext 读取数据到一个结构体中。

var user *User

var sb = dbs.NewSelectBuilder()

sb.ScanContext(ctx, db, &user)

func (*SelectBuilder) ScanRow

func (sb *SelectBuilder) ScanRow(s Session, dst ...interface{}) (err error)

ScanRow 读取数据到基本数据类型的变量中,类似于 database/buf 包中结构体 Rows 的 Scan() 方法。

var name string

var age int

var sb = dbs.NewSelectBuilder()

sb.ScanRow(db, &name, &age)

func (*SelectBuilder) ScanRowContext

func (sb *SelectBuilder) ScanRowContext(ctx context.Context, s Session, dst ...interface{}) (err error)

ScanRowContext 读取数据到基本数据类型的变量中,类似于 database/buf 包中结构体 Rows 的 Scan() 方法。

var name string

var age int

var sb = dbs.NewSelectBuilder()

sb.ScanRowContext(ctx, db, &name, &age)

func (*SelectBuilder) Select

func (sb *SelectBuilder) Select(column interface{}, args ...interface{}) *SelectBuilder

func (*SelectBuilder) Selects

func (sb *SelectBuilder) Selects(columns ...string) *SelectBuilder

func (*SelectBuilder) Suffix

func (sb *SelectBuilder) Suffix(clause interface{}, args ...interface{}) *SelectBuilder

func (*SelectBuilder) Type

func (sb *SelectBuilder) Type() string

func (*SelectBuilder) UsePlaceholder added in v1.2.4

func (sb *SelectBuilder) UsePlaceholder(p Placeholder) *SelectBuilder

func (*SelectBuilder) Where

func (sb *SelectBuilder) Where(clause interface{}, args ...interface{}) *SelectBuilder

func (*SelectBuilder) Write added in v1.2.1

func (sb *SelectBuilder) Write(w Writer) (err error)

type Session added in v1.0.4

type Session interface {
	Prepare(query string) (*sql.Stmt, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

	Exec(query string, args ...interface{}) (sql.Result, error)
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

	QueryRow(query string, args ...any) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

type Transaction added in v1.2.4

type Transaction interface {
	Session

	Rollback() error
	Commit() error
}

type Tx added in v1.2.4

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

func (*Tx) Commit added in v1.2.4

func (tx *Tx) Commit() error

func (*Tx) Exec added in v1.2.4

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

func (*Tx) ExecContext added in v1.2.4

func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*Tx) Prepare added in v1.2.4

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

Prepare 作用同 sql.Tx 的 Prepare 方法。

本方法返回的 sql.Stmt 不会被缓存,不再使用之后需要调用其 Close 方法将其关闭。

func (*Tx) PrepareContext added in v1.2.4

func (tx *Tx) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext 作用同 sql.Tx 的 PrepareContext 方法。

本方法返回的 sql.Stmt 不会被缓存,不再使用之后需要调用其 Close 方法将其关闭。

func (*Tx) Query added in v1.2.4

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

func (*Tx) QueryContext added in v1.2.4

func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*Tx) QueryRow added in v1.2.4

func (tx *Tx) QueryRow(query string, args ...any) *sql.Row

func (*Tx) QueryRowContext added in v1.2.4

func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*Tx) Rollback added in v1.2.4

func (tx *Tx) Rollback() error

func (*Tx) Statement added in v1.2.7

func (tx *Tx) Statement(ctx context.Context, query string) (*sql.Stmt, error)

func (*Tx) Tx added in v1.2.4

func (tx *Tx) Tx() *sql.Tx

type UnionBuilder added in v1.2.6

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

func NewUnionBuilder added in v1.2.6

func NewUnionBuilder() *UnionBuilder

func (*UnionBuilder) GetPlaceholder added in v1.2.6

func (b *UnionBuilder) GetPlaceholder() Placeholder

func (*UnionBuilder) Limit added in v1.2.6

func (ub *UnionBuilder) Limit(limit int64) *UnionBuilder

func (*UnionBuilder) Offset added in v1.2.6

func (ub *UnionBuilder) Offset(offset int64) *UnionBuilder

func (*UnionBuilder) OrderBy added in v1.2.6

func (ub *UnionBuilder) OrderBy(clause ...string) *UnionBuilder

func (*UnionBuilder) SQL added in v1.2.6

func (ub *UnionBuilder) SQL() (string, []interface{}, error)

func (*UnionBuilder) Type added in v1.2.6

func (ub *UnionBuilder) Type() string

func (*UnionBuilder) Union added in v1.2.6

func (ub *UnionBuilder) Union(clauses ...SQLClause) *UnionBuilder

func (*UnionBuilder) UnionAll added in v1.2.6

func (ub *UnionBuilder) UnionAll(clauses ...SQLClause) *UnionBuilder

func (*UnionBuilder) UsePlaceholder added in v1.2.6

func (ub *UnionBuilder) UsePlaceholder(p Placeholder) *UnionBuilder

func (*UnionBuilder) Write added in v1.2.6

func (ub *UnionBuilder) Write(w Writer) (err error)

type UpdateBuilder

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

func NewUpdateBuilder

func NewUpdateBuilder() *UpdateBuilder

func Update

func Update(table string, args ...string) *UpdateBuilder

func (*UpdateBuilder) Exec

func (ub *UpdateBuilder) Exec(s Session) (sql.Result, error)

func (*UpdateBuilder) ExecContext

func (ub *UpdateBuilder) ExecContext(ctx context.Context, s Session) (result sql.Result, err error)

func (*UpdateBuilder) GetPlaceholder added in v1.2.4

func (b *UpdateBuilder) GetPlaceholder() Placeholder

func (*UpdateBuilder) Join

func (ub *UpdateBuilder) Join(join, table, suffix string, args ...interface{}) *UpdateBuilder

func (*UpdateBuilder) LeftJoin

func (ub *UpdateBuilder) LeftJoin(table, suffix string, args ...interface{}) *UpdateBuilder

func (*UpdateBuilder) Limit

func (ub *UpdateBuilder) Limit(limit int64) *UpdateBuilder

func (*UpdateBuilder) Offset

func (ub *UpdateBuilder) Offset(offset int64) *UpdateBuilder

func (*UpdateBuilder) Options

func (ub *UpdateBuilder) Options(options ...string) *UpdateBuilder

func (*UpdateBuilder) OrderBy

func (ub *UpdateBuilder) OrderBy(clause ...string) *UpdateBuilder

func (*UpdateBuilder) Prefix

func (ub *UpdateBuilder) Prefix(clause string, args ...interface{}) *UpdateBuilder

func (*UpdateBuilder) RightJoin

func (ub *UpdateBuilder) RightJoin(table, suffix string, args ...interface{}) *UpdateBuilder

func (*UpdateBuilder) SET

func (ub *UpdateBuilder) SET(column string, value interface{}) *UpdateBuilder

func (*UpdateBuilder) SETS

func (ub *UpdateBuilder) SETS(kvs ...interface{}) *UpdateBuilder

SETS 批量设置需要更新的字段及其值

var name = "my name"

SETS("name", name, "age", 10)

func (*UpdateBuilder) SQL added in v1.2.1

func (ub *UpdateBuilder) SQL() (string, []interface{}, error)

func (*UpdateBuilder) SetMap

func (ub *UpdateBuilder) SetMap(data map[string]interface{}) *UpdateBuilder

func (*UpdateBuilder) Suffix

func (ub *UpdateBuilder) Suffix(clause interface{}, args ...interface{}) *UpdateBuilder

func (*UpdateBuilder) Table

func (ub *UpdateBuilder) Table(table string, args ...string) *UpdateBuilder

func (*UpdateBuilder) Type

func (ub *UpdateBuilder) Type() string

func (*UpdateBuilder) UsePlaceholder added in v1.2.4

func (ub *UpdateBuilder) UsePlaceholder(p Placeholder) *UpdateBuilder

func (*UpdateBuilder) Where

func (ub *UpdateBuilder) Where(clause interface{}, args ...interface{}) *UpdateBuilder

func (*UpdateBuilder) Write added in v1.2.1

func (ub *UpdateBuilder) Write(w Writer) (err error)

type Writer added in v1.0.3

type Writer interface {
	Write(p []byte) (n int, err error)

	WriteString(s string) (n int, err error)

	WriteArgs(args ...interface{})
}

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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