db

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 18, 2023 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountWhere

func CountWhere(model interface{}, psql string, param ...interface{}) (count int64)

CountWhere 根据条件统计指定模型的数据记录总行数

func CreateInBatches

func CreateInBatches(model interface{}, count int) (RowsAffected int64, err error)

CreateInBatches 分批批量插入

func Escape

func Escape(val interface{}) string

Escape the val for sql

func EscapeInLocation

func EscapeInLocation(val interface{}, loc *time.Location) string

EscapeInLocation escape the val with time.Location

func Exec

func Exec(psql string, param ...interface{}) (RowsAffected int64, err error)

Exec 执行原生SQL,并返回受影响行数

func FindOne

func FindOne(dest interface{}, cond ...interface{})

FindOne 获取一条数据

func FindOneByCond

func FindOneByCond(dest interface{}, cond interface{})

FindOneByCond 根据条件获取一条数据

func FindOneWhere

func FindOneWhere(dest interface{}, psql string, param ...interface{})

FindOneWhere 根据预处理语句获取一条数据 psql(Prepared SQL Statement)预处理语句

func FindWhere

func FindWhere(dest interface{}, psql string, param ...interface{})

FindWhere 根据预处理语句获取数据 psql(Prepared SQL Statement)预处理语句

func FormatInLocation

func FormatInLocation(query string, loc *time.Location, args ...interface{}) string

FormatInLocation format the sql with args

func GetDB

func GetDB() *sql.DB

func Raw

func Raw(dest interface{}, psql string, param ...interface{})

Raw 将原生SQL扫描至模型

func Save

func Save(model interface{}) (RowsAffected int64, err error)

Save 保存一条数据

func SaveByFields

func SaveByFields(model interface{}, fields ...string) (RowsAffected int64, err error)

SaveByFields 保存一条数据,并制定字段

func Scan

func Scan(rows *sql.Rows, dest interface{}) error

Scan 扫描

func ScanResult

func ScanResult(rows *sql.Rows, dest interface{}) error

func ScanSlice

func ScanSlice(rows *sql.Rows, dest interface{}) error

func SetSingleQuoteEscaper

func SetSingleQuoteEscaper(escaper string)

SetSingleQuoteEscaper set the singleQuoteEscaper default:\' , e.g. ” 、 \'

func TableCount

func TableCount(table string) (count int64)

TableCount 统计指定表的数据记录总行数

func Updates

func Updates(model interface{}, data map[string]interface{}) (RowsAffected int64, err error)

Updates 更新多列

Types

type Client

type Client struct {
	Clients map[string]*DB
	Configs ClientConfig
	// contains filtered or unexported fields
}

func Open

func Open(configs ClientConfig, log *zap.Logger) (*Client, error)

Open init all the database clients

func (*Client) Close

func (client *Client) Close() error

Close the database

func (*Client) CreateClient

func (client *Client) CreateClient(database string) (db *DB, err error)

CreateClient create the db pool for the database

func (*Client) Use

func (client *Client) Use(database string) (db *DB)

Use get the db's conn

type ClientConfig

type ClientConfig struct {
	Clients map[string]interface{} `json:"clients"`
	Default map[string]interface{} `json:"default"`
}

type ClientDSN

type ClientDSN interface {
	GetDSN(config map[string]interface{}) string
}

type Config

type Config struct {
	Driver            Driver `json:"driver"`
	URL               string `json:"url"`
	Enabled           bool   `json:"enabled"`
	MaxIdleConnection int    `json:"max_idle_connection"`
	MaxOpenConnection int    `json:"max_open_connection"`
	ConnMaxLifetime   int    `json:"conn_max_lifetime"`
}

type DB

type DB struct {
	*sql.DB
	LogSQL bool
	// contains filtered or unexported fields
}

func (*DB) Begin

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

Begin starts a transaction. The default isolation level is dependent on the driver.

func (*DB) BeginTx

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

BeginTx starts a transaction.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.

The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.

func (*DB) Exec

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

Exec 执行查询而不返回任何行 args 用于查询中的任何占位符参数

func (*DB) ExecContext

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

ExecContext 执行查询而不返回任何行 args 用于查询中的任何占位符参数

func (*DB) Init

func (db *DB) Init(conf Config, logger *zap.Logger)

