Quote column names in _psyco_curs_copy_columns
"Joel Nothman" <[email protected]>
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
Postgres allows column names such as "end" and "when" that are reserved words. When executing SQL, one can quote these column names for Postgres to parse them as column names. The copy_to and copy_from methods in psycopg2 accept a column list as an argument and form these into an SQL string. To get the above cases right, quotes are needed around the column names. Currently, the user may pass in a column name quoted in Python, but this is not ideal. The attached patch to cursor_type.c provides a fix. - Joel _______________________________________________ Psycopg mailing list Psycopg-IAPFreCvJWPBWskQ1e/[email protected] http://lists.initd.org/mailman/listinfo/psycopg
quote_cols.diff
(application/octet-stream, 950 B)
diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c
index 82e390b..4294138 100644
--- a/psycopg/cursor_type.c
+++ b/psycopg/cursor_type.c
@@ -1153,14 +1153,17 @@ static int _psyco_curs_copy_columns(PyObject *columns, char *columnlist)
return -1;
}
PyString_AsStringAndSize(col, &colname, &collen);
- if (offset + collen > DEFAULT_COPYBUFF - 2) {
+ if (offset + collen > DEFAULT_COPYBUFF - 4) {
Py_DECREF(col);
Py_DECREF(coliter);
PyErr_SetString(PyExc_ValueError, "column list too long");
return -1;
}
+ /* If not otherwise quoted, quote column names */
+ if (colname[0] != '"') columnlist[offset++] = '"';
strncpy(&columnlist[offset], colname, collen);
offset += collen;
+ if (colname[0] != '"') { columnlist[offset++] = '"'; }
columnlist[offset++] = ',';
Py_DECREF(col);
}