Skip to content

Commit 6dfe337

Browse files
lunnyyifhaoningmeng520Guillermo PrandiGuillermo Prandi
committed
fix statement.LimitN(0) will delete or update all data (go-xorm#1119)
fix test fix nil pointer fix statement.Limit(0) will update or delete all data fix bug when buffersize with iterate (go-xorm#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 <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> 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 (go-xorm#1455) Co-authored-by: Guillermo Prandi <[email protected]> Reviewed-on: https://gitea.com/xorm/xorm/pulls/941 fix update map with version (go-xorm#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 (go-xorm#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 <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> 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 (go-xorm#1455) Improve ci tests (#1477) Rewrite Engine.QuoteTo() to accept multi-part identifiers (#1476) Support local sql log (go-xorm#1338) Fix go mod and update version (go-xorm#1460) Move github.com/go-xorm/xorm to xorm.io/xorm (go-xorm#1459) add support custom type Nullfloat64 (go-xorm#1450) fix bug when query map condtion with no quote (go-xorm#1449) Don't warn when bool column default is 1 but not true (go-xorm#1447) * don't warn when bool column default is 1 but not true * fix default case sensitive Fix sync2 with custom table name (go-xorm#1445) * fix sync2 with custom table name * fix bug on postgres * fix bug on postgres fix bug when update with setexpr (go-xorm#1446) add tidb tests on drone ci (go-xorm#1444) improve sync2 (go-xorm#1443) Fix wrong dbmetas (go-xorm#1442) * add tests for db metas * add more tests * fix bug on mssql Fix default value parse bugs (go-xorm#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 (go-xorm#1441) * fix arg conversion * fix bugs * fix bug on postgres * use traditional positional parameters on insert into select * remove unnecessary tests upgrade core (go-xorm#1440) add tests (go-xorm#1439) add go1.13 tests on drone (go-xorm#1416) Fix bug on insert where (go-xorm#1436) * fix bug on insert where * fix bug * fix lint fix bug when insert multiple slices with customize table name (go-xorm#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 (go-xorm#1432) fix setexpr missing big quotes (go-xorm#1431) * fix setexpr missing big quotes * fix tests * fix tests Add support subquery on SetExpr (go-xorm#1428) * add support subquery on SetExpr * fix tests fix go mod (go-xorm#1427) fix tests (go-xorm#1429) Use strings.Builder instead of builder.StringBuilder (go-xorm#1417) * use strings.Builder instead of builder.StringBuilder * fix dependency * fix dependency Remove unuse get cols code (go-xorm#1413) Add mssql ci test (go-xorm#1410) * add mssql ci test * fix drone test Add insert select where support (go-xorm#1401) Use drone new format (go-xorm#1388) * use drone new format fix get customize type bug (go-xorm#1382) fix bugs (go-xorm#1375) update drone (go-xorm#1374) Add tests for get var (go-xorm#1305) * add test for SQL get * fix tests fix error when get null var (go-xorm#890) * fix error when get null var * add support get for null var * fix bug Remove quotestr totally (go-xorm#1366) * remove QuoteStr() totally * update xorm.core -> v0.7.0 * update dialect Quote remove QuoteStr() usage in dialects (go-xorm#1364) document of FindAndCount() (go-xorm#1365) remove QuoteStr() usage (go-xorm#1360) Co-authored-by: yifhao <[email protected]> Co-authored-by: yifhao <[email protected]> Co-authored-by: Guillermo Prandi <[email protected]> Co-authored-by: Guillermo Prandi <guillep2k@[email protected]> Co-authored-by: yudppp <[email protected]> Co-authored-by: BetaCat <[email protected]> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1119
1 parent 14a0c19 commit 6dfe337

File tree

5 files changed

+40
-30
lines changed

5 files changed

+40
-30
lines changed

session_delete.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
101101
if err != nil {
102102
return 0, err
103103
}
104-
if len(condSQL) == 0 && session.statement.LimitN == 0 {
104+
pLimitN := session.statement.LimitN
105+
if len(condSQL) == 0 && (pLimitN == nil || *pLimitN == 0) {
105106
return 0, ErrNeedDeletedCond
106107
}
107108

@@ -119,8 +120,9 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
119120
if len(session.statement.OrderStr) > 0 {
120121
orderSQL += fmt.Sprintf(" ORDER BY %s", session.statement.OrderStr)
121122
}
122-
if session.statement.LimitN > 0 {
123-
orderSQL += fmt.Sprintf(" LIMIT %d", session.statement.LimitN)
123+
if pLimitN != nil && *pLimitN > 0 {
124+
limitNValue := *pLimitN
125+
orderSQL += fmt.Sprintf(" LIMIT %d", limitNValue)
124126
}
125127

126128
if len(orderSQL) > 0 {
@@ -139,7 +141,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
139141
} else {
140142
deleteSQL += " WHERE " + inSQL
141143
}
142-
// TODO: how to handle delete limit on mssql?
144+
// TODO: how to handle delete limit on mssql?
143145
case core.MSSQL:
144146
return 0, ErrNotImplemented
145147
default:
@@ -180,7 +182,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
180182
} else {
181183
realSQL += " WHERE " + inSQL
182184
}
183-
// TODO: how to handle delete limit on mssql?
185+
// TODO: how to handle delete limit on mssql?
184186
case core.MSSQL:
185187
return 0, ErrNotImplemented
186188
default:

session_iterate.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ func (session *Session) BufferSize(size int) *Session {
6363

6464
func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
6565
var bufferSize = session.statement.bufferSize
66-
var limit = session.statement.LimitN
67-
if limit > 0 && bufferSize > limit {
68-
bufferSize = limit
66+
var pLimitN = session.statement.LimitN
67+
if pLimitN != nil && bufferSize > *pLimitN {
68+
bufferSize = *pLimitN
6969
}
7070
var start = session.statement.Start
7171
v := rValue(bean)
@@ -94,8 +94,8 @@ func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
9494
}
9595

9696
start = start + slice.Elem().Len()
97-
if limit > 0 && start+bufferSize > limit {
98-
bufferSize = limit - start
97+
if pLimitN != nil && start+bufferSize > *pLimitN {
98+
bufferSize = *pLimitN - start
9999
}
100100
}
101101

session_update.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,12 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
337337
var tableName = session.statement.TableName()
338338
// TODO: Oracle support needed
339339
var top string
340-
if st.LimitN > 0 {
340+
if st.LimitN != nil {
341+
limitValue := *st.LimitN
341342
if st.Engine.dialect.DBType() == core.MYSQL {
342-
condSQL = condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN)
343+
condSQL = condSQL + fmt.Sprintf(" LIMIT %d", limitValue)
343344
} else if st.Engine.dialect.DBType() == core.SQLITE {
344-
tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN)
345+
tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", limitValue)
345346
cond = cond.And(builder.Expr(fmt.Sprintf("rowid IN (SELECT rowid FROM %v %v)",
346347
session.engine.Quote(tableName), tempCondSQL), condArgs...))
347348
condSQL, condArgs, err = builder.ToSQL(cond)
@@ -352,7 +353,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
352353
condSQL = "WHERE " + condSQL
353354
}
354355
} else if st.Engine.dialect.DBType() == core.POSTGRES {
355-
tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN)
356+
tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", limitValue)
356357
cond = cond.And(builder.Expr(fmt.Sprintf("CTID IN (SELECT CTID FROM %v %v)",
357358
session.engine.Quote(tableName), tempCondSQL), condArgs...))
358359
condSQL, condArgs, err = builder.ToSQL(cond)
@@ -367,7 +368,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
367368
if st.OrderStr != "" && st.Engine.dialect.DBType() == core.MSSQL &&
368369
table != nil && len(table.PrimaryKeys) == 1 {
369370
cond = builder.Expr(fmt.Sprintf("%s IN (SELECT TOP (%d) %s FROM %v%v)",
370-
table.PrimaryKeys[0], st.LimitN, table.PrimaryKeys[0],
371+
table.PrimaryKeys[0], limitValue, table.PrimaryKeys[0],
371372
session.engine.Quote(tableName), condSQL), condArgs...)
372373

373374
condSQL, condArgs, err = builder.ToSQL(cond)
@@ -378,7 +379,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
378379
condSQL = "WHERE " + condSQL
379380
}
380381
} else {
381-
top = fmt.Sprintf("TOP (%d) ", st.LimitN)
382+
top = fmt.Sprintf("TOP (%d) ", limitValue)
382383
}
383384
}
384385
}

