2016-11-03 18:16:01 -04:00
|
|
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
|
|
|
//
|
|
|
|
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
2021-04-22 20:08:53 -04:00
|
|
|
"encoding/json"
|
2016-11-03 18:16:01 -04:00
|
|
|
"fmt"
|
2018-07-03 17:58:31 -04:00
|
|
|
"io"
|
2016-11-03 18:16:01 -04:00
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mysqlStmt struct {
|
|
|
|
mc *mysqlConn
|
|
|
|
id uint32
|
|
|
|
paramCount int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stmt *mysqlStmt) Close() error {
|
2018-07-03 17:58:31 -04:00
|
|
|
if stmt.mc == nil || stmt.mc.closed.IsSet() {
|
|
|
|
// driver.Stmt.Close can be called more than once, thus this function
|
|
|
|
// has to be idempotent.
|
|
|
|
// See also Issue #450 and golang/go#16019.
|
|
|
|
//errLog.Print(ErrInvalidConn)
|
2016-11-03 18:16:01 -04:00
|
|
|
return driver.ErrBadConn
|
|
|
|
}
|
|
|
|
|
|
|
|
err := stmt.mc.writeCommandPacketUint32(comStmtClose, stmt.id)
|
|
|
|
stmt.mc = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stmt *mysqlStmt) NumInput() int {
|
|
|
|
return stmt.paramCount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stmt *mysqlStmt) ColumnConverter(idx int) driver.ValueConverter {
|
|
|
|
return converter{}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:08:53 -04:00
|
|
|
func (stmt *mysqlStmt) CheckNamedValue(nv *driver.NamedValue) (err error) {
|
|
|
|
nv.Value, err = converter{}.ConvertValue(nv.Value)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:16:01 -04:00
|
|
|
func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
|
2018-07-03 17:58:31 -04:00
|
|
|
if stmt.mc.closed.IsSet() {
|
2016-11-03 18:16:01 -04:00
|
|
|
errLog.Print(ErrInvalidConn)
|
|
|
|
return nil, driver.ErrBadConn
|
|
|
|
}
|
|
|
|
// Send command
|
|
|
|
err := stmt.writeExecutePacket(args)
|
|
|
|
if err != nil {
|
2018-07-03 17:58:31 -04:00
|
|
|
return nil, stmt.mc.markBadConn(err)
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mc := stmt.mc
|
|
|
|
|
|
|
|
mc.affectedRows = 0
|
|
|
|
mc.insertId = 0
|
|
|
|
|
|
|
|
// Read Result
|
|
|
|
resLen, err := mc.readResultSetHeaderPacket()
|
2018-07-03 17:58:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resLen > 0 {
|
|
|
|
// Columns
|
|
|
|
if err = mc.readUntilEOF(); err != nil {
|
|
|
|
return nil, err
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
2018-07-03 17:58:31 -04:00
|
|
|
|
|
|
|
// Rows
|
|
|
|
if err := mc.readUntilEOF(); err != nil {
|
|
|
|
return nil, err
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:58:31 -04:00
|
|
|
if err := mc.discardResults(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mysqlResult{
|
|
|
|
affectedRows: int64(mc.affectedRows),
|
|
|
|
insertId: int64(mc.insertId),
|
|
|
|
}, nil
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
|
2018-07-03 17:58:31 -04:00
|
|
|
return stmt.query(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stmt *mysqlStmt) query(args []driver.Value) (*binaryRows, error) {
|
|
|
|
if stmt.mc.closed.IsSet() {
|
2016-11-03 18:16:01 -04:00
|
|
|
errLog.Print(ErrInvalidConn)
|
|
|
|
return nil, driver.ErrBadConn
|
|
|
|
}
|
|
|
|
// Send command
|
|
|
|
err := stmt.writeExecutePacket(args)
|
|
|
|
if err != nil {
|
2018-07-03 17:58:31 -04:00
|
|
|
return nil, stmt.mc.markBadConn(err)
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mc := stmt.mc
|
|
|
|
|
|
|
|
// Read Result
|
|
|
|
resLen, err := mc.readResultSetHeaderPacket()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rows := new(binaryRows)
|
|
|
|
|
|
|
|
if resLen > 0 {
|
|
|
|
rows.mc = mc
|
2018-07-03 17:58:31 -04:00
|
|
|
rows.rs.columns, err = mc.readColumns(resLen)
|
|
|
|
} else {
|
|
|
|
rows.rs.done = true
|
|
|
|
|
|
|
|
switch err := rows.NextResultSet(); err {
|
|
|
|
case nil, io.EOF:
|
|
|
|
return rows, nil
|
|
|
|
default:
|
|
|
|
return nil, err
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows, err
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:08:53 -04:00
|
|
|
var jsonType = reflect.TypeOf(json.RawMessage{})
|
|
|
|
|
2016-11-03 18:16:01 -04:00
|
|
|
type converter struct{}
|
|
|
|
|
2018-07-03 17:58:31 -04:00
|
|
|
// ConvertValue mirrors the reference/default converter in database/sql/driver
|
|
|
|
// with _one_ exception. We support uint64 with their high bit and the default
|
|
|
|
// implementation does not. This function should be kept in sync with
|
|
|
|
// database/sql/driver defaultConverter.ConvertValue() except for that
|
|
|
|
// deliberate difference.
|
2016-11-03 18:16:01 -04:00
|
|
|
func (c converter) ConvertValue(v interface{}) (driver.Value, error) {
|
|
|
|
if driver.IsValue(v) {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:58:31 -04:00
|
|
|
if vr, ok := v.(driver.Valuer); ok {
|
|
|
|
sv, err := callValuerValue(vr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-22 20:08:53 -04:00
|
|
|
if driver.IsValue(sv) {
|
|
|
|
return sv, nil
|
2018-07-03 17:58:31 -04:00
|
|
|
}
|
2021-04-22 20:08:53 -04:00
|
|
|
// A value returend from the Valuer interface can be "a type handled by
|
|
|
|
// a database driver's NamedValueChecker interface" so we should accept
|
|
|
|
// uint64 here as well.
|
|
|
|
if u, ok := sv.(uint64); ok {
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("non-Value type %T returned from Value", sv)
|
2018-07-03 17:58:31 -04:00
|
|
|
}
|
2016-11-03 18:16:01 -04:00
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
switch rv.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
// indirect pointers
|
|
|
|
if rv.IsNil() {
|
|
|
|
return nil, nil
|
2018-07-03 17:58:31 -04:00
|
|
|
} else {
|
|
|
|
return c.ConvertValue(rv.Elem().Interface())
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
return rv.Int(), nil
|
2020-07-11 17:07:52 -04:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
return rv.Uint(), nil
|
2016-11-03 18:16:01 -04:00
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
return rv.Float(), nil
|
2018-07-03 17:58:31 -04:00
|
|
|
case reflect.Bool:
|
|
|
|
return rv.Bool(), nil
|
|
|
|
case reflect.Slice:
|
2021-04-22 20:08:53 -04:00
|
|
|
switch t := rv.Type(); {
|
|
|
|
case t == jsonType:
|
|
|
|
return v, nil
|
|
|
|
case t.Elem().Kind() == reflect.Uint8:
|
2018-07-03 17:58:31 -04:00
|
|
|
return rv.Bytes(), nil
|
2021-04-22 20:08:53 -04:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported type %T, a slice of %s", v, t.Elem().Kind())
|
2018-07-03 17:58:31 -04:00
|
|
|
}
|
|
|
|
case reflect.String:
|
|
|
|
return rv.String(), nil
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unsupported type %T, a %s", v, rv.Kind())
|
|
|
|
}
|
2018-07-03 17:58:31 -04:00
|
|
|
|
|
|
|
var valuerReflectType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
|
|
|
|
|
|
|
|
// callValuerValue returns vr.Value(), with one exception:
|
|
|
|
// If vr.Value is an auto-generated method on a pointer type and the
|
|
|
|
// pointer is nil, it would panic at runtime in the panicwrap
|
|
|
|
// method. Treat it like nil instead.
|
|
|
|
//
|
|
|
|
// This is so people can implement driver.Value on value types and
|
|
|
|
// still use nil pointers to those types to mean nil/NULL, just like
|
|
|
|
// string/*string.
|
|
|
|
//
|
|
|
|
// This is an exact copy of the same-named unexported function from the
|
|
|
|
// database/sql package.
|
|
|
|
func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
|
|
|
|
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Ptr &&
|
|
|
|
rv.IsNil() &&
|
|
|
|
rv.Type().Elem().Implements(valuerReflectType) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return vr.Value()
|
|
|
|
}
|