File tree Expand file tree Collapse file tree 3 files changed +23
-3
lines changed Expand file tree Collapse file tree 3 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -81,7 +81,12 @@ func SelectOne[T any](q *Query[T]) (*gorm.DB, T) {
81
81
82
82
func SelectList [T any ](q * Query [T ]) (* gorm.DB , []T ) {
83
83
var results []T
84
- resultDb := GormDb .Order (q .OrderBuilder .String ()).Select (q .Columns ).Where (q .QueryBuilder .String (), q .Args ... ).Find (& results )
84
+ resultDb := GormDb .Select (q .Columns ).Where (q .QueryBuilder .String (), q .Args ... ).
85
+ Order (q .OrderBuilder .String ())
86
+ if q .GroupBuilder .Len () > 0 {
87
+ resultDb .Group (q .GroupBuilder .String ())
88
+ }
89
+ resultDb .Find (& results )
85
90
return resultDb , results
86
91
}
87
92
Original file line number Diff line number Diff line change 1
1
package gormplus
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"gorm.io/driver/mysql"
6
7
"gorm.io/gorm"
@@ -116,10 +117,13 @@ func TestSelectOne(t *testing.T) {
116
117
117
118
func TestSelectList (t * testing.T ) {
118
119
q := Query [Test1 ]{}
119
- q .OrderByDesc ("price" ). OrderByAsc ( "code" )
120
+ q .Group ("price" , "code" )
120
121
db , result := SelectList (& q )
121
122
fmt .Println (db .RowsAffected )
122
- fmt .Println (result )
123
+ for _ , v := range result {
124
+ marshal , _ := json .Marshal (v )
125
+ fmt .Println (string (marshal ))
126
+ }
123
127
}
124
128
125
129
func TestSelectCount (t * testing.T ) {
Original file line number Diff line number Diff line change 9
9
type Query [T any ] struct {
10
10
Columns []string
11
11
OrderBuilder strings.Builder
12
+ GroupBuilder strings.Builder
12
13
QueryBuilder strings.Builder
13
14
Args []any
14
15
LastCond string
@@ -102,6 +103,16 @@ func (q *Query[T]) OrderByAsc(columns ...string) *Query[T] {
102
103
return q
103
104
}
104
105
106
+ func (q * Query [T ]) Group (columns ... string ) * Query [T ] {
107
+ for _ , v := range columns {
108
+ if q .GroupBuilder .Len () > 0 {
109
+ q .GroupBuilder .WriteString (constants .COMMA )
110
+ }
111
+ q .GroupBuilder .WriteString (v )
112
+ }
113
+ return q
114
+ }
115
+
105
116
func (q * Query [T ]) addCond (column string , val any , condType string ) {
106
117
if q .LastCond != constants .And && q .LastCond != constants .Or && q .QueryBuilder .Len () > 0 {
107
118
q .QueryBuilder .WriteString (constants .And )
You can’t perform that action at this time.
0 commit comments