https://github.com/worldline-go/query
query parser statements
https://github.com/worldline-go/query
goqu query
Last synced: about 1 year ago
JSON representation
query parser statements
- Host: GitHub
- URL: https://github.com/worldline-go/query
- Owner: worldline-go
- License: mit
- Created: 2025-04-02T11:18:51.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-07T03:36:08.000Z (about 1 year ago)
- Last Synced: 2025-04-10T00:57:20.817Z (about 1 year ago)
- Topics: goqu, query
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Query
[](https://raw.githubusercontent.com/worldline-go/query/main/LICENSE)
[](https://sonarcloud.io/summary/overall?id=worldline-go_query)
[](https://github.com/worldline-go/query/actions)
[](https://goreportcard.com/report/github.com/worldline-go/query)
[](https://pkg.go.dev/github.com/worldline-go/query)
Query is an adaptor of http query to expressions. Check adapters to convert it to sql or other expressions.
```sh
go get github.com/worldline-go/query
```
## Usage
Parse url and extract query parameters with RAW, give to `query.Parse` to convert it to expression.
Use an adapter to convert it to sql or other expressions.
```go
urlStr := "http://example.com?name=foo,bar|nick=bar&age[lt]=1&sort=-age&limit=10&offset=5&fields=id,name"
parsedURL, err := url.Parse(urlStr)
// ...
query, err := query.Parse(parsedURL.RawQuery)
// ...
sql, params, err := adaptergoqu.Select(query, goqu.From("test")).ToSQL()
// ...
// SQL: SELECT "id", "name" FROM "test" WHERE ((("name" IN ('foo', 'bar')) OR ("nick" = 'bar')) AND ("age" < '1')) ORDER BY "age" DESC LIMIT 10 OFFSET 5
// Params: []
```
If some value separated by `,` it will be converted to `IN` operator.
There are a list of `[ ]` operators that can be used in the query string:
`eq, ne, gt, lt, gte, lte, like, ilike, nlike, nilike, in, nin, is, not`
### Validation
```go
validator := query.NewValidator(
WithValue("member", WithRequired()),
)
// after that use it to validate
err := validator.Validate(query)
if err != nil {
// handle error
}
```