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"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mysqlField struct {
|
|
|
|
tableName string
|
|
|
|
name string
|
|
|
|
flags fieldFlag
|
|
|
|
fieldType byte
|
|
|
|
decimals byte
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:03:13 +00:00
|
|
|
type resultSet struct {
|
2017-05-24 13:40:58 +00:00
|
|
|
columns []mysqlField
|
|
|
|
columnNames []string
|
|
|
|
done bool
|
2017-03-31 00:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type mysqlRows struct {
|
|
|
|
mc *mysqlConn
|
|
|
|
rs resultSet
|
2015-04-27 23:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type binaryRows struct {
|
|
|
|
mysqlRows
|
|
|
|
}
|
|
|
|
|
|
|
|
type textRows struct {
|
|
|
|
mysqlRows
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rows *mysqlRows) Columns() []string {
|
2017-05-24 13:40:58 +00:00
|
|
|
if rows.rs.columnNames != nil {
|
|
|
|
return rows.rs.columnNames
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:03:13 +00:00
|
|
|
columns := make([]string, len(rows.rs.columns))
|
2016-02-18 19:19:02 +00:00
|
|
|
if rows.mc != nil && rows.mc.cfg.ColumnsWithAlias {
|
2015-04-27 23:19:51 +00:00
|
|
|
for i := range columns {
|
2017-03-31 00:03:13 +00:00
|
|
|
if tableName := rows.rs.columns[i].tableName; len(tableName) > 0 {
|
|
|
|
columns[i] = tableName + "." + rows.rs.columns[i].name
|
2015-08-19 01:12:51 +00:00
|
|
|
} else {
|
2017-03-31 00:03:13 +00:00
|
|
|
columns[i] = rows.rs.columns[i].name
|
2015-08-19 01:12:51 +00:00
|
|
|
}
|
2015-04-27 23:19:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := range columns {
|
2017-03-31 00:03:13 +00:00
|
|
|
columns[i] = rows.rs.columns[i].name
|
2015-04-27 23:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-24 13:40:58 +00:00
|
|
|
|
|
|
|
rows.rs.columnNames = columns
|
2015-04-27 23:19:51 +00:00
|
|
|
return columns
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:03:13 +00:00
|
|
|
func (rows *mysqlRows) Close() (err error) {
|
2015-04-27 23:19:51 +00:00
|
|
|
mc := rows.mc
|
|
|
|
if mc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if mc.netConn == nil {
|
|
|
|
return ErrInvalidConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove unread packets from stream
|
2017-03-31 00:03:13 +00:00
|
|
|
if !rows.rs.done {
|
|
|
|
err = mc.readUntilEOF()
|
|
|
|
}
|
2016-02-18 19:19:02 +00:00
|
|
|
if err == nil {
|
|
|
|
if err = mc.discardResults(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 23:19:51 +00:00
|
|
|
rows.mc = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:03:13 +00:00
|
|
|
func (rows *mysqlRows) HasNextResultSet() (b bool) {
|
|
|
|
if rows.mc == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return rows.mc.status&statusMoreResultsExists != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rows *mysqlRows) nextResultSet() (int, error) {
|
|
|
|
if rows.mc == nil {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
if rows.mc.netConn == nil {
|
|
|
|
return 0, ErrInvalidConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove unread packets from stream
|
|
|
|
if !rows.rs.done {
|
|
|
|
if err := rows.mc.readUntilEOF(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
rows.rs.done = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rows.HasNextResultSet() {
|
|
|
|
rows.mc = nil
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
rows.rs = resultSet{}
|
|
|
|
return rows.mc.readResultSetHeaderPacket()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rows *mysqlRows) nextNotEmptyResultSet() (int, error) {
|
|
|
|
for {
|
|
|
|
resLen, err := rows.nextResultSet()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resLen > 0 {
|
|
|
|
return resLen, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rows.rs.done = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 13:40:58 +00:00
|
|
|
func (rows *binaryRows) NextResultSet() error {
|
2017-03-31 00:03:13 +00:00
|
|
|
resLen, err := rows.nextNotEmptyResultSet()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-24 13:40:58 +00:00
|
|
|
rows.rs.columns, err = rows.mc.readColumns(resLen)
|
|
|
|
return err
|
2017-03-31 00:03:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 23:19:51 +00:00
|
|
|
func (rows *binaryRows) Next(dest []driver.Value) error {
|
|
|
|
if mc := rows.mc; mc != nil {
|
|
|
|
if mc.netConn == nil {
|
|
|
|
return ErrInvalidConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch next row from stream
|
|
|
|
return rows.readRow(dest)
|
|
|
|
}
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:03:13 +00:00
|
|
|
func (rows *textRows) NextResultSet() (err error) {
|
|
|
|
resLen, err := rows.nextNotEmptyResultSet()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rows.rs.columns, err = rows.mc.readColumns(resLen)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-27 23:19:51 +00:00
|
|
|
func (rows *textRows) Next(dest []driver.Value) error {
|
|
|
|
if mc := rows.mc; mc != nil {
|
|
|
|
if mc.netConn == nil {
|
|
|
|
return ErrInvalidConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch next row from stream
|
|
|
|
return rows.readRow(dest)
|
|
|
|
}
|
|
|
|
return io.EOF
|
|
|
|
}
|