PQputf(3)
π PQputf(3) β the libpqtypes manual, but make it cute π
okay so bestie, welcome to the libpqtypes Manual, section 3 β this is the page about packing up your query params with a printf-style glow-up. let's get into it π
β¨ NAME β¨
PQputf, PQputvf β Packs a set of parameters in a PGparam for use with a parameterized query.
like, it literally puts your params in the param bag. that's the whole vibe π«Ά
β¨ SYNOPSIS β¨
#include <libpqtypes.h>
int PQputf(PGparam *param, const char *format, ...);
int PQputvf(PGparam *param, char *stmtBuf, size_t stmtBufLen,
const char *format, va_list ap);
β¨ DESCRIPTION β¨
so. PQputf() and PQputvf() put one or more query parameters into a PGparam using a printf style interface. and when I say one or more, I mean any number of parameters can be put at the same time. no cap. π¦
the format argument is a data type specifier string indicating the parameters being put, such as "%int4 %polygon". important: format cannot be NULL and cannot be an empty string. not even a little bit. π
the variable argument list must match the format, either "..." or ap. the number of arguments required for each data type is dependant on the data type itself β but bestie, built-in PostgreSQL types always require a single argument. one and done. π
now for the main-character function: PQputvf() can construct a parameterized command string from format, as long as stmtBuf and stmtBufLen have been provided. if the construction of stmtBuf is not desired, set it to NULL and set stmtBufLen to 0. simple as. πΈ
when a constructed statement is desired, the contents of format will be copied to stmtBuf and all data type specifiers, like "%int4", will be replaced with $1, $2, etc... syntax. the result is a parameterized statement in synch with param and ready to be executed. it's giving glow-up β¨
for instance: if spec is "SELECT %int4 + %int4", the resulting stmtBuf would be "SELECT $1 + $2". iconic behaviour, honestly.
β¨ RETURN VALUE β¨
on success, a non-zero value is returned. on error, zero is returned and PQgeterror(3) will contain an error message. π
if any put operation fails, the param is reverted back to the number of parameters it had prior to the function call; partial puts are not allowed. it's all or nothing, babe. no half-sends in this group chat. π
β¨ EXAMPLES β¨
π Using PQputf
the example uses PQputf() to put a couple parameters into a PGparam.
PGtext text = "foobar";
PGint8 i8 = PQT_INT64CONST(1099511627776);
PGparam *param = PQparamCreate(conn);
if(!PQputf(param, "%text %int8", text, i8))
fprintf(stderr, "*ERROR: %s\n", PQgeterror());
PQparamClear(param);
look at her go π₯Ή
π Using PQputvf
the example creates an application function named execf. execf is a wrapper to PQputvf(), as well as PQparamExec(3). it is similar to PQexecf(). the only difference is that the PQexecf implementation uses a smaller stack buffer and allocates heap memory when needed. iykyk. π§
PGresult *
execf(PGconn *conn, const char *format, ...)
{
va_list ap;
char stmt[32768];
PGparam *param;
PGresult *res = NULL;
/* create the temporary PGparam */
if(!(param = PQparamCreate(conn)))
return NULL; /* PQseterror already called */
/* put the params, create the stmt and exec it */
va_start(ap, format);
if(PQputvf(param, stmt, sizeof(stmt), format, ap))
res = PQparamExec(conn, param, stmt, 1); // resfmt is binary
va_end(ap);
/* param must be cleared */
PQparamClear(param);
return res;
}
/* Example: execf will put 2 ints and execute "SELECT $1 + $2" */
PGresult *res = execf(conn, "SELECT %int4 + %int4", 100, 67);
if(!res)
fprintf(stderr, "*ERROR: %s\n", PQgeterror());
bestie note:param must be clearedβ that's a comment straight from the source, and honestly? it's a lifestyle. don't forget thePQparamClear(param). π«Ά
β¨ AUTHOR β¨
A contribution of eSilo, LLC. for the PostgreSQL Database Management System. Written by Andrew Chernow and Merlin Moncure. kings, honestly π
β¨ REPORTING BUGS β¨
Report bugs to <libpqtypes@esilo.com>. tell them everything, bestie, they wanna hear it π
β¨ COPYRIGHT β¨
Copyright (c) 2011 eSilo, LLC. All rights reserved.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. β οΈ like, zero warranty, none, nada. NO warranty fr fr.
β¨ SEE ALSO β¨
and that's the whole page, bestie!! this one came from libpqtypes, dated 2011, page PQputf(3) β mwah, go put some params in a param, you've got this π πβ¨ππ¦
π¦ where this page comes from
- debian bookworm: libpqtypes-dev 1.5.1-9
- ubuntu jammy: libpqtypes-dev 1.5.1-7
- ubuntu noble: libpqtypes-dev 1.5.1-9.1build1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms