2016-11-04 03:46:01 +05:30
|
|
|
// 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 (
|
2020-03-22 20:42:55 +05:30
|
|
|
"errors"
|
2016-11-04 03:46:01 +05:30
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
"xorm.io/builder"
|
|
|
|
"xorm.io/xorm/core"
|
|
|
|
"xorm.io/xorm/internal/utils"
|
2016-11-04 03:46:01 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Rows rows wrapper a rows to
|
|
|
|
type Rows struct {
|
2021-08-13 04:41:42 +05:30
|
|
|
session *Session
|
|
|
|
rows *core.Rows
|
|
|
|
beanType reflect.Type
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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 17:09:52 +05:30
|
|
|
var err error
|
2016-11-04 03:46:01 +05:30
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
beanValue := reflect.ValueOf(bean)
|
|
|
|
if beanValue.Kind() != reflect.Ptr {
|
|
|
|
return nil, errors.New("needs a pointer to a value")
|
|
|
|
} else if beanValue.Elem().Kind() == reflect.Ptr {
|
|
|
|
return nil, errors.New("a pointer to a pointer is not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.session.statement.SetRefBean(bean); err != nil {
|
2017-05-02 06:20:33 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:09:52 +05:30
|
|
|
if len(session.statement.TableName()) <= 0 {
|
2016-11-04 03:46:01 +05:30
|
|
|
return nil, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:09:52 +05:30
|
|
|
if rows.session.statement.RawSQL == "" {
|
2020-03-22 20:42:55 +05:30
|
|
|
var autoCond builder.Cond
|
|
|
|
var addedTableName = (len(session.statement.JoinStr) > 0)
|
|
|
|
var table = rows.session.statement.RefTable
|
|
|
|
|
|
|
|
if !session.statement.NoAutoCondition {
|
|
|
|
var err error
|
|
|
|
autoCond, err = session.statement.BuildConds(table, bean, true, true, false, true, addedTableName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// !oinume! Add "<col> IS NULL" to WHERE whatever condiBean is given.
|
|
|
|
// See https://gitea.com/xorm/xorm/issues/179
|
|
|
|
if col := table.DeletedColumn(); col != nil && !session.statement.GetUnscoped() { // tag "deleted" is enabled
|
|
|
|
autoCond = session.statement.CondDeleted(col)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlStr, args, err = rows.session.statement.GenFindSQL(autoCond)
|
2017-08-22 17:09:52 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-04 03:46:01 +05:30
|
|
|
} else {
|
2020-03-22 20:42:55 +05:30
|
|
|
sqlStr = rows.session.statement.GenRawSQL()
|
2017-08-22 17:09:52 +05:30
|
|
|
args = rows.session.statement.RawParams
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2017-09-13 10:48:22 +05:30
|
|
|
rows.rows, err = rows.session.queryRows(sqlStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
rows.Close()
|
|
|
|
return nil, err
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return rows, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next move cursor to next record, return false if end has reached
|
|
|
|
func (rows *Rows) Next() bool {
|
2021-09-16 23:23:28 +05:30
|
|
|
if rows.rows != nil {
|
|
|
|
return rows.rows.Next()
|
|
|
|
}
|
|
|
|
return false
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-09-16 23:23:28 +05:30
|
|
|
if rows.rows != nil {
|
|
|
|
return rows.rows.Err()
|
|
|
|
}
|
|
|
|
return nil
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Scan row record to bean properties
|
|
|
|
func (rows *Rows) Scan(bean interface{}) error {
|
2021-08-13 04:41:42 +05:30
|
|
|
if rows.Err() != nil {
|
|
|
|
return rows.Err()
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2019-06-23 20:52:43 +05:30
|
|
|
if reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
|
2016-11-04 03:46:01 +05:30
|
|
|
return fmt.Errorf("scan arg is incompatible type to [%v]", rows.beanType)
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
if err := rows.session.statement.SetRefBean(bean); err != nil {
|
2017-08-22 17:09:52 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-23 20:52:43 +05:30
|
|
|
fields, err := rows.rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-13 04:41:42 +05:30
|
|
|
types, err := rows.rows.ColumnTypes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-23 20:52:43 +05:30
|
|
|
|
2021-08-13 04:41:42 +05:30
|
|
|
scanResults, err := rows.session.row2Slice(rows.rows, fields, types, bean)
|
2017-08-22 17:09:52 +05:30
|
|
|
if err != nil {
|
2017-05-02 06:20:33 +05:30
|
|
|
return err
|
|
|
|
}
|
2017-04-07 07:17:25 +05:30
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
dataStruct := utils.ReflectValue(bean)
|
2019-06-23 20:52:43 +05:30
|
|
|
_, err = rows.session.slice2Bean(scanResults, fields, bean, &dataStruct, rows.session.statement.RefTable)
|
2017-10-01 22:22:35 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows.session.executeProcessors()
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Close session if session.IsAutoClose is true, and claimed any opened resources
|
|
|
|
func (rows *Rows) Close() error {
|
2017-08-22 17:09:52 +05:30
|
|
|
if rows.session.isAutoClose {
|
2016-11-04 03:46:01 +05:30
|
|
|
defer rows.session.Close()
|
|
|
|
}
|
|
|
|
|
2019-06-23 20:52:43 +05:30
|
|
|
if rows.rows != nil {
|
|
|
|
return rows.rows.Close()
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
2019-06-23 20:52:43 +05:30
|
|
|
|
2021-08-13 04:41:42 +05:30
|
|
|
return rows.Err()
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|