2016-11-03 23:16:01 +01:00
|
|
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2019-06-23 17:22:43 +02:00
|
|
|
"xorm.io/core"
|
2016-11-03 23:16:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Rows rows wrapper a rows to
|
|
|
|
type Rows struct {
|
2017-04-07 03:47:25 +02:00
|
|
|
session *Session
|
|
|
|
rows *core.Rows
|
|
|
|
beanType reflect.Type
|
|
|
|
lastError error
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRows(session *Session, bean interface{}) (*Rows, error) {
|
|
|
|
rows := new(Rows)
|
|
|
|
rows.session = session
|
|
|
|
rows.beanType = reflect.Indirect(reflect.ValueOf(bean)).Type()
|
|
|
|
|
|
|
|
var sqlStr string
|
|
|
|
var args []interface{}
|
2017-08-22 13:39:52 +02:00
|
|
|
var err error
|
2016-11-03 23:16:01 +01:00
|
|
|
|
2018-07-20 04:10:17 +02:00
|
|
|
if err = rows.session.statement.setRefBean(bean); err != nil {
|
2017-05-02 02:50:33 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-22 13:39:52 +02:00
|
|
|
if len(session.statement.TableName()) <= 0 {
|
2016-11-03 23:16:01 +01:00
|
|
|
return nil, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2017-08-22 13:39:52 +02:00
|
|
|
if rows.session.statement.RawSQL == "" {
|
|
|
|
sqlStr, args, err = rows.session.statement.genGetSQL(bean)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-03 23:16:01 +01:00
|
|
|
} else {
|
2017-08-22 13:39:52 +02:00
|
|
|
sqlStr = rows.session.statement.RawSQL
|
|
|
|
args = rows.session.statement.RawParams
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
|
2017-09-13 07:18:22 +02:00
|
|
|
rows.rows, err = rows.session.queryRows(sqlStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
rows.lastError = err
|
|
|
|
rows.Close()
|
|
|
|
return nil, err
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return rows, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next move cursor to next record, return false if end has reached
|
|
|
|
func (rows *Rows) Next() bool {
|
|
|
|
if rows.lastError == nil && rows.rows != nil {
|
|
|
|
hasNext := rows.rows.Next()
|
|
|
|
if !hasNext {
|
|
|
|
rows.lastError = sql.ErrNoRows
|
|
|
|
}
|
|
|
|
return hasNext
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close.
|
|
|
|
func (rows *Rows) Err() error {
|
|
|
|
return rows.lastError
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan row record to bean properties
|
|
|
|
func (rows *Rows) Scan(bean interface{}) error {
|
|
|
|
if rows.lastError != nil {
|
|
|
|
return rows.lastError
|
|
|
|
}
|
|
|
|
|
2019-06-23 17:22:43 +02:00
|
|
|
if reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
|
2016-11-03 23:16:01 +01:00
|
|
|
return fmt.Errorf("scan arg is incompatible type to [%v]", rows.beanType)
|
|
|
|
}
|
|
|
|
|
2018-07-20 04:10:17 +02:00
|
|
|
if err := rows.session.statement.setRefBean(bean); err != nil {
|
2017-08-22 13:39:52 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-23 17:22:43 +02:00
|
|
|
fields, err := rows.rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
scanResults, err := rows.session.row2Slice(rows.rows, fields, bean)
|
2017-08-22 13:39:52 +02:00
|
|
|
if err != nil {
|
2017-05-02 02:50:33 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-04-07 03:47:25 +02:00
|
|
|
|
2018-07-20 04:10:17 +02:00
|
|
|
dataStruct := rValue(bean)
|
2019-06-23 17:22:43 +02:00
|
|
|
_, err = rows.session.slice2Bean(scanResults, fields, bean, &dataStruct, rows.session.statement.RefTable)
|
2017-10-01 18:52:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows.session.executeProcessors()
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close session if session.IsAutoClose is true, and claimed any opened resources
|
|
|
|
func (rows *Rows) Close() error {
|
2017-08-22 13:39:52 +02:00
|
|
|
if rows.session.isAutoClose {
|
2016-11-03 23:16:01 +01:00
|
|
|
defer rows.session.Close()
|
|
|
|
}
|
|
|
|
|
2019-06-23 17:22:43 +02:00
|
|
|
if rows.rows != nil {
|
|
|
|
return rows.rows.Close()
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
2019-06-23 17:22:43 +02:00
|
|
|
|
2016-11-03 23:16:01 +01:00
|
|
|
return rows.lastError
|
|
|
|
}
|