forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-sql-driver/mysql/statement.go

371 lines
5.4 KiB
Go
Raw Normal View History

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