# README
mysqlx
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 asubigint(n)
int(n) unsigned
: Should be configured asuint(n)
smallint(n) unsigned
: Should be configured asusmallint(n)
tinyint(n) unsigned
: Should be configured asutinyint(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.
Changelog
Simple Benchmask Test Result
Please refer to benchmark test file. Temporally only SELECT
is tested. Several conditions among mysqlx, sqlx and gorm(v1) are tested.
Benchmark test statement: go test -bench=. -run=none -benchmem -benchtime=10s
. Test table sise: 100000
.
Select by main key
Select a record by auto-increment main key. This is the most basic way to reading record. We use statements for each package conditions:
- mysqlx:
db.Select(&res, mysqlx.Condition("id", "=", id))
- sqlx (with "= 1234"):
db.Select(&res, "SELECT * FROM t_student WHERE id=" + idStr)
- sqlx (with "= ?"):
db.Select(&res, "SELECT * FROM t_student WHERE id=?", id)
- gorm (with 'Find' function):
d.Where("id = ?", id+1).Find(&res)
- gorm (with 'First' function):
d.First(&res, id)
Package | nanoseconds/op | bytes/op | allocs/op |
---|---|---|---|
mysqlx | 1,038,348 | 1696 | 37 |
sqlx (with "= 1234") | 1,115,127 | 1039 | 18 |
sqlx (with "= ?") | 2,112,185 | 1247 | 26 |
gorm (with 'Find' function) | 2,256,562 | 6641 | 105 |
gorm (with 'First' function) | 1,114,290 | 4295 | 97 |
Select By A VARCHAR Field
One of the t_student
field is generated by uuid. We use statements for each packages:
- mysqlx:
db.Select(&res, Condition("name", "=", name))
- sqlx (with "= 'Alice'"):
db.Select(&res, "SELECT * FROM t_student WHERE name='" + name + "'")
- sqlx (with "= ?"):
db.Select(&res, "SELECT * FROM t_student WHERE name=?", name)
- gorm (with 'Find' function):
d.Where("name = ?", name).Find(&res)
- gorm (with 'First' function):
d.Where("name = ?", name).First(&res)
Package | nanoseconds/op | bytes/op | allocs/op |
---|---|---|---|
mysqlx | 1,247,630 | 1848 | 37 |
sqlx (with "= 'Alice'") | 1,146,627 | 1064 | 18 |
sqlx (with "= ?") | 2,023,415 | 1240 | 25 |
gorm (with 'Find' function) | 2,073,272 | 6625 | 104 |
gorm (with 'First' function) | 2,207,229 | 5377 | 116 |
# Packages
Package structs is a simple test package for mysqlx.
# Functions
Condition return Cond data in function format.
Errorf generates an formatted error object.
GetQueryFromError fetch SQL query statements in returned error type by mysqlx.
Like return Cond data with LIKE operator in function format.
New initialize a *DB object with a given *sqlx.DB, which should be connect a certain database.
Open initialize a *DB object with a valid *sqlx.DB.
ReadStructFields is the same as StructFields.
StructFields returns all valid SQL fields by given structure.
# Structs
Cond is for constructing MySQL WHERE statement.
DB is the main structure for mysqlx.
Error is the error type identified by mysqlx.
Field shows information of a field.
Index shows the information of an index setting.
Limit is for MySQL limit statement.
Offset is for MySQL offset statement.
Options identifies options and parameters for a structure.
Order is for MySQL ORDER BY statement.
Param identifies connect parameters to a database.
Unique shows the information of a unique setting.
# Type aliases
And packages conditions with AND logic.
Or packages conditions with OR logic.
RawStatement identifies raw MySQL statement, which will be directly added into sql statements.