forked from velmie/q2sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds the ability to control SelectBuilder
- Loading branch information
1 parent
f382a96
commit 420a698
Showing
5 changed files
with
115 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/velmie/qparser" | ||
"log" | ||
|
||
"github.com/velmie/q2sql" | ||
"github.com/velmie/q2sql/condition" | ||
"github.com/velmie/q2sql/extension" | ||
) | ||
|
||
func main() { | ||
translator := q2sql.MapTranslator(map[string]string{ | ||
"id": "id", | ||
"title": "title", | ||
"body": "body", | ||
"author": "author", | ||
"createdAt": "created_at", | ||
}) | ||
|
||
allowedConditionsByField := q2sql.AllowedConditions{ | ||
"id": []string{condition.NameIn}, | ||
} | ||
builder := q2sql.NewResourceSelectBuilder( | ||
"articles", | ||
translator, | ||
q2sql.AllowSelectFields([]string{"id", "title", "author"}), | ||
q2sql.AllowFiltering( | ||
allowedConditionsByField, | ||
condition.DefaultConditionMap, | ||
q2sql.DefaultFilterExpressionParser, | ||
), | ||
q2sql.AllowSortingByFields([]string{"created_at", "title"}), | ||
q2sql.Extend(extension.LimitOffsetPagination(extension.Unlimited, extension.Unlimited)), | ||
) | ||
|
||
qstr := "?fields[articles]=id,title,author&filter[id]=in:1,2,3,4,5&sort=-createdAt,title&page[limit]=100" | ||
query, _ := qparser.ParseQuery(qstr) | ||
|
||
// It's possible to pass select builder | ||
sb := new(q2sql.SelectBuilder) | ||
// and define custom conditions | ||
sb.Where(&q2sql.Eq{"author", "Alan Turing"}) | ||
|
||
// not necessary to declare new variable since the "sb" already points to the select builder | ||
_, err := builder.Build(context.Background(), query, sb) | ||
if err != nil { | ||
log.Fatal("failed to build query", err) | ||
} | ||
// or rewrite some parts, for example the line below clears sorting | ||
sb.OrderByParts = nil | ||
|
||
sqlStr, args, err := sb.ToSql() | ||
if err != nil { | ||
log.Fatal("failed to build SQL query", err) | ||
} | ||
fmt.Println(sqlStr) | ||
fmt.Println(args) | ||
// prints | ||
/* | ||
SELECT id, title, author FROM articles WHERE author = ? AND id IN (?,?,?,?,?) LIMIT 100 | ||
[Alan Turing 1 2 3 4 5] | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters