PQputf(3)

πŸ“¦ version 1.5.1, in debian bookworm, ubuntu jammy, ubuntu noble

πŸ’– 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 the PQparamClear(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 (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