Coverage Report

Created: 2026-07-25 06:50

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