mysqlx

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2019 License: BSD-3-Clause Imports: 14 Imported by: 1

README

mysqlx

Build status report Latest License

More Badages

Supported Go version

  • Go 1.11
  • Go 1.12
  • Go 1.13

Usage

// TODO

Supported MySQL data types

  • Signed Integers:
    • bigint(n), int(n), smallint(n), tinyint(n)
  • Unsigned Integers:
    • bigint(n) unsigned: Should be configured as ubigint(n)
    • int(n) unsigned: Should be configured as uint(n)
    • smallint(n) unsigned: Should be configured as usmallint(n)
    • tinyint(n) unsigned: Should be configured as utinyint(n)
  • Date / Time Types:
    • timestamp
    • datetime, datetime(n)
    • time, time(n)
    • date
    • year

Notes

Error sql: **** unsupported Scan, storing driver.Value type []uint8 into type *time.Time

reference: Stackoverflow

This is because sqlx does not parse *time.Time by default. Add "parseTime=true" parameter then opening MySQL with sqlx.

References for Travis CI yaml writting

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cond

type Cond struct {
	Param    string
	Operator string
	Value    interface{}
}

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

func New(db *sqlx.DB) (ret *DB, err error)

New initialize a *DB object with a given *sqlx.DB, which should be connect a certain database.

func Open

func Open(param Param) (ret *DB, err error)

Open initialize a *DB object with a valid *sqlx.DB

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) CreateTable

func (d *DB) CreateTable(v interface{}, opts ...Options) error

CreateTable creates a table if not exist. If the table exists, it will alter it if necessary

func (*DB) Database

func (d *DB) Database() string

Database returns database name in DB

func (*DB) Delete

func (d *DB) Delete(prototype interface{}, args ...interface{}) (sql.Result, error)

Delete executes SQL DELETE statement with given conditions

func (*DB) Insert

func (d *DB) Insert(v interface{}, opts ...Options) (result sql.Result, err error)

Insert insert a given structure. auto-increment fields will be ignored

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) KeepAlive

func (d *DB) KeepAlive()

KeepAlive automatically keeps alive with database

func (*DB) MustCreateTable

func (d *DB) MustCreateTable(v interface{}, opts ...Options)

MustCreateTable is same as CreateTable. But is panics if error.

func (*DB) ReadStructFields

func (d *DB) ReadStructFields(s interface{}) (ret []*Field, err error)

ReadStructFields returns all valid SQL fields by given structure and will buffer it

func (*DB) ReadTableFields

func (d *DB) ReadTableFields(table string) (ret []*Field, err error)

ReadTableFields returns all fields in given table

func (*DB) ReadTableIndexes

func (d *DB) ReadTableIndexes(table string) (map[string]*Index, map[string]*Unique, error)

ReadTableIndexes returns all indexes and uniques of given table name

func (*DB) Select

func (d *DB) Select(dst interface{}, args ...interface{}) error

Select execute a SQL select statement

func (*DB) SelectFields

func (d *DB) SelectFields(s interface{}) (string, error)

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) Sqlx

func (d *DB) Sqlx() *sqlx.DB

Sqlx return the *sqlx.DB object

func (*DB) StopKeepAlive

func (d *DB) StopKeepAlive()

StopKeepAlive stops the keep-alive operation

func (*DB) StructFields

func (*DB) StructFields(s interface{}) (ret []*Field, err error)

StructFields is the same as ReadStructFields

func (*DB) Update

func (d *DB) Update(prototype interface{}, fields map[string]interface{}, args ...interface{}) (sql.Result, error)

Update execute UPDATE SQL statement with given structure and conditions

type Field

type Field struct {
	Name          string
	Type          string
	Nullable      bool
	Default       string
	Comment       string
	AutoIncrement bool
	// contains filtered or unexported fields
}

Field shows information of a field

func ReadStructFields

func ReadStructFields(s interface{}) (ret []*Field, err error)

ReadStructFields is the same as StructFields

func StructFields

func StructFields(s interface{}) (ret []*Field, err error)

StructFields returns all valid SQL fields by given structure

type Index

type Index struct {
	Name   string
	Fields []string
}

Index shows the information of an index setting

func (*Index) Check

func (i *Index) Check() error

Check checks if an index object is valid

type Limit

type Limit struct {
	Limit int
}

Limit is for MySQL limit statement

type Offset

type Offset struct {
	Offset int
}

Offset is for MySQL offset statement

type Options

type Options struct {
	TableName      string
	TableDescption string
	Indexes        []Index
	Uniques        []Unique
}

Options identifies options and parameters for a structure

type Order

type Order struct {
	Param string
	Seq   string
}

Order is for MySQL ORDER BY statement

type Param

type Param struct {
	Host   string
	Port   int
	User   string
	Pass   string
	DBName string
}

Param identifies connect parameters to a database

type Unique

type Unique struct {
	Name   string
	Fields []string
}

Unique shows the information of a unique setting

func (*Unique) Check

func (u *Unique) Check() error

Check checks if an unique object is valid

Jump to

Keyboard shortcuts

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