dbx

package module
v0.0.0-...-884c8a7 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: MIT Imports: 13 Imported by: 0

README

ORM library for Golang

Open Database

db, err := Open("sqlite3", "file:locked.sqlite")

Create Table

sql := `create table accounts(
    id                       integer primary key autoincrement,
    status                   integer default 0,
    nickname                 varchar(24) not null,
    avatar                   varchar(512),
)`
rs, err := db.SQLExec(a)
if err != nil {
    panic(err)
}

Insert

//Define Struct
type Account struct {
    ID        int            `dbx:"column:id" `
    NickName  string         `dbx:"column:nickname" `
    Status    int            `dbx:"column:status" `
    Avatar    sql.NullString `dbx:"column:avatar"`
}

func (a Account) TableName() string {
    return "accounts"
}

_, err = db.StructInsert(&Account{
    NickName: "jack",
    Avatar: sql.NullString{},
    Status:   0,
})

Query

var account = make([]*Account, 0)
err = db.SQLFind(&account, "select * from accounts")

var nameArr []string
err = db.SQLFind(&nameArr, "select name from accounts")

var accountSingle = Account{}
err = db.SQLFirst(&accountSingle, "select * from accounts where id = ?", 33)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func TimeFormat

func TimeFormat(t *time.Time) string

Types

type Column

type Column interface {
	Take(v interface{}) (interface{}, error)
	Put(v interface{}) (interface{}, error)
}

type CommonSQLGenerator

type CommonSQLGenerator struct {
}

func NewCommonSQLGenerator

func NewCommonSQLGenerator() *CommonSQLGenerator

func (CommonSQLGenerator) InsertSQL

func (CommonSQLGenerator) InsertSQL(value interface{}) (autoIncrement *reflect.Value, query string, args []interface{}, err error)

func (CommonSQLGenerator) UpdateSQL

func (CommonSQLGenerator) UpdateSQL(value interface{}, columns ...string) (query string, args []interface{}, err error)

type DB

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

func Connect

func Connect(db *sql.DB) *DB

Connect a database to dbx

func Open

func Open(driverName string, dataSourceName string) (*DB, error)

Open a database

Example
db, err = Open("mysql", "root:basebitxdp@tcp(172.18.0.210:32600)/enigma2_accountx?parseTime=True&loc=Local")
if err != nil {
	panic(err)
}
Output:

func (*DB) Begin

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

Begin a Transaction

func (*DB) BeginTx

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

BeginTx begin a Transaction with opts

func (*DB) Close

func (d *DB) Close() error

Close the database and prevents newDBX queries from starting. Close then waits for all queries that have started processing on the server to finish.

It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.

func (DB) Exec

