Skip to content

Commit 19b983c

Browse files
committed
select with timeout
1 parent 7821c27 commit 19b983c

File tree

3 files changed

+57
-40
lines changed

3 files changed

+57
-40
lines changed

orm/query.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Query[T Table] struct {
3636
withCtes []SubQuery
3737
windows []SubQuery
3838
self *Query[SubQuery]
39+
selectTimeout string
3940
}
4041

4142
//query table[struct] generics

orm/query_select.go

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,79 @@
11
package orm
22

3-
import "strings"
3+
import (
4+
"strconv"
5+
"strings"
6+
"time"
7+
)
48

59
type SelectForUpdateType string
610

711
const (
8-
SelectForUpdateTypeDefault SelectForUpdateType = "for update"
9-
SelectForUpdateTypeNowait SelectForUpdateType = "for update nowait"
10-
SelectForUpdateTypeSkipLocked SelectForUpdateType = "for update skip locked"
12+
SelectForUpdateTypeDefault SelectForUpdateType = "for update"
13+
SelectForUpdateTypeNowait SelectForUpdateType = "for update nowait"
14+
SelectForUpdateTypeSkipLocked SelectForUpdateType = "for update skip locked"
1115
)
1216

1317
func (m Query[T]) SelectRank(column interface{}, alias string) Query[T] {
14-
return m.SelectOver("rank()", func(query Query[T]) Query[T] {
15-
return query.OrderBy(column)
16-
}, alias)
18+
return m.SelectOver("rank()", func(query Query[T]) Query[T] {
19+
return query.OrderBy(column)
20+
}, alias)
1721
}
1822

1923
func (m Query[T]) SelectRankDesc(column interface{}, alias string) Query[T] {
20-
return m.SelectOver("rank()", func(query Query[T]) Query[T] {
21-
return query.OrderByDesc(column)
22-
}, alias)
24+
return m.SelectOver("rank()", func(query Query[T]) Query[T] {
25+
return query.OrderByDesc(column)
26+
}, alias)
2327
}
2428

2529
func (m Query[T]) SelectOver(windowFunc string, f func(query Query[T]) Query[T], alias string) Query[T] {
26-
partitionStart := len(m.partitionbys)
27-
orderStart := len(m.orderbys)
28-
nq := f(m)
29-
partitions := nq.partitionbys[partitionStart:]
30-
orders := nq.orderbys[orderStart:]
30+
partitionStart := len(m.partitionbys)
31+
orderStart := len(m.orderbys)
32+
nq := f(m)
33+
partitions := nq.partitionbys[partitionStart:]
34+
orders := nq.orderbys[orderStart:]
3135

32-
m.setErr(nq.result.Err)
36+
m.setErr(nq.result.Err)
3337

34-
newSelect := windowFunc + " over ("
35-
if len(partitions) > 0 {
36-
newSelect += "partition by " + strings.Join(partitions, ",") + " "
37-
}
38-
if len(orders) > 0 {
39-
newSelect += "order by " + strings.Join(orders, ",")
40-
}
41-
newSelect += ")"
38+
newSelect := windowFunc + " over ("
39+
if len(partitions) > 0 {
40+
newSelect += "partition by " + strings.Join(partitions, ",") + " "
41+
}
42+
if len(orders) > 0 {
43+
newSelect += "order by " + strings.Join(orders, ",")
44+
}
45+
newSelect += ")"
4246

43-
newSelect += " as " + alias
47+
newSelect += " as " + alias
4448

45-
m.columns = append(m.columns, newSelect)
46-
return m
49+
m.columns = append(m.columns, newSelect)
50+
return m
4751
}
4852

4953
func (m Query[T]) SelectOverRaw(windowFunc string, windowName string, alias string) Query[T] {
50-
newSelect := windowFunc + " over " + windowName + " as " + alias
51-
m.columns = append(m.columns, newSelect)
52-
return m
54+
newSelect := windowFunc + " over " + windowName + " as " + alias
55+
m.columns = append(m.columns, newSelect)
56+
return m
5357
}
5458

5559
func (m Query[T]) Select(columns ...interface{}) Query[T] {
56-
m.columns = append(m.columns, columns...)
57-
return m
60+
m.columns = append(m.columns, columns...)
61+
return m
5862
}
5963

6064
func (m Query[T]) ForUpdate(forUpdateType ...SelectForUpdateType) Query[T] {
61-
if len(forUpdateType) == 0 {
62-
m.forUpdate = SelectForUpdateTypeDefault
63-
} else {
64-
m.forUpdate = forUpdateType[0]
65-
}
66-
return m
65+
if len(forUpdateType) == 0 {
66+
m.forUpdate = SelectForUpdateTypeDefault
67+
} else {
68+
m.forUpdate = forUpdateType[0]
69+
}
70+
return m
71+
}
72+
73+
func (m Query[T]) SelectWithTimeout(duration time.Duration) Query[T] {
74+
ms := duration.Milliseconds()
75+
if ms > 0 {
76+
m.selectTimeout = "/*+ MAX_EXECUTION_TIME(+" + strconv.FormatInt(ms, 10) + "+) */"
77+
}
78+
return m
6779
}

orm/query_select_gen.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ func (m Query[T]) SubQuery() SubQuery {
1818
tempTable.err = mt.result.Err
1919

2020
return tempTable
21-
2221
} else {
2322
mt := m
2423
tempTable := mt.generateSelectQuery(mt.columns...)
@@ -84,7 +83,12 @@ func (m Query[T]) generateSelectQuery(columns ...interface{}) SubQuery {
8483

8584
orderLimitOffsetStr := m.getOrderAndLimitSqlStr()
8685

87-
rawSql += "select " + selectStr
86+
var selectKeyword = "select"
87+
if m.selectTimeout != "" {
88+
selectKeyword += " " + m.selectTimeout
89+
}
90+
91+
rawSql += selectKeyword + " " + selectStr
8892

8993
if tableStr != "" {
9094
rawSql += " from " + tableStr

0 commit comments

Comments
 (0)