Documentation
¶
Index ¶
- func Scan(rows *sql.Rows, dest interface{}) error
- func ScanResult(rows *sql.Rows, dest interface{}) error
- func ScanSlice(rows *sql.Rows, dest interface{}) error
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (db *DB) Create(table string, value any) (sql.Result, error)
- func (db *DB) CreateContext(ctx context.Context, table string, value any) (sql.Result, error)
- func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (db *DB) Query(query string, args ...interface{}) *RowsResult
- func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) *RowsResult
- func (db *DB) QueryRow(query string, args ...interface{}) *RowResult
- func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *RowResult
- func (db *DB) RawDB() *sql.DB
- func (db *DB) Update(table string, value any, where string, whereArgs ...any) (sql.Result, error)
- func (db *DB) UpdateColumns(table string, columns map[string]any, where string, whereArgs ...any) (sql.Result, error)
- func (db *DB) UpdateColumnsContext(ctx context.Context, table string, columns map[string]any, where string, ...) (sql.Result, error)
- func (db *DB) UpdateContext(ctx context.Context, table string, value any, where string, whereArgs ...any) (sql.Result, error)
- type DefaultLogger
- func (l DefaultLogger) Debug(ctx context.Context, format string, v ...interface{})
- func (l DefaultLogger) Error(ctx context.Context, format string, v ...interface{})
- func (l DefaultLogger) Info(ctx context.Context, format string, v ...interface{})
- func (l DefaultLogger) Warn(ctx context.Context, format string, v ...interface{})
- type Dialect
- type DialectClientOption
- type DialectClientOptionPool
- type DialectConfig
- type DialectDSN
- type LoggerInterface
- type Mssql
- type Mysql
- type RowResult
- type RowsResult
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) Create(table string, value any) (sql.Result, error)
- func (tx *Tx) CreateContext(ctx context.Context, table string, value any) (sql.Result, error)
- func (tx *Tx) Exec(query string, args ...interface{}) (sql.Result, error)
- func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (tx *Tx) Query(query string, args ...interface{}) *RowsResult
- func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) *RowsResult
- func (tx *Tx) QueryRow(query string, args ...interface{}) *RowResult
- func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *RowResult
- func (tx *Tx) Rollback() error
- func (tx *Tx) Update(table string, value any, where string, whereArgs ...any) (sql.Result, error)
- func (tx *Tx) UpdateColumns(table string, columns map[string]any, where string, whereArgs ...any) (sql.Result, error)
- func (tx *Tx) UpdateColumnsContext(ctx context.Context, table string, columns map[string]any, where string, ...) (sql.Result, error)
- func (tx *Tx) UpdateContext(ctx context.Context, table string, value any, where string, whereArgs ...any) (sql.Result, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ScanResult ¶
Types ¶
type DB ¶
func (*DB) Begin ¶
Begin starts a transaction. The default isolation level is dependent on the driver.
func (*DB) BeginTx ¶
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) Create ¶ added in v0.8.0
Create inserts value into table using db tags on exported struct fields.
Example:
type User struct {
ID int64 `db:"id"`
Name string `db:"name"`
}
user := &User{Name: "alice"}
_, err := db.Create("users", user)
func (*DB) CreateContext ¶ added in v0.8.0
CreateContext inserts value into table using db tags on exported struct fields.
Example:
ctx := context.Background()
_, err := db.CreateContext(ctx, "users", &User{Name: "alice"})
func (*DB) Exec ¶
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query
func (*DB) ExecContext ¶
func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*DB) Query ¶
func (db *DB) Query(query string, args ...interface{}) *RowsResult
Query executes a query that returns RowsResult, typically a SELECT. The args are for any placeholder parameters in the query.
func (*DB) QueryContext ¶
func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) *RowsResult
QueryContext executes a query that returns RowsResult, typically a SELECT. The args are for any placeholder parameters in the query.
func (*DB) QueryRow ¶
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 (*DB) QueryRowContext ¶
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. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
func (*DB) Update ¶ added in v0.8.0
Update updates table columns from value using db tags on exported struct fields. where is required to avoid updating all rows.
Example:
user := &User{ID: 1, Name: "alice"}
_, err := db.Update("users", user, "id=?", user.ID)
func (*DB) UpdateColumns ¶ added in v0.8.0
func (*DB) UpdateColumnsContext ¶ added in v0.8.0
func (*DB) UpdateContext ¶ added in v0.8.0
func (db *DB) UpdateContext(ctx context.Context, table string, value any, where string, whereArgs ...any) (sql.Result, error)
UpdateContext updates table columns from value using db tags on exported struct fields. where is required to avoid updating all rows.
Example:
ctx := context.Background() _, err := db.UpdateContext(ctx, "users", user, "id=?", user.ID)
type DefaultLogger ¶
type DefaultLogger struct {
}
func (DefaultLogger) Debug ¶
func (l DefaultLogger) Debug(ctx context.Context, format string, v ...interface{})
func (DefaultLogger) Error ¶
func (l DefaultLogger) Error(ctx context.Context, format string, v ...interface{})
type Dialect ¶
type Dialect struct {
Clients map[string]*DB
Configs DialectConfig
// contains filtered or unexported fields
}
func Open ¶
func Open(configs DialectConfig, log LoggerInterface) (*Dialect, error)
Init init all the database clients
func (*Dialect) CreateClient ¶
CreateClient create the db pool for the database
type DialectClientOption ¶
type DialectClientOption struct {
Host string `json:"host"`
Port int `json:"port"`
User string `json:"user"`
Password string `json:"password"`
Database string `json:"database"`
Dialect string `json:"dialect"`
Logging *bool `json:"logging"`
Pool *DialectClientOptionPool `json:"pool"`
Charset string `json:"charset"`
DialectOptions map[string]string `json:"dialectOptions"`
}
type DialectClientOptionPool ¶
type DialectConfig ¶
type DialectConfig struct {
Clients map[string]*DialectClientOption `json:"clients"`
Default *DialectClientOption `json:"default"`
}
type DialectDSN ¶
type DialectDSN interface {
GetDialectDSN(database string, config *DialectClientOption) string
}
type LoggerInterface ¶
type Mssql ¶
type Mssql struct {
}
Mssql mssql dialector
func (Mssql) GetDialectDSN ¶
func (m Mssql) GetDialectDSN(database string, config *DialectClientOption) string
GetDialectDSN
**config:{
"clients": {
"share":{
"host": "127.0.0.1",
"user": "sa",
"password": "test123",
"database": "test"
},
"test":{
"host": "127.0.0.1",
"port": 1433,
"user": "sa",
"password": "test123",
"database": "test",
"pool": {
"maxIdleConns": 20,
"maxLeftTime": 60000,
"maxOpenConns": 50
},
"dialectOptions": {
"dial timeout": "10"
}
}
},
"default": {
"port": 1433,
"dialect": "sqlserver",
"pool": {
"maxIdleConns": 2,
"maxLeftTime": 60000,
"maxOpenConns": 5
},
"dialectOptions": {
"dial timeout": "3"
}
}
}
*
type Mysql ¶
type Mysql struct {
}
Mysql mysql dialector
func (Mysql) GetDialectDSN ¶
func (m Mysql) GetDialectDSN(database string, config *DialectClientOption) string
GetDialectDSN
**config:{
"clients": {
"share":{
"host": "127.0.0.1",
"user": "test",
"password": "test123",
"database": "test"
},
"test":{
"host": "127.0.0.1",
"port": 3307,
"user": "test",
"password": "test123",
"database": "test",
"pool": {
"maxIdleConns": 20,
"maxLeftTime": 60000,
"maxOpenConns": 50
},
"dialectOptions": {
"writeTimeout": "2000ms",
"readTimeout": "2000ms",
"timeout":"2000ms"
}
}
},
"default": {
"port": 3306,
"dialect": "mysql",
"pool": {
"maxIdleConns": 2,
"maxLeftTime": 60000,
"maxOpenConns": 5
},
"dialectOptions": {
"writeTimeout": "3000ms",
"readTimeout": "3000ms",
"timeout":"3000ms"
}
}
}
*
type RowResult ¶
type RowResult struct {
LastError error
// contains filtered or unexported fields
}
type RowsResult ¶
func (RowsResult) Close ¶
func (r RowsResult) Close() error
Close returns the connection to the connection pool
type Tx ¶
func (*Tx) Create ¶ added in v0.8.0
Create inserts value into table within the current transaction.
Example:
_, err := tx.Create("users", &User{Name: "alice"})
func (*Tx) CreateContext ¶ added in v0.8.0
CreateContext inserts value into table within the current transaction.
Example:
ctx := context.Background()
_, err := tx.CreateContext(ctx, "users", &User{Name: "alice"})
func (*Tx) Exec ¶
Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.
func (*Tx) ExecContext ¶
func (tx *Tx) 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 (*Tx) Query ¶
func (tx *Tx) Query(query string, args ...interface{}) *RowsResult
Query executes a query that returns rows, typically a SELECT.
func (*Tx) QueryContext ¶
func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) *RowsResult
QueryContext executes a query that returns rows, typically a SELECT.
func (*Tx) QueryRow ¶
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 (*Tx) QueryRowContext ¶
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 (*Tx) Update ¶ added in v0.8.0
Update updates table columns from value within the current transaction. where is required to avoid updating all rows.
Example:
_, err := tx.Update("users", user, "id=?", user.ID)
func (*Tx) UpdateColumns ¶ added in v0.8.0
func (*Tx) UpdateColumnsContext ¶ added in v0.8.0
func (*Tx) UpdateContext ¶ added in v0.8.0
func (tx *Tx) UpdateContext(ctx context.Context, table string, value any, where string, whereArgs ...any) (sql.Result, error)
UpdateContext updates table columns from value within the current transaction. where is required to avoid updating all rows.
Example:
ctx := context.Background() _, err := tx.UpdateContext(ctx, "users", user, "id=?", user.ID)