Documentation ¶
Index ¶
- func HasPK(fields []*Field) bool
- func ReadTableColumns(any interface{}) ([]string, error)
- func ReadTableName(any interface{}) (string, string)
- func ResolveReadParams(params []interface{}) (string, []interface{}, error)
- func SQLTableNameOf(any interface{}) string
- func SetDefaultPK(fields []*Field)
- type Client
- type DB
- func (db *DB) Begin(ctx context.Context) (*Tx, error)
- func (db *DB) CheckIfTableExists(name string) bool
- func (db *DB) Create(record interface{}) error
- func (db *DB) CreateAndGetResult(record interface{}) (stdsql.Result, error)
- func (db *DB) CreateAndRead(record interface{}) error
- func (db *DB) CreateTable(st interface{}, ifexists bool) error
- func (db *DB) CreateTables(structs ...interface{}) error
- func (db *DB) Delete(record interface{}) error
- func (db *DB) DropTable(st interface{}, ifexists bool) error
- func (db *DB) DropTables(structs ...interface{}) error
- func (db *DB) Exec(sql string, params ...interface{}) (stdsql.Result, error)
- func (db *DB) Ping() error
- func (db *DB) Query(sql string, params ...interface{}) (*stdsql.Rows, error)
- func (db *DB) Read(scanTo interface{}, params ...interface{}) error
- func (db *DB) ResetTables(structs ...interface{}) error
- func (db *DB) Update(record interface{}) error
- func (db *DB) WithContext(ctx context.Context) *WithContext
- type ExecFn
- type Field
- type FieldIteration
- func (iteration *FieldIteration) IsEmbeddedStruct() bool
- func (iteration *FieldIteration) Name() string
- func (iteration *FieldIteration) Next() bool
- func (iteration *FieldIteration) SQLOptions() (*sql.Options, error)
- func (iteration *FieldIteration) TypeField() reflect.StructField
- func (iteration *FieldIteration) Value() interface{}
- func (iteration *FieldIteration) ValueField() reflect.Value
- type QueryFn
- type Row
- type RowValue
- type Scan
- type Table
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) Create(record interface{}) error
- func (tx *Tx) CreateAndRead(record interface{}) error
- func (tx *Tx) Delete(record interface{}) error
- func (tx *Tx) Exec(sql string, params ...interface{}) (stdsql.Result, error)
- func (tx *Tx) Query(sql string, params ...interface{}) (*stdsql.Rows, error)
- func (tx *Tx) Read(scanTo interface{}, params ...interface{}) error
- func (tx *Tx) Rollback() error
- func (tx *Tx) Update(record interface{}) error
- type WithContext
- func (ctx *WithContext) Create(record interface{}) error
- func (ctx *WithContext) CreateAndRead(record interface{}) error
- func (ctx *WithContext) Delete(record interface{}) error
- func (ctx *WithContext) Exec(sql string, params ...interface{}) (stdsql.Result, error)
- func (ctx *WithContext) Query(sql string, params ...interface{}) (*stdsql.Rows, error)
- func (ctx *WithContext) Read(scanTo interface{}, params ...interface{}) error
- func (ctx *WithContext) Update(record interface{}) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadTableColumns ¶
Return table columns for given struct, pointer to struct or slice of structs.
func ReadTableName ¶
Return struct name and SQL table name
func ResolveReadParams ¶
Arguments of the common CRUD functions can start with a query in string type, followed by parameters for the query itself. ResolveReadParams takes a list of any type, returns a query as a string type, parameters as a slice of interface, and potentially an error the last parameter.
func SQLTableNameOf ¶
func SQLTableNameOf(any interface{}) string
Find out what given interface should be called in the database. It first looks up if a table name was explicitly specified (see "table-name" option), or automatically generates a plural name from the name of the struct type.
func SetDefaultPK ¶
func SetDefaultPK(fields []*Field)
If no PK is specified, then set `id` to be PK.
Types ¶
type DB ¶
func Connect ¶
Establish DB connection and return a crud.DB instance w/ methods needed for accessing / writing the database. Example call: Connect("mysql", "root:123456@tcp(localhost:3306)/database_name?parseTime=true")
func (*DB) Begin ¶
Start a DB transaction. It returns an interface w/ most of the methods DB provides.
func (*DB) CheckIfTableExists ¶
Runs a query to check if the given table exists and returns bool
func (*DB) CreateAndGetResult ¶
func (*DB) CreateAndRead ¶
Inserts given record and scans the inserted row back to the given row.
func (*DB) CreateTable ¶
Takes any valid struct and creates a SQL table from it.
func (*DB) CreateTables ¶
Creates multiple tables from given any amount of structs. Calls `CreateTable` internally.
func (*DB) Delete ¶
Generates and executes a DELETE query for given struct record. It matches the database row by finding out the primary key field defined in the table schema.
func (*DB) DropTables ¶
Drops correspoinding SQL tables of the given structs.
func (*DB) Exec ¶
Run any query on the database client, passing parameters optionally. Returns sql.Result.
func (*DB) Query ¶
Run any query on the database client, passing parameters optionally. Its difference with `Exec` method is returning `sql.Rows` instead of `sql.Result`.
func (*DB) ResetTables ¶
Drops (if they exist) and re-creates corresponding SQL tables for the given structs.
func (*DB) Update ¶
Finding out the primary-key field of the given row, updates the corresponding record on the table with the values in the given record.
func (*DB) WithContext ¶
func (db *DB) WithContext(ctx context.Context) *WithContext
Return a database client that wraps underlying SQL execution methods with the context specified
type Field ¶
func GetFieldsOf ¶
Get DB fields of any valid struct given
type FieldIteration ¶
type FieldIteration struct { Index int Length int ReflectValue reflect.Value ReflectType reflect.Type }
func NewFieldIteration ¶
func NewFieldIteration(st interface{}) *FieldIteration
Take any kind of struct and return a FieldIteration instance which helps walking the fields of the given struct one by one reading its name, value and SQL options
func (*FieldIteration) IsEmbeddedStruct ¶
func (iteration *FieldIteration) IsEmbeddedStruct() bool
func (*FieldIteration) Name ¶
func (iteration *FieldIteration) Name() string
func (*FieldIteration) Next ¶
func (iteration *FieldIteration) Next() bool
func (*FieldIteration) SQLOptions ¶
func (iteration *FieldIteration) SQLOptions() (*sql.Options, error)
func (*FieldIteration) TypeField ¶
func (iteration *FieldIteration) TypeField() reflect.StructField
func (*FieldIteration) Value ¶
func (iteration *FieldIteration) Value() interface{}
func (*FieldIteration) ValueField ¶
func (iteration *FieldIteration) ValueField() reflect.Value
type RowValue ¶
type RowValue struct { SQLColumn string Value interface{} }
func GetRowValuesOf ¶
Scans given struct record and returns a list of crud.Row instances for each struct field. It's useful for extracting values and corresponding SQL meta information from structs representing database tables.
type Scan ¶
type Scan struct { To interface{} ToPointers bool ToStructs bool SQLColumnDict map[string]string Table *Table }
type Table ¶
func NewTable ¶
Create an internal representation of a database table, including its fields from given struct record
func (*Table) PrimaryKeyField ¶
func (*Table) SQLColumnDict ¶
func (*Table) SQLOptions ¶
func (*Table) SQLUpdateColumnSet ¶
func (*Table) SQLUpdateValueSet ¶
func (table *Table) SQLUpdateValueSet() []interface{}
type Tx ¶
func (*Tx) CreateAndRead ¶
Inserts given record and scans the inserted row back to the given row.
func (*Tx) Delete ¶
Executes a DELETE query on the transaction for given struct record. It matches the database row by finding out the primary key field defined in the table schema.
func (*Tx) Read ¶
Run a select query on the databaase (w/ given parameters optionally) and scan the result(s) to the target interface specified as the first parameter.
Usage Example:
user := &User{} err := tx.Read(user, "SELECT * FROM users WHERE id = ?", 1)
users := &[]*User{} err := tx.Read(users, "SELECT * FROM users", 1)
type WithContext ¶
func (*WithContext) Create ¶
func (ctx *WithContext) Create(record interface{}) error
Insert given record to the database.
func (*WithContext) CreateAndRead ¶
func (ctx *WithContext) CreateAndRead(record interface{}) error
Inserts given record and scans the inserted row back to the given row.
func (*WithContext) Delete ¶
func (ctx *WithContext) Delete(record interface{}) error
Executes a DELETE query on the transaction for given struct record. It matches the database row by finding out the primary key field defined in the table schema.
func (*WithContext) Exec ¶
func (ctx *WithContext) Exec(sql string, params ...interface{}) (stdsql.Result, error)
Execute any SQL query on the context client. Returns sql.Result.
func (*WithContext) Query ¶
func (ctx *WithContext) Query(sql string, params ...interface{}) (*stdsql.Rows, error)
Execute any SQL query on the context client. Returns sql.Rows.
func (*WithContext) Read ¶
func (ctx *WithContext) Read(scanTo interface{}, params ...interface{}) error
Run a select query on the databaase (w/ given parameters optionally) and scan the result(s) to the target interface specified as the first parameter.
func (*WithContext) Update ¶
func (ctx *WithContext) Update(record interface{}) error
Run an update query on the transaction, finding out the primary-key field of the given row.