func (e DB) Exec(query string, args ...interface{}) (rs sql.Result, err error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (DB) ExecContext

func (e DB) ExecContext(ctx context.Context, query string, args ...interface{}) (rs sql.Result, err error)

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (DB) Get

func (e DB) Get(dest interface{}, query string, args ...interface{}) (err error)

Get execute the query and scan the first row to dest, dest must be a pointer. if dest is a type supported by the database (string , int, []byte, time.Time, etc.) and there is only one column, the row will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column. An sql.ErrNoRows is returned if the result set is empty.

func (DB) GetContext

func (e DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error)

GetContext execute the query and scan the first row to dest, dest must be a pointer. if dest is a type supported by the database (string , int, []byte, time.Time, etc.) and there is only one column, the row will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column. An sql.ErrNoRows is returned if the result set is empty.

func (DB) Insert

func (e DB) Insert(value interface{}) (sql.Result, error)

Insert a struct to database

func (DB) InsertContext

func (e DB) InsertContext(ctx context.Context, value interface{}) (rs sql.Result, err error)

InsertContext insert a struct to database

func (DB) MustExec

func (e DB) MustExec(query string, args ...interface{}) (rs sql.Result)

MustExec is the same as Exec but panics if cannot insert.

func (DB) MustGet

func (e DB) MustGet(dest interface{}, query string, args ...interface{}) bool

MustGet is the same as Get, if there is a error in query, it will panics, but sql.ErrNoRows will return false

func (DB) MustInsert

func (e DB) MustInsert(value interface{}) sql.Result

MustInsert is the same as Insert but panics if cannot insert.

func (DB) MustNamedExec

func (e DB) MustNamedExec(query string, arg map[string]interface{}) sql.Result

MustNamedExec is the same as NamedExec but panics if cannot insert.

func (DB) MustNamedGet

func (e DB) MustNamedGet(dest interface{}, query string, arg map[string]interface{}) bool

MustNamedGet is the same as NamedGet, if there is a error in query, it will panics, but sql.ErrNoRows will return false

func (DB) MustNamedQuery

func (e DB) MustNamedQuery(dest interface{}, query string, arg map[string]interface{})

MustNamedQuery like NamedQuery but panics if cannot query.

func (DB) MustQuery

func (e DB) MustQuery(dest interface{}, query string, args ...interface{})

MustQuery is the same as Query but panics if cannot query.

func (DB) MustUpdate

func (e DB) MustUpdate(value interface{}, columns ...string) (rs sql.Result)

MustUpdate is the same as Update, but panics if cannot update.

func (DB) NamedExec

func (e DB) NamedExec(query string, arg map[string]interface{}) (sql.Result, error)

NamedExec executes a Named query without returning any rows. The arg are for any placeholder parameters in the query.

func (DB) NamedExecContext

func (e DB) NamedExecContext(ctx context.Context, query string, arg map[string]interface{}) (sql.Result, error)

NamedExecContext executes a Named query without returning any rows. The arg are for any placeholder parameters in the query.

func (DB) NamedGet

func (e DB) NamedGet(dest interface{}, query string, arg map[string]interface{}) (err error)

NamedGet execute the Named query and scan the first row to dest, dest must be a pointer. if dest is a type supported by the database (string , int, []byte, time.Time, etc.) and there is only one column, the row will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column. A sql.ErrNoRows is returned if the result set is empty.

func (DB) NamedGetContext

func (e DB) NamedGetContext(ctx context.Context, dest interface{}, query string, arg map[string]interface{}) (err error)

NamedGetContext execute the Named query and scan the first row to dest, dest must be a pointer. if dest is a type supported by the database (string , int, []byte, time.Time, etc.) and there is only one column, the row will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column. A sql.ErrNoRows is returned if the result set is empty.

func (DB) NamedQuery

func (e DB) NamedQuery(dest interface{}, query string, arg map[string]interface{}) (err error)

NamedQuery execute the query and scan the rows to dest, dest must be a slice of pointer. if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.) and there is only one column, the rows will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.

func (DB) NamedQueryContext

func (e DB) NamedQueryContext(ctx context.Context, dest interface{}, query string, arg map[string]interface{}) (err error)

NamedQueryContext execute the query and scan the rows to dest, dest must be a slice of pointer. if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.) and there is only one column, the rows will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.

func (*DB) Options

func (d *DB) Options() *Options

func (DB) Prepare

func (e DB) Prepare(query string) (*Stmt, error)

Prepare creates a prepared statement

func (DB) PrepareContext

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

PrepareContext creates a prepared statement

func (DB) Query

func (e DB) Query(dest interface{}, query string, args ...interface{}) (err error)

Query execute the query and scan the rows to dest, dest must be a slice of pointer. if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.) and there is only one column, the rows will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.

func (DB) QueryContext

func (e DB) QueryContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error)

QueryContext execute the query and scan the rows to dest, dest must be a slice of pointer. if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.) and there is only one column, the rows will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.

func (*DB) RawDB

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

RawDB return a raw sql.DB object

func (*DB) Transaction

func (d *DB) Transaction(fn func(*Tx) error) error

Transaction begin a transaction and commit automatically,automatically roll back when there is an error.

func (DB) Update

func (e DB) Update(value interface{}, columns ...string) (sql.Result, error)

Update the rows according to the value of structure, if the column name is specified, only the specified column is updated.

func (DB) UpdateContext

func (e DB) UpdateContext(ctx context.Context, value interface{}, columns ...string) (rs sql.Result, err error)

