https://github.com/qjebbs/go-sqlf
go-sqlf focuses only on building SQL queries by free combination of fragments.
https://github.com/qjebbs/go-sqlf
golang sql sqlbuilder
Last synced: 7 months ago
JSON representation
go-sqlf focuses only on building SQL queries by free combination of fragments.
- Host: GitHub
- URL: https://github.com/qjebbs/go-sqlf
- Owner: qjebbs
- License: mit
- Created: 2023-04-10T07:14:57.000Z (about 3 years ago)
- Default Branch: v2
- Last Pushed: 2024-10-28T05:37:03.000Z (over 1 year ago)
- Last Synced: 2025-02-14T16:16:17.700Z (over 1 year ago)
- Topics: golang, sql, sqlbuilder
- Language: Go
- Homepage:
- Size: 216 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
Package `sqlf` focuses on building SQL queries by free combination of fragments.
The package exports only a few functions and methods, but improves a lot on the
reusability and extensibility of SQL, which are the main challenges we encounter
when writing SQL.
## Fragment
Unlike any other sql builder or ORMs, `Fragment` is the only concept you need to learn.
Fragment is usually a part of a SQL query, which uses exactly the same syntax as
`database/sql`, but provides the ability to combine them in any way.
```go
import (
"fmt"
"github.com/qjebbs/go-sqlf/v2"
)
func Example_basic() {
query, args, _ := sqlf.Ff(
"SELECT * FROM foo WHERE #join('#fragment', ' AND ')", // join fragments
sqlf.Fa("baz = $1", true), // `database/sql` style
sqlf.Fa("bar BETWEEN ? AND ?", 1, 100), // `database/sql` style
).BuildQuery(syntax.Dollar)
fmt.Println(query)
fmt.Println(args)
// Output:
// SELECT * FROM foo WHERE baz = $1 AND bar BETWEEN $2 AND $3
// [true 1 100]
}
```
Explanation:
- We pay attention only to the references inside a fragment, not between fragments.
- `#join`, `#arg`, `#f`, etc., are preprocessing functions, which will be explained later.
- See `Example_deeperLook` of [example_test.go](./example_test.go) for what happend inside the *sqlf.Fragment.
## Preprocessing Functions
| name | description | example |
| ----------- | -------------------------------- | ---------------------- |
| f, fragment | fragments at index | #f1, #fragment1 |
| join | Join the template with separator | #join('#f', ' AND ') |
| | Join from index 3 to end | #join('#f', ',', 3) |
| | Join from index 3 to 6 | #join('#f', ',', 3, 6) |
| arg | arguments at index | #join('#arg', ',') |
Note:
- #f1 is equivalent to #f(1), which is a special syntax to call preprocessing functions when an integer (usually an index) is the only argument.
- Expressions in the #join template are functions, not function calls.
See Example `ContextWithFuncs` of [example_test.go](./example_test.go) for how to
register custom preprocessing functions, and implementing global arguments/fragments.
## QueryBuilder
`*sqlb.QueryBuilder` is a high-level abstraction of SQL queries for building complex queries,
with `*sqlf.Fragment` as its underlying foundation.
See [sqlb/example_test.go](./sqlb/example_test.go) for examples.