aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql/command.c
diff options
context:
space:
mode:
authorPeter Eisentraut <peter@eisentraut.org>2022-11-15 13:50:27 +0100
committerPeter Eisentraut <peter@eisentraut.org>2022-11-15 14:27:46 +0100
commit5b66de3433e2110b38a2b32aaaa0b9cdac8aacdb (patch)
treebc8f7c7f5fb7bd19d70d2ec6561fd7a0fc815768 /src/bin/psql/command.c
parenta9e9a9f32b35edf129c88e8b929ef223f8511f59 (diff)
downloadpostgresql-5b66de3433e2110b38a2b32aaaa0b9cdac8aacdb.tar.gz
postgresql-5b66de3433e2110b38a2b32aaaa0b9cdac8aacdb.zip
psql: Add command to use extended query protocol
This adds a new psql command \bind that sets query parameters and causes the next query to be sent using the extended query protocol. Example: SELECT $1, $2 \bind 'foo' 'bar' \g This may be useful for psql scripting, but one of the main purposes is also to be able to test various aspects of the extended query protocol from psql and to write tests more easily. Reviewed-by: Corey Huinker <corey.huinker@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/e8dd1cd5-0e04-3598-0518-a605159fe314@enterprisedb.com
Diffstat (limited to 'src/bin/psql/command.c')
-rw-r--r--src/bin/psql/command.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index ab613dd49e0..3b06169ba0d 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -63,6 +63,7 @@ static backslashResult exec_command(const char *cmd,
PQExpBuffer query_buf,
PQExpBuffer previous_buf);
static backslashResult exec_command_a(PsqlScanState scan_state, bool active_branch);
+static backslashResult exec_command_bind(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_C(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_connect(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_cd(PsqlScanState scan_state, bool active_branch,
@@ -308,6 +309,8 @@ exec_command(const char *cmd,
if (strcmp(cmd, "a") == 0)
status = exec_command_a(scan_state, active_branch);
+ else if (strcmp(cmd, "bind") == 0)
+ status = exec_command_bind(scan_state, active_branch);
else if (strcmp(cmd, "C") == 0)
status = exec_command_C(scan_state, active_branch);
else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
@@ -454,6 +457,40 @@ exec_command_a(PsqlScanState scan_state, bool active_branch)
}
/*
+ * \bind -- set query parameters
+ */
+static backslashResult
+exec_command_bind(PsqlScanState scan_state, bool active_branch)
+{
+ backslashResult status = PSQL_CMD_SKIP_LINE;
+
+ if (active_branch)
+ {
+ char *opt;
+ int nparams = 0;
+ int nalloc = 0;
+
+ pset.bind_params = NULL;
+
+ while ((opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false)))
+ {
+ nparams++;
+ if (nparams > nalloc)
+ {
+ nalloc = nalloc ? nalloc * 2 : 1;
+ pset.bind_params = pg_realloc_array(pset.bind_params, char *, nalloc);
+ }
+ pset.bind_params[nparams - 1] = pg_strdup(opt);
+ }
+
+ pset.bind_nparams = nparams;
+ pset.bind_flag = true;
+ }
+
+ return status;
+}
+
+/*
* \C -- override table title (formerly change HTML caption)
*/
static backslashResult