open-vault/vendor/github.com/go-sql-driver/mysql/statement.go

179 lines
3.7 KiB
Go
Raw Normal View History

2015-04-27 23:19:51 +00: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"
2015-06-29 22:05:44 +00:00
"fmt"
2017-03-31 00:03:13 +00:00
"io"
2015-06-29 22:05:44 +00:00
"reflect"
"strconv"
2015-04-27 23:19:51 +00:00
)
type mysqlStmt struct {
mc *mysqlConn
id uint32
paramCount int
}
func (stmt *mysqlStmt) Close() error {
2017-06-16 15:14:18 +00:00
if stmt.mc == nil || stmt.mc.closed.IsSet() {
2016-11-16 23:22:54 +00:00
// 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)
2015-04-27 23:19:51 +00:00
return driver.ErrBadConn
}
err := stmt.mc.writeCommandPacketUint32(comStmtClose, stmt.id)
stmt.mc = nil
return err
}
func (stmt *mysqlStmt) NumInput() int {
return stmt.paramCount
}
2015-06-29 22:05:44 +00:00
func (stmt *mysqlStmt) ColumnConverter(idx int) driver.ValueConverter {
return converter{}
}
2015-04-27 23:19:51 +00:00
func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
2017-06-16 15:14:18 +00:00
if stmt.mc.closed.IsSet() {
2015-04-27 23:19:51 +00:00
errLog.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
// Send command
err := stmt.writeExecutePacket(args)
if err != nil {
2017-09-05 22:06:47 +00:00
return nil, stmt.mc.markBadConn(err)
2015-04-27 23:19:51 +00:00
}
mc := stmt.mc
mc.affectedRows = 0
mc.insertId = 0
// Read Result
resLen, err := mc.readResultSetHeaderPacket()
2017-03-31 00:03:13 +00:00
if err != nil {
return nil, err
}
2015-04-27 23:19:51 +00:00
2017-03-31 00:03:13 +00:00
if resLen > 0 {
// Columns
if err = mc.readUntilEOF(); err != nil {
return nil, err
2015-04-27 23:19:51 +00:00
}
2017-03-31 00:03:13 +00:00
// Rows
if err := mc.readUntilEOF(); err != nil {
return nil, err
2015-04-27 23:19:51 +00:00
}
}
2017-03-31 00:03:13 +00:00
if err := mc.discardResults(); err != nil {
return nil, err
}
return &mysqlResult{
affectedRows: int64(mc.affectedRows),
insertId: int64(mc.insertId),
}, nil
2015-04-27 23:19:51 +00:00
}
func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
2017-06-16 15:14:18 +00:00
return stmt.query(args)
}
func (stmt *mysqlStmt) query(args []driver.Value) (*binaryRows, error) {
if stmt.mc.closed.IsSet() {
2015-04-27 23:19:51 +00:00
errLog.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
// Send command
err := stmt.writeExecutePacket(args)
if err != nil {
2017-09-05 22:06:47 +00:00
return nil, stmt.mc.markBadConn(err)
2015-04-27 23:19:51 +00: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
2017-05-24 13:40:58 +00:00
rows.rs.columns, err = mc.readColumns(resLen)
2017-03-31 00:03:13 +00:00
} else {
rows.rs.done = true
switch err := rows.NextResultSet(); err {
case nil, io.EOF:
return rows, nil
default:
return nil, err
}
2015-04-27 23:19:51 +00:00
}
return rows, err
}
2015-06-29 22:05:44 +00:00
type converter struct{}
func (c converter) ConvertValue(v interface{}) (driver.Value, error) {
if driver.IsValue(v) {
return v, nil
}
2018-01-26 23:51:00 +00:00
if v != nil {
if valuer, ok := v.(driver.Valuer); ok {
return valuer.Value()
}
}
2015-06-29 22:05:44 +00:00
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Ptr:
// indirect pointers
if rv.IsNil() {
return nil, nil
}
return c.ConvertValue(rv.Elem().Interface())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return rv.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
return int64(rv.Uint()), nil
case reflect.Uint64:
u64 := rv.Uint()
if u64 >= 1<<63 {
return strconv.FormatUint(u64, 10), nil
}
return int64(u64), nil
case reflect.Float32, reflect.Float64:
return rv.Float(), nil
2018-01-26 23:51:00 +00:00
case reflect.Bool:
return rv.Bool(), nil
case reflect.Slice:
ek := rv.Type().Elem().Kind()
if ek == reflect.Uint8 {
return rv.Bytes(), nil
}
return nil, fmt.Errorf("unsupported type %T, a slice of %s", v, ek)
case reflect.String:
return rv.String(), nil
2015-06-29 22:05:44 +00:00
}
return nil, fmt.Errorf("unsupported type %T, a %s", v, rv.Kind())
}