Browse Source

Fix rows bug (#1576)

Fix rows bug

Reviewed-on: #1576
tags/v1.0.0
Lunny Xiao 3 weeks ago
parent
commit
0f166d82da
3 changed files with 55 additions and 2 deletions
  1. +37
    -1
      rows.go
  2. +15
    -1
      rows_test.go
  3. +3
    -0
      session_raw.go

+ 37
- 1
rows.go View File

@@ -6,9 +6,11 @@ package xorm

import (
"database/sql"
"errors"
"fmt"
"reflect"

"xorm.io/builder"
"xorm.io/xorm/core"
"xorm.io/xorm/internal/utils"
)
@@ -30,6 +32,13 @@ func newRows(session *Session, bean interface{}) (*Rows, error) {
var args []interface{}
var err error

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 {
return nil, err
}
@@ -39,7 +48,34 @@ func newRows(session *Session, bean interface{}) (*Rows, error) {
}

if rows.session.statement.RawSQL == "" {
sqlStr, args, err = rows.session.statement.GenGetSQL(bean)
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
var colName = session.engine.Quote(col.Name)
if addedTableName {
var nm = session.statement.TableName()
if len(session.statement.TableAlias) > 0 {
nm = session.statement.TableAlias
}
colName = session.engine.Quote(nm) + "." + colName
}

autoCond = session.statement.CondDeleted(col)
}
}

sqlStr, args, err = rows.session.statement.GenFindSQL(autoCond)
if err != nil {
return nil, err
}


+ 15
- 1
rows_test.go View File

@@ -104,7 +104,6 @@ func TestRowsMyTableName(t *testing.T) {

rows, err := testEngine.Table(tableName).Rows(new(UserRowsMyTable))
assert.NoError(t, err)
defer rows.Close()

cnt = 0
user := new(UserRowsMyTable)
@@ -114,6 +113,21 @@ func TestRowsMyTableName(t *testing.T) {
cnt++
}
assert.EqualValues(t, 1, cnt)

rows.Close()

rows, err = testEngine.Table(tableName).Rows(&UserRowsMyTable{
Id: 2,
})
assert.NoError(t, err)
cnt = 0
user = new(UserRowsMyTable)
for rows.Next() {
err = rows.Scan(user)
assert.NoError(t, err)
cnt++
}
assert.EqualValues(t, 0, cnt)
}

type UserRowsSpecTable struct {


+ 3
- 0
session_raw.go View File

@@ -23,6 +23,9 @@ func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{})

func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Rows, error) {
defer session.resetStatement()
if session.statement.LastError != nil {
return nil, session.statement.LastError
}

session.queryPreprocess(&sqlStr, args...)



Loading…
Cancel
Save