Init 初始化数据连接

func (*DB) Query

func (db *DB) Query(query string, args ...interface{}) *RowsResult

Query 执行一个返回 RowsResult 的查询,通常是一个 SELECT args 用于查询中的任何占位符参数

func (*DB) QueryContext

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

QueryContext 执行一个返回 RowsResult 的查询,通常是一个 SELECT args 用于查询中的任何占位符参数

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...interface{}) *RowResult

QueryRow 执行一个预期最多返回一行的查询 QueryRow 总是返回一个非零值。 错误被推迟到调用行的 Scan 方法 否则,*Row 的 Scan 扫描第一个选定的行并丢弃其余的部分

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *RowResult

QueryRowContext 执行一个预期最多返回一行的查询 QueryRowContext 总是返回一个非零值。 错误被推迟到调用行的 Scan 方法。 如果查询没有选择任何行,*Row 的 Scan 将返回 ErrNoRows 否则,*Row 的 Scan 扫描第一个选定的行并丢弃其余的部分

func (*DB) RawDB

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

RawDB 返回*sql.DB

type DelStatus

type DelStatus int8
const (
	Undeleted DelStatus = 0
	Deleted   DelStatus = 1
)

func (DelStatus) String

func (delStatus DelStatus) String() string

type Driver

type Driver int32
const (
	MariaDB Driver = 0
	MySQL   Driver = 1
	PgSQL   Driver = 2
)

func (Driver) String

func (driver Driver) String() string

type Mssql

type Mssql struct {
}

func (*Mssql) GetDSN

func (mssql *Mssql) GetDSN(config map[string]interface{}) string

GetDSN 获取数据源名称 https://github.com/denisenkom/go-mssqldb sqlserver://username:password@host:port/instance?param1=value&param2=value

type Mysql

type Mysql struct {
}

func (*Mysql) GetDSN

func (mysql *Mysql) GetDSN(config map[string]interface{}) string

GetDSN 获取数据源名称 https://github.com/go-sql-driver/mysql [driver[:password]@(host)][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]

func (*Mysql) Init

func (mysql *Mysql) Init(c Config) (err error)

type Orm

type Orm struct {
}

func (*Orm) Init

func (orm *Orm) Init(tablePrefix string, logger *zap.Logger)

Init 初始化ORM

type RowResult

type RowResult struct {
	LastError error
	// contains filtered or unexported fields
}

func (*RowResult) Err

func (r *RowResult) Err() error

Err 返回结果的最后错误

func (*RowResult) Scan

func (r *RowResult) Scan(dest interface{}) error

Scan RowResult's scan

type RowsResult

type RowsResult struct {
	*sql.Rows
	LastError error
}

func (*RowsResult) Close

func (r *RowsResult) Close() error

Close 将连接返回到连接池

func (*RowsResult) Raw

func (r *RowsResult) Raw() (*sql.Rows, error)

Raw 返回一行数据

func (*RowsResult) Scan

func (r *RowsResult) Scan(dest interface{}) error

Scan 扫描

type SQL

type SQL struct {
}

func (SQL) Format

func (sql SQL) Format(query string, args ...interface{}) string

Format 使用参数格式化SQL

type Trans

type Trans struct {
	*sql.Tx
	DB            *DB
	TransactionID string
}

func (*Trans) Commit

func (tx *Trans) Commit() error

Commit commits the transaction.

func (*Trans) Exec

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

Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.

func (*Trans) ExecContext

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

ExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE.

func (*Trans) Query

func (tx *Trans) Query(query string, args ...interface{}) *RowsResult

Query executes a query that returns rows, typically a SELECT.

func (*Trans) QueryContext

func (tx *Trans) QueryContext(ctx context.Context, query string, args ...interface{}) *RowsResult

QueryContext executes a query that returns rows, typically a SELECT.

func (*Trans) QueryRow

func (tx *Trans) QueryRow(query string, args ...interface{}) *RowResult

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until Row's Scan method is called. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*Trans) QueryRowContext

func (tx *Trans) QueryRowContext(ctx context.Context, query string, args ...interface{}) *RowResult

QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*Trans) Rollback

func (tx *Trans) Rollback() error

Rollback aborts the transaction.

Jump to

Keyboard shortcuts

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