summaryrefslogtreecommitdiffstats
path: root/vendor/xorm.io/builder/builder_limit.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-06-23 23:22:43 +0800
committerLauris BH <lauris@nix.lv>2019-06-23 18:22:43 +0300
commitaa7c34cf86081b71f796f6e5da60e62a3cda43ce (patch)
treec9e4b4f1490b43e942fa71b37748072d3fe441e8 /vendor/xorm.io/builder/builder_limit.go
parentbaefea311f1a66a97f9a2779ad3342f4f8167d28 (diff)
downloadgitea-aa7c34cf86081b71f796f6e5da60e62a3cda43ce.tar.gz
gitea-aa7c34cf86081b71f796f6e5da60e62a3cda43ce.zip
Fix error log when loading issues caused by a xorm bug (#7271)
* fix error log when loading issues caused by a xorm bug * upgrade packages * fix fmt * fix Consistency * fix tests
Diffstat (limited to 'vendor/xorm.io/builder/builder_limit.go')
-rw-r--r--vendor/xorm.io/builder/builder_limit.go100
1 files changed, 100 insertions, 0 deletions
diff --git a/vendor/xorm.io/builder/builder_limit.go b/vendor/xorm.io/builder/builder_limit.go
new file mode 100644
index 0000000000..82435dacbd
--- /dev/null
+++ b/vendor/xorm.io/builder/builder_limit.go
@@ -0,0 +1,100 @@
+// Copyright 2018 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 builder
+
+import (
+ "fmt"
+ "strings"
+)
+
+func (b *Builder) limitWriteTo(w Writer) error {
+ if strings.TrimSpace(b.dialect) == "" {
+ return ErrDialectNotSetUp
+ }
+
+ if b.limitation != nil {
+ limit := b.limitation
+ if limit.offset < 0 || limit.limitN <= 0 {
+ return ErrInvalidLimitation
+ }
+ // erase limit condition
+ b.limitation = nil
+ ow := w.(*BytesWriter)
+
+ switch strings.ToLower(strings.TrimSpace(b.dialect)) {
+ case ORACLE:
+ if len(b.selects) == 0 {
+ b.selects = append(b.selects, "*")
+ }
+
+ var final *Builder
+ selects := b.selects
+ b.selects = append(selects, "ROWNUM RN")
+
+ var wb *Builder
+ if b.optype == unionType {
+ wb = Dialect(b.dialect).Select("at.*", "ROWNUM RN").
+ From(b, "at")
+ } else {
+ wb = b
+ }
+
+ if limit.offset == 0 {
+ final = Dialect(b.dialect).Select(selects...).From(wb, "at").
+ Where(Lte{"at.RN": limit.limitN})
+ } else {
+ sub := Dialect(b.dialect).Select("*").
+ From(b, "at").Where(Lte{"at.RN": limit.offset + limit.limitN})
+
+ final = Dialect(b.dialect).Select(selects...).From(sub, "att").
+ Where(Gt{"att.RN": limit.offset})
+ }
+
+ return final.WriteTo(ow)
+ case SQLITE, MYSQL, POSTGRES:
+ // if type UNION, we need to write previous content back to current writer
+ if b.optype == unionType {
+ if err := b.WriteTo(ow); err != nil {
+ return err
+ }
+ }
+
+ if limit.offset == 0 {
+ fmt.Fprint(ow, " LIMIT ", limit.limitN)
+ } else {
+ fmt.Fprintf(ow, " LIMIT %v OFFSET %v", limit.limitN, limit.offset)
+ }
+ case MSSQL:
+ if len(b.selects) == 0 {
+ b.selects = append(b.selects, "*")
+ }
+
+ var final *Builder
+ selects := b.selects
+ b.selects = append(append([]string{fmt.Sprintf("TOP %d %v", limit.limitN+limit.offset, b.selects[0])},
+ b.selects[1:]...), "ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RN")
+
+ var wb *Builder
+ if b.optype == unionType {
+ wb = Dialect(b.dialect).Select("*", "ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RN").
+ From(b, "at")
+ } else {
+ wb = b
+ }
+
+ if limit.offset == 0 {
+ final = Dialect(b.dialect).Select(selects...).From(wb, "at")
+ } else {
+ final = Dialect(b.dialect).Select(selects...).From(wb, "at").Where(Gt{"at.RN": limit.offset})
+ }
+
+ return final.WriteTo(ow)
+ default:
+ return ErrNotSupportType
+ }
+ }
+
+ return nil
+}