Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化informix分页查询语句,更优雅 #146

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,24 +247,6 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
if len(sb.selectCols) > 0 {
buf.WriteLeadingString("SELECT ")

if flavor == Informix {
if sb.offset >= 0 {
buf.WriteString("SKIP ")
buf.WriteString(strconv.Itoa(sb.offset))
buf.WriteString(" ")
}

if sb.limit > 0 {
if sb.offset < 0 {
buf.WriteString("SKIP 0 ")
}

buf.WriteString("FIRST ")
buf.WriteString(strconv.Itoa(sb.limit))
buf.WriteString(" ")
}
}

if sb.distinct {
buf.WriteString("DISTINCT ")
}
Expand Down Expand Up @@ -442,10 +424,16 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
}
}
case Informix:
// If ORDER BY is not set, sort column #1 by default.
// It's required to make OFFSET...FETCH work.
if len(sb.orderByCols) == 0 && (sb.limit >= 0 || sb.offset >= 0) {
buf.WriteLeadingString("ORDER BY 1")
// [SKIP N] FIRST M
// M must be greater than 0
if sb.limit > 0 {
if sb.offset >= 0 {
buf.WriteLeadingString("SKIP ")
buf.WriteString(strconv.Itoa(sb.offset))
}

buf.WriteLeadingString("FIRST ")
buf.WriteString(strconv.Itoa(sb.limit))
}
}

Expand Down
8 changes: 4 additions & 4 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ func ExampleSelectBuilder_limit_offset() {
//
// Informix
// #1: SELECT * FROM user
// #2: SELECT SKIP 0 * FROM user ORDER BY 1
// #3: SELECT SKIP 0 FIRST 1 * FROM user ORDER BY 1
// #4: SELECT SKIP 0 FIRST 1 * FROM user ORDER BY 1
// #5: SELECT SKIP 1 FIRST 1 * FROM user ORDER BY id
// #2: SELECT * FROM user
// #3: SELECT * FROM user SKIP 0 FIRST 1
// #4: SELECT * FROM user FIRST 1
// #5: SELECT * FROM user ORDER BY id SKIP 1 FIRST 1
}

func ExampleSelectBuilder_ForUpdate() {
Expand Down
Loading