You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.6 KiB
122 lines
2.6 KiB
8 years ago
|
// 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"
|
||
|
|
||
6 years ago
|
"xorm.io/core"
|
||
8 years ago
|
)
|
||
|
|
||
|
// Rows rows wrapper a rows to
|
||
|
type Rows struct {
|
||
8 years ago
|
session *Session
|
||
|
rows *core.Rows
|
||
|
beanType reflect.Type
|
||
|
lastError error
|
||
8 years ago
|
}
|
||
|
|
||
|
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{}
|
||
7 years ago
|
var err error
|
||
8 years ago
|
|
||
7 years ago
|
if err = rows.session.statement.setRefBean(bean); err != nil {
|
||
8 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
7 years ago
|
if len(session.statement.TableName()) <= 0 {
|
||
8 years ago
|
return nil, ErrTableNotFound
|
||
|
}
|
||
|
|
||
7 years ago
|
if rows.session.statement.RawSQL == "" {
|
||
|
sqlStr, args, err = rows.session.statement.genGetSQL(bean)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
8 years ago
|
} else {
|
||
7 years ago
|
sqlStr = rows.session.statement.RawSQL
|
||
|
args = rows.session.statement.RawParams
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
rows.rows, err = rows.session.queryRows(sqlStr, args...)
|
||
|
if err != nil {
|
||
|
rows.lastError = err
|
||
|
rows.Close()
|
||
|
return nil, err
|
||
8 years ago
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
6 years ago
|
if reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
|
||
8 years ago
|
return fmt.Errorf("scan arg is incompatible type to [%v]", rows.beanType)
|
||
|
}
|
||
|
|
||
7 years ago
|
if err := rows.session.statement.setRefBean(bean); err != nil {
|
||
7 years ago
|
return err
|
||
|
}
|
||
|
|
||
6 years ago
|
fields, err := rows.rows.Columns()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
scanResults, err := rows.session.row2Slice(rows.rows, fields, bean)
|
||
7 years ago
|
if err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
dataStruct := rValue(bean)
|
||
6 years ago
|
_, err = rows.session.slice2Bean(scanResults, fields, bean, &dataStruct, rows.session.statement.RefTable)
|
||
7 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return rows.session.executeProcessors()
|
||
8 years ago
|
}
|
||
|
|
||
|
// Close session if session.IsAutoClose is true, and claimed any opened resources
|
||
|
func (rows *Rows) Close() error {
|
||
7 years ago
|
if rows.session.isAutoClose {
|
||
8 years ago
|
defer rows.session.Close()
|
||
|
}
|
||
|
|
||
6 years ago
|
if rows.rows != nil {
|
||
|
return rows.rows.Close()
|
||
8 years ago
|
}
|
||
6 years ago
|
|
||
8 years ago
|
return rows.lastError
|
||
|
}
|