From: Billy G. A. <bal...@us...> - 2001-09-30 06:31:37
|
Update of /cvsroot/pypgsql/pypgsql In directory usw-pr-cvs1:/tmp/cvs-serv32224 Modified Files: PgSQL.py libpqmodule.c Log Message: 27SEP2001 bga Added different quoting/escaping if the result is to be used as part of a PostgreSQL array. Index: PgSQL.py =================================================================== RCS file: /cvsroot/pypgsql/pypgsql/PgSQL.py,v retrieving revision 1.27 retrieving revision 1.28 diff -C2 -d -r1.27 -r1.28 *** PgSQL.py 2001/09/27 17:02:53 1.27 --- PgSQL.py 2001/09/30 06:31:33 1.28 *************** *** 1405,1409 **** elif type(_i) in [DateTime.DateTimeType, DateTime.DateTimeDeltaType]: _j = '%s"%s",' % (_j, _i) ! elif type(_i) == PgInt8Type or type(_i) == PgInt2Type: _j = '%s%s,' % (_j, str(_i)) else: --- 1405,1409 ---- elif type(_i) in [DateTime.DateTimeType, DateTime.DateTimeDeltaType]: _j = '%s"%s",' % (_j, _i) ! elif type(_i) == PgInt2Type or type(_i) == PgInt8Type: _j = '%s%s,' % (_j, str(_i)) else: Index: libpqmodule.c =================================================================== RCS file: /cvsroot/pypgsql/pypgsql/libpqmodule.c,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** libpqmodule.c 2001/09/26 05:41:34 1.16 --- libpqmodule.c 2001/09/30 06:31:33 1.17 *************** *** 32,35 **** --- 32,37 ---- | Date Ini Description | | --------- --- ------------------------------------------------------- | + | 27SEP2001 bga Added different quoting/escaping if the result is to be | + | used as part of a PostgreSQL array. | | 26SEP2001 bga Change the quoting/escaping helper routines so that | | they also escape the double quote character. | *************** *** 179,196 **** static PyObject *libPQquoteString(PyObject *self, PyObject *args) { ! int i, j, slen, byte; char *sin; char *sout; PyObject *result; ! if (!PyArg_ParseTuple(args,"s:PgQuoteString", &sin)) return NULL; slen = strlen(sin); ! sout = (char *)PyMem_Malloc((4*slen)+3); // Assume every char will be quoted if (sout == (char *)NULL) return PyErr_NoMemory(); ! sout[0] = '\''; for (i = 0, j = 1; i < slen; i++) --- 181,201 ---- static PyObject *libPQquoteString(PyObject *self, PyObject *args) { ! int i, j, slen, byte, forArray=0; char *sin; char *sout; PyObject *result; ! if (!PyArg_ParseTuple(args,"s|i:PgQuoteString", &sin, &forArray)) return NULL; + // Calculate the size for the new memory string. slen = strlen(sin); ! i = (slen * (forArray ? 7 : 4)) + 3; ! ! sout = (char *)PyMem_Malloc(i); // Assume every char will be quoted if (sout == (char *)NULL) return PyErr_NoMemory(); ! sout[0] = (forArray ? '"' : '\''); for (i = 0, j = 1; i < slen; i++) *************** *** 199,208 **** { case '"': case '\'': - case '\\': sout[j++] = '\\'; sout[j++] = sin[i]; break; case '\b': sout[j++] = '\\'; --- 204,230 ---- { case '"': + if (forArray) + { + sout[j++] = '\\'; + sout[j++] = '\\'; + } + sout[j++] = sin[i]; + break; + case '\'': sout[j++] = '\\'; sout[j++] = sin[i]; break; + case '\\': + sout[j++] = sin[i]; + sout[j++] = sin[i]; + if (forArray) + { + sout[j++] = sin[i]; + sout[j++] = sin[i]; + } + break; + case '\b': sout[j++] = '\\'; *************** *** 236,239 **** --- 258,267 ---- byte = (unsigned char)sin[i]; sout[j++] = '\\'; + if (forArray) + { + sout[j++] = '\\'; + sout[j++] = '\\'; + sout[j++] = '\\'; + } sout[j++] = DIG((byte >> 6) & 3); sout[j++] = DIG((byte >> 3) & 7); *************** *** 245,249 **** } ! sout[j++] = '\''; sout[j] = (char)0; --- 273,277 ---- } ! sout[j++] = (forArray ? '"' : '\''); sout[j] = (char)0; *************** *** 257,261 **** static char libPQquoteBytea_Doc[] = ! "PgQuoteString(string) -> string\n" " This is a helper function that will quote a Python String (that can " "contain embedded NUL bytes) in a manner\n that is acceptable to " --- 285,289 ---- static char libPQquoteBytea_Doc[] = ! "PgQuoteString(string, forArray) -> string\n" " This is a helper function that will quote a Python String (that can " "contain embedded NUL bytes) in a manner\n that is acceptable to " *************** *** 266,301 **** | able for use in PostgreSQL. The quoting rules are: | | | ! | 1. Non-printable characters are quoted as \OOO where OOO is the | ! | octal representation of the characters ordinal number. | | | ! | 2. The backslash is quoted as \\\\. | | | ! | 3. The single quote is quoted as \'. | | | ! | 4. The double quote is quoted as \". | | | | 5. All other characters are unchanged. | | | ! | Note: The double quote (") is escaped in case the quoted string is | ! | part of a PostgreSQL array, which uses double quotes around | ! | string values instead of single quotes. It saves having to do | ! | a global search and replace of double quotes in Python. | \***********************************************************************/ static PyObject *libPQquoteBytea(PyObject *self, PyObject *args) { ! int i, j, slen, byte; char *sin; char *sout; PyObject *result; ! if (!PyArg_ParseTuple(args,"s#:PgQuoteString", &sin, &slen)) return NULL; ! sout = (char *)PyMem_Malloc((4*slen)+3); // Assume every char will be quoted if (sout == (char *)NULL) return PyErr_NoMemory(); ! sout[0] = '\''; for (i = 0, j = 1; i < slen; i++) --- 294,337 ---- | able for use in PostgreSQL. The quoting rules are: | | | ! | 1. The NUL character is escaped as \\000. | | | ! | 2. Non-printable characters are escaped as \OOO where OOO is the | ! | octal representation of the characters ordinal number. | | | ! | 3. The backslash is escaped as \\\\. | | | ! | 4. The single quote is escaped as \'. | | | | 5. All other characters are unchanged. | | | ! | Note: If the optional forArray argument is 1, then the escaping is | ! | changed as follows: | ! | 1. The NUL character is escaped as \\\\000. | ! | 2. Non-printable characters are escapes as \\\\OOO. | ! | 3. The backslash is escpaed as \\\\\\\\. | ! | 4. The single quote is escaped as \'. | ! | 5. The double quote is escaped as \\". | ! | 6. All other characters are unchanged. | \***********************************************************************/ static PyObject *libPQquoteBytea(PyObject *self, PyObject *args) { ! int i, j, slen, byte, forArray = 0; char *sin; char *sout; PyObject *result; ! if (!PyArg_ParseTuple(args,"s#|i:PgQuoteBytea", &sin, &slen, &forArray)) return NULL; ! // Calculate the size for the new memory string. ! slen = strlen(sin); ! i = (slen * (forArray ? 8 : 4)) + 3; ! ! sout = (char *)PyMem_Malloc(i); // Assume every char will be quoted if (sout == (char *)NULL) return PyErr_NoMemory(); ! sout[0] = (forArray ? '"' : '\''); for (i = 0, j = 1; i < slen; i++) *************** *** 304,307 **** --- 340,351 ---- { case '"': + if (forArray) + { + sout[j++] = '\\'; + sout[j++] = '\\'; + } + sout[j++] = sin[i]; + break; + case '\'': sout[j++] = '\\'; *************** *** 314,317 **** --- 358,368 ---- sout[j++] = sin[i]; sout[j++] = sin[i]; + if (forArray) + { + sout[j++] = sin[i]; + sout[j++] = sin[i]; + sout[j++] = sin[i]; + sout[j++] = sin[i]; + } break; *************** *** 319,322 **** --- 370,378 ---- sout[j++] = '\\'; sout[j++] = '\\'; + if (forArray) + { + sout[j++] = '\\'; + sout[j++] = '\\'; + } sout[j++] = '0'; sout[j++] = '0'; *************** *** 330,333 **** --- 386,395 ---- byte = (unsigned char)sin[i]; sout[j++] = '\\'; + if (forArray) + { + sout[j++] = '\\'; + sout[j++] = '\\'; + sout[j++] = '\\'; + } sout[j++] = DIG((byte >> 6) & 3); sout[j++] = DIG((byte >> 3) & 7); *************** *** 339,343 **** } ! sout[j++] = '\''; sout[j] = (char)0; --- 401,405 ---- } ! sout[j++] = (forArray ? '"' : '\''); sout[j] = (char)0; |