Categorygithub.com/Andrew-M-C/go.mysqlx
modulepackage
0.3.0
Repository: https://github.com/andrew-m-c/go.mysqlx.git
Documentation: pkg.go.dev

# 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.

Changelog

Click here

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)
Packagenanoseconds/opbytes/opallocs/op
mysqlx1,038,348169637
sqlx (with "= 1234")1,115,127103918
sqlx (with "= ?")2,112,185124726
gorm (with 'Find' function)2,256,5626641105
gorm (with 'First' function)1,114,290429597

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)
Packagenanoseconds/opbytes/opallocs/op
mysqlx1,247,630184837
sqlx (with "= 'Alice'")1,146,627106418
sqlx (with "= ?")2,023,415124025
gorm (with 'Find' function)2,073,2726625104
gorm (with 'First' function)2,207,2295377116

# 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.