@@ -11,8 +11,8 @@ type OrderByBuilder struct {
11
11
allowedColumns []string
12
12
}
13
13
14
- // OrderBy create an OrderByBuilder with allowed columns to prevent sql injection. NB: any input is allowed if it is not provided
15
- func (b * Builder ) OrderBy (allowedColumns ... string ) * OrderByBuilder {
14
+ // Order create an OrderByBuilder with allowed columns to prevent sql injection. NB: any input is allowed if it is not provided
15
+ func (b * Builder ) Order (allowedColumns ... string ) * OrderByBuilder {
16
16
ob := & OrderByBuilder {
17
17
Builder : b ,
18
18
allowedColumns : allowedColumns ,
@@ -34,8 +34,35 @@ func (ob *OrderByBuilder) isAllowed(col string) bool {
34
34
})
35
35
}
36
36
37
- // Asc order by ASC with columns
38
- func (ob * OrderByBuilder ) Asc (columns ... string ) * OrderByBuilder {
37
+ // By order by raw sql. eg By("a asc, b desc")
38
+ func (ob * OrderByBuilder ) By (raw string ) * OrderByBuilder {
39
+ cols := strings .Split (raw , "," )
40
+
41
+ var n int
42
+ var items []string
43
+ var by string
44
+ for _ , col := range cols {
45
+ items = strings .Split (strings .TrimSpace (col ), " " )
46
+ n = len (items )
47
+ switch n {
48
+ case 1 :
49
+ ob .ByAsc (strings .TrimSpace (col ))
50
+ case 2 :
51
+ by = strings .TrimSpace (items [1 ])
52
+ if strings .EqualFold (by , "ASC" ) {
53
+ ob .ByAsc (strings .TrimSpace (items [0 ]))
54
+ } else if strings .EqualFold (by , "DESC" ) {
55
+ ob .ByDesc (strings .TrimSpace (items [0 ]))
56
+ }
57
+ }
58
+ }
59
+
60
+ return ob
61
+
62
+ }
63
+
64
+ // ByAsc order by ascending with columns
65
+ func (ob * OrderByBuilder ) ByAsc (columns ... string ) * OrderByBuilder {
39
66
for _ , c := range columns {
40
67
if ob .isAllowed (c ) {
41
68
if ob .isWritten {
@@ -49,8 +76,8 @@ func (ob *OrderByBuilder) Asc(columns ...string) *OrderByBuilder {
49
76
return ob
50
77
}
51
78
52
- // Desc order by desc with columns
53
- func (ob * OrderByBuilder ) Desc (columns ... string ) * OrderByBuilder {
79
+ // ByDesc order by descending with columns
80
+ func (ob * OrderByBuilder ) ByDesc (columns ... string ) * OrderByBuilder {
54
81
for _ , c := range columns {
55
82
if ob .isAllowed (c ) {
56
83
if ob .isWritten {
0 commit comments