1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Add PQescapeIdentifier() to libpq

Christopher Kings-Lynne
This commit is contained in:
Bruce Momjian
2006-06-27 00:03:42 +00:00
parent 59a853e48b
commit 3b1790f987
4 changed files with 106 additions and 4 deletions

View File

@ -1,4 +1,4 @@
# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.11 2006/05/28 22:42:05 tgl Exp $
# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.12 2006/06/27 00:03:41 momjian Exp $
# Functions to be exported by libpq DLLs
PQconnectdb 1
PQsetdbLogin 2
@ -130,3 +130,5 @@ PQescapeByteaConn 127
PQencryptPassword 128
PQisthreadsafe 129
enlargePQExpBuffer 130
PQescapeIdentifier 131

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.186 2006/05/28 21:13:54 tgl Exp $
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.187 2006/06/27 00:03:41 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -2515,6 +2515,42 @@ PQescapeString(char *to, const char *from, size_t length)
static_std_strings);
}
/*
* Escaping arbitrary strings to get valid SQL identifier strings.
*
* Replaces " with "".
*
* length is the length of the source string. (Note: if a terminating NUL
* is encountered sooner, PQescapeIdentifier stops short of "length"; the behavior
* is thus rather like strncpy.)
*
* For safety the buffer at "to" must be at least 2*length + 1 bytes long.
* A terminating NUL character is added to the output string, whether the
* input is NUL-terminated or not.
*
* Returns the actual length of the output (not counting the terminating NUL).
*/
size_t
PQescapeIdentifier(char *to, const char *from, size_t length)
{
const char *source = from;
char *target = to;
size_t remaining = length;
while (remaining > 0 && *source != '\0')
{
if (*source == '"')
*target++ = *source;
*target++ = *source++;
remaining--;
}
/* Write the terminating NUL character. */
*target = '\0';
return target - to;
}
/*
* PQescapeBytea - converts from binary string to the
* minimal encoding necessary to include the string in an SQL

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.129 2006/05/23 22:13:19 momjian Exp $
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.130 2006/06/27 00:03:42 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -436,6 +436,8 @@ extern unsigned char *PQescapeByteaConn(PGconn *conn,
size_t *to_length);
extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
size_t *retbuflen);
extern size_t PQescapeIdentifier(char *to, const char *from, size_t length);
/* These forms are deprecated! */
extern size_t PQescapeString(char *to, const char *from, size_t length);
extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,