UpdateContext update the rows according to the value of structure, if the column name is specified, only the specified column is updated.

type Executor

type Executor interface {
	// NamedExecContext executes a Named query without returning any rows.
	// The arg are for any placeholder parameters in the query.
	NamedExecContext(ctx context.Context, query string, arg map[string]interface{}) (sql.Result, error)

	// NamedExec executes a Named query without returning any rows.
	// The arg are for any placeholder parameters in the query.
	NamedExec(query string, arg map[string]interface{}) (sql.Result, error)

	// MustNamedExec is the same as  NamedExec but panics if cannot insert.
	MustNamedExec(query string, arg map[string]interface{}) sql.Result

	// NamedGetContext execute the Named query and scan the first row to dest, dest must be a pointer.
	// if dest is a type supported by the database (string , int, []byte, time.Time, etc.)
	// and there is only one column, the row will be assigned to dest.
	// if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.
	// A sql.ErrNoRows is returned if the result set is empty.
	NamedGetContext(ctx context.Context, dest interface{}, query string, arg map[string]interface{}) error

	// NamedGet execute the Named query and scan the first row to dest, dest must be a pointer.
	// if dest is a type supported by the database (string , int, []byte, time.Time, etc.)
	// and there is only one column, the row will be assigned to dest.
	// if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.
	// A sql.ErrNoRows is returned if the result set is empty.
	NamedGet(dest interface{}, query string, arg map[string]interface{}) error

	// MustNamedGet is the same as NamedGet, if there is a error in query, it will panics,
	// but sql.ErrNoRows will return false
	MustNamedGet(dest interface{}, query string, arg map[string]interface{}) bool

	// NamedQueryContext execute the query and scan the rows to dest, dest must be a slice of pointer.
	// if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.)
	// and there is only one column, the rows will be assigned to dest.
	// if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.
	NamedQueryContext(ctx context.Context, dest interface{}, query string, arg map[string]interface{}) error

	// NamedQuery execute the query and scan the rows to dest, dest must be a slice of pointer.
	// if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.)
	// and there is only one column, the rows will be assigned to dest.
	// if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.
	NamedQuery(dest interface{}, query string, arg map[string]interface{}) error

	// MustNamedQuery like NamedQuery but panics if cannot query.
	MustNamedQuery(dest interface{}, query string, arg map[string]interface{})

	// PrepareContext creates a prepared statement
	PrepareContext(ctx context.Context, query string) (*Stmt, error)

	// Prepare creates a prepared statement
	Prepare(query string) (*Stmt, error)

	// ExecContext executes a query without returning any rows.
	// The args are for any placeholder parameters in the query.
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

	// Exec executes a query without returning any rows.
	// The args are for any placeholder parameters in the query.
	Exec(query string, args ...interface{}) (sql.Result, error)

	// MustExec is the same as  Exec but panics if cannot insert.
	MustExec(query string, args ...interface{}) sql.Result

	// GetContext execute the query and scan the first row to dest, dest must be a pointer.
	// if dest is a type supported by the database (string , int, []byte, time.Time, etc.)
	// and there is only one column, the row will be assigned to dest.
	// if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.
	// A sql.ErrNoRows is returned if the result set is empty.
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error)

	// Get execute the query and scan the first row to dest, dest must be a pointer.
	// If dest is a type supported by the database (string , int, []byte, time.Time, etc.)
	// and there is only one column, the row will be assigned to dest.
	// If dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.
	// A sql.ErrNoRows is returned, if the query selects no rows.
	Get(dest interface{}, query string, args ...interface{}) (err error)

	// MustGet is the same as Get, if there is a error in query, it will panics,
	// but sql.ErrNoRows will return false
	MustGet(dest interface{}, query string, args ...interface{}) bool

	// QueryContext execute the query and scan the rows to dest, dest must be a slice of pointer.
	// if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.)
	// and there is only one column, the rows will be assigned to dest.
	// if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.
	QueryContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error)

	// Query execute the query and scan the rows to dest, dest must be a slice of pointer.
	// if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.)
	// and there is only one column, the rows will be assigned to dest.
	// if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.
	Query(dest interface{}, query string, args ...interface{}) (err error)

	// MustQuery is the same as Query but panics if cannot query.
	MustQuery(dest interface{}, query string, args ...interface{})

	// InsertContext insert a struct to database
	InsertContext(ctx context.Context, value interface{}) (rs sql.Result, err error)

	// Insert a struct to database
	Insert(value interface{}) (sql.Result, error)

	// MustInsert is the same as Insert but panics if cannot insert.
	MustInsert(value interface{}) sql.Result

	// UpdateContext update the rows according to the value of structure, if the column name is specified,
	// only the specified column is updated.
	UpdateContext(ctx context.Context, value interface{}, columns ...string) (rs sql.Result, err error)

	// Update the rows according to the value of structure, if the column name is specified,
	// only the specified column is updated.
	Update(value interface{}, columns ...string) (sql.Result, error)

	// MustUpdate is the same as Update, but panics if cannot update.
	MustUpdate(value interface{}, columns ...string) (rs sql.Result)
}

