Skip to content

Commit

Permalink
🩹 修改 Oracle12c 及以上版本 limit 和 offset 使用绑定参数
Browse files Browse the repository at this point in the history
Ref: gorm#6806

Signed-off-by: liutianqi <[email protected]>
  • Loading branch information
iTanken committed Jan 30, 2024
1 parent d8b991d commit 11f8df9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ func (d Dialector) RewriteLimit(c clause.Clause, builder clause.Builder) {

if offset := limit.Offset; offset > 0 {
_, _ = builder.WriteString(" OFFSET ")
_, _ = builder.WriteString(strconv.Itoa(offset))
builder.AddVar(builder, offset)
_, _ = builder.WriteString(" ROWS")
}
if hasLimit {
_, _ = builder.WriteString(" FETCH NEXT ")
_, _ = builder.WriteString(strconv.Itoa(limitRows))
builder.AddVar(builder, limitRows)
_, _ = builder.WriteString(" ROWS ONLY")
}
}
Expand Down
21 changes: 21 additions & 0 deletions oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package oracle

import (
"database/sql"
"encoding/json"
"log"
"os"
"reflect"
Expand Down Expand Up @@ -134,6 +135,26 @@ func TestCountLimit0(t *testing.T) {
}
}

func TestLimit(t *testing.T) {
db, err := dbNamingCase, dbErrors[0]
if err != nil {
t.Fatal(err)
}
if db == nil {
t.Log("db is nil!")
return
}
TestMergeCreate(t)

var data []TestTableUser
result := db.Model(&TestTableUser{}).Limit(10).Find(&data)
if err = result.Error; err != nil {
t.Fatal(err)
}
dataBytes, _ := json.MarshalIndent(data, "", " ")
t.Logf("Limit(10) got size = %d, data = %s", len(data), dataBytes)
}

func TestAddSessionParams(t *testing.T) {
db, err := dbIgnoreCase, dbErrors[1]
if err != nil {
Expand Down

0 comments on commit 11f8df9

Please sign in to comment.