Documentation
¶
Overview ¶
Package mysql is a pure-Go (CGO=0) reimplementation of the Ruby mysql2 gem's Mysql2 API. Upstream, mysql2 is a C extension linking libmysqlclient; this package instead binds github.com/go-sql-driver/mysql — a pure-Go, CGO-free implementation of the MySQL client/server protocol — through database/sql, so the whole stack links statically with CGO_ENABLED=0 on every 64-bit target the go-* ecosystem supports (amd64, arm64, riscv64, loong64, ppc64le, s390x).
The API mirrors Mysql2::Client, Mysql2::Result, Mysql2::Statement and Mysql2::Error:
c, _ := mysql.NewClient(mysql.Options{Host: "127.0.0.1", Username: "root", Database: "test"})
defer c.Close()
res, _ := c.Query("SELECT id, name FROM hosts")
for _, h := range res.Hashes() {
fmt.Println(h["id"], h["name"]) // int64, string
}
Result values crossing the boundary use mysql2's MySQL->Ruby cast table:
MySQL column type Go (this package) Ruby (rbgo binding) TINYINT..BIGINT, YEAR int64 / uint64 Integer FLOAT, DOUBLE float64 Float DECIMAL / NEWDECIMAL Decimal (a string) BigDecimal DATE Date Date DATETIME, TIMESTAMP time.Time (naive UTC) Time TIME string String CHAR/VARCHAR/TEXT/… string String (UTF-8) BLOB/BINARY/BIT/GEOMETRY []byte String (ASCII-8BIT) NULL nil nil
The cast is governed by the same options mysql2 exposes — cast, cast_booleans, symbolize_keys, and as: :array vs :hash — carried on QueryOptions. Errors map to Mysql2::Error via the Error type, exposing #error_number and #sql_state.
The connection is a host seam: the deterministic test suite drives the whole query -> result -> cast pipeline through an in-process fake database/sql driver, so it reaches 100% coverage without a live server, while an optional env-gated oracle round-trips against a real MySQL when MYSQL2_TEST_DSN is set.
Index ¶
- type Client
- func (c *Client) AffectedRows() int64
- func (c *Client) Close() error
- func (c *Client) Closed() bool
- func (c *Client) Escape(s string) string
- func (c *Client) LastID() int64
- func (c *Client) Ping() bool
- func (c *Client) Prepare(query string) (*Statement, error)
- func (c *Client) Query(query string, opts ...QueryOptions) (*Result, error)
- func (c *Client) ServerInfo() (ServerInfo, error)
- type Date
- type Decimal
- type Error
- type HashRow
- type Options
- type QueryOptions
- type Result
- func (r *Result) As() string
- func (r *Result) Count() int
- func (r *Result) Each(fn func(any) error) error
- func (r *Result) EachArray(fn func(Row) error) error
- func (r *Result) EachHash(fn func(HashRow) error) error
- func (r *Result) Fields() []string
- func (r *Result) Hashes() []HashRow
- func (r *Result) Rows() []Row
- func (r *Result) SymbolizeKeys() bool
- type Row
- type ServerInfo
- type Statement
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a handle to a MySQL server, mirroring Mysql2::Client. It wraps a *sql.DB over the go-sql-driver backend.
func NewClient ¶
NewClient connects to a MySQL server described by opts and returns a Client, mirroring Mysql2::Client.new. It opens the pool and verifies the connection with a ping (mysql2 connects eagerly), wrapping any failure as a *mysql.Error.
func (*Client) AffectedRows ¶
AffectedRows returns the number of rows changed by the most recent statement (Mysql2::Client#affected_rows).
func (*Client) Close ¶
Close closes the client and its connection pool (Mysql2::Client#close). Closing an already-closed client is a no-op, matching the gem.
func (*Client) Escape ¶
Escape escapes a string for safe interpolation into a statement (Mysql2::Client#escape).
func (*Client) LastID ¶
LastID returns the AUTO_INCREMENT value generated by the most recent INSERT (Mysql2::Client#last_id / #insert_id).
func (*Client) Ping ¶
Ping checks the connection is alive (Mysql2::Client#ping), returning false on failure like the gem rather than an error.
func (*Client) Prepare ¶
Prepare compiles query into a *Statement (Mysql2::Client#prepare). Bind parameters positionally on Execute using MySQL's `?` placeholders.
func (*Client) Query ¶
func (c *Client) Query(query string, opts ...QueryOptions) (*Result, error)
Query runs sql and returns a *Result for a row-producing statement, or a nil Result for a statement that changes rows (INSERT / UPDATE / DELETE / DDL), mirroring Mysql2::Client#query (which returns a Result or nil). After a non-row statement AffectedRows and LastID reflect it. An optional QueryOptions overrides the client defaults for this call.
func (*Client) ServerInfo ¶
func (c *Client) ServerInfo() (ServerInfo, error)
ServerInfo queries the server version (Mysql2::Client#server_info) via SELECT VERSION() and returns it parsed.
type Date ¶
Date is a calendar date from a DATE column, with no time-of-day or zone — mirroring the Ruby Date mysql2 returns for DATE. The rbgo binding maps it to a Ruby Date; keeping it distinct from time.Time lets the host pick Date vs Time.
type Decimal ¶
type Decimal string
Decimal is a fixed-point value from a DECIMAL / NEWDECIMAL column, carried as its exact decimal string so no precision is lost. The rbgo binding turns it into a Ruby BigDecimal (mysql2's cast for DECIMAL). Its String is the raw database text (e.g. "3.14").
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is a database error mirroring Mysql2::Error. It carries the MySQL error number and SQLSTATE the rbgo binding exposes as #error_number and #sql_state, plus the human-readable message (#message). A driver error that is not a server error (a dial failure, a context cancellation) surfaces as an Error with number 0 and the generic "HY000" SQLSTATE, matching how mysql2 wraps client-side failures.
func (*Error) ErrorNumber ¶
ErrorNumber returns the MySQL server error number (Mysql2::Error#error_number / #errno), e.g. 1062 for a duplicate-key violation. It is 0 for a client-side error with no server number.
type HashRow ¶
HashRow is a single result row keyed by column name — the shape Mysql2::Result yields by default (`as: :hash`).
type Options ¶
type Options struct {
Host string
Port int
Username string
Password string
Database string
Socket string // unix-socket path; when set, Net becomes "unix"
Encoding string // connection charset (mysql2 :encoding), e.g. "utf8mb4"
Flags []string // capability flags, e.g. "MULTI_STATEMENTS", "FOUND_ROWS"
ConnectTimeout time.Duration // dial timeout (mysql2 :connect_timeout)
ReadTimeout time.Duration // I/O read timeout (mysql2 :read_timeout)
WriteTimeout time.Duration // I/O write timeout (mysql2 :write_timeout)
// QueryDefaults overrides the per-client default query options. When nil the
// client uses DefaultQueryOptions().
QueryDefaults *QueryOptions
}
Options configures a Client, mirroring the keyword arguments of Mysql2::Client.new (host:, port:, username:, password:, database:, socket:, encoding:, flags:, connect_timeout:, read_timeout:, write_timeout:, …).
type QueryOptions ¶
type QueryOptions struct {
// Cast toggles type casting (mysql2 :cast). When false every value is
// returned as a String. Default true.
Cast bool
// CastBooleans casts TINYINT columns to bool (mysql2 :cast_booleans).
CastBooleans bool
// SymbolizeKeys requests Symbol hash keys (mysql2 :symbolize_keys); carried
// for the host to honour, as Go has no Symbol.
SymbolizeKeys bool
// As selects the row shape: asHash ("hash", default) or asArray ("array").
As string
}
QueryOptions carries the per-query options mysql2 accepts on Client#query and merges from Client.new's default_query_options. The zero value is not the gem's default (mysql2 defaults cast:true); build one with DefaultQueryOptions and adjust, or set Client.QueryDefaults.
func DefaultQueryOptions ¶
func DefaultQueryOptions() QueryOptions
DefaultQueryOptions returns the gem's default query options: cast on, no boolean cast, string keys, hash rows.
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result is a buffered result set, mirroring Mysql2::Result. mysql2 returns one from Client#query / Statement#execute for a statement that yields rows. It is Enumerable over its rows; each row is a HashRow (keyed by column name, the default) or a Row (a positional array, the `as: :array` shape), selected by the QueryOptions the query ran with. Fields and Count mirror #fields and #count / #size.
func (*Result) As ¶
As returns the row shape the result was built for ("hash" or "array"), mirroring the query's `as:` option.
func (*Result) Each ¶
Each iterates the result in its configured shape (Mysql2::Result#each): it yields a HashRow by default, or a Row when the query used as: :array. fn receives an any it type-switches; iteration stops at the first error.
func (*Result) EachArray ¶
EachArray calls fn once per row as a positional array, stopping at and returning the first error fn returns (the block form of #each with as: :array).
func (*Result) EachHash ¶
EachHash calls fn once per row as a name-keyed map, stopping at and returning the first error fn returns (the block form of #each).
func (*Result) Hashes ¶
Hashes returns every row as a name-keyed map — the default `as: :hash` shape. A duplicate column name keeps the last column's value, matching how a Ruby Hash overwrites a repeated key.
func (*Result) Rows ¶
Rows returns every row as a positional array in column order — the `as: :array` shape (Mysql2::Result#each with as: :array).
func (*Result) SymbolizeKeys ¶
SymbolizeKeys reports whether the query requested symbol keys (Mysql2::Result built with symbolize_keys: true). Go has no Symbol type, so the flag is carried for the rbgo binding to intern the string keys into Ruby Symbols; the maps here are always string-keyed.
type Row ¶
type Row = []Value
Row is a single result row as a slice of column Values in column order — the shape Mysql2::Result yields with `as: :array`.
type ServerInfo ¶
ServerInfo describes the connected server, mirroring the Hash Mysql2::Client#server_info returns: the version string and a packed numeric id (major*10000 + minor*100 + patch).
type Statement ¶
type Statement struct {
// contains filtered or unexported fields
}
Statement is a prepared statement, mirroring Mysql2::Statement. Client#prepare compiles the SQL; Execute binds the given parameters and runs it, returning a *Result for a row-producing statement or a nil Result otherwise (after which the owning client's AffectedRows / LastID reflect the change).
func (*Statement) Close ¶
Close releases the prepared statement (Mysql2::Statement#close). Closing an already-closed statement is a no-op.
func (*Statement) Execute ¶
Execute binds args to the statement's placeholders and runs it (Mysql2::Statement#execute). For a row-producing statement it returns a *Result using the client's default query options; for a changing statement it returns a nil Result and updates the client's AffectedRows / LastID. Use ExecuteOpts to override the query options.
func (*Statement) ExecuteOpts ¶
func (s *Statement) ExecuteOpts(qo QueryOptions, args ...any) (*Result, error)
ExecuteOpts is Execute with explicit per-execution query options.
type Value ¶
type Value = any
Value is a datum crossing the Go<->MySQL boundary. On a cast result it is one of: nil, int64, uint64, float64, Decimal, Date, time.Time, string, []byte, or bool. On binds this package accepts int / int32 / int64 / uint / float32 / float64 / string / []byte / bool / time.Time / nil.
