pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: jwieck@debis.com
To: tgl@sss.pgh.pa.us
Cc: ppauly@usa.net
Cc: pgsql-sql@hub.org
Subject: Re: [SQL] Re: Odd characters in inserted data...
Date: Mon, 30 Nov 1998 18:08:29 +0100 (MET)
Message-ID: <m0zkWoI-000EBjC@orion.SAPserv.Hamburg.dsh.de> (raw)
In-Reply-To: <28740.912442162@sss.pgh.pa.us>

Tom Lane wrote:

> Now, as to mechanism, what I currently do is to define a function
>    const char * SQLstring (const char * str)
> which returns "NULL" if str is NULL, otherwise the string surrounded
> with single quotes and having internal special characters escaped.
> Then the SQL statement is constructed with
>    sprintf(query, "update tablename set comment = %s where ...",
>         SQLstring(str), ...);
> Notice that by having the quotes stuck on by SQLstring, not by the printf
> format, I can deal easily with null field values.
>
> This is a lot easier to get right in C++, where the function can return
> a temporary string object that will get deallocated automatically after
> use.  In C the most reasonable way to proceed is to have SQLstring
> return a pointer to a static area, which means you can't invoke

    To  get  out of this, the function SQLstring(char *str) could
    use malloc'ed memory to return it's result and  remember  all
    the chunks that it allocated in some static held array. After
    all, another function SQLstring_free() might be  called  that
    has  to  tidy up.  The array itself is also malloc'ed and can
    grow if many strings are used.

    Still not that pretty, but it's half way of  automation.  And
    if  the  SQLstring_free() is forgotten once, never mind - the
    next call will do it anyway.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #





view thread (3+ messages)  latest in thread

Message-ID: <m0zkWoI-000EBjC@orion.SAPserv.Hamburg.dsh.de>
Permalink:  ../m0zkWoI-000EBjC@orion.SAPserv.Hamburg.dsh.de/
Also on:    postgresql.org/message-id/m0zkWoI-000EBjC@orion.SAPserv.Hamburg.dsh.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: jwieck@debis.com, tgl@sss.pgh.pa.us, ppauly@usa.net, pgsql-sql@hub.org
  Subject: Re: [SQL] Re: Odd characters in inserted data...
  In-Reply-To: <m0zkWoI-000EBjC@orion.SAPserv.Hamburg.dsh.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox