How does this grab folks? It's sort of a homegrown printf just for our
queries, with the following replacements:

%s -- string, gets escaped automatically
%d -- integer
%l -- u64_t
%u -- unsigned integer

%T -- table prefix
%D -- to_date function
%C -- to_char function


void testdbstring(void)
{
        char *goquery;
        char *query = "SELECT * FROM %Ttable WHERE "
                " string = '%s' AND int = %d "
                " AND u64_t = %l AND unsigned = %u "
                " AND to_date = %D and to_char = %C ";
        char *string = "string";
        int integer = -12345;
        unsigned unint = 12345;
        u64_t bigint = 1222333444;

        dbstring(&goquery, query, string, integer, bigint, unint);

        printf("%s\n", goquery);
}


The query string is allocated, strings escaped, parameters replaced:

    SELECT * FROM dbmail_table WHERE
      string = 'string' AND int = -12345
      AND u64_t = 1222333444ND unsigned = 12345
      AND to_date =  TO_DATE( foo )  and to_char =  TO_CHAR( foo )


Aaron

--

Reply via email to