Skip to content

Commit ce019f6

Browse files
authored
Improve %s docs (#33)
1 parent 86c3c3a commit ce019f6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

GUIDE.md

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ Examples in [example_test.go](example_test.go) explicitly show that query argume
88

99
The `%s` verb should be used with an extra care, no user input should be passed through it.
1010

11+
## SQL injections and `%s` usage
12+
13+
`builq` is language-agnostic query builder that doesn't differentiate Postgres SQL syntax from MySQL. The `%s` verb was introduced to give flexibility to the library users.
14+
15+
The following query is valid for `builq` but isn't valid for Postgres:
16+
17+
```go
18+
const tableName = "my_table"
19+
user := "admin"
20+
21+
q := builq.New()
22+
q("SELECT * FROM %$ WHERE username = %$", tableName, user)
23+
24+
// will generate query: SELECT * FROM $1 WHERE username = $2
25+
```
26+
27+
The query above is correct for `builq` and is incorrect for Postgres (error `SQLSTATE 42601`). Exactly for such cases, `%s` was added:
28+
29+
```go
30+
q("SELECT * FROM %s WHERE username = %$", tableName, user)
31+
32+
// will generate query: SELECT * FROM my_table WHERE username = $1
33+
```
34+
35+
Remember that `%s` should be used with care, and as mentioned in the section above, no user input should be passed via `%s`.
36+
1137
## Compile-time queries
1238

1339
To enforce compile-time queries `builq.Builder` accepts only constant strings:

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This tiny library helps to build queries and handles parameter indexing.
1919
* Simple and easy.
2020
* Safe and fast.
2121
* Tested.
22+
* Language-agnostic.
2223
* Dependency-free.
2324

2425
See [docs][pkg-url] or [GUIDE.md](GUIDE.md) for more details.

0 commit comments

Comments
 (0)