From a609d6c9d176dacf4f56ddca27850083b287d483 Mon Sep 17 00:00:00 2001 From: jacksonmowry <96317858+jacksonmowry@users.noreply.github.com> Date: Mon, 31 Jul 2023 18:26:45 +0000 Subject: [PATCH] db.pg: add parameter syntax to docs (#19003) --- vlib/db/pg/README.md | 11 +++++++++++ vlib/db/pg/pg.v | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/vlib/db/pg/README.md b/vlib/db/pg/README.md index 59fd4ac56f..df50199966 100644 --- a/vlib/db/pg/README.md +++ b/vlib/db/pg/README.md @@ -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) } +``` diff --git a/vlib/db/pg/pg.v b/vlib/db/pg/pg.v index 1c114ae507..fca33cb5d7 100644 --- a/vlib/db/pg/pg.v +++ b/vlib/db/pg/pg.v @@ -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]) }