LCOV - code coverage report
Current view: top level - ogr - ogrfielddefn.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 440 518 84.9 %
Date: 2024-11-21 22:18:42 Functions: 79 85 92.9 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  The OGRFieldDefn class implementation.
       5             :  * Author:   Frank Warmerdam, warmerda@home.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 1999,  Les Technologies SoftMap Inc.
       9             :  * Copyright (c) 2009-2013, Even Rouault <even dot rouault at spatialys.com>
      10             :  *
      11             :  * SPDX-License-Identifier: MIT
      12             :  ****************************************************************************/
      13             : 
      14             : #include "cpl_port.h"
      15             : #include "ogr_feature.h"
      16             : 
      17             : #include <cstring>
      18             : 
      19             : #include "ogr_api.h"
      20             : #include "ogr_core.h"
      21             : #include "ogr_p.h"
      22             : #include "ograpispy.h"
      23             : #include "cpl_conv.h"
      24             : #include "cpl_error.h"
      25             : #include "cpl_string.h"
      26             : 
      27             : /************************************************************************/
      28             : /*                            OGRFieldDefn()                            */
      29             : /************************************************************************/
      30             : 
      31             : /**
      32             :  * \brief Constructor.
      33             :  *
      34             :  * By default, fields have no width, precision, are nullable and not ignored.
      35             :  *
      36             :  * @param pszNameIn the name of the new field.
      37             :  * @param eTypeIn the type of the new field.
      38             :  */
      39             : 
      40      618524 : OGRFieldDefn::OGRFieldDefn(const char *pszNameIn, OGRFieldType eTypeIn)
      41      618524 :     : pszName(CPLStrdup(pszNameIn)), pszAlternativeName(CPLStrdup("")),
      42             :       eType(eTypeIn), eJustify(OJUndefined),
      43             :       // Should nWidth & nPrecision be defined in some particular way for
      44             :       // numbers?
      45             :       nWidth(0), nPrecision(0), pszDefault(nullptr), bIgnore(FALSE),
      46      618524 :       eSubType(OFSTNone), bNullable(TRUE), bUnique(FALSE)
      47             : {
      48      618524 : }
      49             : 
      50             : /************************************************************************/
      51             : /*                            OGRFieldDefn()                            */
      52             : /************************************************************************/
      53             : 
      54             : /**
      55             :  * \brief Constructor.
      56             :  *
      57             :  * Create by cloning an existing field definition.
      58             :  *
      59             :  * @param poPrototype the field definition to clone.
      60             :  */
      61             : 
      62      722776 : OGRFieldDefn::OGRFieldDefn(const OGRFieldDefn *poPrototype)
      63      722776 :     : pszName(CPLStrdup(poPrototype->GetNameRef())),
      64      722776 :       pszAlternativeName(CPLStrdup(poPrototype->GetAlternativeNameRef())),
      65     1445550 :       eType(poPrototype->GetType()), eJustify(poPrototype->GetJustify()),
      66     1445550 :       nWidth(poPrototype->GetWidth()), nPrecision(poPrototype->GetPrecision()),
      67             :       pszDefault(nullptr),
      68             :       bIgnore(FALSE),  // TODO(schwehr): Can we use IsIgnored()?
      69     1445550 :       eSubType(poPrototype->GetSubType()), bNullable(poPrototype->IsNullable()),
      70      722776 :       bUnique(poPrototype->IsUnique()),
      71      722776 :       m_osDomainName(poPrototype->m_osDomainName),
      72      722776 :       m_osComment(poPrototype->GetComment()),
      73     1445550 :       m_nTZFlag(poPrototype->GetTZFlag())
      74             : {
      75      722776 :     SetDefault(poPrototype->GetDefault());
      76      722776 : }
      77             : 
      78             : /************************************************************************/
      79             : /*                           OGR_Fld_Create()                           */
      80             : /************************************************************************/
      81             : /**
      82             :  * \brief Create a new field definition.
      83             :  *
      84             :  * By default, fields have no width, precision, are nullable and not ignored.
      85             :  *
      86             :  * This function is the same as the CPP method OGRFieldDefn::OGRFieldDefn().
      87             :  *
      88             :  * @param pszName the name of the new field definition.
      89             :  * @param eType the type of the new field definition.
      90             :  * @return handle to the new field definition.
      91             :  */
      92             : 
      93       78004 : OGRFieldDefnH OGR_Fld_Create(const char *pszName, OGRFieldType eType)
      94             : 
      95             : {
      96       78004 :     return OGRFieldDefn::ToHandle(new OGRFieldDefn(pszName, eType));
      97             : }
      98             : 
      99             : /************************************************************************/
     100             : /*                           ~OGRFieldDefn()                            */
     101             : /************************************************************************/
     102             : 
     103     1341270 : OGRFieldDefn::~OGRFieldDefn()
     104             : 
     105             : {
     106     1341270 :     CPLFree(pszName);
     107     1341270 :     CPLFree(pszAlternativeName);
     108     1341270 :     CPLFree(pszDefault);
     109     1341270 : }
     110             : 
     111             : /************************************************************************/
     112             : /*                          OGR_Fld_Destroy()                           */
     113             : /************************************************************************/
     114             : /**
     115             :  * \brief Destroy a field definition.
     116             :  *
     117             :  * @param hDefn handle to the field definition to destroy.
     118             :  */
     119             : 
     120       77991 : void OGR_Fld_Destroy(OGRFieldDefnH hDefn)
     121             : 
     122             : {
     123       77991 :     delete OGRFieldDefn::FromHandle(hDefn);
     124       77991 : }
     125             : 
     126             : /************************************************************************/
     127             : /*                              SetName()                               */
     128             : /************************************************************************/
     129             : 
     130             : /**
     131             :  * \brief Reset the name of this field.
     132             :  *
     133             :  * This method is the same as the C function OGR_Fld_SetName().
     134             :  *
     135             :  * Note that once a OGRFieldDefn has been added to a layer definition with
     136             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
     137             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
     138             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
     139             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
     140             :  *
     141             :  * @param pszNameIn the new name to apply.
     142             :  */
     143             : 
     144      110059 : void OGRFieldDefn::SetName(const char *pszNameIn)
     145             : 
     146             : {
     147      110059 :     if (m_bSealed)
     148             :     {
     149           5 :         CPLError(CE_Failure, CPLE_AppDefined,
     150             :                  "OGRFieldDefn::SetName() not allowed on a sealed object");
     151           5 :         return;
     152             :     }
     153      110054 :     if (pszName != pszNameIn)
     154             :     {
     155      110047 :         CPLFree(pszName);
     156      110047 :         pszName = CPLStrdup(pszNameIn);
     157             :     }
     158             : }
     159             : 
     160             : /************************************************************************/
     161             : /*                          OGR_Fld_SetName()                           */
     162             : /************************************************************************/
     163             : /**
     164             :  * \brief Reset the name of this field.
     165             :  *
     166             :  * This function is the same as the CPP method OGRFieldDefn::SetName().
     167             :  *
     168             :  * Note that once a OGRFieldDefn has been added to a layer definition with
     169             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
     170             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
     171             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
     172             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
     173             :  *
     174             :  * @param hDefn handle to the field definition to apply the new name to.
     175             :  * @param pszName the new name to apply.
     176             :  */
     177             : 
     178           2 : void OGR_Fld_SetName(OGRFieldDefnH hDefn, const char *pszName)
     179             : 
     180             : {
     181           2 :     OGRFieldDefn::FromHandle(hDefn)->SetName(pszName);
     182           2 : }
     183             : 
     184             : /************************************************************************/
     185             : /*                             GetNameRef()                             */
     186             : /************************************************************************/
     187             : 
     188             : /**
     189             :  * \fn const char *OGRFieldDefn::GetNameRef();
     190             :  *
     191             :  * \brief Fetch name of this field.
     192             :  *
     193             :  * This method is the same as the C function OGR_Fld_GetNameRef().
     194             :  *
     195             :  * @return pointer to an internal name string that should not be freed or
     196             :  * modified.
     197             :  */
     198             : 
     199             : /************************************************************************/
     200             : /*                         OGR_Fld_GetNameRef()                         */
     201             : /************************************************************************/
     202             : /**
     203             :  * \brief Fetch name of this field.
     204             :  *
     205             :  * This function is the same as the CPP method OGRFieldDefn::GetNameRef().
     206             :  *
     207             :  * @param hDefn handle to the field definition.
     208             :  * @return the name of the field definition.
     209             :  *
     210             :  */
     211             : 
     212      213620 : const char *OGR_Fld_GetNameRef(OGRFieldDefnH hDefn)
     213             : 
     214             : {
     215             : 
     216             : #ifdef OGRAPISPY_ENABLED
     217      213620 :     if (bOGRAPISpyEnabled)
     218           2 :         OGRAPISpy_Fld_GetXXXX(hDefn, "GetNameRef");
     219             : #endif
     220             : 
     221      213620 :     return OGRFieldDefn::FromHandle(hDefn)->GetNameRef();
     222             : }
     223             : 
     224             : /************************************************************************/
     225             : /*                              SetAlternativeName()                    */
     226             : /************************************************************************/
     227             : 
     228             : /**
     229             :  * \brief Reset the alternative name (or "alias") for this field.
     230             :  *
     231             :  * The alternative name is an optional attribute for a field which can provide
     232             :  * a more user-friendly, descriptive name of a field which is not subject to
     233             :  * the usual naming constraints defined by the data provider.
     234             :  *
     235             :  * This is a metadata style attribute only: the alternative name cannot
     236             :  * be used in place of the actual field name during SQL queries or other
     237             :  * field name dependent API calls.
     238             :  *
     239             :  * This method is the same as the C function OGR_Fld_SetAlternativeName().
     240             :  *
     241             :  * Note that once a OGRFieldDefn has been added to a layer definition with
     242             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
     243             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
     244             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
     245             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
     246             :  *
     247             :  * @param pszAlternativeNameIn the new alternative name to apply.
     248             :  *
     249             :  * @since GDAL 3.2
     250             :  */
     251             : 
     252       71228 : void OGRFieldDefn::SetAlternativeName(const char *pszAlternativeNameIn)
     253             : 
     254             : {
     255       71228 :     if (m_bSealed)
     256             :     {
     257           1 :         CPLError(CE_Failure, CPLE_AppDefined,
     258             :                  "OGRFieldDefn::SetAlternativeName() not allowed on a sealed "
     259             :                  "object");
     260           1 :         return;
     261             :     }
     262       71227 :     if (pszAlternativeName != pszAlternativeNameIn)
     263             :     {
     264       71227 :         CPLFree(pszAlternativeName);
     265       71227 :         pszAlternativeName = CPLStrdup(pszAlternativeNameIn);
     266             :     }
     267             : }
     268             : 
     269             : /************************************************************************/
     270             : /*                          OGR_Fld_SetAlternativeName()                */
     271             : /************************************************************************/
     272             : /**
     273             :  * \brief Reset the alternative name (or "alias") for this field.
     274             :  *
     275             :  * The alternative name is an optional attribute for a field which can provide
     276             :  * a more user-friendly, descriptive name of a field which is not subject to
     277             :  * the usual naming constraints defined by the data provider.
     278             :  *
     279             :  * This is a metadata style attribute only: the alternative name cannot
     280             :  * be used in place of the actual field name during SQL queries or other
     281             :  * field name dependent API calls.
     282             :  *
     283             :  * This function is the same as the CPP method
     284             :  * OGRFieldDefn::SetAlternativeName().
     285             :  *
     286             :  * Note that once a OGRFieldDefn has been added to a layer definition with
     287             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
     288             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
     289             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
     290             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
     291             :  *
     292             :  * @param hDefn handle to the field definition to apply the new alternative name
     293             :  * to.
     294             :  * @param pszAlternativeName the new alternative name to apply.
     295             :  *
     296             :  * @since GDAL 3.2
     297             :  */
     298             : 
     299          19 : void OGR_Fld_SetAlternativeName(OGRFieldDefnH hDefn,
     300             :                                 const char *pszAlternativeName)
     301             : 
     302             : {
     303          19 :     OGRFieldDefn::FromHandle(hDefn)->SetAlternativeName(pszAlternativeName);
     304          19 : }
     305             : 
     306             : /************************************************************************/
     307             : /*                             GetAlternativeNameRef()                  */
     308             : /************************************************************************/
     309             : 
     310             : /**
     311             :  * \fn const char *OGRFieldDefn::GetAlternativeNameRef();
     312             :  *
     313             :  * \brief Fetch the alternative name (or "alias") for this field.
     314             :  *
     315             :  * The alternative name is an optional attribute for a field which can provide
     316             :  * a more user-friendly, descriptive name of a field which is not subject to
     317             :  * the usual naming constraints defined by the data provider.
     318             :  *
     319             :  * This is a metadata style attribute only: the alternative name cannot
     320             :  * be used in place of the actual field name during SQL queries or other
     321             :  * field name dependent API calls.
     322             :  *
     323             :  * This method is the same as the C function OGR_Fld_GetAlternativeNameRef().
     324             :  *
     325             :  * @return pointer to an internal alternative name string that should not be
     326             :  * freed or modified.
     327             :  *
     328             :  * @since GDAL 3.2
     329             :  */
     330             : 
     331             : /************************************************************************/
     332             : /*                         OGR_Fld_GetAlternativeNameRef()              */
     333             : /************************************************************************/
     334             : /**
     335             :  * \brief Fetch the alternative name (or "alias") for this field.
     336             :  *
     337             :  * The alternative name is an optional attribute for a field which can provide
     338             :  * a more user-friendly, descriptive name of a field which is not subject to
     339             :  * the usual naming constraints defined by the data provider.
     340             :  *
     341             :  * This is a metadata style attribute only: the alternative name cannot
     342             :  * be used in place of the actual field name during SQL queries or other
     343             :  * field name dependent API calls.
     344             :  *
     345             :  * This function is the same as the CPP method
     346             :  * OGRFieldDefn::GetAlternativeNameRef().
     347             :  *
     348             :  * @param hDefn handle to the field definition.
     349             :  * @return the alternative name of the field definition.
     350             :  *
     351             :  * @since GDAL 3.2
     352             :  */
     353             : 
     354          60 : const char *OGR_Fld_GetAlternativeNameRef(OGRFieldDefnH hDefn)
     355             : 
     356             : {
     357             : 
     358             : #ifdef OGRAPISPY_ENABLED
     359          60 :     if (bOGRAPISpyEnabled)
     360           0 :         OGRAPISpy_Fld_GetXXXX(hDefn, "GetAlternativeNameRef");
     361             : #endif
     362             : 
     363          60 :     return OGRFieldDefn::FromHandle(hDefn)->GetAlternativeNameRef();
     364             : }
     365             : 
     366             : /************************************************************************/
     367             : /*                              GetType()                               */
     368             : /************************************************************************/
     369             : 
     370             : /**
     371             :  * \fn OGRFieldType OGRFieldDefn::GetType() const;
     372             :  *
     373             :  * \brief Fetch type of this field.
     374             :  *
     375             :  * This method is the same as the C function OGR_Fld_GetType().
     376             :  *
     377             :  * @return field type.
     378             :  */
     379             : 
     380             : /************************************************************************/
     381             : /*                          OGR_Fld_GetType()                           */
     382             : /************************************************************************/
     383             : /**
     384             :  * \brief Fetch type of this field.
     385             :  *
     386             :  * This function is the same as the CPP method OGRFieldDefn::GetType().
     387             :  *
     388             :  * @param hDefn handle to the field definition to get type from.
     389             :  * @return field type.
     390             :  */
     391             : 
     392      105564 : OGRFieldType OGR_Fld_GetType(OGRFieldDefnH hDefn)
     393             : 
     394             : {
     395             : 
     396             : #ifdef OGRAPISPY_ENABLED
     397      105564 :     if (bOGRAPISpyEnabled)
     398           2 :         OGRAPISpy_Fld_GetXXXX(hDefn, "GetType");
     399             : #endif
     400             : 
     401      105564 :     return OGRFieldDefn::FromHandle(hDefn)->GetType();
     402             : }
     403             : 
     404             : /************************************************************************/
     405             : /*                              SetType()                               */
     406             : /************************************************************************/
     407             : 
     408             : /**
     409             :  * \brief Set the type of this field.
     410             :  * This should never be done to an OGRFieldDefn
     411             :  * that is already part of an OGRFeatureDefn.
     412             :  *
     413             :  * This method is the same as the C function OGR_Fld_SetType().
     414             :  *
     415             :  * Note that once a OGRFieldDefn has been added to a layer definition with
     416             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
     417             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
     418             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
     419             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
     420             :  *
     421             :  * @param eTypeIn the new field type.
     422             :  */
     423             : 
     424      273937 : void OGRFieldDefn::SetType(OGRFieldType eTypeIn)
     425             : {
     426      273937 :     if (m_bSealed)
     427             :     {
     428           1 :         CPLError(CE_Failure, CPLE_AppDefined,
     429             :                  "OGRFieldDefn::SetType() not allowed on a sealed object");
     430           1 :         return;
     431             :     }
     432      273936 :     if (!OGR_AreTypeSubTypeCompatible(eTypeIn, eSubType))
     433             :     {
     434           0 :         CPLError(CE_Warning, CPLE_AppDefined,
     435             :                  "Type and subtype of field definition are not compatible. "
     436             :                  "Resetting to OFSTNone");
     437           0 :         eSubType = OFSTNone;
     438             :     }
     439      273936 :     eType = eTypeIn;
     440             : }
     441             : 
     442             : /************************************************************************/
     443             : /*                          OGR_Fld_SetType()                           */
     444             : /************************************************************************/
     445             : /**
     446             :  * \brief Set the type of this field.
     447             :  * This should never be done to an OGRFieldDefn
     448             :  * that is already part of an OGRFeatureDefn.
     449             :  *
     450             :  * This function is the same as the CPP method OGRFieldDefn::SetType().
     451             :  *
     452             :  * Note that once a OGRFieldDefn has been added to a layer definition with
     453             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
     454             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
     455             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
     456             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
     457             :  *
     458             :  * @param hDefn handle to the field definition to set type to.
     459             :  * @param eType the new field type.
     460             :  */
     461             : 
     462           0 : void OGR_Fld_SetType(OGRFieldDefnH hDefn, OGRFieldType eType)
     463             : 
     464             : {
     465           0 :     OGRFieldDefn::FromHandle(hDefn)->SetType(eType);
     466           0 : }
     467             : 
     468             : /************************************************************************/
     469             : /*                             GetSubType()                             */
     470             : /************************************************************************/
     471             : 
     472             : /**
     473             :  * \fn OGRFieldSubType OGRFieldDefn::GetSubType() const;
     474             :  *
     475             :  * \brief Fetch subtype of this field.
     476             :  *
     477             :  * This method is the same as the C function OGR_Fld_GetSubType().
     478             :  *
     479             :  * @return field subtype.
     480             :  * @since GDAL 2.0
     481             :  */
     482             : 
     483             : /************************************************************************/
     484             : /*                         OGR_Fld_GetSubType()                         */
     485             : /************************************************************************/
     486             : /**
     487             :  * \brief Fetch subtype of this field.
     488             :  *
     489             :  * This function is the same as the CPP method OGRFieldDefn::GetSubType().
     490             :  *
     491             :  * @param hDefn handle to the field definition to get subtype from.
     492             :  * @return field subtype.
     493             :  * @since GDAL 2.0
     494             :  */
     495             : 
     496       88502 : OGRFieldSubType OGR_Fld_GetSubType(OGRFieldDefnH hDefn)
     497             : 
     498             : {
     499             : 
     500             : #ifdef OGRAPISPY_ENABLED
     501       88502 :     if (bOGRAPISpyEnabled)
     502           2 :         OGRAPISpy_Fld_GetXXXX(hDefn, "GetSubType");
     503             : #endif
     504             : 
     505       88502 :     return OGRFieldDefn::FromHandle(hDefn)->GetSubType();
     506             : }
     507             : 
     508             : /************************************************************************/
     509             : /*                             SetSubType()                             */
     510             : /************************************************************************/
     511             : 
     512             : /**
     513             :  * \brief Set the subtype of this field.
     514             :  * This should never be done to an OGRFieldDefn
     515             :  * that is already part of an OGRFeatureDefn.
     516             :  *
     517             :  * This method is the same as the C function OGR_Fld_SetSubType().
     518             :  *
     519             :  * Note that once a OGRFieldDefn has been added to a layer definition with
     520             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
     521             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
     522             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
     523             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
     524             :  *
     525             :  * @param eSubTypeIn the new field subtype.
     526             :  * @since GDAL 2.0
     527             :  */
     528      310349 : void OGRFieldDefn::SetSubType(OGRFieldSubType eSubTypeIn)
     529             : {
     530      310349 :     if (m_bSealed)
     531             :     {
     532           1 :         CPLError(CE_Failure, CPLE_AppDefined,
     533             :                  "OGRFieldDefn::SetSubType() not allowed on a sealed object");
     534           1 :         return;
     535             :     }
     536      310348 :     if (!OGR_AreTypeSubTypeCompatible(eType, eSubTypeIn))
     537             :     {
     538           3 :         CPLError(CE_Warning, CPLE_AppDefined,
     539             :                  "Type and subtype of field definition are not compatible. "
     540             :                  "Resetting to OFSTNone");
     541           3 :         eSubType = OFSTNone;
     542             :     }
     543             :     else
     544             :     {
     545      310345 :         eSubType = eSubTypeIn;
     546             :     }
     547             : }
     548             : 
     549             : /************************************************************************/
     550             : /*                         OGR_Fld_SetSubType()                         */
     551             : /************************************************************************/
     552             : /**
     553             :  * \brief Set the subtype of this field.
     554             :  * This should never be done to an OGRFieldDefn
     555             :  * that is already part of an OGRFeatureDefn.
     556             :  *
     557             :  * This function is the same as the CPP method OGRFieldDefn::SetSubType().
     558             :  *
     559             :  * Note that once a OGRFieldDefn has been added to a layer definition with
     560             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
     561             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
     562             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
     563             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
     564             :  *
     565             :  * @param hDefn handle to the field definition to set type to.
     566             :  * @param eSubType the new field subtype.
     567             :  * @since GDAL 2.0
     568             :  */
     569             : 
     570         555 : void OGR_Fld_SetSubType(OGRFieldDefnH hDefn, OGRFieldSubType eSubType)
     571             : 
     572             : {
     573         555 :     OGRFieldDefn::FromHandle(hDefn)->SetSubType(eSubType);
     574         555 : }
     575             : 
     576             : /************************************************************************/
     577             : /*                             SetDefault()                             */
     578             : /************************************************************************/
     579             : 
     580             : /**
     581             :  * \brief Set default field value.
     582             :  *
     583             :  * The default field value is taken into account by drivers (generally those
     584             :  * with a SQL interface) that support it at field creation time. OGR will
     585             :  * generally not automatically set the default field value to null fields by
     586             :  * itself when calling OGRFeature::CreateFeature() / OGRFeature::SetFeature(),
     587             :  * but will let the low-level layers to do the job. So retrieving the feature
     588             :  * from the layer is recommended.
     589             :  *
     590             :  * The accepted values are NULL, a numeric value, a literal value enclosed
     591             :  * between single quote characters (and inner single quote characters escaped by
     592             :  * repetition of the single quote character),
     593             :  * CURRENT_TIMESTAMP, CURRENT_TIME, CURRENT_DATE or
     594             :  * a driver specific expression (that might be ignored by other drivers).
     595             :  * For a datetime literal value, format should be 'YYYY/MM/DD HH:MM:SS[.sss]'
     596             :  * (considered as UTC time).
     597             :  *
     598             :  * Drivers that support writing DEFAULT clauses will advertise the
     599             :  * GDAL_DCAP_DEFAULT_FIELDS driver metadata item.
     600             :  *
     601             :  * This function is the same as the C function OGR_Fld_SetDefault().
     602             :  *
     603             :  * Note that once a OGRFieldDefn has been added to a layer definition with
     604             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
     605             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
     606             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
     607             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
     608             :  *
     609             :  * @param pszDefaultIn new default field value or NULL pointer.
     610             :  *
     611             :  * @since GDAL 2.0
     612             :  */
     613             : 
     614      724114 : void OGRFieldDefn::SetDefault(const char *pszDefaultIn)
     615             : 
     616             : {
     617      724114 :     if (m_bSealed)
     618             :     {
     619           1 :         CPLError(CE_Failure, CPLE_AppDefined,
     620             :                  "OGRFieldDefn::SetDefault() not allowed on a sealed object");
     621           1 :         return;
     622             :     }
     623      724113 :     CPLFree(pszDefault);
     624      724113 :     pszDefault = nullptr;
     625             : 
     626      724113 :     if (pszDefaultIn && pszDefaultIn[0] == '\'' &&
     627        1672 :         pszDefaultIn[strlen(pszDefaultIn) - 1] == '\'')
     628             :     {
     629        1671 :         const char *pszPtr = pszDefaultIn + 1;  // Used after for.
     630       16182 :         for (; *pszPtr != '\0'; pszPtr++)
     631             :         {
     632       16181 :             if (*pszPtr == '\'')
     633             :             {
     634        1722 :                 if (pszPtr[1] == '\0')
     635        1669 :                     break;
     636          53 :                 if (pszPtr[1] != '\'')
     637             :                 {
     638           1 :                     CPLError(CE_Failure, CPLE_AppDefined,
     639             :                              "Incorrectly quoted string literal");
     640           1 :                     return;
     641             :                 }
     642          52 :                 pszPtr++;
     643             :             }
     644             :         }
     645        1670 :         if (*pszPtr == '\0')
     646             :         {
     647           1 :             CPLError(CE_Failure, CPLE_AppDefined,
     648             :                      "Incorrectly quoted string literal");
     649           1 :             return;
     650             :         }
     651             :     }
     652             : 
     653      724111 :     pszDefault = pszDefaultIn ? CPLStrdup(pszDefaultIn) : nullptr;
     654             : }
     655             : 
     656             : /************************************************************************/
     657             : /*                         OGR_Fld_SetDefault()                         */
     658             : /************************************************************************/
     659             : 
     660             : /**
     661             :  * \brief Set default field value.
     662             :  *
     663             :  * The default field value is taken into account by drivers (generally those
     664             :  * with a SQL interface) that support it at field creation time. OGR will
     665             :  * generally not automatically set the default field value to null fields by
     666             :  * itself when calling OGRFeature::CreateFeature() / OGRFeature::SetFeature(),
     667             :  * but will let the low-level layers to do the job. So retrieving the feature
     668             :  * from the layer is recommended.
     669             :  *
     670             :  * The accepted values are NULL, a numeric value, a literal value enclosed
     671             :  * between single quote characters (and inner single quote characters escaped by
     672             :  * repetition of the single quote character),
     673             :  * CURRENT_TIMESTAMP, CURRENT_TIME, CURRENT_DATE or
     674             :  * a driver specific expression (that might be ignored by other drivers).
     675             :  * For a datetime literal value, format should be 'YYYY/MM/DD HH:MM:SS[.sss]'
     676             :  * (considered as UTC time).
     677             :  *
     678             :  * Drivers that support writing DEFAULT clauses will advertise the
     679             :  * GDAL_DCAP_DEFAULT_FIELDS driver metadata item.
     680             :  *
     681             :  * This function is the same as the C++ method OGRFieldDefn::SetDefault().
     682             :  *
     683             :  * Note that once a OGRFieldDefn has been added to a layer definition with
     684             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
     685             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
     686             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
     687             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
     688             :  *
     689             :  * @param hDefn handle to the field definition.
     690             :  * @param pszDefault new default field value or NULL pointer.
     691             :  *
     692             :  * @since GDAL 2.0
     693             :  */
     694             : 
     695         154 : void CPL_DLL OGR_Fld_SetDefault(OGRFieldDefnH hDefn, const char *pszDefault)
     696             : {
     697         154 :     OGRFieldDefn::FromHandle(hDefn)->SetDefault(pszDefault);
     698         154 : }
     699             : 
     700             : /************************************************************************/
     701             : /*                             GetDefault()                             */
     702             : /************************************************************************/
     703             : 
     704             : /**
     705             :  * \brief Get default field value.
     706             :  *
     707             :  * This function is the same as the C function OGR_Fld_GetDefault().
     708             :  *
     709             :  * @return default field value or NULL.
     710             :  * @since GDAL 2.0
     711             :  */
     712             : 
     713      756879 : const char *OGRFieldDefn::GetDefault() const
     714             : 
     715             : {
     716      756879 :     return pszDefault;
     717             : }
     718             : 
     719             : /************************************************************************/
     720             : /*                         OGR_Fld_GetDefault()                         */
     721             : /************************************************************************/
     722             : 
     723             : /**
     724             :  * \brief Get default field value.
     725             :  *
     726             :  * This function is the same as the C++ method OGRFieldDefn::GetDefault().
     727             :  *
     728             :  * @param hDefn handle to the field definition.
     729             :  * @return default field value or NULL.
     730             :  * @since GDAL 2.0
     731             :  */
     732             : 
     733         141 : const char *OGR_Fld_GetDefault(OGRFieldDefnH hDefn)
     734             : {
     735         141 :     return OGRFieldDefn::FromHandle(hDefn)->GetDefault();
     736             : }
     737             : 
     738             : /************************************************************************/
     739             : /*                        IsDefaultDriverSpecific()                     */
     740             : /************************************************************************/
     741             : 
     742             : /**
     743             :  * \brief Returns whether the default value is driver specific.
     744             :  *
     745             :  * Driver specific default values are those that are *not* NULL, a
     746             :  * numeric value, a literal value enclosed between single quote
     747             :  * characters, CURRENT_TIMESTAMP, CURRENT_TIME, CURRENT_DATE or
     748             :  * datetime literal value.
     749             :  *
     750             :  * This method is the same as the C function
     751             :  * OGR_Fld_IsDefaultDriverSpecific().
     752             :  *
     753             :  * @return TRUE if the default value is driver specific.
     754             :  * @since GDAL 2.0
     755             :  */
     756             : 
     757         216 : int OGRFieldDefn::IsDefaultDriverSpecific() const
     758             : {
     759         216 :     if (pszDefault == nullptr)
     760           1 :         return FALSE;
     761             : 
     762         215 :     if (EQUAL(pszDefault, "NULL") || EQUAL(pszDefault, "CURRENT_TIMESTAMP") ||
     763         195 :         EQUAL(pszDefault, "CURRENT_TIME") || EQUAL(pszDefault, "CURRENT_DATE"))
     764          31 :         return FALSE;
     765             : 
     766         184 :     if (pszDefault[0] == '\'' && pszDefault[strlen(pszDefault) - 1] == '\'')
     767          80 :         return FALSE;
     768             : 
     769         104 :     char *pszEnd = nullptr;
     770         104 :     CPLStrtod(pszDefault, &pszEnd);
     771         104 :     if (*pszEnd == '\0')
     772          73 :         return FALSE;
     773             : 
     774          31 :     return TRUE;
     775             : }
     776             : 
     777             : /************************************************************************/
     778             : /*                     OGR_Fld_IsDefaultDriverSpecific()                */
     779             : /************************************************************************/
     780             : 
     781             : /**
     782             :  * \brief Returns whether the default value is driver specific.
     783             :  *
     784             :  * Driver specific default values are those that are *not* NULL, a
     785             :  * numeric value, a literal value enclosed between single quote
     786             :  * characters, CURRENT_TIMESTAMP, CURRENT_TIME, CURRENT_DATE or
     787             :  * datetime literal value.
     788             :  *
     789             :  * This function is the same as the C++ method
     790             :  * OGRFieldDefn::IsDefaultDriverSpecific().
     791             :  *
     792             :  * @param hDefn handle to the field definition
     793             :  * @return TRUE if the default value is driver specific.
     794             :  * @since GDAL 2.0
     795             :  */
     796             : 
     797           3 : int OGR_Fld_IsDefaultDriverSpecific(OGRFieldDefnH hDefn)
     798             : {
     799           3 :     return OGRFieldDefn::FromHandle(hDefn)->IsDefaultDriverSpecific();
     800             : }
     801             : 
     802             : /************************************************************************/
     803             : /*                          GetFieldTypeName()                          */
     804             : /************************************************************************/
     805             : 
     806             : /**
     807             :  * \brief Fetch human readable name for a field type.
     808             :  *
     809             :  * This static method is the same as the C function OGR_GetFieldTypeName().
     810             :  *
     811             :  * @param eType the field type to get name for.
     812             :  *
     813             :  * @return pointer to an internal static name string. It should not be
     814             :  * modified or freed.
     815             :  */
     816             : 
     817       13704 : const char *OGRFieldDefn::GetFieldTypeName(OGRFieldType eType)
     818             : 
     819             : {
     820       13704 :     switch (eType)
     821             :     {
     822        2762 :         case OFTInteger:
     823        2762 :             return "Integer";
     824             : 
     825         886 :         case OFTInteger64:
     826         886 :             return "Integer64";
     827             : 
     828        1176 :         case OFTReal:
     829        1176 :             return "Real";
     830             : 
     831        5506 :         case OFTString:
     832        5506 :             return "String";
     833             : 
     834         869 :         case OFTIntegerList:
     835         869 :             return "IntegerList";
     836             : 
     837         209 :         case OFTInteger64List:
     838         209 :             return "Integer64List";
     839             : 
     840         624 :         case OFTRealList:
     841         624 :             return "RealList";
     842             : 
     843         339 :         case OFTStringList:
     844         339 :             return "StringList";
     845             : 
     846         239 :         case OFTBinary:
     847         239 :             return "Binary";
     848             : 
     849         233 :         case OFTDate:
     850         233 :             return "Date";
     851             : 
     852         188 :         case OFTTime:
     853         188 :             return "Time";
     854             : 
     855         409 :         case OFTDateTime:
     856         409 :             return "DateTime";
     857             : 
     858         264 :         case OFTWideString:
     859             :         case OFTWideStringList:
     860         264 :             break;
     861             :     }
     862             : 
     863         264 :     return "(unknown)";
     864             : }
     865             : 
     866             : /************************************************************************/
     867             : /*                        OGR_GetFieldTypeName()                        */
     868             : /************************************************************************/
     869             : /**
     870             :  * \brief Fetch human readable name for a field type.
     871             :  *
     872             :  * This function is the same as the CPP method
     873             :  * OGRFieldDefn::GetFieldTypeName().
     874             :  *
     875             :  * @param eType the field type to get name for.
     876             :  * @return the name.
     877             :  */
     878             : 
     879        2145 : const char *OGR_GetFieldTypeName(OGRFieldType eType)
     880             : 
     881             : {
     882        2145 :     return OGRFieldDefn::GetFieldTypeName(eType);
     883             : }
     884             : 
     885             : /************************************************************************/
     886             : /*                        GetFieldSubTypeName()                         */
     887             : /************************************************************************/
     888             : 
     889             : /**
     890             :  * \brief Fetch human readable name for a field subtype.
     891             :  *
     892             :  * This static method is the same as the C function OGR_GetFieldSubTypeName().
     893             :  *
     894             :  * @param eSubType the field subtype to get name for.
     895             :  *
     896             :  * @return pointer to an internal static name string. It should not be
     897             :  * modified or freed.
     898             :  *
     899             :  * @since GDAL 2.0
     900             :  */
     901             : 
     902        3318 : const char *OGRFieldDefn::GetFieldSubTypeName(OGRFieldSubType eSubType)
     903             : 
     904             : {
     905        3318 :     switch (eSubType)
     906             :     {
     907        1139 :         case OFSTNone:
     908        1139 :             break;
     909             : 
     910         555 :         case OFSTBoolean:
     911         555 :             return "Boolean";
     912             : 
     913         897 :         case OFSTInt16:
     914         897 :             return "Int16";
     915             : 
     916         210 :         case OFSTFloat32:
     917         210 :             return "Float32";
     918             : 
     919         510 :         case OFSTJSON:
     920         510 :             return "JSON";
     921             : 
     922           7 :         case OFSTUUID:
     923           7 :             return "UUID";
     924             :     }
     925        1139 :     return "None";
     926             : }
     927             : 
     928             : /************************************************************************/
     929             : /*                       OGR_GetFieldSubTypeName()                      */
     930             : /************************************************************************/
     931             : /**
     932             :  * \brief Fetch human readable name for a field subtype.
     933             :  *
     934             :  * This function is the same as the CPP method
     935             :  * OGRFieldDefn::GetFieldSubTypeName().
     936             :  *
     937             :  * @param eSubType the field subtype to get name for.
     938             :  * @return the name.
     939             :  *
     940             :  * @since GDAL 2.0
     941             :  */
     942             : 
     943        2280 : const char *OGR_GetFieldSubTypeName(OGRFieldSubType eSubType)
     944             : 
     945             : {
     946        2280 :     return OGRFieldDefn::GetFieldSubTypeName(eSubType);
     947             : }
     948             : 
     949             : /************************************************************************/
     950             : /*                       OGR_IsValidTypeAndSubType()                    */
     951             : /************************************************************************/
     952             : /**
     953             :  * \brief Return if type and subtype are compatible
     954             :  *
     955             :  * @param eType the field type.
     956             :  * @param eSubType the field subtype.
     957             :  * @return TRUE if type and subtype are compatible
     958             :  *
     959             :  * @since GDAL 2.0
     960             :  */
     961             : 
     962      584290 : int OGR_AreTypeSubTypeCompatible(OGRFieldType eType, OGRFieldSubType eSubType)
     963             : {
     964      584290 :     if (eSubType == OFSTNone)
     965      566060 :         return TRUE;
     966       18230 :     if (eSubType == OFSTBoolean || eSubType == OFSTInt16)
     967        6564 :         return eType == OFTInteger || eType == OFTIntegerList;
     968       11666 :     if (eSubType == OFSTFloat32)
     969        3161 :         return eType == OFTReal || eType == OFTRealList;
     970        8505 :     if (eSubType == OFSTJSON)
     971        8498 :         return eType == OFTString;
     972           7 :     if (eSubType == OFSTUUID)
     973           7 :         return eType == OFTString;
     974           0 :     return FALSE;
     975             : }
     976             : 
     977             : /************************************************************************/
     978             : /*                             GetJustify()                             */
     979             : /************************************************************************/
     980             : 
     981             : /**
     982             :  * \fn OGRJustification OGRFieldDefn::GetJustify() const;
     983             :  *
     984             :  * \brief Get the justification for this field.
     985             :  *
     986             :  * Note: no driver is know to use the concept of field justification.
     987             :  *
     988             :  * This method is the same as the C function OGR_Fld_GetJustify().
     989             :  *
     990             :  * @return the justification.
     991             :  */
     992             : 
     993             : /************************************************************************/
     994             : /*                         OGR_Fld_GetJustify()                         */
     995             : /************************************************************************/
     996             : /**
     997             :  * \brief Get the justification for this field.
     998             :  *
     999             :  * This function is the same as the CPP method OGRFieldDefn::GetJustify().
    1000             :  *
    1001             :  * Note: no driver is know to use the concept of field justification.
    1002             :  *
    1003             :  * @param hDefn handle to the field definition to get justification from.
    1004             :  * @return the justification.
    1005             :  */
    1006             : 
    1007           0 : OGRJustification OGR_Fld_GetJustify(OGRFieldDefnH hDefn)
    1008             : 
    1009             : {
    1010           0 :     return OGRFieldDefn::FromHandle(hDefn)->GetJustify();
    1011             : }
    1012             : 
    1013             : /************************************************************************/
    1014             : /*                             SetJustify()                             */
    1015             : /************************************************************************/
    1016             : 
    1017             : /**
    1018             :  * \fn void OGRFieldDefn::SetJustify( OGRJustification eJustify );
    1019             :  *
    1020             :  * \brief Set the justification for this field.
    1021             :  *
    1022             :  * Note: no driver is know to use the concept of field justification.
    1023             :  *
    1024             :  * This method is the same as the C function OGR_Fld_SetJustify().
    1025             :  *
    1026             :  * @param eJustify the new justification.
    1027             :  */
    1028             : 
    1029             : /************************************************************************/
    1030             : /*                         OGR_Fld_SetJustify()                         */
    1031             : /************************************************************************/
    1032             : /**
    1033             :  * \brief Set the justification for this field.
    1034             :  *
    1035             :  * Note: no driver is know to use the concept of field justification.
    1036             :  *
    1037             :  * This function is the same as the CPP method OGRFieldDefn::SetJustify().
    1038             :  *
    1039             :  * @param hDefn handle to the field definition to set justification to.
    1040             :  * @param eJustify the new justification.
    1041             :  */
    1042             : 
    1043           0 : void OGR_Fld_SetJustify(OGRFieldDefnH hDefn, OGRJustification eJustify)
    1044             : 
    1045             : {
    1046           0 :     OGRFieldDefn::FromHandle(hDefn)->SetJustify(eJustify);
    1047           0 : }
    1048             : 
    1049             : /************************************************************************/
    1050             : /*                              GetWidth()                              */
    1051             : /************************************************************************/
    1052             : 
    1053             : /**
    1054             :  * \fn int OGRFieldDefn::GetWidth() const;
    1055             :  *
    1056             :  * \brief Get the formatting width for this field.
    1057             :  *
    1058             :  * This method is the same as the C function OGR_Fld_GetWidth().
    1059             :  *
    1060             :  * @return the width, zero means no specified width.
    1061             :  */
    1062             : 
    1063             : /************************************************************************/
    1064             : /*                          OGR_Fld_GetWidth()                          */
    1065             : /************************************************************************/
    1066             : /**
    1067             :  * \brief Get the formatting width for this field.
    1068             :  *
    1069             :  * This function is the same as the CPP method OGRFieldDefn::GetWidth().
    1070             :  *
    1071             :  * @param hDefn handle to the field definition to get width from.
    1072             :  * @return the width, zero means no specified width.
    1073             :  */
    1074             : 
    1075        2731 : int OGR_Fld_GetWidth(OGRFieldDefnH hDefn)
    1076             : 
    1077             : {
    1078        2731 :     return OGRFieldDefn::FromHandle(hDefn)->GetWidth();
    1079             : }
    1080             : 
    1081             : /************************************************************************/
    1082             : /*                              SetWidth()                              */
    1083             : /************************************************************************/
    1084             : 
    1085             : /**
    1086             :  * \brief Set the formatting width for this field in characters.
    1087             :  *
    1088             :  * This method is the same as the C function OGR_Fld_SetWidth().
    1089             :  *
    1090             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1091             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1092             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1093             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1094             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
    1095             :  *
    1096             :  * @param nWidthIn the new width.
    1097             :  */
    1098      392000 : void OGRFieldDefn::SetWidth(int nWidthIn)
    1099             : {
    1100      392000 :     if (m_bSealed)
    1101             :     {
    1102           1 :         CPLError(CE_Failure, CPLE_AppDefined,
    1103             :                  "OGRFieldDefn::SetWidth() not allowed on a sealed object");
    1104           1 :         return;
    1105             :     }
    1106      391999 :     nWidth = MAX(0, nWidthIn);
    1107             : }
    1108             : 
    1109             : /************************************************************************/
    1110             : /*                          OGR_Fld_SetWidth()                          */
    1111             : /************************************************************************/
    1112             : /**
    1113             :  * \brief Set the formatting width for this field in characters.
    1114             :  *
    1115             :  * This function is the same as the CPP method OGRFieldDefn::SetWidth().
    1116             :  *
    1117             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1118             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1119             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1120             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1121             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
    1122             :  *
    1123             :  * @param hDefn handle to the field definition to set width to.
    1124             :  * @param nNewWidth the new width.
    1125             :  */
    1126             : 
    1127         339 : void OGR_Fld_SetWidth(OGRFieldDefnH hDefn, int nNewWidth)
    1128             : 
    1129             : {
    1130         339 :     OGRFieldDefn::FromHandle(hDefn)->SetWidth(nNewWidth);
    1131         339 : }
    1132             : 
    1133             : /************************************************************************/
    1134             : /*                            GetPrecision()                            */
    1135             : /************************************************************************/
    1136             : 
    1137             : /**
    1138             :  * \fn int OGRFieldDefn::GetPrecision() const;
    1139             :  *
    1140             :  * \brief Get the formatting precision for this field.
    1141             :  * This should normally be
    1142             :  * zero for fields of types other than OFTReal.
    1143             :  *
    1144             :  * This method is the same as the C function OGR_Fld_GetPrecision().
    1145             :  *
    1146             :  * @return the precision.
    1147             :  */
    1148             : 
    1149             : /************************************************************************/
    1150             : /*                        OGR_Fld_GetPrecision()                        */
    1151             : /************************************************************************/
    1152             : /**
    1153             :  * \brief Get the formatting precision for this field.
    1154             :  * This should normally be
    1155             :  * zero for fields of types other than OFTReal.
    1156             :  *
    1157             :  * This function is the same as the CPP method OGRFieldDefn::GetPrecision().
    1158             :  *
    1159             :  * @param hDefn handle to the field definition to get precision from.
    1160             :  * @return the precision.
    1161             :  */
    1162             : 
    1163        1604 : int OGR_Fld_GetPrecision(OGRFieldDefnH hDefn)
    1164             : 
    1165             : {
    1166        1604 :     return OGRFieldDefn::FromHandle(hDefn)->GetPrecision();
    1167             : }
    1168             : 
    1169             : /************************************************************************/
    1170             : /*                            SetPrecision()                            */
    1171             : /************************************************************************/
    1172             : 
    1173             : /**
    1174             :  * \brief Set the formatting precision for this field in characters.
    1175             :  *
    1176             :  * This should normally be zero for fields of types other than OFTReal.
    1177             :  *
    1178             :  * This method is the same as the C function OGR_Fld_SetPrecision().
    1179             :  *
    1180             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1181             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1182             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1183             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1184             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
    1185             :  *
    1186             :  * @param nPrecisionIn the new precision.
    1187             :  */
    1188      184662 : void OGRFieldDefn::SetPrecision(int nPrecisionIn)
    1189             : {
    1190      184662 :     if (m_bSealed)
    1191             :     {
    1192           1 :         CPLError(CE_Failure, CPLE_AppDefined,
    1193             :                  "OGRFieldDefn::SetPrecision() not allowed on a sealed object");
    1194           1 :         return;
    1195             :     }
    1196      184661 :     nPrecision = nPrecisionIn;
    1197             : }
    1198             : 
    1199             : /************************************************************************/
    1200             : /*                        OGR_Fld_SetPrecision()                        */
    1201             : /************************************************************************/
    1202             : /**
    1203             :  * \brief Set the formatting precision for this field in characters.
    1204             :  *
    1205             :  * This should normally be zero for fields of types other than OFTReal.
    1206             :  *
    1207             :  * This function is the same as the CPP method OGRFieldDefn::SetPrecision().
    1208             :  *
    1209             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1210             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1211             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1212             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1213             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
    1214             :  *
    1215             :  * @param hDefn handle to the field definition to set precision to.
    1216             :  * @param nPrecision the new precision.
    1217             :  */
    1218             : 
    1219          24 : void OGR_Fld_SetPrecision(OGRFieldDefnH hDefn, int nPrecision)
    1220             : 
    1221             : {
    1222          24 :     OGRFieldDefn::FromHandle(hDefn)->SetPrecision(nPrecision);
    1223          24 : }
    1224             : 
    1225             : /************************************************************************/
    1226             : /*                            GetTZFlag()                               */
    1227             : /************************************************************************/
    1228             : 
    1229             : /**
    1230             :  * \fn int OGRFieldDefn::GetTZFlag() const;
    1231             :  *
    1232             :  * \brief Get the time zone flag.
    1233             :  *
    1234             :  * Only applies to OFTTime, OFTDate and OFTDateTime fields.
    1235             :  *
    1236             :  * Cf OGR_TZFLAG_UNKNOWN, OGR_TZFLAG_LOCALTIME, OGR_TZFLAG_MIXED_TZ and
    1237             :  * OGR_TZFLAG_UTC
    1238             :  *
    1239             :  * This method is the same as the C function OGR_Fld_GetTZFlag().
    1240             :  *
    1241             :  * @return the time zone flag.
    1242             :  * @since GDAL 3.8
    1243             :  */
    1244             : 
    1245             : /************************************************************************/
    1246             : /*                        OGR_Fld_GetTZFlag()                           */
    1247             : /************************************************************************/
    1248             : /**
    1249             :  * \brief Get the time zone flag.
    1250             :  *
    1251             :  * Only applies to OFTTime, OFTDate and OFTDateTime fields.
    1252             :  *
    1253             :  * Cf OGR_TZFLAG_UNKNOWN, OGR_TZFLAG_LOCALTIME, OGR_TZFLAG_MIXED_TZ and
    1254             :  * OGR_TZFLAG_UTC
    1255             :  *
    1256             :  * @param hDefn handle to the field definition .
    1257             :  * @return the time zone flag.
    1258             :  * @since GDAL 3.8
    1259             :  */
    1260             : 
    1261           0 : int OGR_Fld_GetTZFlag(OGRFieldDefnH hDefn)
    1262             : 
    1263             : {
    1264           0 :     return OGRFieldDefn::FromHandle(hDefn)->GetTZFlag();
    1265             : }
    1266             : 
    1267             : /************************************************************************/
    1268             : /*                             SetTZFlag()                              */
    1269             : /************************************************************************/
    1270             : 
    1271             : /**
    1272             :  * \brief Set the time zone flag.
    1273             :  *
    1274             :  * Only applies to OFTTime, OFTDate and OFTDateTime fields.
    1275             :  *
    1276             :  * Cf OGR_TZFLAG_UNKNOWN, OGR_TZFLAG_LOCALTIME, OGR_TZFLAG_MIXED_TZ and
    1277             :  * OGR_TZFLAG_UTC
    1278             :  *
    1279             :  * This method is the same as the C function OGR_Fld_SetTZFlag().
    1280             :  *
    1281             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1282             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1283             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1284             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1285             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
    1286             :  *
    1287             :  * @param nTZFlag the new time zone flag.
    1288             :  * @since GDAL 3.8
    1289             :  */
    1290        2991 : void OGRFieldDefn::SetTZFlag(int nTZFlag)
    1291             : {
    1292        2991 :     if (m_bSealed)
    1293             :     {
    1294           1 :         CPLError(CE_Failure, CPLE_AppDefined,
    1295             :                  "OGRFieldDefn::SetTZFlag() not allowed on a sealed object");
    1296           1 :         return;
    1297             :     }
    1298        2990 :     m_nTZFlag = nTZFlag;
    1299             : }
    1300             : 
    1301             : /************************************************************************/
    1302             : /*                         OGR_Fld_SetTZFlag()                          */
    1303             : /************************************************************************/
    1304             : /**
    1305             :  * \brief Set the formatting precision for this field in characters.
    1306             :  *
    1307             :  * Set the time zone flag.
    1308             :  *
    1309             :  * Only applies to OFTTime, OFTDate and OFTDateTime fields.
    1310             :  *
    1311             :  * Cf OGR_TZFLAG_UNKNOWN, OGR_TZFLAG_LOCALTIME, OGR_TZFLAG_MIXED_TZ and
    1312             :  * OGR_TZFLAG_UTC
    1313             :  *
    1314             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1315             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1316             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1317             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1318             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
    1319             :  *
    1320             :  * @param hDefn handle to the field definition to set precision to.
    1321             :  * @param nTZFlag the new time zone flag.
    1322             :  * @since GDAL 3.8
    1323             :  */
    1324             : 
    1325           6 : void OGR_Fld_SetTZFlag(OGRFieldDefnH hDefn, int nTZFlag)
    1326             : 
    1327             : {
    1328           6 :     OGRFieldDefn::FromHandle(hDefn)->SetTZFlag(nTZFlag);
    1329           6 : }
    1330             : 
    1331             : /************************************************************************/
    1332             : /*                                Set()                                 */
    1333             : /************************************************************************/
    1334             : 
    1335             : /**
    1336             :  * \brief Set defining parameters for a field in one call.
    1337             :  *
    1338             :  * This method is the same as the C function OGR_Fld_Set().
    1339             :  *
    1340             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1341             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1342             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1343             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1344             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
    1345             :  *
    1346             :  * @param pszNameIn the new name to assign.
    1347             :  * @param eTypeIn the new type (one of the OFT values like OFTInteger).
    1348             :  * @param nWidthIn the preferred formatting width.  Defaults to zero indicating
    1349             :  * undefined.
    1350             :  * @param nPrecisionIn number of decimals places for formatting, defaults to
    1351             :  * zero indicating undefined.
    1352             :  * @param eJustifyIn the formatting justification (OJLeft or OJRight), defaults
    1353             :  * to OJUndefined.
    1354             :  */
    1355             : 
    1356       85769 : void OGRFieldDefn::Set(const char *pszNameIn, OGRFieldType eTypeIn,
    1357             :                        int nWidthIn, int nPrecisionIn,
    1358             :                        OGRJustification eJustifyIn)
    1359             : {
    1360       85769 :     if (m_bSealed)
    1361             :     {
    1362           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    1363             :                  "OGRFieldDefn::Set() not allowed on a sealed object");
    1364           0 :         return;
    1365             :     }
    1366       85769 :     SetName(pszNameIn);
    1367       85769 :     SetType(eTypeIn);
    1368       85769 :     SetWidth(nWidthIn);
    1369       85769 :     SetPrecision(nPrecisionIn);
    1370       85769 :     SetJustify(eJustifyIn);
    1371             : }
    1372             : 
    1373             : /************************************************************************/
    1374             : /*                            OGR_Fld_Set()                             */
    1375             : /************************************************************************/
    1376             : /**
    1377             :  * \brief Set defining parameters for a field in one call.
    1378             :  *
    1379             :  * This function is the same as the CPP method OGRFieldDefn::Set().
    1380             :  *
    1381             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1382             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1383             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1384             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1385             :  * OGRFieldDefn, for drivers that support AlterFieldDefn.
    1386             :  *
    1387             :  * @param hDefn handle to the field definition to set to.
    1388             :  * @param pszNameIn the new name to assign.
    1389             :  * @param eTypeIn the new type (one of the OFT values like OFTInteger).
    1390             :  * @param nWidthIn the preferred formatting width.  Defaults to zero indicating
    1391             :  * undefined.
    1392             :  * @param nPrecisionIn number of decimals places for formatting, defaults to
    1393             :  * zero indicating undefined.
    1394             :  * @param eJustifyIn the formatting justification (OJLeft or OJRight), defaults
    1395             :  * to OJUndefined.
    1396             :  */
    1397             : 
    1398           0 : void OGR_Fld_Set(OGRFieldDefnH hDefn, const char *pszNameIn,
    1399             :                  OGRFieldType eTypeIn, int nWidthIn, int nPrecisionIn,
    1400             :                  OGRJustification eJustifyIn)
    1401             : 
    1402             : {
    1403           0 :     OGRFieldDefn::FromHandle(hDefn)->Set(pszNameIn, eTypeIn, nWidthIn,
    1404             :                                          nPrecisionIn, eJustifyIn);
    1405           0 : }
    1406             : 
    1407             : /************************************************************************/
    1408             : /*                             IsIgnored()                              */
    1409             : /************************************************************************/
    1410             : 
    1411             : /**
    1412             :  * \fn int OGRFieldDefn::IsIgnored() const;
    1413             :  *
    1414             :  * \brief Return whether this field should be omitted when fetching features
    1415             :  *
    1416             :  * This method is the same as the C function OGR_Fld_IsIgnored().
    1417             :  *
    1418             :  * @return ignore state
    1419             :  */
    1420             : 
    1421             : /************************************************************************/
    1422             : /*                         OGR_Fld_IsIgnored()                          */
    1423             : /************************************************************************/
    1424             : 
    1425             : /**
    1426             :  * \brief Return whether this field should be omitted when fetching features
    1427             :  *
    1428             :  * This method is the same as the C++ method OGRFieldDefn::IsIgnored().
    1429             :  *
    1430             :  * @param hDefn handle to the field definition
    1431             :  * @return ignore state
    1432             :  */
    1433             : 
    1434           6 : int OGR_Fld_IsIgnored(OGRFieldDefnH hDefn)
    1435             : {
    1436           6 :     return OGRFieldDefn::FromHandle(hDefn)->IsIgnored();
    1437             : }
    1438             : 
    1439             : /************************************************************************/
    1440             : /*                            SetIgnored()                              */
    1441             : /************************************************************************/
    1442             : 
    1443             : /**
    1444             :  * \fn void OGRFieldDefn::SetIgnored( int ignore );
    1445             :  *
    1446             :  * \brief Set whether this field should be omitted when fetching features
    1447             :  *
    1448             :  * This method is the same as the C function OGR_Fld_SetIgnored().
    1449             :  *
    1450             :  * This method should not be called on a object returned with
    1451             :  * OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead, the
    1452             :  * OGRLayer::SetIgnoredFields() method should be called.
    1453             :  *
    1454             :  * @param ignore ignore state
    1455             :  */
    1456             : 
    1457             : /************************************************************************/
    1458             : /*                        OGR_Fld_SetIgnored()                          */
    1459             : /************************************************************************/
    1460             : 
    1461             : /**
    1462             :  * \brief Set whether this field should be omitted when fetching features
    1463             :  *
    1464             :  * This method is the same as the C++ method OGRFieldDefn::SetIgnored().
    1465             :  *
    1466             :  * This method should not be called on a object returned with
    1467             :  * OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead, the
    1468             :  * OGRLayer::SetIgnoredFields() method should be called.
    1469             :  *
    1470             :  * @param hDefn handle to the field definition
    1471             :  * @param ignore ignore state
    1472             :  */
    1473             : 
    1474           0 : void OGR_Fld_SetIgnored(OGRFieldDefnH hDefn, int ignore)
    1475             : {
    1476           0 :     OGRFieldDefn::FromHandle(hDefn)->SetIgnored(ignore);
    1477           0 : }
    1478             : 
    1479             : /************************************************************************/
    1480             : /*                             IsSame()                                 */
    1481             : /************************************************************************/
    1482             : 
    1483             : /**
    1484             :  * \brief Test if the field definition is identical to the other one.
    1485             :  *
    1486             :  * @param poOtherFieldDefn the other field definition to compare to.
    1487             :  * @return TRUE if the field definition is identical to the other one.
    1488             :  */
    1489             : 
    1490         621 : int OGRFieldDefn::IsSame(const OGRFieldDefn *poOtherFieldDefn) const
    1491             : {
    1492        1241 :     return strcmp(pszName, poOtherFieldDefn->pszName) == 0 &&
    1493         620 :            strcmp(pszAlternativeName, poOtherFieldDefn->pszAlternativeName) ==
    1494         619 :                0 &&
    1495         619 :            eType == poOtherFieldDefn->eType &&
    1496         619 :            eSubType == poOtherFieldDefn->eSubType &&
    1497         619 :            nWidth == poOtherFieldDefn->nWidth &&
    1498         618 :            nPrecision == poOtherFieldDefn->nPrecision &&
    1499        1236 :            bNullable == poOtherFieldDefn->bNullable &&
    1500        1859 :            m_osComment == poOtherFieldDefn->m_osComment &&
    1501        1238 :            m_nTZFlag == poOtherFieldDefn->m_nTZFlag;
    1502             : }
    1503             : 
    1504             : /************************************************************************/
    1505             : /*                             IsNullable()                             */
    1506             : /************************************************************************/
    1507             : 
    1508             : /**
    1509             :  * \fn int OGRFieldDefn::IsNullable() const
    1510             :  *
    1511             :  * \brief Return whether this field can receive null values.
    1512             :  *
    1513             :  * By default, fields are nullable.
    1514             :  *
    1515             :  * Even if this method returns FALSE (i.e not-nullable field), it doesn't mean
    1516             :  * that OGRFeature::IsFieldSet() will necessary return TRUE, as fields can be
    1517             :  * temporary unset and null/not-null validation is usually done when
    1518             :  * OGRLayer::CreateFeature()/SetFeature() is called.
    1519             :  *
    1520             :  * This method is the same as the C function OGR_Fld_IsNullable().
    1521             :  *
    1522             :  * @return TRUE if the field is authorized to be null.
    1523             :  * @since GDAL 2.0
    1524             :  */
    1525             : 
    1526             : /************************************************************************/
    1527             : /*                         OGR_Fld_IsNullable()                         */
    1528             : /************************************************************************/
    1529             : 
    1530             : /**
    1531             :  * \brief Return whether this field can receive null values.
    1532             :  *
    1533             :  * By default, fields are nullable.
    1534             :  *
    1535             :  * Even if this method returns FALSE (i.e not-nullable field), it doesn't mean
    1536             :  * that OGRFeature::IsFieldSet() will necessary return TRUE, as fields can be
    1537             :  * temporary unset and null/not-null validation is usually done when
    1538             :  * OGRLayer::CreateFeature()/SetFeature() is called.
    1539             :  *
    1540             :  * This method is the same as the C++ method OGRFieldDefn::IsNullable().
    1541             :  *
    1542             :  * @param hDefn handle to the field definition
    1543             :  * @return TRUE if the field is authorized to be null.
    1544             :  * @since GDAL 2.0
    1545             :  */
    1546             : 
    1547          80 : int OGR_Fld_IsNullable(OGRFieldDefnH hDefn)
    1548             : {
    1549          80 :     return OGRFieldDefn::FromHandle(hDefn)->IsNullable();
    1550             : }
    1551             : 
    1552             : /************************************************************************/
    1553             : /*                            SetNullable()                             */
    1554             : /************************************************************************/
    1555             : 
    1556             : /**
    1557             :  * \fn void OGRFieldDefn::SetNullable( int bNullableIn );
    1558             :  *
    1559             :  * \brief Set whether this field can receive null values.
    1560             :  *
    1561             :  * By default, fields are nullable, so this method is generally called with
    1562             :  * FALSE to set a not-null constraint.
    1563             :  *
    1564             :  * Drivers that support writing not-null constraint will advertise the
    1565             :  * GDAL_DCAP_NOTNULL_FIELDS driver metadata item.
    1566             :  *
    1567             :  * This method is the same as the C function OGR_Fld_SetNullable().
    1568             :  *
    1569             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1570             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1571             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1572             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1573             :  * OGRFieldDefn, for drivers that support AlterFieldDefn().
    1574             :  *
    1575             :  * @param bNullableIn FALSE if the field must have a not-null constraint.
    1576             :  * @since GDAL 2.0
    1577             :  */
    1578      182730 : void OGRFieldDefn::SetNullable(int bNullableIn)
    1579             : {
    1580      182730 :     if (m_bSealed)
    1581             :     {
    1582           1 :         CPLError(CE_Failure, CPLE_AppDefined,
    1583             :                  "OGRFieldDefn::SetNullable() not allowed on a sealed object");
    1584           1 :         return;
    1585             :     }
    1586      182729 :     bNullable = bNullableIn;
    1587             : }
    1588             : 
    1589             : /************************************************************************/
    1590             : /*                        OGR_Fld_SetNullable()                          */
    1591             : /************************************************************************/
    1592             : 
    1593             : /**
    1594             :  * \brief Set whether this field can receive null values.
    1595             :  *
    1596             :  * By default, fields are nullable, so this method is generally called with
    1597             :  * FALSE to set a not-null constraint.
    1598             :  *
    1599             :  * Drivers that support writing not-null constraint will advertise the
    1600             :  * GDAL_DCAP_NOTNULL_FIELDS driver metadata item.
    1601             :  *
    1602             :  * This method is the same as the C++ method OGRFieldDefn::SetNullable().
    1603             :  *
    1604             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1605             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1606             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1607             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1608             :  * OGRFieldDefn, for drivers that support AlterFieldDefn().
    1609             :  *
    1610             :  * @param hDefn handle to the field definition
    1611             :  * @param bNullableIn FALSE if the field must have a not-null constraint.
    1612             :  * @since GDAL 2.0
    1613             :  */
    1614             : 
    1615         190 : void OGR_Fld_SetNullable(OGRFieldDefnH hDefn, int bNullableIn)
    1616             : {
    1617         190 :     OGRFieldDefn::FromHandle(hDefn)->SetNullable(bNullableIn);
    1618         190 : }
    1619             : 
    1620             : /************************************************************************/
    1621             : /*                             IsUnique()                               */
    1622             : /************************************************************************/
    1623             : 
    1624             : /**
    1625             :  * \fn int OGRFieldDefn::IsUnique() const
    1626             :  *
    1627             :  * \brief Return whether this field has a unique constraint.
    1628             :  *
    1629             :  * By default, fields have no unique constraint.
    1630             :  *
    1631             :  * This method is the same as the C function OGR_Fld_IsUnique().
    1632             :  *
    1633             :  * @return TRUE if the field has a unique constraint.
    1634             :  * @since GDAL 3.2
    1635             :  */
    1636             : 
    1637             : /************************************************************************/
    1638             : /*                         OGR_Fld_IsUnique()                         */
    1639             : /************************************************************************/
    1640             : 
    1641             : /**
    1642             :  * \brief Return whether this field has a unique constraint.
    1643             :  *
    1644             :  * By default, fields have no unique constraint.
    1645             :  *
    1646             :  * This method is the same as the C++ method OGRFieldDefn::IsUnique().
    1647             :  *
    1648             :  * @param hDefn handle to the field definition
    1649             :  * @return TRUE if the field has a unique constraint.
    1650             :  * @since GDAL 3.2
    1651             :  */
    1652             : 
    1653          80 : int OGR_Fld_IsUnique(OGRFieldDefnH hDefn)
    1654             : {
    1655          80 :     return OGRFieldDefn::FromHandle(hDefn)->IsUnique();
    1656             : }
    1657             : 
    1658             : /*********************************************************  ***************/
    1659             : /*                            SetUnique()                             */
    1660             : /************************************************************************/
    1661             : 
    1662             : /**
    1663             :  * \brief Set whether this field has a unique constraint.
    1664             :  *
    1665             :  * By default, fields have no unique constraint, so this method is generally
    1666             :  * called with TRUE to set a unique constraint.
    1667             :  *
    1668             :  * Drivers that support writing unique constraint will advertise the
    1669             :  * GDAL_DCAP_UNIQUE_FIELDS driver metadata item.
    1670             :  *
    1671             :  * This method is the same as the C function OGR_Fld_SetUnique().
    1672             :  *
    1673             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1674             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1675             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1676             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1677             :  * OGRFieldDefn, for drivers that support AlterFieldDefn().
    1678             :  *
    1679             :  * @param bUniqueIn TRUE if the field must have a unique constraint.
    1680             :  * @since GDAL 3.2
    1681             :  */
    1682       68084 : void OGRFieldDefn::SetUnique(int bUniqueIn)
    1683             : {
    1684       68084 :     if (m_bSealed)
    1685             :     {
    1686           1 :         CPLError(CE_Failure, CPLE_AppDefined,
    1687             :                  "OGRFieldDefn::SetUnique() not allowed on a sealed object");
    1688           1 :         return;
    1689             :     }
    1690       68083 :     bUnique = bUniqueIn;
    1691             : }
    1692             : 
    1693             : /************************************************************************/
    1694             : /*                        OGR_Fld_SetUnique()                            */
    1695             : /************************************************************************/
    1696             : 
    1697             : /**
    1698             :  * \brief Set whether this field has a unique constraint.
    1699             :  *
    1700             :  * By default, fields have no unique constraint, so this method is generally
    1701             :  *called with TRUE to set a unique constraint.
    1702             :  *
    1703             :  * Drivers that support writing unique constraint will advertise the
    1704             :  * GDAL_DCAP_UNIQUE_FIELDS driver metadata item.
    1705             :  *field can receive null values.
    1706             :  *
    1707             :  * This method is the same as the C++ method OGRFieldDefn::SetUnique().
    1708             :  *
    1709             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1710             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1711             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1712             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1713             :  * OGRFieldDefn, for drivers that support AlterFieldDefn().
    1714             :  *
    1715             :  * @param hDefn handle to the field definition
    1716             :  * @param bUniqueIn TRUE if the field must have a unique constraint.
    1717             :  * @since GDAL 3.2
    1718             :  */
    1719             : 
    1720          28 : void OGR_Fld_SetUnique(OGRFieldDefnH hDefn, int bUniqueIn)
    1721             : {
    1722          28 :     OGRFieldDefn::FromHandle(hDefn)->SetUnique(bUniqueIn);
    1723          28 : }
    1724             : 
    1725             : /************************************************************************/
    1726             : /*                           GetDomainName()                            */
    1727             : /************************************************************************/
    1728             : 
    1729             : /**
    1730             :  * \fn const std::string& OGRFieldDefn::GetDomainName() const
    1731             :  *
    1732             :  * \brief Return the name of the field domain for this field.
    1733             :  *
    1734             :  * By default, none (empty string) is returned.
    1735             :  *
    1736             :  * Field domains (OGRFieldDomain class) are attached at the GDALDataset level
    1737             :  * and should be retrieved with GDALDataset::GetFieldDomain().
    1738             :  *
    1739             :  * This method is the same as the C function OGR_Fld_GetDomainName().
    1740             :  *
    1741             :  * @return the field domain name, or an empty string if there is none.
    1742             :  * @since GDAL 3.3
    1743             :  */
    1744             : 
    1745             : /************************************************************************/
    1746             : /*                      OGR_Fld_GetDomainName()                         */
    1747             : /************************************************************************/
    1748             : 
    1749             : /**
    1750             :  * \brief Return the name of the field domain for this field.
    1751             :  *
    1752             :  * By default, none (empty string) is returned.
    1753             :  *
    1754             :  * Field domains (OGRFieldDomain class) are attached at the GDALDataset level
    1755             :  * and should be retrieved with GDALDatasetGetFieldDomain().
    1756             :  *
    1757             :  * This method is the same as the C++ method OGRFieldDefn::GetDomainName().
    1758             :  *
    1759             :  * @param hDefn handle to the field definition
    1760             :  * @return the field domain name, or an empty string if there is none.
    1761             :  * @since GDAL 3.3
    1762             :  */
    1763             : 
    1764          34 : const char *OGR_Fld_GetDomainName(OGRFieldDefnH hDefn)
    1765             : {
    1766          34 :     return OGRFieldDefn::FromHandle(hDefn)->GetDomainName().c_str();
    1767             : }
    1768             : 
    1769             : /************************************************************************/
    1770             : /*                           SetDomainName()                            */
    1771             : /************************************************************************/
    1772             : 
    1773             : /**
    1774             :  * \brief Set the name of the field domain for this field.
    1775             :  *
    1776             :  * Field domains (OGRFieldDomain) are attached at the GDALDataset level.
    1777             :  *
    1778             :  * This method is the same as the C function OGR_Fld_SetDomainName().
    1779             :  *
    1780             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1781             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1782             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1783             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1784             :  * OGRFieldDefn, for drivers that support AlterFieldDefn().
    1785             :  *
    1786             :  * @param osDomainName Field domain name.
    1787             :  * @since GDAL 3.3
    1788             :  */
    1789       13625 : void OGRFieldDefn::SetDomainName(const std::string &osDomainName)
    1790             : {
    1791       13625 :     if (m_bSealed)
    1792             :     {
    1793           1 :         CPLError(
    1794             :             CE_Failure, CPLE_AppDefined,
    1795             :             "OGRFieldDefn::SetDomainName() not allowed on a sealed object");
    1796           1 :         return;
    1797             :     }
    1798       13624 :     m_osDomainName = osDomainName;
    1799             : }
    1800             : 
    1801             : /************************************************************************/
    1802             : /*                      OGR_Fld_SetDomainName()                         */
    1803             : /************************************************************************/
    1804             : 
    1805             : /**
    1806             :  * \brief Set the name of the field domain for this field.
    1807             :  *
    1808             :  * Field domains (OGRFieldDomain) are attached at the GDALDataset level.
    1809             :  *
    1810             :  * This method is the same as the C++ method OGRFieldDefn::SetDomainName().
    1811             :  *
    1812             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1813             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1814             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1815             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1816             :  * OGRFieldDefn, for drivers that support AlterFieldDefn().
    1817             :  *
    1818             :  * @param hDefn handle to the field definition
    1819             :  * @param pszFieldName Field domain name.
    1820             :  * @since GDAL 3.3
    1821             :  */
    1822             : 
    1823          21 : void OGR_Fld_SetDomainName(OGRFieldDefnH hDefn, const char *pszFieldName)
    1824             : {
    1825          21 :     OGRFieldDefn::FromHandle(hDefn)->SetDomainName(pszFieldName ? pszFieldName
    1826             :                                                                 : "");
    1827          21 : }
    1828             : 
    1829             : /************************************************************************/
    1830             : /*                           GetComment()                               */
    1831             : /************************************************************************/
    1832             : 
    1833             : /**
    1834             :  * \fn const std::string& OGRFieldDefn::GetComment() const
    1835             :  *
    1836             :  * \brief Return the (optional) comment for this field.
    1837             :  *
    1838             :  * By default, none (empty string) is returned.
    1839             :  *
    1840             :  * This method is the same as the C function OGR_Fld_GetComment().
    1841             :  *
    1842             :  * @return the field comment, or an empty string if there is none.
    1843             :  * @since GDAL 3.7
    1844             :  */
    1845             : 
    1846             : /************************************************************************/
    1847             : /*                      OGR_Fld_GetComment()                            */
    1848             : /************************************************************************/
    1849             : 
    1850             : /**
    1851             :  * \brief Return the (optional) comment for this field.
    1852             :  *
    1853             :  * By default, none (empty string) is returned.
    1854             :  *
    1855             :  * This method is the same as the C++ method OGRFieldDefn::GetComment().
    1856             :  *
    1857             :  * @param hDefn handle to the field definition
    1858             :  * @return the comment, or an empty string if there is none.
    1859             :  * @since GDAL 3.7
    1860             :  */
    1861             : 
    1862          58 : const char *OGR_Fld_GetComment(OGRFieldDefnH hDefn)
    1863             : {
    1864          58 :     return OGRFieldDefn::FromHandle(hDefn)->GetComment().c_str();
    1865             : }
    1866             : 
    1867             : /************************************************************************/
    1868             : /*                           SetComment()                               */
    1869             : /************************************************************************/
    1870             : 
    1871             : /**
    1872             :  * \brief Set the comment for this field.
    1873             :  *
    1874             :  * This method is the same as the C function OGR_Fld_SetComment().
    1875             :  *
    1876             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1877             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1878             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1879             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1880             :  * OGRFieldDefn, for drivers that support AlterFieldDefn().
    1881             :  *
    1882             :  * @param osComment Field comment.
    1883             :  * @since GDAL 3.7
    1884             :  */
    1885        2875 : void OGRFieldDefn::SetComment(const std::string &osComment)
    1886             : {
    1887        2875 :     if (m_bSealed)
    1888             :     {
    1889           1 :         CPLError(CE_Failure, CPLE_AppDefined,
    1890             :                  "OGRFieldDefn::SetComment() not allowed on a sealed object");
    1891           1 :         return;
    1892             :     }
    1893        2874 :     m_osComment = osComment;
    1894             : }
    1895             : 
    1896             : /************************************************************************/
    1897             : /*                      OGR_Fld_SetComment()                            */
    1898             : /************************************************************************/
    1899             : 
    1900             : /**
    1901             :  * \brief Set the comment for this field.
    1902             :  *
    1903             :  * This method is the same as the C++ method OGRFieldDefn::SetComment().
    1904             :  *
    1905             :  * Note that once a OGRFieldDefn has been added to a layer definition with
    1906             :  * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
    1907             :  * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
    1908             :  * OGRLayer::AlterFieldDefn() should be called on a new instance of
    1909             :  * OGRFieldDefn, for drivers that support AlterFieldDefn().
    1910             :  *
    1911             :  * @param hDefn handle to the field definition
    1912             :  * @param pszComment Field comment.
    1913             :  * @since GDAL 3.7
    1914             :  */
    1915             : 
    1916          32 : void OGR_Fld_SetComment(OGRFieldDefnH hDefn, const char *pszComment)
    1917             : {
    1918          32 :     OGRFieldDefn::FromHandle(hDefn)->SetComment(pszComment ? pszComment : "");
    1919          32 : }
    1920             : 
    1921             : /************************************************************************/
    1922             : /*                        OGRUpdateFieldType()                          */
    1923             : /************************************************************************/
    1924             : 
    1925             : /**
    1926             :  * \brief Update the type of a field definition by "merging" its existing type
    1927             :  * with a new type.
    1928             :  *
    1929             :  * The update is done such as broadening the type. For example a OFTInteger
    1930             :  * updated with OFTInteger64 will be promoted to OFTInteger64.
    1931             :  *
    1932             :  * @param poFDefn the field definition whose type must be updated.
    1933             :  * @param eNewType the new field type to merge into the existing type.
    1934             :  * @param eNewSubType the new field subtype to merge into the existing subtype.
    1935             :  * @since GDAL 2.1
    1936             :  */
    1937             : 
    1938          32 : void OGRUpdateFieldType(OGRFieldDefn *poFDefn, OGRFieldType eNewType,
    1939             :                         OGRFieldSubType eNewSubType)
    1940             : {
    1941          32 :     OGRFieldType eType = poFDefn->GetType();
    1942          32 :     if (eType == OFTInteger)
    1943             :     {
    1944           2 :         if (eNewType == OFTInteger && poFDefn->GetSubType() == OFSTBoolean &&
    1945             :             eNewSubType != OFSTBoolean)
    1946             :         {
    1947           0 :             poFDefn->SetSubType(OFSTNone);
    1948             :         }
    1949           2 :         else if (eNewType == OFTInteger64 || eNewType == OFTReal)
    1950             :         {
    1951           0 :             poFDefn->SetSubType(OFSTNone);
    1952           0 :             poFDefn->SetType(eNewType);
    1953             :         }
    1954           2 :         else if (eNewType == OFTIntegerList || eNewType == OFTInteger64List ||
    1955           2 :                  eNewType == OFTRealList || eNewType == OFTStringList)
    1956             :         {
    1957           0 :             if (eNewType != OFTIntegerList || eNewSubType != OFSTBoolean)
    1958           0 :                 poFDefn->SetSubType(OFSTNone);
    1959           0 :             poFDefn->SetType(eNewType);
    1960             :         }
    1961           2 :         else if (eNewType != OFTInteger)
    1962             :         {
    1963           0 :             poFDefn->SetSubType(OFSTNone);
    1964           0 :             poFDefn->SetType(OFTString);
    1965             :         }
    1966             :     }
    1967          30 :     else if (eType == OFTInteger64)
    1968             :     {
    1969           1 :         if (eNewType == OFTReal)
    1970             :         {
    1971           0 :             poFDefn->SetSubType(OFSTNone);
    1972           0 :             poFDefn->SetType(eNewType);
    1973             :         }
    1974           1 :         else if (eNewType == OFTIntegerList)
    1975             :         {
    1976           0 :             poFDefn->SetSubType(OFSTNone);
    1977           0 :             poFDefn->SetType(OFTInteger64List);
    1978             :         }
    1979           1 :         else if (eNewType == OFTInteger64List || eNewType == OFTRealList ||
    1980             :                  eNewType == OFTStringList)
    1981             :         {
    1982           0 :             poFDefn->SetSubType(OFSTNone);
    1983           0 :             poFDefn->SetType(eNewType);
    1984             :         }
    1985           1 :         else if (eNewType != OFTInteger && eNewType != OFTInteger64)
    1986             :         {
    1987           0 :             poFDefn->SetSubType(OFSTNone);
    1988           0 :             poFDefn->SetType(OFTString);
    1989             :         }
    1990             :     }
    1991          29 :     else if (eType == OFTReal)
    1992             :     {
    1993           2 :         if (eNewType == OFTIntegerList || eNewType == OFTInteger64List ||
    1994             :             eNewType == OFTRealList)
    1995             :         {
    1996           0 :             poFDefn->SetType(OFTRealList);
    1997             :         }
    1998           2 :         else if (eNewType == OFTStringList)
    1999             :         {
    2000           0 :             poFDefn->SetType(OFTStringList);
    2001             :         }
    2002           2 :         else if (eNewType != OFTInteger && eNewType != OFTInteger64 &&
    2003             :                  eNewType != OFTReal)
    2004             :         {
    2005           0 :             poFDefn->SetSubType(OFSTNone);
    2006           0 :             poFDefn->SetType(OFTString);
    2007             :         }
    2008             :     }
    2009          27 :     else if (eType == OFTIntegerList)
    2010             :     {
    2011           1 :         if (eNewType == OFTIntegerList &&
    2012           2 :             poFDefn->GetSubType() == OFSTBoolean && eNewSubType != OFSTBoolean)
    2013             :         {
    2014           0 :             poFDefn->SetSubType(OFSTNone);
    2015             :         }
    2016           1 :         else if (eNewType == OFTInteger64 || eNewType == OFTInteger64List)
    2017             :         {
    2018           0 :             poFDefn->SetSubType(OFSTNone);
    2019           0 :             poFDefn->SetType(OFTInteger64List);
    2020             :         }
    2021           1 :         else if (eNewType == OFTReal || eNewType == OFTRealList)
    2022             :         {
    2023           0 :             poFDefn->SetSubType(OFSTNone);
    2024           0 :             poFDefn->SetType(OFTRealList);
    2025             :         }
    2026           1 :         else if (eNewType != OFTInteger && eNewType != OFTIntegerList)
    2027             :         {
    2028           0 :             poFDefn->SetSubType(OFSTNone);
    2029           0 :             poFDefn->SetType(OFTStringList);
    2030             :         }
    2031             :     }
    2032          26 :     else if (eType == OFTInteger64List)
    2033             :     {
    2034           1 :         if (eNewType == OFTReal || eNewType == OFTRealList)
    2035           0 :             poFDefn->SetType(OFTRealList);
    2036           1 :         else if (eNewType != OFTInteger && eNewType != OFTInteger64 &&
    2037           1 :                  eNewType != OFTIntegerList && eNewType != OFTInteger64List)
    2038             :         {
    2039           0 :             poFDefn->SetSubType(OFSTNone);
    2040           0 :             poFDefn->SetType(OFTStringList);
    2041             :         }
    2042             :     }
    2043          25 :     else if (eType == OFTRealList)
    2044             :     {
    2045           1 :         if (eNewType != OFTInteger && eNewType != OFTInteger64 &&
    2046           1 :             eNewType != OFTReal && eNewType != OFTIntegerList &&
    2047           1 :             eNewType != OFTInteger64List && eNewType != OFTRealList)
    2048             :         {
    2049           0 :             poFDefn->SetSubType(OFSTNone);
    2050           0 :             poFDefn->SetType(OFTStringList);
    2051             :         }
    2052             :     }
    2053          24 :     else if (eType == OFTDateTime)
    2054             :     {
    2055           1 :         if (eNewType != OFTDateTime && eNewType != OFTDate)
    2056             :         {
    2057           0 :             poFDefn->SetType(OFTString);
    2058             :         }
    2059             :     }
    2060          23 :     else if (eType == OFTDate || eType == OFTTime)
    2061             :     {
    2062           2 :         if (eNewType == OFTDateTime)
    2063           0 :             poFDefn->SetType(OFTDateTime);
    2064           2 :         else if (eNewType != eType)
    2065           0 :             poFDefn->SetType(OFTString);
    2066             :     }
    2067          21 :     else if (eType == OFTString && eNewType == OFTStringList)
    2068             :     {
    2069           0 :         poFDefn->SetType(OFTStringList);
    2070             :     }
    2071          32 : }
    2072             : 
    2073             : /************************************************************************/
    2074             : /*                        OGRFieldDefn::Seal()                          */
    2075             : /************************************************************************/
    2076             : 
    2077             : /** Seal a OGRFieldDefn.
    2078             :  *
    2079             :  * A sealed OGRFieldDefn can not be modified while it is sealed.
    2080             :  *
    2081             :  * This method should only be called by driver implementations.
    2082             :  *
    2083             :  * @since GDAL 3.9
    2084             :  */
    2085     4708220 : void OGRFieldDefn::Seal()
    2086             : {
    2087     4708220 :     m_bSealed = true;
    2088     4708220 : }
    2089             : 
    2090             : /************************************************************************/
    2091             : /*                        OGRFieldDefn::Unseal()                        */
    2092             : /************************************************************************/
    2093             : 
    2094             : /** Unseal a OGRFieldDefn.
    2095             :  *
    2096             :  * Undo OGRFieldDefn::Seal()
    2097             :  *
    2098             :  * Using GetTemporaryUnsealer() is recommended for most use cases.
    2099             :  *
    2100             :  * This method should only be called by driver implementations.
    2101             :  *
    2102             :  * @since GDAL 3.9
    2103             :  */
    2104     4671680 : void OGRFieldDefn::Unseal()
    2105             : {
    2106     4671680 :     m_bSealed = false;
    2107     4671680 : }
    2108             : 
    2109             : /************************************************************************/
    2110             : /*                    OGRFieldDefn::GetTemporaryUnsealer()              */
    2111             : /************************************************************************/
    2112             : 
    2113             : /** Return an object that temporary unseals the OGRFieldDefn
    2114             :  *
    2115             :  * The returned object calls Unseal() initially, and when it is destroyed
    2116             :  * it calls Seal().
    2117             :  *
    2118             :  * This method should only be called by driver implementations.
    2119             :  *
    2120             :  * It is also possible to use the helper method whileUnsealing(). Example:
    2121             :  * whileUnsealing(poFieldDefn)->some_method()
    2122             :  *
    2123             :  * @since GDAL 3.9
    2124             :  */
    2125         154 : OGRFieldDefn::TemporaryUnsealer OGRFieldDefn::GetTemporaryUnsealer()
    2126             : {
    2127         154 :     return TemporaryUnsealer(this);
    2128             : }
    2129             : 
    2130             : /************************************************************************/
    2131             : /*                          OGRFieldDomain()                            */
    2132             : /************************************************************************/
    2133             : 
    2134             : /*! @cond Doxygen_Suppress */
    2135        1290 : OGRFieldDomain::OGRFieldDomain(const std::string &osName,
    2136             :                                const std::string &osDescription,
    2137             :                                OGRFieldDomainType eDomainType,
    2138             :                                OGRFieldType eFieldType,
    2139        1290 :                                OGRFieldSubType eFieldSubType)
    2140             :     : m_osName(osName), m_osDescription(osDescription),
    2141             :       m_eDomainType(eDomainType), m_eFieldType(eFieldType),
    2142        1290 :       m_eFieldSubType(eFieldSubType)
    2143             : {
    2144        1290 : }
    2145             : 
    2146             : /*! @endcond */
    2147             : 
    2148             : /************************************************************************/
    2149             : /*                         ~OGRFieldDomain()                            */
    2150             : /************************************************************************/
    2151             : 
    2152             : OGRFieldDomain::~OGRFieldDomain() = default;
    2153             : 
    2154             : /************************************************************************/
    2155             : /*                         ~OGRFieldDomain()                            */
    2156             : /************************************************************************/
    2157             : 
    2158             : /** Destroy a field domain.
    2159             :  *
    2160             :  * This is the same as the C++ method OGRFieldDomain::~OGRFieldDomain()
    2161             :  *
    2162             :  * @param hFieldDomain the field domain.
    2163             :  * @since GDAL 3.3
    2164             :  */
    2165          44 : void OGR_FldDomain_Destroy(OGRFieldDomainH hFieldDomain)
    2166             : {
    2167          44 :     delete OGRFieldDomain::FromHandle(hFieldDomain);
    2168          44 : }
    2169             : 
    2170             : /************************************************************************/
    2171             : /*                        OGRCodedFieldDomain()                         */
    2172             : /************************************************************************/
    2173             : 
    2174        1194 : OGRCodedFieldDomain::OGRCodedFieldDomain(const std::string &osName,
    2175             :                                          const std::string &osDescription,
    2176             :                                          OGRFieldType eFieldType,
    2177             :                                          OGRFieldSubType eFieldSubType,
    2178        1194 :                                          std::vector<OGRCodedValue> &&asValues)
    2179             :     : OGRFieldDomain(osName, osDescription, OFDT_CODED, eFieldType,
    2180             :                      eFieldSubType),
    2181        1194 :       m_asValues(std::move(asValues))
    2182             : {
    2183             :     // Guard
    2184        1194 :     if (m_asValues.empty() || m_asValues.back().pszCode != nullptr)
    2185             :     {
    2186             :         OGRCodedValue cv;
    2187        1133 :         cv.pszCode = nullptr;
    2188        1133 :         cv.pszValue = nullptr;
    2189        1133 :         m_asValues.emplace_back(cv);
    2190             :     }
    2191        1194 : }
    2192             : 
    2193             : /************************************************************************/
    2194             : /*                     OGR_CodedFldDomain_Create()                      */
    2195             : /************************************************************************/
    2196             : 
    2197             : /** Creates a new coded field domain.
    2198             :  *
    2199             :  * This is the same as the C++ method OGRCodedFieldDomain::OGRCodedFieldDomain()
    2200             :  * (except that the C function copies the enumeration, whereas the C++
    2201             :  * method moves it)
    2202             :  *
    2203             :  * @param pszName        Domain name. Should not be NULL.
    2204             :  * @param pszDescription Domain description (can be NULL)
    2205             :  * @param eFieldType     Field type. Generally numeric. Potentially OFTDateTime
    2206             :  * @param eFieldSubType  Field subtype.
    2207             :  * @param enumeration    Enumeration as (code, value) pairs. Should not be
    2208             :  *                       NULL. The end of the enumeration is marked by a code
    2209             :  *                       set to NULL. The enumeration will be copied.
    2210             :  *                       Each code should appear only once, but it is the
    2211             :  *                       responsibility of the user to check it.
    2212             :  * @return a new handle that should be freed with OGR_FldDomain_Destroy(),
    2213             :  *         or NULL in case of error.
    2214             :  * @since GDAL 3.3
    2215             :  */
    2216          60 : OGRFieldDomainH OGR_CodedFldDomain_Create(const char *pszName,
    2217             :                                           const char *pszDescription,
    2218             :                                           OGRFieldType eFieldType,
    2219             :                                           OGRFieldSubType eFieldSubType,
    2220             :                                           const OGRCodedValue *enumeration)
    2221             : {
    2222          60 :     VALIDATE_POINTER1(pszName, __func__, nullptr);
    2223          60 :     VALIDATE_POINTER1(enumeration, __func__, nullptr);
    2224          60 :     size_t count = 0;
    2225         203 :     for (int i = 0; enumeration[i].pszCode != nullptr; ++i)
    2226             :     {
    2227         143 :         ++count;
    2228             :     }
    2229         120 :     std::vector<OGRCodedValue> asValues;
    2230             :     try
    2231             :     {
    2232          60 :         asValues.reserve(count + 1);
    2233             :     }
    2234           0 :     catch (const std::exception &e)
    2235             :     {
    2236           0 :         CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what());
    2237           0 :         return nullptr;
    2238             :     }
    2239          60 :     bool error = false;
    2240         203 :     for (int i = 0; enumeration[i].pszCode != nullptr; ++i)
    2241             :     {
    2242             :         OGRCodedValue cv;
    2243         143 :         cv.pszCode = VSI_STRDUP_VERBOSE(enumeration[i].pszCode);
    2244         143 :         if (cv.pszCode == nullptr)
    2245             :         {
    2246           0 :             error = true;
    2247           0 :             break;
    2248             :         }
    2249         143 :         if (enumeration[i].pszValue)
    2250             :         {
    2251         120 :             cv.pszValue = VSI_STRDUP_VERBOSE(enumeration[i].pszValue);
    2252         120 :             if (cv.pszValue == nullptr)
    2253             :             {
    2254           0 :                 VSIFree(cv.pszCode);
    2255           0 :                 error = true;
    2256           0 :                 break;
    2257             :             }
    2258             :         }
    2259             :         else
    2260             :         {
    2261          23 :             cv.pszValue = nullptr;
    2262             :         }
    2263         143 :         asValues.emplace_back(cv);
    2264             :     }
    2265          60 :     if (error)
    2266             :     {
    2267           0 :         for (auto &cv : asValues)
    2268             :         {
    2269           0 :             VSIFree(cv.pszCode);
    2270           0 :             VSIFree(cv.pszValue);
    2271             :         }
    2272           0 :         return nullptr;
    2273             :     }
    2274             :     // Add guard
    2275             :     {
    2276             :         OGRCodedValue cv;
    2277          60 :         cv.pszCode = nullptr;
    2278          60 :         cv.pszValue = nullptr;
    2279          60 :         asValues.emplace_back(cv);
    2280             :     }
    2281         180 :     return OGRFieldDomain::ToHandle(new OGRCodedFieldDomain(
    2282             :         pszName, pszDescription ? pszDescription : "", eFieldType,
    2283         120 :         eFieldSubType, std::move(asValues)));
    2284             : }
    2285             : 
    2286             : /************************************************************************/
    2287             : /*                   OGRCodedFieldDomain::Clone()                       */
    2288             : /************************************************************************/
    2289             : 
    2290          40 : OGRCodedFieldDomain *OGRCodedFieldDomain::Clone() const
    2291             : {
    2292          80 :     auto poDomain = cpl::down_cast<OGRCodedFieldDomain *>(
    2293             :         OGRFieldDomain::FromHandle(OGR_CodedFldDomain_Create(
    2294          40 :             m_osName.c_str(), m_osDescription.c_str(), m_eFieldType,
    2295          40 :             m_eFieldSubType, m_asValues.data())));
    2296          40 :     poDomain->SetMergePolicy(m_eMergePolicy);
    2297          40 :     poDomain->SetSplitPolicy(m_eSplitPolicy);
    2298          40 :     return poDomain;
    2299             : }
    2300             : 
    2301             : /************************************************************************/
    2302             : /*                      ~OGRCodedFieldDomain()                          */
    2303             : /************************************************************************/
    2304             : 
    2305        2387 : OGRCodedFieldDomain::~OGRCodedFieldDomain()
    2306             : {
    2307       19700 :     for (auto &cv : m_asValues)
    2308             :     {
    2309       18506 :         CPLFree(cv.pszCode);
    2310       18506 :         CPLFree(cv.pszValue);
    2311             :     }
    2312        2387 : }
    2313             : 
    2314             : /************************************************************************/
    2315             : /*                       OGRRangeFieldDomain()                          */
    2316             : /************************************************************************/
    2317             : 
    2318             : // cppcheck-suppress uninitMemberVar
    2319          62 : OGRRangeFieldDomain::OGRRangeFieldDomain(
    2320             :     const std::string &osName, const std::string &osDescription,
    2321             :     OGRFieldType eFieldType, OGRFieldSubType eFieldSubType,
    2322             :     const OGRField &sMin, bool bMinIsInclusive, const OGRField &sMax,
    2323          62 :     bool bMaxIsInclusive)
    2324             :     : OGRFieldDomain(osName, osDescription, OFDT_RANGE, eFieldType,
    2325             :                      eFieldSubType),
    2326             :       m_sMin(sMin), m_sMax(sMax), m_bMinIsInclusive(bMinIsInclusive),
    2327          62 :       m_bMaxIsInclusive(bMaxIsInclusive)
    2328             : {
    2329          62 :     CPLAssert(eFieldType == OFTInteger || eFieldType == OFTInteger64 ||
    2330             :               eFieldType == OFTReal || eFieldType == OFTDateTime);
    2331          62 : }
    2332             : 
    2333             : /************************************************************************/
    2334             : /*                     OGR_RangeFldDomain_Create()                      */
    2335             : /************************************************************************/
    2336             : 
    2337          12 : static OGRField GetUnsetField()
    2338             : {
    2339             :     OGRField sUnset;
    2340          12 :     OGR_RawField_SetUnset(&sUnset);
    2341          12 :     return sUnset;
    2342             : }
    2343             : 
    2344             : /** Creates a new range field domain.
    2345             :  *
    2346             :  * This is the same as the C++ method
    2347             :  * OGRRangeFieldDomain::OGRRangeFieldDomain().
    2348             :  *
    2349             :  * @param pszName        Domain name. Should not be NULL.
    2350             :  * @param pszDescription Domain description (can be NULL)
    2351             :  * @param eFieldType     Field type. Among OFTInteger, OFTInteger64, OFTReal
    2352             :  *                       and OFTDateTime.
    2353             :  * @param eFieldSubType  Field subtype.
    2354             :  * @param psMin          Minimum value (can be NULL). The member in the union
    2355             :  *                       that is read is consistent with eFieldType
    2356             :  * @param bMinIsInclusive Whether the minimum value is included in the range.
    2357             :  * @param psMax          Maximum value (can be NULL). The member in the union
    2358             :  *                       that is read is consistent with eFieldType
    2359             :  * @param bMaxIsInclusive Whether the maximum value is included in the range.
    2360             :  * @return a new handle that should be freed with OGR_FldDomain_Destroy()
    2361             :  * @since GDAL 3.3
    2362             :  */
    2363          12 : OGRFieldDomainH OGR_RangeFldDomain_Create(
    2364             :     const char *pszName, const char *pszDescription, OGRFieldType eFieldType,
    2365             :     OGRFieldSubType eFieldSubType, const OGRField *psMin, bool bMinIsInclusive,
    2366             :     const OGRField *psMax, bool bMaxIsInclusive)
    2367             : {
    2368          12 :     VALIDATE_POINTER1(pszName, __func__, nullptr);
    2369          12 :     if (eFieldType != OFTInteger && eFieldType != OFTInteger64 &&
    2370           2 :         eFieldType != OFTReal && eFieldType != OFTDateTime)
    2371             :     {
    2372           0 :         CPLError(CE_Failure, CPLE_NotSupported, "Unsupported field type");
    2373           0 :         return nullptr;
    2374             :     }
    2375          12 :     const OGRField unsetField = GetUnsetField();
    2376          36 :     return OGRFieldDomain::ToHandle(new OGRRangeFieldDomain(
    2377             :         pszName, pszDescription ? pszDescription : "", eFieldType,
    2378             :         eFieldSubType, psMin ? *psMin : unsetField, bMinIsInclusive,
    2379          24 :         psMax ? *psMax : unsetField, bMaxIsInclusive));
    2380             : }
    2381             : 
    2382             : /************************************************************************/
    2383             : /*                        OGRGlobFieldDomain()                          */
    2384             : /************************************************************************/
    2385             : 
    2386          34 : OGRGlobFieldDomain::OGRGlobFieldDomain(const std::string &osName,
    2387             :                                        const std::string &osDescription,
    2388             :                                        OGRFieldType eFieldType,
    2389             :                                        OGRFieldSubType eFieldSubType,
    2390          34 :                                        const std::string &osGlob)
    2391             :     : OGRFieldDomain(osName, osDescription, OFDT_GLOB, eFieldType,
    2392             :                      eFieldSubType),
    2393          34 :       m_osGlob(osGlob)
    2394             : {
    2395          34 : }
    2396             : 
    2397             : /************************************************************************/
    2398             : /*                       OGR_GlobFldDomain_Create()                     */
    2399             : /************************************************************************/
    2400             : 
    2401             : /** Creates a new glob field domain.
    2402             :  *
    2403             :  * This is the same as the C++ method OGRGlobFieldDomain::OGRGlobFieldDomain()
    2404             :  *
    2405             :  * @param pszName        Domain name. Should not be NULL.
    2406             :  * @param pszDescription Domain description (can be NULL)
    2407             :  * @param eFieldType     Field type.
    2408             :  * @param eFieldSubType  Field subtype.
    2409             :  * @param pszGlob        Glob expression. Should not be NULL.
    2410             :  * @return a new handle that should be freed with OGR_FldDomain_Destroy()
    2411             :  * @since GDAL 3.3
    2412             :  */
    2413          14 : OGRFieldDomainH OGR_GlobFldDomain_Create(const char *pszName,
    2414             :                                          const char *pszDescription,
    2415             :                                          OGRFieldType eFieldType,
    2416             :                                          OGRFieldSubType eFieldSubType,
    2417             :                                          const char *pszGlob)
    2418             : {
    2419          14 :     VALIDATE_POINTER1(pszName, __func__, nullptr);
    2420          14 :     VALIDATE_POINTER1(pszGlob, __func__, nullptr);
    2421          42 :     return OGRFieldDomain::ToHandle(
    2422             :         new OGRGlobFieldDomain(pszName, pszDescription ? pszDescription : "",
    2423          28 :                                eFieldType, eFieldSubType, pszGlob));
    2424             : }
    2425             : 
    2426             : /************************************************************************/
    2427             : /*                       OGR_FldDomain_GetName()                        */
    2428             : /************************************************************************/
    2429             : 
    2430             : /** Get the name of the field domain.
    2431             :  *
    2432             :  * This is the same as the C++ method OGRFieldDomain::GetName()
    2433             :  *
    2434             :  * @param hFieldDomain Field domain handle.
    2435             :  * @return the field domain name.
    2436             :  * @since GDAL 3.3
    2437             :  */
    2438          57 : const char *OGR_FldDomain_GetName(OGRFieldDomainH hFieldDomain)
    2439             : {
    2440          57 :     return OGRFieldDomain::FromHandle(hFieldDomain)->GetName().c_str();
    2441             : }
    2442             : 
    2443             : /************************************************************************/
    2444             : /*                     OGR_FldDomain_GetDescription()                   */
    2445             : /************************************************************************/
    2446             : 
    2447             : /** Get the description of the field domain.
    2448             :  *
    2449             :  * This is the same as the C++ method OGRFieldDomain::GetDescription()
    2450             :  *
    2451             :  * @param hFieldDomain Field domain handle.
    2452             :  * @return the field domain description (might be empty string).
    2453             :  * @since GDAL 3.3
    2454             :  */
    2455          61 : const char *OGR_FldDomain_GetDescription(OGRFieldDomainH hFieldDomain)
    2456             : {
    2457          61 :     return OGRFieldDomain::FromHandle(hFieldDomain)->GetDescription().c_str();
    2458             : }
    2459             : 
    2460             : /************************************************************************/
    2461             : /*                     OGR_FldDomain_GetDomainType()                    */
    2462             : /************************************************************************/
    2463             : 
    2464             : /** Get the type of the field domain.
    2465             :  *
    2466             :  * This is the same as the C++ method OGRFieldDomain::GetDomainType()
    2467             :  *
    2468             :  * @param hFieldDomain Field domain handle.
    2469             :  * @return the type of the field domain.
    2470             :  * @since GDAL 3.3
    2471             :  */
    2472          54 : OGRFieldDomainType OGR_FldDomain_GetDomainType(OGRFieldDomainH hFieldDomain)
    2473             : {
    2474          54 :     return OGRFieldDomain::FromHandle(hFieldDomain)->GetDomainType();
    2475             : }
    2476             : 
    2477             : /************************************************************************/
    2478             : /*                     OGR_FldDomain_GetFieldType()                     */
    2479             : /************************************************************************/
    2480             : 
    2481             : /** Get the field type of the field domain.
    2482             :  *
    2483             :  * This is the same as the C++ method OGRFieldDomain::GetFieldType()
    2484             :  *
    2485             :  * @param hFieldDomain Field domain handle.
    2486             :  * @return the field type of the field domain.
    2487             :  * @since GDAL 3.3
    2488             :  */
    2489          84 : OGRFieldType OGR_FldDomain_GetFieldType(OGRFieldDomainH hFieldDomain)
    2490             : {
    2491          84 :     return OGRFieldDomain::FromHandle(hFieldDomain)->GetFieldType();
    2492             : }
    2493             : 
    2494             : /************************************************************************/
    2495             : /*                   OGR_FldDomain_GetFieldSubType()                    */
    2496             : /************************************************************************/
    2497             : 
    2498             : /** Get the field subtype of the field domain.
    2499             :  *
    2500             :  * This is the same as OGRFieldDomain::GetFieldSubType()
    2501             :  *
    2502             :  * @param hFieldDomain Field domain handle.
    2503             :  * @return the field subtype of the field domain.
    2504             :  * @since GDAL 3.3
    2505             :  */
    2506          40 : OGRFieldSubType OGR_FldDomain_GetFieldSubType(OGRFieldDomainH hFieldDomain)
    2507             : {
    2508          40 :     return OGRFieldDomain::FromHandle(hFieldDomain)->GetFieldSubType();
    2509             : }
    2510             : 
    2511             : /************************************************************************/
    2512             : /*                    OGR_FldDomain_GetSplitPolicy()                    */
    2513             : /************************************************************************/
    2514             : 
    2515             : /** Get the split policy of the field domain.
    2516             :  *
    2517             :  * This is the same as the C++ method OGRFieldDomain::GetSplitPolicy()
    2518             :  *
    2519             :  * @param hFieldDomain Field domain handle.
    2520             :  * @return the split policy of the field domain.
    2521             :  * @since GDAL 3.3
    2522             :  */
    2523             : 
    2524             : OGRFieldDomainSplitPolicy
    2525           2 : OGR_FldDomain_GetSplitPolicy(OGRFieldDomainH hFieldDomain)
    2526             : {
    2527           2 :     return OGRFieldDomain::FromHandle(hFieldDomain)->GetSplitPolicy();
    2528             : }
    2529             : 
    2530             : /************************************************************************/
    2531             : /*                    OGR_FldDomain_SetSplitPolicy()                    */
    2532             : /************************************************************************/
    2533             : 
    2534             : /** Set the split policy of the field domain.
    2535             :  *
    2536             :  * This is the same as the C++ method OGRFieldDomain::SetSplitPolicy()
    2537             :  *
    2538             :  * @param hFieldDomain Field domain handle.
    2539             :  * @param policy the split policy of the field domain.
    2540             :  * @since GDAL 3.3
    2541             :  */
    2542             : 
    2543           1 : void OGR_FldDomain_SetSplitPolicy(OGRFieldDomainH hFieldDomain,
    2544             :                                   OGRFieldDomainSplitPolicy policy)
    2545             : {
    2546           1 :     OGRFieldDomain::FromHandle(hFieldDomain)->SetSplitPolicy(policy);
    2547           1 : }
    2548             : 
    2549             : /************************************************************************/
    2550             : /*                    OGR_FldDomain_GetMergePolicy()                    */
    2551             : /************************************************************************/
    2552             : 
    2553             : /** Get the merge policy of the field domain.
    2554             :  *
    2555             :  * This is the same as the C++ method OGRFieldDomain::GetMergePolicy()
    2556             :  *
    2557             :  * @param hFieldDomain Field domain handle.
    2558             :  * @return the merge policy of the field domain.
    2559             :  * @since GDAL 3.3
    2560             :  */
    2561             : 
    2562             : OGRFieldDomainMergePolicy
    2563           2 : OGR_FldDomain_GetMergePolicy(OGRFieldDomainH hFieldDomain)
    2564             : {
    2565           2 :     return OGRFieldDomain::FromHandle(hFieldDomain)->GetMergePolicy();
    2566             : }
    2567             : 
    2568             : /************************************************************************/
    2569             : /*                    OGR_FldDomain_SetMergePolicy()                    */
    2570             : /************************************************************************/
    2571             : 
    2572             : /** Set the merge policy of the field domain.
    2573             :  *
    2574             :  * This is the same as the C++ method OGRFieldDomain::SetMergePolicy()
    2575             :  *
    2576             :  * @param hFieldDomain Field domain handle.
    2577             :  * @param policy the merge policy of the field domain.
    2578             :  * @since GDAL 3.3
    2579             :  */
    2580             : 
    2581           1 : void OGR_FldDomain_SetMergePolicy(OGRFieldDomainH hFieldDomain,
    2582             :                                   OGRFieldDomainMergePolicy policy)
    2583             : {
    2584           1 :     OGRFieldDomain::FromHandle(hFieldDomain)->SetMergePolicy(policy);
    2585           1 : }
    2586             : 
    2587             : /************************************************************************/
    2588             : /*                  OGR_CodedFldDomain_GetEnumeration()                 */
    2589             : /************************************************************************/
    2590             : 
    2591             : /** Get the enumeration as (code, value) pairs.
    2592             :  *
    2593             :  * The end of the enumeration is signaled by code == NULL
    2594             :  *
    2595             :  * This is the same as the C++ method OGRCodedFieldDomain::GetEnumeration()
    2596             :  *
    2597             :  * @param hFieldDomain Field domain handle.
    2598             :  * @return the (code, value) pairs, or nullptr in case of error.
    2599             :  * @since GDAL 3.3
    2600             :  */
    2601             : const OGRCodedValue *
    2602          38 : OGR_CodedFldDomain_GetEnumeration(OGRFieldDomainH hFieldDomain)
    2603             : {
    2604             :     // The user should normally only call us with the right object type, but
    2605             :     // it doesn't hurt to check.
    2606          38 :     auto poFieldDomain = dynamic_cast<const OGRCodedFieldDomain *>(
    2607          76 :         OGRFieldDomain::FromHandle(hFieldDomain));
    2608          38 :     if (!poFieldDomain)
    2609             :     {
    2610           1 :         CPLError(
    2611             :             CE_Failure, CPLE_AppDefined,
    2612             :             "This function should be called with a coded field domain object");
    2613           1 :         return nullptr;
    2614             :     }
    2615          37 :     return poFieldDomain->GetEnumeration();
    2616             : }
    2617             : 
    2618             : /************************************************************************/
    2619             : /*                     OGR_RangeFldDomain_GetMin()                      */
    2620             : /************************************************************************/
    2621             : 
    2622             : /** Get the minimum value.
    2623             :  *
    2624             :  * Which member in the returned OGRField enum must be read depends on the field
    2625             :  * type.
    2626             :  *
    2627             :  * If no minimum value is set, the OGR_RawField_IsUnset() will return true when
    2628             :  * called on the result.
    2629             :  *
    2630             :  * This is the same as the C++ method OGRRangeFieldDomain::GetMin()
    2631             :  *
    2632             :  * @param hFieldDomain Field domain handle.
    2633             :  * @param pbIsInclusiveOut set to true if the minimum is included in the range.
    2634             :  * @return the minimum value.
    2635             :  * @since GDAL 3.3
    2636             :  */
    2637          21 : const OGRField *OGR_RangeFldDomain_GetMin(OGRFieldDomainH hFieldDomain,
    2638             :                                           bool *pbIsInclusiveOut)
    2639             : {
    2640             :     // The user should normally only call us with the right object type, but
    2641             :     // it doesn't hurt to check.
    2642          21 :     auto poFieldDomain = dynamic_cast<const OGRRangeFieldDomain *>(
    2643          42 :         OGRFieldDomain::FromHandle(hFieldDomain));
    2644          21 :     if (!poFieldDomain)
    2645             :     {
    2646           0 :         CPLError(
    2647             :             CE_Failure, CPLE_AppDefined,
    2648             :             "This function should be called with a range field domain object");
    2649           0 :         static const OGRField dummyField = GetUnsetField();
    2650           0 :         return &dummyField;
    2651             :     }
    2652          21 :     bool bIsInclusive = false;
    2653          21 :     const auto &ret = poFieldDomain->GetMin(bIsInclusive);
    2654          21 :     if (pbIsInclusiveOut)
    2655           7 :         *pbIsInclusiveOut = bIsInclusive;
    2656          21 :     return &ret;
    2657             : }
    2658             : 
    2659             : /************************************************************************/
    2660             : /*                     OGR_RangeFldDomain_GetMax()                      */
    2661             : /************************************************************************/
    2662             : 
    2663             : /** Get the maximum value.
    2664             :  *
    2665             :  * Which member in the returned OGRField enum must be read depends on the field
    2666             :  * type.
    2667             :  *
    2668             :  * If no maximum value is set, the OGR_RawField_IsUnset() will return true when
    2669             :  * called on the result.
    2670             :  *
    2671             :  * This is the same as the C++ method OGRRangeFieldDomain::GetMax()
    2672             :  *
    2673             :  * @param hFieldDomain Field domain handle.
    2674             :  * @param pbIsInclusiveOut set to true if the maximum is included in the range.
    2675             :  * @return the maximum value.
    2676             :  * @since GDAL 3.3
    2677             :  */
    2678          21 : const OGRField *OGR_RangeFldDomain_GetMax(OGRFieldDomainH hFieldDomain,
    2679             :                                           bool *pbIsInclusiveOut)
    2680             : {
    2681             :     // The user should normally only call us with the right object type, but
    2682             :     // it doesn't hurt to check.
    2683          21 :     auto poFieldDomain = dynamic_cast<const OGRRangeFieldDomain *>(
    2684          42 :         OGRFieldDomain::FromHandle(hFieldDomain));
    2685          21 :     if (!poFieldDomain)
    2686             :     {
    2687           0 :         CPLError(
    2688             :             CE_Failure, CPLE_AppDefined,
    2689             :             "This function should be called with a range field domain object");
    2690           0 :         static const OGRField dummyField = GetUnsetField();
    2691           0 :         return &dummyField;
    2692             :     }
    2693          21 :     bool bIsInclusive = false;
    2694          21 :     const auto &ret = poFieldDomain->GetMax(bIsInclusive);
    2695          21 :     if (pbIsInclusiveOut)
    2696           7 :         *pbIsInclusiveOut = bIsInclusive;
    2697          21 :     return &ret;
    2698             : }
    2699             : 
    2700             : /************************************************************************/
    2701             : /*                     OGR_GlobFldDomain_GetGlob()                      */
    2702             : /************************************************************************/
    2703             : 
    2704             : /** Get the glob expression.
    2705             :  *
    2706             :  * This is the same as the C++ method OGRGlobFieldDomain::GetGlob()
    2707             :  *
    2708             :  * @param hFieldDomain Field domain handle.
    2709             :  * @return the glob expression, or nullptr in case of error
    2710             :  * @since GDAL 3.3
    2711             :  */
    2712           9 : const char *OGR_GlobFldDomain_GetGlob(OGRFieldDomainH hFieldDomain)
    2713             : {
    2714             :     // The user should normally only call us with the right object type, but
    2715             :     // it doesn't hurt to check.
    2716           9 :     auto poFieldDomain = dynamic_cast<const OGRGlobFieldDomain *>(
    2717          18 :         OGRFieldDomain::FromHandle(hFieldDomain));
    2718           9 :     if (!poFieldDomain)
    2719             :     {
    2720           1 :         CPLError(
    2721             :             CE_Failure, CPLE_AppDefined,
    2722             :             "This function should be called with a glob field domain object");
    2723           1 :         return nullptr;
    2724             :     }
    2725           8 :     return poFieldDomain->GetGlob().c_str();
    2726             : }

Generated by: LCOV version 1.14