|
1 | 1 | package orm |
2 | 2 |
|
3 | | -import "strings" |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | +) |
4 | 8 |
|
5 | 9 | type SelectForUpdateType string |
6 | 10 |
|
7 | 11 | 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" |
11 | 15 | ) |
12 | 16 |
|
13 | 17 | 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) |
17 | 21 | } |
18 | 22 |
|
19 | 23 | 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) |
23 | 27 | } |
24 | 28 |
|
25 | 29 | 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:] |
31 | 35 |
|
32 | | - m.setErr(nq.result.Err) |
| 36 | + m.setErr(nq.result.Err) |
33 | 37 |
|
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 += ")" |
42 | 46 |
|
43 | | - newSelect += " as " + alias |
| 47 | + newSelect += " as " + alias |
44 | 48 |
|
45 | | - m.columns = append(m.columns, newSelect) |
46 | | - return m |
| 49 | + m.columns = append(m.columns, newSelect) |
| 50 | + return m |
47 | 51 | } |
48 | 52 |
|
49 | 53 | 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 |
53 | 57 | } |
54 | 58 |
|
55 | 59 | 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 |
58 | 62 | } |
59 | 63 |
|
60 | 64 | 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 |
67 | 79 | } |
0 commit comments