Documentation ¶
Index ¶
- func GetQueryFromError(e error) string
- type And
- type Cond
- type DB
- func (d *DB) AutoCreateTable()
- func (d *DB) CreateOrAlterTableStatements(v interface{}, opts ...Options) (exists bool, statements []string, err error)
- func (d *DB) CreateTable(v interface{}, opts ...Options) error
- func (d *DB) CurrentDatabase() (string, error)
- func (d *DB) Database() string
- func (d *DB) Delete(prototype interface{}, args ...interface{}) (sql.Result, error)
- func (d *DB) Insert(v interface{}, opts ...Options) (result sql.Result, err error)
- func (d *DB) InsertFields(s interface{}, backQuoted bool) (keys []string, values []string, err error)
- func (d *DB) InsertIfNotExists(insert interface{}, conds ...interface{}) (res sql.Result, err error)
- func (d *DB) InsertMany(records interface{}, opts ...Options) (result sql.Result, err error)
- func (d *DB) InsertOnDuplicateKeyUpdate(v interface{}, updates map[string]interface{}, opts ...Options) (result sql.Result, err error)
- func (d *DB) KeepAlive()
- func (d *DB) MustCreateTable(v interface{}, opts ...Options)
- func (d *DB) ReadStructFields(s interface{}) (ret []*Field, err error)
- func (d *DB) ReadTableFields(table string) (ret []*Field, err error)
- func (d *DB) ReadTableIndexes(table string) (map[string]*Index, map[string]*Unique, error)
- func (d *DB) Select(dst interface{}, args ...interface{}) error
- func (d *DB) SelectFields(s interface{}) (string, error)
- func (d *DB) SelectOrInsert(insert interface{}, selectResult interface{}, conds ...interface{}) (res sql.Result, err error)
- func (d *DB) Sqlx() *sqlx.DB
- func (d *DB) StopKeepAlive()
- func (*DB) StructFields(s interface{}) (ret []*Field, err error)
- func (d *DB) Update(prototype interface{}, fields map[string]interface{}, args ...interface{}) (sql.Result, error)
- type Error
- type Field
- type Index
- type Limit
- type Offset
- type Options
- type Or
- type Order
- type Param
- type RawStatement
- type Unique
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetQueryFromError ¶ added in v0.3.0
GetQueryFromError fetch SQL query statements in returned error type by mysqlx.
Types ¶
type And ¶ added in v0.2.0
type And []interface{}
And packages conditions with AND logic. Only Cond, *Cond, Or, And types are acceptable in Conds slice.
type Cond ¶
Cond is for constructing MySQL WHERE statement
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is the main structure for mysqlx
func New ¶
New initialize a *DB object with a given *sqlx.DB, which should be connect a certain database.
func (*DB) AutoCreateTable ¶
func (d *DB) AutoCreateTable()
AutoCreateTable enables auto table creation. DB will check if table is created previously before each access to the DB.
Please do NOT invoke this unless you need to automatically create table in runtime.
Note 1: if a table was created once by mysqlx.DB after it was initialized, it will be noted as "created" and cached. Then mysqlx.DB will not check into MySQL DB again.
Note 2: auto-table-creation will NOT be activated in Select() function!
func (*DB) CreateOrAlterTableStatements ¶ added in v0.2.0
func (d *DB) CreateOrAlterTableStatements(v interface{}, opts ...Options) (exists bool, statements []string, err error)
CreateOrAlterTableStatements returns 'CREATE TABLE ... IF NOT EXISTS ...' or 'ALTER TABLE ...' statements, but will not execute them. If the table does not exists, 'CREATE TABLE ...' statement will be returned. If the table exists and needs no alteration, an empty string slice would be returned. Otherwise, a string slice with 'ALTER TABLE ...' statements would be returned.
The returned exists identifies if the table exists in database.
func (*DB) CreateTable ¶
CreateTable creates a table if not exist. If the table exists, it will alter it if necessary
func (*DB) CurrentDatabase ¶ added in v0.2.0
CurrentDatabase gets current operating database
func (*DB) InsertFields ¶
func (d *DB) InsertFields(s interface{}, backQuoted bool) (keys []string, values []string, err error)
InsertFields return keys and values for inserting. Auto-increment fields will be ignored
func (*DB) InsertIfNotExists ¶
func (d *DB) InsertIfNotExists(insert interface{}, conds ...interface{}) (res sql.Result, err error)
InsertIfNotExists is the same as SelectOrInsert but lacking select statement
func (*DB) InsertMany ¶ added in v0.3.0
InsertMany insert multiple records into table. If additional option with table name is not given, mysqlx will use the FIRST table name in records for all.
func (*DB) InsertOnDuplicateKeyUpdate ¶ added in v0.3.0
func (d *DB) InsertOnDuplicateKeyUpdate( v interface{}, updates map[string]interface{}, opts ...Options, ) (result sql.Result, err error)
InsertOnDuplicateKeyUpdate executes 'INSERT ... ON DUPLICATE KEY UPDATE ...' statements. This function is a combination of Insert and Update, without WHERE conditions.
func (*DB) MustCreateTable ¶
MustCreateTable is same as CreateTable. But is panics if error.
func (*DB) ReadStructFields ¶
ReadStructFields returns all valid SQL fields by given structure and will buffer it
func (*DB) ReadTableFields ¶
ReadTableFields returns all fields in given table
func (*DB) ReadTableIndexes ¶
ReadTableIndexes returns all indexes and uniques of given table name
func (*DB) SelectFields ¶
SelectFields returns all valid SQL fields in given structure
func (*DB) SelectOrInsert ¶
func (d *DB) SelectOrInsert(insert interface{}, selectResult interface{}, conds ...interface{}) (res sql.Result, err error)
SelectOrInsert executes update-if-not-exists statement
func (*DB) StopKeepAlive ¶
func (d *DB) StopKeepAlive()
StopKeepAlive stops the keep-alive operation
func (*DB) StructFields ¶
StructFields is the same as ReadStructFields
type Error ¶ added in v0.3.0
type Error struct {
// contains filtered or unexported fields
}
Error is the error type identified by mysqlx
type Field ¶
type Field struct { Name string Type string Nullable bool Default string Comment string AutoIncrement bool OnUpdate string // contains filtered or unexported fields }
Field shows information of a field
func ReadStructFields ¶
ReadStructFields is the same as StructFields
func StructFields ¶
StructFields returns all valid SQL fields by given structure
type Options ¶
type Options struct { // TableName defines the table name of this object TableName string // TableDescption defines the description of the table, it is used in create table statement TableDescption string // Indexes defines the indexes of the table Indexes []Index // Uniques defines the uniques of the table Uniques []Unique // CreateTableParams defines additional variables in create table statements. // There are three default variaments, which could be replaced: // ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 CreateTableParams map[string]string // DoNotExec stop the actual database executing process if it is set as true. Instead, CURD // functions would return an Error object with SQL query statement. This could used for troubleshot. // Please use GetQueryFromError() function to get the query statement. DoNotExec bool }
Options identifies options and parameters for a structure
type Or ¶ added in v0.2.0
type Or []interface{}
Or packages conditions with OR logic. Only Cond, *Cond, Or, And types are acceptable in the slice.
type RawStatement ¶ added in v0.3.0
type RawStatement string
RawStatement identifies raw MySQL statement, which will be directly added into sql statements. Now RawStatement is available in Update function.