Skip to content

Commit 0c00065

Browse files
committed
PG: implement OGRLayer::FindFieldIndex()
1 parent b98de29 commit 0c00065

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

autotest/ogr/ogr_pg.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6231,3 +6231,41 @@ def test_ogr_pg_empty_search_path(pg_ds):
62316231

62326232
finally:
62336233
ds.ExecuteSQL(f"ALTER ROLE {current_user} SET search_path = {old_search_path}")
6234+
6235+
6236+
###############################################################################
6237+
# Test appending to a layer where a field name was truncated to 63 characters.
6238+
6239+
6240+
@only_without_postgis
6241+
@gdaltest.enable_exceptions()
6242+
def test_ogr_pg_findfield(pg_ds):
6243+
6244+
src_ds = ogr.GetDriverByName("Memory").CreateDataSource("")
6245+
src_lyr = src_ds.CreateLayer("test_very_long_field_name")
6246+
src_lyr.CreateField(
6247+
ogr.FieldDefn(
6248+
"veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeryyyyyyyyyyyyyyyyyyyyyyloooooooooooooong"
6249+
)
6250+
)
6251+
f = ogr.Feature(src_lyr.GetLayerDefn())
6252+
f.SetField(0, "foo")
6253+
src_lyr.CreateFeature(f)
6254+
6255+
with gdal.quiet_errors():
6256+
gdal.VectorTranslate(pg_ds.GetDescription(), src_ds)
6257+
6258+
with gdal.quiet_errors():
6259+
gdal.VectorTranslate(pg_ds.GetDescription(), src_ds, accessMode="append")
6260+
6261+
lyr = pg_ds.GetLayerByName("test_very_long_field_name")
6262+
assert [f.GetField(0) for f in lyr] == ["foo", None]
6263+
6264+
with gdal.quiet_errors():
6265+
gdal.VectorTranslate(
6266+
pg_ds.GetDescription(),
6267+
src_ds,
6268+
accessMode="append",
6269+
relaxedFieldNameMatch=True,
6270+
)
6271+
assert [f.GetField(0) for f in lyr] == ["foo", None, "foo"]

ogr/ogrsf_frmts/pg/ogr_pg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ class OGRPGTableLayer final : public OGRPGLayer
445445
GDALProgressFunc pfnProgress,
446446
void *pProgressData) override;
447447

448+
int FindFieldIndex(const char *pszFieldName, int bExactMatch) override;
449+
448450
// follow methods are not base class overrides
449451
void SetLaunderFlag(int bFlag)
450452
{

ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4034,4 +4034,25 @@ OGRGeometryTypeCounter *OGRPGTableLayer::GetGeometryTypes(
40344034
return pasRet;
40354035
}
40364036

4037+
/************************************************************************/
4038+
/* FindFieldIndex() */
4039+
/************************************************************************/
4040+
4041+
int OGRPGTableLayer::FindFieldIndex(const char *pszFieldName, int bExactMatch)
4042+
{
4043+
const auto poLayerDefn = GetLayerDefn();
4044+
int iField = poLayerDefn->GetFieldIndex(pszFieldName);
4045+
4046+
if (!bExactMatch && iField < 0 && bLaunderColumnNames)
4047+
{
4048+
CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
4049+
char *pszSafeName =
4050+
OGRPGCommonLaunderName(pszFieldName, "PG", m_bUTF8ToASCII);
4051+
iField = poLayerDefn->GetFieldIndex(pszSafeName);
4052+
CPLFree(pszSafeName);
4053+
}
4054+
4055+
return iField;
4056+
}
4057+
40374058
#undef PQexec

0 commit comments

Comments
 (0)