Skip to content

Commit

Permalink
fix: ORDER BY clause position is incorrect (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp authored Dec 22, 2022
1 parent b40bc92 commit b68e95a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
6 changes: 3 additions & 3 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ type Select struct {
Columns []string
From string
Where Condition
OrderBy string
Limit int
Offset int
OrderBy string
ForUpdate bool
}

Expand Down Expand Up @@ -137,9 +137,9 @@ type Update struct {
Table string
Set map[string]any
Where Condition
OrderBy string
Limit int
Offset int
OrderBy string
}

func (q Update) Validate() error {
Expand Down Expand Up @@ -183,9 +183,9 @@ func (q Update) Query() (string, []any, error) {
type Delete struct {
From string
Where Condition
OrderBy string
Limit int
Offset int
OrderBy string
}

func (d Delete) Validate() error {
Expand Down
22 changes: 14 additions & 8 deletions query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestQueryBuilder(t *testing.T) {
OrderBy: "id DESC",
ForUpdate: true,
},
stmt: "SELECT `id`,`age` FROM `table` WHERE id = ? LIMIT ? OFFSET ? ORDER BY id DESC FOR UPDATE",
stmt: "SELECT `id`,`age` FROM `table` WHERE id = ? ORDER BY id DESC LIMIT ? OFFSET ? FOR UPDATE",
args: []any{1, 2, 3},
},
{
Expand All @@ -82,18 +82,24 @@ func TestQueryBuilder(t *testing.T) {
"id": 1,
"name": "go",
},
Where: NewCondition(`id = ?`, 2),
Where: NewCondition(`id = ?`, 2),
OrderBy: "id",
Limit: 3,
Offset: 4,
},
stmt: "UPDATE `table` SET `id` = ?,`name` = ? WHERE id = ?",
args: []any{1, "go", 2},
stmt: "UPDATE `table` SET `id` = ?,`name` = ? WHERE id = ? ORDER BY id LIMIT ? OFFSET ?",
args: []any{1, "go", 2, 3, 4},
},
{
query: Delete{
From: "table",
Where: NewCondition(`id = ?`, 1),
From: "table",
Where: NewCondition(`id = ?`, 1),
OrderBy: "id",
Limit: 2,
Offset: 3,
},
stmt: "DELETE FROM `table` WHERE id = ?",
args: []any{1},
stmt: "DELETE FROM `table` WHERE id = ? ORDER BY id LIMIT ? OFFSET ?",
args: []any{1, 2, 3},
},
}
for _, v := range arr {
Expand Down

0 comments on commit b68e95a

Please sign in to comment.