Improve string quotng support

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/sqlclient/trunk@38373 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2015-03-02 09:36:50 +00:00
parent e3e350e859
commit 73d1202aed
2 changed files with 33 additions and 18 deletions

View file

@ -1,3 +1,10 @@
2015-03-02 Richard Frith-Macdonald <rfm@gnu.org>
* Postgres.h: Drop support for old versions of postgres which didn't
support standard conforming strings. This allows us to always turn
on standard conforming strings and be able to quote string and bytea
objects whether the database connection has been established or not.
2014-12-11 Richard Frith-Macdonald <rfm@gnu.org> 2014-12-11 Richard Frith-Macdonald <rfm@gnu.org>
* SQLClient.h: * SQLClient.h:

View file

@ -60,14 +60,12 @@
typedef struct { typedef struct {
PGconn *_connection; PGconn *_connection;
BOOL _escapeStrings; /* Can we use E'...' syntax? */
int _backendPID; int _backendPID;
} ConnectionInfo; } ConnectionInfo;
#define cInfo ((ConnectionInfo*)(self->extra)) #define cInfo ((ConnectionInfo*)(self->extra))
#define backendPID (cInfo->_backendPID) #define backendPID (cInfo->_backendPID)
#define connection (cInfo->_connection) #define connection (cInfo->_connection)
#define escapeStrings (cInfo->_escapeStrings)
static NSDate *future = nil; static NSDate *future = nil;
static NSNull *null = nil; static NSNull *null = nil;
@ -208,12 +206,6 @@ connectQuote(NSString *str)
p = PQparameterStatus(connection, "standard_conforming_strings"); p = PQparameterStatus(connection, "standard_conforming_strings");
if (p != 0) if (p != 0)
{ {
/* The standard conforming strings setting exists,
* so the E'...' syntax must exist and we can use
* it for byte arrays.
*/
escapeStrings = YES;
/* If the escape_string_warning setting is on, /* If the escape_string_warning setting is on,
* the server will warn about backslashes even * the server will warn about backslashes even
* in properly quoted strings, so turn it off. * in properly quoted strings, so turn it off.
@ -230,7 +222,10 @@ connectQuote(NSString *str)
} }
else else
{ {
escapeStrings = NO; PQfinish(connection);
connection = 0;
connected = NO;
[self debug: @"Postgres without standard conforming strings"];
} }
if ([self debugging] > 0) if ([self debugging] > 0)
@ -701,10 +696,7 @@ static unsigned int trim(char *str)
unsigned length = 0; unsigned length = 0;
unsigned i; unsigned i;
if (YES == escapeStrings) ptr[length++] = 'E';
{
ptr[length++] = 'E';
}
ptr[length++] = '\''; ptr[length++] = '\'';
for (i = 0; i < sLen; i++) for (i = 0; i < sLen; i++)
{ {
@ -744,10 +736,7 @@ static unsigned int trim(char *str)
unsigned int length = sLen + 2; unsigned int length = sLen + 2;
unsigned int i; unsigned int i;
if (YES == escapeStrings) length++; // Allow for leading 'E'
{
length++; // Allow for leading 'E'
}
for (i = 0; i < sLen; i++) for (i = 0; i < sLen; i++)
{ {
unsigned char c = src[i]; unsigned char c = src[i];
@ -1029,6 +1018,22 @@ static unsigned int trim(char *str)
unsigned l = [d length]; unsigned l = [d length];
unsigned char *to = NSZoneMalloc(NSDefaultMallocZone(), (l * 2) + 3); unsigned char *to = NSZoneMalloc(NSDefaultMallocZone(), (l * 2) + 3);
#if 1
const char *from = (const char*)[d bytes];
unsigned i = 0;
unsigned j = 0;
to[j++] = '\'';
while (i < l)
{
if ('\'' == (to[j++] = from[i++]))
{
to[j++] = '\'';
}
}
to[j++] = '\'';
l = j - 2;
#else
#ifdef HAVE_PQESCAPESTRINGCONN #ifdef HAVE_PQESCAPESTRINGCONN
int err; int err;
@ -1036,7 +1041,8 @@ static unsigned int trim(char *str)
NS_DURING NS_DURING
{ {
[self connect]; [self connect];
l = PQescapeStringConn(connection, (char*)(to + 1), [d bytes], l, &err); l = PQescapeStringConn(connection,
(char*)(to + 1), [d bytes], l, &err);
} }
NS_HANDLER NS_HANDLER
{ {
@ -1051,6 +1057,8 @@ static unsigned int trim(char *str)
#endif #endif
to[0] = '\''; to[0] = '\'';
to[l + 1] = '\''; to[l + 1] = '\'';
#endif
s = [[NSString alloc] initWithBytesNoCopy: to s = [[NSString alloc] initWithBytesNoCopy: to
length: l + 2 length: l + 2
encoding: NSUTF8StringEncoding encoding: NSUTF8StringEncoding