Documentation
¶
Index ¶
- Variables
- func MapRow(row *sql.Rows, pointerOfStruct interface{}) error
- func MapRows(rows *sql.Rows, structPtrOrSlicePtr interface{}) error
- func ParseTags(tag string) (map[string]string, error)
- func ParseType(t string, nullable bool) (string, error)
- func Ptr[T any](t T) *T
- func QueryForBulkInsert[T Model](modelPtrs ...T) (q.Query, error)
- func QueryForInsert(modelPtr Model) (q.Query, *reflect.Value, error)
- func QueryForUpdateModel(updateStructPtr ModelUpdate, where q.Condition) (q.Query, error)
- func Transaction(db *sql.DB, ctx context.Context, opts *sql.TxOptions, ...) error
- func Where(str string, args ...any) q.Condition
- type Column
- type ColumnSplitter
- type DB
- type Executor
- type Finder
- type GenerateOptions
- type Generator
- type Mapperdeprecated
- type Model
- type ModelMetadata
- type ModelUpdate
- type OpenFunc
- type OpenOptions
- type Parser
- type Saver
- type SerialMapper
- type StmtExecutor
- type Table
- type Tx
Constants ¶
This section is empty.
Variables ¶
var ErrRecordNotFound = errors.New("record not found")
Error returned when record not found
Functions ¶
func MapRow ¶
MapRow reads data from single row and maps those columns into destination struct. pointerOfStruct MUST BE a pointer of struct. It closes rows after mapping regardless error occurred.
Example:
var user User err := exql.MapRow(rows, &user)
func MapRows ¶
MapRows reads all data from rows and maps those columns for each destination struct. pointerOfSliceOfStruct MUST BE a pointer of slice of pointer of struct. It closes rows after mapping regardless error occurred.
Example:
var users []*Users err := exql.MapRows(rows, &users)
func QueryForUpdateModel ¶
func Transaction ¶
Types ¶
type Column ¶
type Column struct { FieldName string `json:"field_name"` FieldType string `json:"field_type"` FieldIndex int `json:"field_index"` GoFieldType string `json:"go_field_type"` Nullable bool `json:"nullable"` DefaultValue sql.NullString `json:"default_value"` Key sql.NullString `json:"key"` Extra sql.NullString `json:"extra"` }
func (*Column) ParseExtra ¶
func (*Column) UpdateField ¶
type ColumnSplitter ¶
type DB ¶
type DB interface { Saver Mapper Finder // DB returns *sql.DB object. DB() *sql.DB // SetDB sets *sql.DB object. SetDB(db *sql.DB) // Transaction begins a transaction and commits after the callback is called. // If an error is returned from the callback, it is rolled back. // Internally call tx.BeginTx(context.Background(), nil). Transaction(callback func(tx Tx) error) error // TransactionWithContext is same as Transaction(). // Internally call tx.BeginTx(ctx, opts). TransactionWithContext(ctx context.Context, opts *sql.TxOptions, callback func(tx Tx) error) error // Close calls db.Close(). Close() error }
func Open ¶
func Open(opts *OpenOptions) (DB, error)
Open opens the connection to the database and makes exql.DB interface.
func OpenContext ¶ added in v2.2.0
func OpenContext(ctx context.Context, opts *OpenOptions) (DB, error)
OpenContext opens the connection to the database and makes exql.DB interface. If something failed, it retries automatically until given retry strategies satisfied or aborts handshaking.
Example:
db, err := exql.Open(context.Background(), &exql.OpenOptions{ Url: "user:pass@tcp(127.0.0.1:3306)/database?charset=utf8mb4&parseTime=True&loc=Local", MaxRetryCount: 3, RetryInterval: 10, //sec })
type Executor ¶
type Executor interface { Exec(query string, args ...any) (sql.Result, error) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) Query(query string, args ...any) (*sql.Rows, error) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) QueryRow(query string, args ...any) *sql.Row QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row Prepare(stmt string) (*sql.Stmt, error) PrepareContext(ctx context.Context, stmt string) (*sql.Stmt, error) }
Executor is an abstraction of both sql.DB/sql.Tx
type Finder ¶
type Finder interface { Find(q query.Query, destPtrOfStruct any) error FindContext(ctx context.Context, q query.Query, destPtrOfStruct any) error FindMany(q query.Query, destSlicePtrOfStruct any) error FindManyContext(ctx context.Context, q query.Query, destSlicePtrOfStruct any) error }
Finder is an interface to execute select query and map rows into the destination.
type GenerateOptions ¶
type Generator ¶
type Generator interface {
Generate(opts *GenerateOptions) error
}
func NewGenerator ¶
type Mapper
deprecated
type Mapper interface { // Deprecated: Use Find or MapRow. It will be removed in next version. Map(rows *sql.Rows, destPtr any) error // Deprecated: Use FindContext or MapRows. It will be removed in next version. MapMany(rows *sql.Rows, destSlicePtr any) error }
Deprecated: Use Finder It will be removed in next version.
type ModelMetadata ¶
type ModelMetadata struct { TableName string AutoIncrementField *reflect.Value PrimaryKeyColumns []string PrimaryKeyValues []any Values q.KeyIterator[any] }
func AggregateModelMetadata ¶
func AggregateModelMetadata(modelPtr Model) (*ModelMetadata, error)
type ModelUpdate ¶
type ModelUpdate interface {
UpdateTableName() string
}
type OpenOptions ¶
type Saver ¶
type Saver interface { Insert(structPtr Model) (sql.Result, error) InsertContext(ctx context.Context, structPtr Model) (sql.Result, error) Update(table string, set map[string]any, where q.Condition) (sql.Result, error) UpdateModel(updaterStructPtr ModelUpdate, where q.Condition) (sql.Result, error) UpdateContext(ctx context.Context, table string, set map[string]any, where q.Condition) (sql.Result, error) UpdateModelContext(ctx context.Context, updaterStructPtr ModelUpdate, where q.Condition) (sql.Result, error) Delete(table string, where q.Condition) (sql.Result, error) DeleteContext(ctx context.Context, table string, where q.Condition) (sql.Result, error) Exec(query q.Query) (sql.Result, error) ExecContext(ctx context.Context, query q.Query) (sql.Result, error) Query(query q.Query) (*sql.Rows, error) QueryContext(ctx context.Context, query q.Query) (*sql.Rows, error) QueryRow(query q.Query) (*sql.Row, error) QueryRowContext(ctx context.Context, query q.Query) (*sql.Row, error) }
type SerialMapper ¶
type SerialMapper interface { // Map reads joined rows and maps columns for each destination serially. // The second argument, pointerOfStruct, MUST BE a pointer of the struct. // // NOTE: DO NOT FORGET to close rows manually, as it WON'T do it automatically. // // Example: // // var user User // var favorite UserFavorite // defer rows.Close() // err := m.Map(rows, &user, &favorite) Map(rows *sql.Rows, pointersOfStruct ...any) error }
SerialMapper is an interface for mapping a joined row into one or more destinations serially.
func NewSerialMapper ¶
func NewSerialMapper(s ColumnSplitter) SerialMapper
type StmtExecutor ¶
type StmtExecutor interface { Executor // Close calls all retained *sql.Stmt and clears the buffer. // DON'T forget to call this on the manual use. Close() error }
StmtExecutor is the Executor that caches queries as *sql.Stmt. It uses the cached Stmt for the next execution if query is identical. They are held until Close() is called. This is useful for the case of executing the same query repeatedly in the for-loop. It may prevent errors caused by the db's connection pool.
Example:
stmtExecer := exql.NewStmtExecutor(tx.Tx()) defer stmtExecer.Close() stmtSaver := exql.NewSaver(stmtExecer)
func NewStmtExecutor ¶
func NewStmtExecutor(ex Executor) StmtExecutor
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
mocks
|
|
mock_exql
Package mock_exql is a generated GoMock package.
|
Package mock_exql is a generated GoMock package. |
mock_query
Package mock_query is a generated GoMock package.
|
Package mock_query is a generated GoMock package. |
This file is generated by exql.
|
This file is generated by exql. |
tool
|
|