type Logger

type Logger interface {
	Printf(sql string)
}

type Options

type Options struct {
	Logger     Logger
	Generator  SQLGenerator
	Location   *time.Location
	TimeFormat func(t *time.Time) string
}

type SQLGenerator

type SQLGenerator interface {
	UpdateSQL(value interface{}, columns ...string) (query string, args []interface{}, err error)
	InsertSQL(value interface{}) (autoIncrement *reflect.Value, query string, args []interface{}, err error)
}

type Stmt

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

Stmt is a preparer statement. it is like sql.Stmt.

func (*Stmt) Close

func (s *Stmt) Close() error

func (*Stmt) Exec

func (s *Stmt) Exec() (sql.Result, error)

func (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)

func (*Stmt) Get

func (s *Stmt) Get(dest interface{}, args ...interface{}) error

func (*Stmt) GetContext

func (s *Stmt) GetContext(ctx context.Context, dest interface{}, args ...interface{}) error

func (*Stmt) Query

func (s *Stmt) Query(dest interface{}, args ...interface{}) error

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, dest interface{}, args ...interface{}) error

type Table

type Table interface {
	TableName() string
}

Table is a interface with TableName

type Tx

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

func (*Tx) Commit

func (t *Tx) Commit() error

Commit the transaction

func (Tx) Exec

