fix statement.LimitN(0) will delete or update all data (#1119)

fix test

fix nil pointer

fix statement.Limit(0) will update or delete all data

fix bug when buffersize with iterate (#941)

Merge branch 'master' into lunny/fix_buffer_iterate

Exclude schema from index name (#1505)

Merge branch 'master' into fix-schema-idx

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499

fix vet

fix drone lint

remove go1.10 test on drone

Exclude schema from the index name

Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505

fix test

fix bug

fix bug when buffersize with iterate

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499

fix vet

fix drone lint

remove go1.10 test on drone

Fix update with Alias (#1455)

Co-authored-by: Guillermo Prandi <guillep2k@noreply.gitea.io>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/941

fix update map with version (#1448)

fix test

fix update map with version

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499

fix vet

fix drone lint

remove go1.10 test on drone

Fix update with Alias (#1455)

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1448

Exclude schema from index name (#1505)

Merge branch 'master' into fix-schema-idx

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499

fix vet

fix drone lint

remove go1.10 test on drone

Exclude schema from the index name

Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499

fix vet

fix drone lint

remove go1.10 test on drone

Fix update with Alias (#1455)

Improve ci tests (#1477)

Rewrite Engine.QuoteTo() to accept multi-part identifiers (#1476)

Support local sql log (#1338)

Fix go mod and update version (#1460)

Move github.com/go-xorm/xorm to xorm.io/xorm (#1459)

add support custom type Nullfloat64 (#1450)

fix bug when query map condtion with no quote (#1449)

Don't warn when bool column default is 1 but not true (#1447)

* don't warn when bool column default is 1 but not true

* fix default case sensitive

Fix sync2 with custom table name (#1445)

* fix sync2 with custom table name

* fix bug on postgres

* fix bug on postgres

fix bug when update with setexpr (#1446)

add tidb tests on drone ci (#1444)

improve sync2 (#1443)

Fix wrong dbmetas (#1442)

* add tests for db metas

* add more tests

* fix bug on mssql

Fix default value parse bugs (#1437)

* fix default value

* fix default value tags

* fix postgres default

* fix default on postgres

* fix default on postgres

* fix mssql default

fix arg conversion (#1441)

* fix arg conversion

* fix bugs

* fix bug on postgres

* use traditional positional parameters on insert into select

* remove unnecessary tests

upgrade core (#1440)

add tests (#1439)

add go1.13 tests on drone (#1416)

Fix bug on insert where (#1436)

* fix bug on insert where

* fix bug

* fix lint

fix bug when insert multiple slices with customize table name (#1433)

* fix bug when insert multiple slices with customize table name

* fix tests on mssql

* fix tests

fix insert where with bool bug on mssql (#1432)

fix setexpr missing big quotes (#1431)

* fix setexpr missing big quotes

* fix tests

* fix tests

Add support subquery on SetExpr (#1428)

* add support subquery on SetExpr

* fix tests

fix go mod (#1427)

fix tests (#1429)

Use strings.Builder instead of builder.StringBuilder (#1417)

* use strings.Builder instead of builder.StringBuilder

* fix dependency

* fix dependency

Remove unuse get cols code (#1413)

Add mssql ci test (#1410)

* add mssql ci test

* fix drone test

Add insert select where support (#1401)

Use drone new format (#1388)

* use drone new format
fix get customize type bug (#1382)

fix bugs (#1375)

update drone (#1374)

Add tests for get var (#1305)

* add test for SQL get
* fix tests

fix error when get null var (#890)

* fix error when get null var
* add support get for null var
* fix bug

Remove quotestr totally (#1366)

* remove QuoteStr() totally

* update xorm.core -> v0.7.0

* update dialect Quote

remove QuoteStr() usage in dialects (#1364)

document of FindAndCount() (#1365)

remove QuoteStr() usage (#1360)

Co-authored-by: yifhao <yifhao@tencent.com>
Co-authored-by: yifhao <1124210681@qq.com>
Co-authored-by: Guillermo Prandi <guillep2k@noreply.gitea.io>
Co-authored-by: Guillermo Prandi <guillep2k@noreply@gitea.io>
Co-authored-by: yudppp <yu.d.ppp@gmail.com>
Co-authored-by: BetaCat <outman99@hotmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/1119
This commit is contained in:
Lunny Xiao 2020-01-20 08:22:06 +00:00
parent 14a0c19a0c
commit 6dfe337869
5 changed files with 40 additions and 30 deletions

View File

@ -101,7 +101,8 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
if len(condSQL) == 0 && session.statement.LimitN == 0 { pLimitN := session.statement.LimitN
if len(condSQL) == 0 && (pLimitN == nil || *pLimitN == 0) {
return 0, ErrNeedDeletedCond return 0, ErrNeedDeletedCond
} }
@ -119,8 +120,9 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
if len(session.statement.OrderStr) > 0 { if len(session.statement.OrderStr) > 0 {
orderSQL += fmt.Sprintf(" ORDER BY %s", session.statement.OrderStr) orderSQL += fmt.Sprintf(" ORDER BY %s", session.statement.OrderStr)
} }
if session.statement.LimitN > 0 { if pLimitN != nil && *pLimitN > 0 {
orderSQL += fmt.Sprintf(" LIMIT %d", session.statement.LimitN) limitNValue := *pLimitN
orderSQL += fmt.Sprintf(" LIMIT %d", limitNValue)
} }
if len(orderSQL) > 0 { if len(orderSQL) > 0 {

View File

@ -63,9 +63,9 @@ func (session *Session) BufferSize(size int) *Session {
func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error { func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
var bufferSize = session.statement.bufferSize var bufferSize = session.statement.bufferSize
var limit = session.statement.LimitN var pLimitN = session.statement.LimitN
if limit > 0 && bufferSize > limit { if pLimitN != nil && bufferSize > *pLimitN {
bufferSize = limit bufferSize = *pLimitN
} }
var start = session.statement.Start var start = session.statement.Start
v := rValue(bean) v := rValue(bean)
@ -94,8 +94,8 @@ func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
} }
start = start + slice.Elem().Len() start = start + slice.Elem().Len()
if limit > 0 && start+bufferSize > limit { if pLimitN != nil && start+bufferSize > *pLimitN {
bufferSize = limit - start bufferSize = *pLimitN - start
} }
} }

View File

@ -337,11 +337,12 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
var tableName = session.statement.TableName() var tableName = session.statement.TableName()
// TODO: Oracle support needed // TODO: Oracle support needed
var top string var top string
if st.LimitN > 0 { if st.LimitN != nil {
limitValue := *st.LimitN
if st.Engine.dialect.DBType() == core.MYSQL { if st.Engine.dialect.DBType() == core.MYSQL {
condSQL = condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN) condSQL = condSQL + fmt.Sprintf(" LIMIT %d", limitValue)
} else if st.Engine.dialect.DBType() == core.SQLITE { } else if st.Engine.dialect.DBType() == core.SQLITE {
tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN) tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", limitValue)
cond = cond.And(builder.Expr(fmt.Sprintf("rowid IN (SELECT rowid FROM %v %v)", cond = cond.And(builder.Expr(fmt.Sprintf("rowid IN (SELECT rowid FROM %v %v)",
session.engine.Quote(tableName), tempCondSQL), condArgs...)) session.engine.Quote(tableName), tempCondSQL), condArgs...))
condSQL, condArgs, err = builder.ToSQL(cond) condSQL, condArgs, err = builder.ToSQL(cond)
@ -352,7 +353,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
condSQL = "WHERE " + condSQL condSQL = "WHERE " + condSQL
} }
} else if st.Engine.dialect.DBType() == core.POSTGRES { } else if st.Engine.dialect.DBType() == core.POSTGRES {
tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN) tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", limitValue)
cond = cond.And(builder.Expr(fmt.Sprintf("CTID IN (SELECT CTID FROM %v %v)", cond = cond.And(builder.Expr(fmt.Sprintf("CTID IN (SELECT CTID FROM %v %v)",
session.engine.Quote(tableName), tempCondSQL), condArgs...)) session.engine.Quote(tableName), tempCondSQL), condArgs...))
condSQL, condArgs, err = builder.ToSQL(cond) condSQL, condArgs, err = builder.ToSQL(cond)
@ -367,7 +368,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
if st.OrderStr != "" && st.Engine.dialect.DBType() == core.MSSQL && if st.OrderStr != "" && st.Engine.dialect.DBType() == core.MSSQL &&
table != nil && len(table.PrimaryKeys) == 1 { table != nil && len(table.PrimaryKeys) == 1 {
cond = builder.Expr(fmt.Sprintf("%s IN (SELECT TOP (%d) %s FROM %v%v)", cond = builder.Expr(fmt.Sprintf("%s IN (SELECT TOP (%d) %s FROM %v%v)",
table.PrimaryKeys[0], st.LimitN, table.PrimaryKeys[0], table.PrimaryKeys[0], limitValue, table.PrimaryKeys[0],
session.engine.Quote(tableName), condSQL), condArgs...) session.engine.Quote(tableName), condSQL), condArgs...)
condSQL, condArgs, err = builder.ToSQL(cond) condSQL, condArgs, err = builder.ToSQL(cond)
@ -378,7 +379,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
condSQL = "WHERE " + condSQL condSQL = "WHERE " + condSQL
} }
} else { } else {
top = fmt.Sprintf("TOP (%d) ", st.LimitN) top = fmt.Sprintf("TOP (%d) ", limitValue)
} }
} }
} }

View File

@ -20,7 +20,7 @@ type Statement struct {
RefTable *core.Table RefTable *core.Table
Engine *Engine Engine *Engine
Start int Start int
LimitN int LimitN *int
idParam *core.PK idParam *core.PK
OrderStr string OrderStr string
JoinStr string JoinStr string
@ -65,7 +65,7 @@ type Statement struct {
func (statement *Statement) Init() { func (statement *Statement) Init() {
statement.RefTable = nil statement.RefTable = nil
statement.Start = 0 statement.Start = 0
statement.LimitN = 0 statement.LimitN = nil
statement.OrderStr = "" statement.OrderStr = ""
statement.UseCascade = true statement.UseCascade = true
statement.JoinStr = "" statement.JoinStr = ""
@ -671,7 +671,7 @@ func (statement *Statement) Top(limit int) *Statement {
// Limit generate LIMIT start, limit statement // Limit generate LIMIT start, limit statement
func (statement *Statement) Limit(limit int, start ...int) *Statement { func (statement *Statement) Limit(limit int, start ...int) *Statement {
statement.LimitN = limit statement.LimitN = &limit
if len(start) > 0 { if len(start) > 0 {
statement.Start = start[0] statement.Start = start[0]
} }
@ -1071,9 +1071,11 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n
fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr) fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr)
} }
pLimitN := statement.LimitN
if dialect.DBType() == core.MSSQL { if dialect.DBType() == core.MSSQL {
if statement.LimitN > 0 { if pLimitN != nil {
top = fmt.Sprintf("TOP %d ", statement.LimitN) LimitNValue := *pLimitN
top = fmt.Sprintf("TOP %d ", LimitNValue)
} }
if statement.Start > 0 { if statement.Start > 0 {
var column string var column string
@ -1134,12 +1136,16 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n
if needLimit { if needLimit {
if dialect.DBType() != core.MSSQL && dialect.DBType() != core.ORACLE { if dialect.DBType() != core.MSSQL && dialect.DBType() != core.ORACLE {
if statement.Start > 0 { if statement.Start > 0 {
fmt.Fprintf(&buf, " LIMIT %v OFFSET %v", statement.LimitN, statement.Start) if pLimitN != nil {
} else if statement.LimitN > 0 { fmt.Fprintf(&buf, " LIMIT %v OFFSET %v", *pLimitN, statement.Start)
fmt.Fprint(&buf, " LIMIT ", statement.LimitN) } else {
fmt.Fprintf(&buf, "LIMIT 0 OFFSET %v", statement.Start)
}
} else if pLimitN != nil {
fmt.Fprint(&buf, " LIMIT ", *pLimitN)
} }
} else if dialect.DBType() == core.ORACLE { } else if dialect.DBType() == core.ORACLE {
if statement.Start != 0 || statement.LimitN != 0 { if statement.Start != 0 || pLimitN != nil {
oldString := buf.String() oldString := buf.String()
buf.Reset() buf.Reset()
rawColStr := columnStr rawColStr := columnStr
@ -1147,7 +1153,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n
rawColStr = "at.*" rawColStr = "at.*"
} }
fmt.Fprintf(&buf, "SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d", fmt.Fprintf(&buf, "SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d",
columnStr, rawColStr, oldString, statement.Start+statement.LimitN, statement.Start) columnStr, rawColStr, oldString, statement.Start+*pLimitN, statement.Start)
} }
} }
} }
@ -1204,8 +1210,9 @@ func (statement *Statement) convertIDSQL(sqlStr string) string {
} }
var top string var top string
if statement.LimitN > 0 && statement.Engine.dialect.DBType() == core.MSSQL { pLimitN := statement.LimitN
top = fmt.Sprintf("TOP %d ", statement.LimitN) if pLimitN != nil && statement.Engine.dialect.DBType() == core.MSSQL {
top = fmt.Sprintf("TOP %d ", *pLimitN)
} }
newsql := fmt.Sprintf("SELECT %s%s FROM %v", top, colstrs, sqls[1]) newsql := fmt.Sprintf("SELECT %s%s FROM %v", top, colstrs, sqls[1])

View File

@ -7,8 +7,8 @@ package xorm
import ( import (
"testing" "testing"
"xorm.io/core"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"xorm.io/core"
) )
type IDGonicMapper struct { type IDGonicMapper struct {
@ -76,7 +76,7 @@ func TestSameMapperID(t *testing.T) {
for _, tb := range tables { for _, tb := range tables {
if tb.Name == "IDSameMapper" { if tb.Name == "IDSameMapper" {
if len(tb.PKColumns()) != 1 || tb.PKColumns()[0].Name != "ID" { if len(tb.PKColumns()) != 1 || tb.PKColumns()[0].Name != "ID" {
t.Fatal(tb) t.Fatalf("tb %s tb.PKColumns() is %d not 1, tb.PKColumns()[0].Name is %s not ID", tb.Name, len(tb.PKColumns()), tb.PKColumns()[0].Name)
} }
return return
} }