Skip to content

Commit

Permalink
PG: implement OGRLayer::FindFieldIndex()
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Oct 29, 2024
1 parent b98de29 commit 0c00065
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
38 changes: 38 additions & 0 deletions autotest/ogr/ogr_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6231,3 +6231,41 @@ def test_ogr_pg_empty_search_path(pg_ds):

finally:
ds.ExecuteSQL(f"ALTER ROLE {current_user} SET search_path = {old_search_path}")


###############################################################################
# Test appending to a layer where a field name was truncated to 63 characters.


@only_without_postgis
@gdaltest.enable_exceptions()
def test_ogr_pg_findfield(pg_ds):

src_ds = ogr.GetDriverByName("Memory").CreateDataSource("")
src_lyr = src_ds.CreateLayer("test_very_long_field_name")
src_lyr.CreateField(
ogr.FieldDefn(
"veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeryyyyyyyyyyyyyyyyyyyyyyloooooooooooooong"
)
)
f = ogr.Feature(src_lyr.GetLayerDefn())
f.SetField(0, "foo")
src_lyr.CreateFeature(f)

with gdal.quiet_errors():
gdal.VectorTranslate(pg_ds.GetDescription(), src_ds)

with gdal.quiet_errors():
gdal.VectorTranslate(pg_ds.GetDescription(), src_ds, accessMode="append")

lyr = pg_ds.GetLayerByName("test_very_long_field_name")
assert [f.GetField(0) for f in lyr] == ["foo", None]

with gdal.quiet_errors():
gdal.VectorTranslate(
pg_ds.GetDescription(),
src_ds,
accessMode="append",
relaxedFieldNameMatch=True,
)
assert [f.GetField(0) for f in lyr] == ["foo", None, "foo"]
2 changes: 2 additions & 0 deletions ogr/ogrsf_frmts/pg/ogr_pg.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ class OGRPGTableLayer final : public OGRPGLayer
GDALProgressFunc pfnProgress,
void *pProgressData) override;

int FindFieldIndex(const char *pszFieldName, int bExactMatch) override;

// follow methods are not base class overrides
void SetLaunderFlag(int bFlag)
{
Expand Down
21 changes: 21 additions & 0 deletions ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4034,4 +4034,25 @@ OGRGeometryTypeCounter *OGRPGTableLayer::GetGeometryTypes(
return pasRet;
}

/************************************************************************/
/* FindFieldIndex() */
/************************************************************************/

int OGRPGTableLayer::FindFieldIndex(const char *pszFieldName, int bExactMatch)
{
const auto poLayerDefn = GetLayerDefn();
int iField = poLayerDefn->GetFieldIndex(pszFieldName);

if (!bExactMatch && iField < 0 && bLaunderColumnNames)
{
CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
char *pszSafeName =
OGRPGCommonLaunderName(pszFieldName, "PG", m_bUTF8ToASCII);
iField = poLayerDefn->GetFieldIndex(pszSafeName);
CPLFree(pszSafeName);
}

return iField;
}

#undef PQexec

0 comments on commit 0c00065

Please sign in to comment.