func (e Tx) Exec(query string, args ...interface{}) (rs sql.Result, err error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (Tx) ExecContext

func (e Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (rs sql.Result, err error)

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (Tx) Get

func (e Tx) Get(dest interface{}, query string, args ...interface{}) (err error)

Get execute the query and scan the first row to dest, dest must be a pointer. if dest is a type supported by the database (string , int, []byte, time.Time, etc.) and there is only one column, the row will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column. An sql.ErrNoRows is returned if the result set is empty.

func (Tx) GetContext

func (e Tx) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error)

GetContext execute the query and scan the first row to dest, dest must be a pointer. if dest is a type supported by the database (string , int, []byte, time.Time, etc.) and there is only one column, the row will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column. An sql.ErrNoRows is returned if the result set is empty.

func (Tx) Insert

func (e Tx) Insert(value interface{}) (sql.Result, error)

Insert a struct to database

func (Tx) InsertContext

func (e Tx) InsertContext(ctx context.Context, value interface{}) (rs sql.Result, err error)

InsertContext insert a struct to database

func (Tx) MustExec

func (e Tx) MustExec(query string, args ...interface{}) (rs sql.Result)

MustExec is the same as Exec but panics if cannot insert.

func (Tx) MustGet

func (e Tx) MustGet(dest interface{}, query string, args ...interface{}) bool

MustGet is the same as Get, if there is a error in query, it will panics, but sql.ErrNoRows will return false

func (Tx) MustInsert

func (e Tx) MustInsert(value interface{}) sql.Result

MustInsert is the same as Insert but panics if cannot insert.

func (Tx) MustNamedExec

func (e Tx) MustNamedExec(query string, arg map[string]interface{}) sql.Result

MustNamedExec is the same as NamedExec but panics if cannot insert.

func (Tx) MustNamedGet

func (e Tx) MustNamedGet(dest interface{}, query string, arg map[string]interface{}) bool

MustNamedGet is the same as NamedGet, if there is a error in query, it will panics, but sql.ErrNoRows will return false

func (Tx) MustNamedQuery

func (e Tx) MustNamedQuery(dest interface{}, query string, arg map[string]interface{})

MustNamedQuery like NamedQuery but panics if cannot query.

func (Tx) MustQuery

func (e Tx) MustQuery(dest interface{}, query string, args ...interface{})

MustQuery is the same as Query but panics if cannot query.

func (Tx) MustUpdate

func (e Tx) MustUpdate(value interface{}, columns ...string) (rs sql.Result)

MustUpdate is the same as Update, but panics if cannot update.

func (Tx) NamedExec

func (e Tx) NamedExec(query string, arg map[string]interface{}) (sql.Result, error)

NamedExec executes a Named query without returning any rows. The arg are for any placeholder parameters in the query.

func (Tx) NamedExecContext

func (e Tx) NamedExecContext(ctx context.Context, query string, arg map[string]interface{}) (sql.Result, error)

NamedExecContext executes a Named query without returning any rows. The arg are for any placeholder parameters in the query.

func (Tx) NamedGet

func (e Tx) NamedGet(dest interface{}, query string, arg map[string]interface{}) (err error)

NamedGet execute the Named query and scan the first row to dest, dest must be a pointer. if dest is a type supported by the database (string , int, []byte, time.Time, etc.) and there is only one column, the row will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column. A sql.ErrNoRows is returned if the result set is empty.

func (Tx) NamedGetContext

func (e Tx) NamedGetContext(ctx context.Context, dest interface{}, query string, arg map[string]interface{}) (err error)

NamedGetContext execute the Named query and scan the first row to dest, dest must be a pointer. if dest is a type supported by the database (string , int, []byte, time.Time, etc.) and there is only one column, the row will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column. A sql.ErrNoRows is returned if the result set is empty.

func (Tx) NamedQuery

func (e Tx) NamedQuery(dest interface{}, query string, arg map[string]interface{}) (err error)

NamedQuery execute the query and scan the rows to dest, dest must be a slice of pointer. if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.) and there is only one column, the rows will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.

func (Tx) NamedQueryContext

func (e Tx) NamedQueryContext(ctx context.Context, dest interface{}, query string, arg map[string]interface{}) (err error)

NamedQueryContext execute the query and scan the rows to dest, dest must be a slice of pointer. if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.) and there is only one column, the rows will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.

func (Tx) Prepare

func (e Tx) Prepare(query string) (*Stmt, error)

Prepare creates a prepared statement

func (Tx) PrepareContext

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

PrepareContext creates a prepared statement

func (Tx) Query

func (e Tx) Query(dest interface{}, query string, args ...interface{}) (err error)

Query execute the query and scan the rows to dest, dest must be a slice of pointer. if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.) and there is only one column, the rows will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.

func (Tx) QueryContext

func (e Tx) QueryContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error)

QueryContext execute the query and scan the rows to dest, dest must be a slice of pointer. if dest is a type supported by the database ([]string , []int, []byte, []time.Time, etc.) and there is only one column, the rows will be assigned to dest. if dest is a struct, it will be mapped to the dbx tag field in the struct according to the name of each column.

func (*Tx) Rollback

func (t *Tx) Rollback() error

Rollback the transaction

func (Tx) Update

func (e Tx) Update(value interface{}, columns ...string) (sql.Result, error)

Update the rows according to the value of structure, if the column name is specified, only the specified column is updated.

func (Tx) UpdateContext

func (e Tx) UpdateContext(ctx context.Context, value interface{}, columns ...string) (rs sql.Result, err error)

UpdateContext update the rows according to the value of structure, if the column name is specified, only the specified column is updated.

Directories

Path Synopsis
internal/module
Code generated by dbx-generator.
Code generated by dbx-generator.
internal

Jump to

Keyboard shortcuts

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