statement.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Statement struct {
2020
RefTable *core.Table
2121
Engine *Engine
2222
Start int
23-
LimitN int
23+
LimitN *int
2424
idParam *core.PK
2525
OrderStr string
2626
JoinStr string
@@ -65,7 +65,7 @@ type Statement struct {
6565
func (statement *Statement) Init() {
6666
statement.RefTable = nil
6767
statement.Start = 0
68-
statement.LimitN = 0
68+
statement.LimitN = nil
6969
statement.OrderStr = ""
7070
statement.UseCascade = true
7171
statement.JoinStr = ""
@@ -671,7 +671,7 @@ func (statement *Statement) Top(limit int) *Statement {
671671

672672
// Limit generate LIMIT start, limit statement
673673
func (statement *Statement) Limit(limit int, start ...int) *Statement {
674-
statement.LimitN = limit
674+
statement.LimitN = &limit
675675
if len(start) > 0 {
676676
statement.Start = start[0]
677677
}
@@ -1071,9 +1071,11 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n
10711071
fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr)
10721072
}
10731073

1074+
pLimitN := statement.LimitN
10741075
if dialect.DBType() == core.MSSQL {
1075-
if statement.LimitN > 0 {
1076-
top = fmt.Sprintf("TOP %d ", statement.LimitN)
1076+
if pLimitN != nil {
1077+
LimitNValue := *pLimitN
1078+
top = fmt.Sprintf("TOP %d ", LimitNValue)
10771079
}
10781080
if statement.Start > 0 {
10791081
var column string
@@ -1134,20 +1136,24 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n
11341136
if needLimit {
11351137
if dialect.DBType() != core.MSSQL && dialect.DBType() != core.ORACLE {
11361138
if statement.Start > 0 {
1137-
fmt.Fprintf(&buf, " LIMIT %v OFFSET %v", statement.LimitN, statement.Start)
1138-
} else if statement.LimitN > 0 {
1139-
fmt.Fprint(&buf, " LIMIT ", statement.LimitN)
1139+
if pLimitN != nil {
1140+
fmt.Fprintf(&buf, " LIMIT %v OFFSET %v", *pLimitN, statement.Start)
1141+
} else {
1142+
fmt.Fprintf(&buf, "LIMIT 0 OFFSET %v", statement.Start)
1143+
}
1144+
} else if pLimitN != nil {
1145+
fmt.Fprint(&buf, " LIMIT ", *pLimitN)
11401146
}
11411147
} else if dialect.DBType() == core.ORACLE {
1142-
if statement.Start != 0 || statement.LimitN != 0 {
1148+
if statement.Start != 0 || pLimitN != nil {
11431149
oldString := buf.String()
11441150
buf.Reset()
11451151
rawColStr := columnStr
11461152
if rawColStr == "*" {
11471153
rawColStr = "at.*"
11481154
}
11491155
fmt.Fprintf(&buf, "SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d",
1150-
columnStr, rawColStr, oldString, statement.Start+statement.LimitN, statement.Start)
1156+
columnStr, rawColStr, oldString, statement.Start+*pLimitN, statement.Start)
11511157
}
11521158
}
11531159
}
@@ -1204,8 +1210,9 @@ func (statement *Statement) convertIDSQL(sqlStr string) string {
12041210
}
12051211

12061212
var top string
1207-
if statement.LimitN > 0 && statement.Engine.dialect.DBType() == core.MSSQL {
1208-
top = fmt.Sprintf("TOP %d ", statement.LimitN)
1213+
pLimitN := statement.LimitN
1214+
if pLimitN != nil && statement.Engine.dialect.DBType() == core.MSSQL {
1215+
top = fmt.Sprintf("TOP %d ", *pLimitN)
12091216
}
12101217

12111218
newsql := fmt.Sprintf("SELECT %s%s FROM %v", top, colstrs, sqls[1])

tag_id_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ package xorm
77
import (
88
"testing"
99

10-
"xorm.io/core"
1110
"github.com/stretchr/testify/assert"
11+
"xorm.io/core"
1212
)
1313

1414
type IDGonicMapper struct {
@@ -76,7 +76,7 @@ func TestSameMapperID(t *testing.T) {
7676
for _, tb := range tables {
7777
if tb.Name == "IDSameMapper" {
7878
if len(tb.PKColumns()) != 1 || tb.PKColumns()[0].Name != "ID" {
79-
t.Fatal(tb)
79+
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)
8080
}
8181
return
8282
}

0 commit comments

Comments
 (0)