You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: GUIDE.md
+26
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,32 @@ Examples in [example_test.go](example_test.go) explicitly show that query argume
8
8
9
9
The `%s` verb should be used with an extra care, no user input should be passed through it.
10
10
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
+
11
37
## Compile-time queries
12
38
13
39
To enforce compile-time queries `builq.Builder` accepts only constant strings:
0 commit comments