1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

db.pg: add parameter syntax to docs (#19003)

This commit is contained in:
jacksonmowry 2023-07-31 18:26:45 +00:00 committed by GitHub
parent fd81bae361
commit a609d6c9d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -51,3 +51,14 @@ Read this section to learn how to install and connect to PostgreSQL
[*Windows*](https://www.postgresqltutorial.com/install-postgresql);
[*Linux*](https://www.postgresqltutorial.com/postgresql-getting-started/install-postgresql-linux);
[*macOS*](https://www.postgresqltutorial.com/postgresql-getting-started/install-postgresql-macos).
## Using Parameterized Queries
Parameterized queries (exec_param, etc.) in V require the use of the following syntax: ($n).
The number following the $ specifies which parameter from the argument array to use.
```v ignore
db.exec_param_many('INSERT INTO users (username, password) VALUES ($1, $2)', ['tom', 'securePassword']) or { panic(err) }
db.exec_param('SELECT * FROM users WHERE username = ($1) limit 1', 'tom') or { panic(err) }
```

View File

@ -251,7 +251,7 @@ pub fn (db DB) exec_one(query string) !Row {
return row
}
// exec_param_many executes a query with the provided parameters
// exec_param_many executes a query with the parameters provided as ($1), ($2), ($n)
pub fn (db DB) exec_param_many(query string, params []string) ![]Row {
unsafe {
mut param_vals := []&char{len: params.len}
@ -265,12 +265,12 @@ pub fn (db DB) exec_param_many(query string, params []string) ![]Row {
}
}
// exec_param2 executes a query with 1 parameter, and returns either an error on failure, or the full result set on success
// exec_param2 executes a query with 1 parameter ($1), and returns either an error on failure, or the full result set on success
pub fn (db DB) exec_param(query string, param string) ![]Row {
return db.exec_param_many(query, [param])
}
// exec_param2 executes a query with 2 parameters, and returns either an error on failure, or the full result set on success
// exec_param2 executes a query with 2 parameters ($1) and ($2), and returns either an error on failure, or the full result set on success
pub fn (db DB) exec_param2(query string, param string, param2 string) ![]Row {
return db.exec_param_many(query, [param, param2])
}