Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions autotest/ogr/ogr_pgdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,3 +1673,32 @@ def check_and_remove(needle):
sql = sql[sql.find(needle) + len(needle) :]

check_and_remove(") ON CONFLICT DO NOTHING;")


###############################################################################
# Test that " and \ in string list items are escaped in both COPY and INSERT mode


@pytest.mark.parametrize("pg_use_copy", ["YES", "NO"])
def test_ogr_pgdump_stringlist_escaping(tmp_vsimem, pg_use_copy):

with gdaltest.config_option("PG_USE_COPY", pg_use_copy):
ds = ogr.GetDriverByName("PGDump").CreateDataSource(
tmp_vsimem / "ogr_pgdump_stringlist_escape.sql"
)
lyr = ds.CreateLayer("testlayer", geom_type=ogr.wkbNone)
lyr.CreateField(ogr.FieldDefn("stringlist", ogr.OFTStringList))

feat = ogr.Feature(lyr.GetLayerDefn())
feat.SetFieldStringList(0, ["a\\b", 'c"d'])
lyr.CreateFeature(feat)
ds = None

f = gdal.VSIFOpenL(tmp_vsimem / "ogr_pgdump_stringlist_escape.sql", "rb")
sql = gdal.VSIFReadL(1, 10000, f).decode("utf8")
gdal.VSIFCloseL(f)

if pg_use_copy == "YES":
assert r'{"a\\\\b","c\\"d"}' in sql, sql
else:
assert "ARRAY['a\\b','c\"d']" in sql, sql
2 changes: 1 addition & 1 deletion ogr/ogrsf_frmts/pgdump/ogrpgdumplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ OGRPGDumpEscapeStringList(char **papszItems, bool bForInsertOrUpdate,

while (*pszStr)
{
if (*pszStr == '"')
if (*pszStr == '"' || *pszStr == '\\')
osStr += "\\";
osStr += *pszStr;
pszStr++;
Expand Down
Loading