Coverage Report

Created: 2026-04-01 06:20

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
/*                             GetJustify()                             */
1128
/************************************************************************/
1129
1130
/**
1131
 * \fn OGRJustification OGRFieldDefn::GetJustify() const;
1132
 *
1133
 * \brief Get the justification for this field.
1134
 *
1135
 * Note: no driver is know to use the concept of field justification.
1136
 *
1137
 * This method is the same as the C function OGR_Fld_GetJustify().
1138
 *
1139
 * @return the justification.
1140
 */
1141
1142
/************************************************************************/
1143
/*                         OGR_Fld_GetJustify()                         */
1144
/************************************************************************/
1145
/**
1146
 * \brief Get the justification for this field.
1147
 *
1148
 * This function is the same as the CPP method OGRFieldDefn::GetJustify().
1149
 *
1150
 * Note: no driver is know to use the concept of field justification.
1151
 *
1152
 * @param hDefn handle to the field definition to get justification from.
1153
 * @return the justification.
1154
 */
1155
1156
OGRJustification OGR_Fld_GetJustify(OGRFieldDefnH hDefn)
1157
1158
0
{
1159
0
    return OGRFieldDefn::FromHandle(hDefn)->GetJustify();
1160
0
}
1161
1162
/************************************************************************/
1163
/*                             SetJustify()                             */
1164
/************************************************************************/
1165
1166
/**
1167
 * \fn void OGRFieldDefn::SetJustify( OGRJustification eJustify );
1168
 *
1169
 * \brief Set the justification for this field.
1170
 *
1171
 * Note: no driver is know to use the concept of field justification.
1172
 *
1173
 * This method is the same as the C function OGR_Fld_SetJustify().
1174
 *
1175
 * @param eJustify the new justification.
1176
 */
1177
1178
/************************************************************************/
1179
/*                         OGR_Fld_SetJustify()                         */
1180
/************************************************************************/
1181
/**
1182
 * \brief Set the justification for this field.
1183
 *
1184
 * Note: no driver is know to use the concept of field justification.
1185
 *
1186
 * This function is the same as the CPP method OGRFieldDefn::SetJustify().
1187
 *
1188
 * @param hDefn handle to the field definition to set justification to.
1189
 * @param eJustify the new justification.
1190
 */
1191
1192
void OGR_Fld_SetJustify(OGRFieldDefnH hDefn, OGRJustification eJustify)
1193
1194
0
{
1195
0
    OGRFieldDefn::FromHandle(hDefn)->SetJustify(eJustify);
1196
0
}
1197
1198
/************************************************************************/
1199
/*                              GetWidth()                              */
1200
/************************************************************************/
1201
1202
/**
1203
 * \fn int OGRFieldDefn::GetWidth() const;
1204
 *
1205
 * \brief Get the formatting width for this field.
1206
 *
1207
 * This method is the same as the C function OGR_Fld_GetWidth().
1208
 *
1209
 * @return the width, zero means no specified width.
1210
 */
1211
1212
/************************************************************************/
1213
/*                          OGR_Fld_GetWidth()                          */
1214
/************************************************************************/
1215
/**
1216
 * \brief Get the formatting width for this field.
1217
 *
1218
 * This function is the same as the CPP method OGRFieldDefn::GetWidth().
1219
 *
1220
 * @param hDefn handle to the field definition to get width from.
1221
 * @return the width, zero means no specified width.
1222
 */
1223
1224
int OGR_Fld_GetWidth(OGRFieldDefnH hDefn)
1225
1226
0
{
1227
0
    return OGRFieldDefn::FromHandle(hDefn)->GetWidth();
1228
0
}
1229
1230
/************************************************************************/
1231
/*                              SetWidth()                              */
1232
/************************************************************************/
1233
1234
/**
1235
 * \brief Set the formatting width for this field in characters.
1236
 *
1237
 * This method is the same as the C function OGR_Fld_SetWidth().
1238
 *
1239
 * Note that once a OGRFieldDefn has been added to a layer definition with
1240
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1241
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1242
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1243
 * OGRFieldDefn, for drivers that support AlterFieldDefn.
1244
 *
1245
 * @param nWidthIn the new width.
1246
 */
1247
void OGRFieldDefn::SetWidth(int nWidthIn)
1248
0
{
1249
0
    if (m_bSealed)
1250
0
    {
1251
0
        CPLError(CE_Failure, CPLE_AppDefined,
1252
0
                 "OGRFieldDefn::SetWidth() not allowed on a sealed object");
1253
0
        return;
1254
0
    }
1255
0
    nWidth = std::max(0, nWidthIn);
1256
0
}
1257
1258
/************************************************************************/
1259
/*                          OGR_Fld_SetWidth()                          */
1260
/************************************************************************/
1261
/**
1262
 * \brief Set the formatting width for this field in characters.
1263
 *
1264
 * This function is the same as the CPP method OGRFieldDefn::SetWidth().
1265
 *
1266
 * Note that once a OGRFieldDefn has been added to a layer definition with
1267
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1268
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1269
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1270
 * OGRFieldDefn, for drivers that support AlterFieldDefn.
1271
 *
1272
 * @param hDefn handle to the field definition to set width to.
1273
 * @param nNewWidth the new width.
1274
 */
1275
1276
void OGR_Fld_SetWidth(OGRFieldDefnH hDefn, int nNewWidth)
1277
1278
0
{
1279
0
    OGRFieldDefn::FromHandle(hDefn)->SetWidth(nNewWidth);
1280
0
}
1281
1282
/************************************************************************/
1283
/*                            GetPrecision()                            */
1284
/************************************************************************/
1285
1286
/**
1287
 * \fn int OGRFieldDefn::GetPrecision() const;
1288
 *
1289
 * \brief Get the formatting precision for this field.
1290
 * This should normally be
1291
 * zero for fields of types other than OFTReal.
1292
 *
1293
 * This method is the same as the C function OGR_Fld_GetPrecision().
1294
 *
1295
 * @return the precision.
1296
 */
1297
1298
/************************************************************************/
1299
/*                        OGR_Fld_GetPrecision()                        */
1300
/************************************************************************/
1301
/**
1302
 * \brief Get the formatting precision for this field.
1303
 * This should normally be
1304
 * zero for fields of types other than OFTReal.
1305
 *
1306
 * This function is the same as the CPP method OGRFieldDefn::GetPrecision().
1307
 *
1308
 * @param hDefn handle to the field definition to get precision from.
1309
 * @return the precision.
1310
 */
1311
1312
int OGR_Fld_GetPrecision(OGRFieldDefnH hDefn)
1313
1314
0
{
1315
0
    return OGRFieldDefn::FromHandle(hDefn)->GetPrecision();
1316
0
}
1317
1318
/************************************************************************/
1319
/*                            SetPrecision()                            */
1320
/************************************************************************/
1321
1322
/**
1323
 * \brief Set the formatting precision for this field in characters.
1324
 *
1325
 * This should normally be zero for fields of types other than OFTReal.
1326
 *
1327
 * This method is the same as the C function OGR_Fld_SetPrecision().
1328
 *
1329
 * Note that once a OGRFieldDefn has been added to a layer definition with
1330
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1331
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1332
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1333
 * OGRFieldDefn, for drivers that support AlterFieldDefn.
1334
 *
1335
 * @param nPrecisionIn the new precision.
1336
 */
1337
void OGRFieldDefn::SetPrecision(int nPrecisionIn)
1338
0
{
1339
0
    if (m_bSealed)
1340
0
    {
1341
0
        CPLError(CE_Failure, CPLE_AppDefined,
1342
0
                 "OGRFieldDefn::SetPrecision() not allowed on a sealed object");
1343
0
        return;
1344
0
    }
1345
0
    nPrecision = nPrecisionIn;
1346
0
}
1347
1348
/************************************************************************/
1349
/*                        OGR_Fld_SetPrecision()                        */
1350
/************************************************************************/
1351
/**
1352
 * \brief Set the formatting precision for this field in characters.
1353
 *
1354
 * This should normally be zero for fields of types other than OFTReal.
1355
 *
1356
 * This function is the same as the CPP method OGRFieldDefn::SetPrecision().
1357
 *
1358
 * Note that once a OGRFieldDefn has been added to a layer definition with
1359
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1360
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1361
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1362
 * OGRFieldDefn, for drivers that support AlterFieldDefn.
1363
 *
1364
 * @param hDefn handle to the field definition to set precision to.
1365
 * @param nPrecision the new precision.
1366
 */
1367
1368
void OGR_Fld_SetPrecision(OGRFieldDefnH hDefn, int nPrecision)
1369
1370
0
{
1371
0
    OGRFieldDefn::FromHandle(hDefn)->SetPrecision(nPrecision);
1372
0
}
1373
1374
/************************************************************************/
1375
/*                             GetTZFlag()                              */
1376
/************************************************************************/
1377
1378
/**
1379
 * \fn int OGRFieldDefn::GetTZFlag() const;
1380
 *
1381
 * \brief Get the time zone flag.
1382
 *
1383
 * Only applies to OFTTime, OFTDate and OFTDateTime fields.
1384
 *
1385
 * Cf OGR_TZFLAG_UNKNOWN, OGR_TZFLAG_LOCALTIME, OGR_TZFLAG_MIXED_TZ and
1386
 * OGR_TZFLAG_UTC
1387
 *
1388
 * This method is the same as the C function OGR_Fld_GetTZFlag().
1389
 *
1390
 * @return the time zone flag.
1391
 * @since GDAL 3.8
1392
 */
1393
1394
/************************************************************************/
1395
/*                         OGR_Fld_GetTZFlag()                          */
1396
/************************************************************************/
1397
/**
1398
 * \brief Get the time zone flag.
1399
 *
1400
 * Only applies to OFTTime, OFTDate and OFTDateTime fields.
1401
 *
1402
 * Cf OGR_TZFLAG_UNKNOWN, OGR_TZFLAG_LOCALTIME, OGR_TZFLAG_MIXED_TZ and
1403
 * OGR_TZFLAG_UTC
1404
 *
1405
 * @param hDefn handle to the field definition .
1406
 * @return the time zone flag.
1407
 * @since GDAL 3.8
1408
 */
1409
1410
int OGR_Fld_GetTZFlag(OGRFieldDefnH hDefn)
1411
1412
0
{
1413
0
    return OGRFieldDefn::FromHandle(hDefn)->GetTZFlag();
1414
0
}
1415
1416
/************************************************************************/
1417
/*                             SetTZFlag()                              */
1418
/************************************************************************/
1419
1420
/**
1421
 * \brief Set the time zone flag.
1422
 *
1423
 * Only applies to OFTTime, OFTDate and OFTDateTime fields.
1424
 *
1425
 * Cf OGR_TZFLAG_UNKNOWN, OGR_TZFLAG_LOCALTIME, OGR_TZFLAG_MIXED_TZ and
1426
 * OGR_TZFLAG_UTC
1427
 *
1428
 * This method is the same as the C function OGR_Fld_SetTZFlag().
1429
 *
1430
 * Note that once a OGRFieldDefn has been added to a layer definition with
1431
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1432
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1433
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1434
 * OGRFieldDefn, for drivers that support AlterFieldDefn.
1435
 *
1436
 * @param nTZFlag the new time zone flag.
1437
 * @since GDAL 3.8
1438
 */
1439
void OGRFieldDefn::SetTZFlag(int nTZFlag)
1440
0
{
1441
0
    if (m_bSealed)
1442
0
    {
1443
0
        CPLError(CE_Failure, CPLE_AppDefined,
1444
0
                 "OGRFieldDefn::SetTZFlag() not allowed on a sealed object");
1445
0
        return;
1446
0
    }
1447
0
    m_nTZFlag = nTZFlag;
1448
0
}
1449
1450
/************************************************************************/
1451
/*                         OGR_Fld_SetTZFlag()                          */
1452
/************************************************************************/
1453
/**
1454
 * \brief Set the formatting precision for this field in characters.
1455
 *
1456
 * Set the time zone flag.
1457
 *
1458
 * Only applies to OFTTime, OFTDate and OFTDateTime fields.
1459
 *
1460
 * Cf OGR_TZFLAG_UNKNOWN, OGR_TZFLAG_LOCALTIME, OGR_TZFLAG_MIXED_TZ and
1461
 * OGR_TZFLAG_UTC
1462
 *
1463
 * Note that once a OGRFieldDefn has been added to a layer definition with
1464
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1465
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1466
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1467
 * OGRFieldDefn, for drivers that support AlterFieldDefn.
1468
 *
1469
 * @param hDefn handle to the field definition to set precision to.
1470
 * @param nTZFlag the new time zone flag.
1471
 * @since GDAL 3.8
1472
 */
1473
1474
void OGR_Fld_SetTZFlag(OGRFieldDefnH hDefn, int nTZFlag)
1475
1476
0
{
1477
0
    OGRFieldDefn::FromHandle(hDefn)->SetTZFlag(nTZFlag);
1478
0
}
1479
1480
/************************************************************************/
1481
/*                                Set()                                 */
1482
/************************************************************************/
1483
1484
/**
1485
 * \brief Set defining parameters for a field in one call.
1486
 *
1487
 * This method is the same as the C function OGR_Fld_Set().
1488
 *
1489
 * Note that once a OGRFieldDefn has been added to a layer definition with
1490
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1491
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1492
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1493
 * OGRFieldDefn, for drivers that support AlterFieldDefn.
1494
 *
1495
 * @param pszNameIn the new name to assign.
1496
 * @param eTypeIn the new type (one of the OFT values like OFTInteger).
1497
 * @param nWidthIn the preferred formatting width.  Defaults to zero indicating
1498
 * undefined.
1499
 * @param nPrecisionIn number of decimals places for formatting, defaults to
1500
 * zero indicating undefined.
1501
 * @param eJustifyIn the formatting justification (OJLeft or OJRight), defaults
1502
 * to OJUndefined.
1503
 */
1504
1505
void OGRFieldDefn::Set(const char *pszNameIn, OGRFieldType eTypeIn,
1506
                       int nWidthIn, int nPrecisionIn,
1507
                       OGRJustification eJustifyIn)
1508
0
{
1509
0
    if (m_bSealed)
1510
0
    {
1511
0
        CPLError(CE_Failure, CPLE_AppDefined,
1512
0
                 "OGRFieldDefn::Set() not allowed on a sealed object");
1513
0
        return;
1514
0
    }
1515
0
    SetName(pszNameIn);
1516
0
    SetType(eTypeIn);
1517
0
    SetWidth(nWidthIn);
1518
0
    SetPrecision(nPrecisionIn);
1519
0
    SetJustify(eJustifyIn);
1520
0
}
1521
1522
/************************************************************************/
1523
/*                            OGR_Fld_Set()                             */
1524
/************************************************************************/
1525
/**
1526
 * \brief Set defining parameters for a field in one call.
1527
 *
1528
 * This function is the same as the CPP method OGRFieldDefn::Set().
1529
 *
1530
 * Note that once a OGRFieldDefn has been added to a layer definition with
1531
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1532
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1533
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1534
 * OGRFieldDefn, for drivers that support AlterFieldDefn.
1535
 *
1536
 * @param hDefn handle to the field definition to set to.
1537
 * @param pszNameIn the new name to assign.
1538
 * @param eTypeIn the new type (one of the OFT values like OFTInteger).
1539
 * @param nWidthIn the preferred formatting width.  Defaults to zero indicating
1540
 * undefined.
1541
 * @param nPrecisionIn number of decimals places for formatting, defaults to
1542
 * zero indicating undefined.
1543
 * @param eJustifyIn the formatting justification (OJLeft or OJRight), defaults
1544
 * to OJUndefined.
1545
 */
1546
1547
void OGR_Fld_Set(OGRFieldDefnH hDefn, const char *pszNameIn,
1548
                 OGRFieldType eTypeIn, int nWidthIn, int nPrecisionIn,
1549
                 OGRJustification eJustifyIn)
1550
1551
0
{
1552
0
    OGRFieldDefn::FromHandle(hDefn)->Set(pszNameIn, eTypeIn, nWidthIn,
1553
0
                                         nPrecisionIn, eJustifyIn);
1554
0
}
1555
1556
/************************************************************************/
1557
/*                             IsIgnored()                              */
1558
/************************************************************************/
1559
1560
/**
1561
 * \fn int OGRFieldDefn::IsIgnored() const;
1562
 *
1563
 * \brief Return whether this field should be omitted when fetching features
1564
 *
1565
 * This method is the same as the C function OGR_Fld_IsIgnored().
1566
 *
1567
 * @return ignore state
1568
 */
1569
1570
/************************************************************************/
1571
/*                         OGR_Fld_IsIgnored()                          */
1572
/************************************************************************/
1573
1574
/**
1575
 * \brief Return whether this field should be omitted when fetching features
1576
 *
1577
 * This method is the same as the C++ method OGRFieldDefn::IsIgnored().
1578
 *
1579
 * @param hDefn handle to the field definition
1580
 * @return ignore state
1581
 */
1582
1583
int OGR_Fld_IsIgnored(OGRFieldDefnH hDefn)
1584
0
{
1585
0
    return OGRFieldDefn::FromHandle(hDefn)->IsIgnored();
1586
0
}
1587
1588
/************************************************************************/
1589
/*                             SetIgnored()                             */
1590
/************************************************************************/
1591
1592
/**
1593
 * \fn void OGRFieldDefn::SetIgnored( int ignore );
1594
 *
1595
 * \brief Set whether this field should be omitted when fetching features
1596
 *
1597
 * This method is the same as the C function OGR_Fld_SetIgnored().
1598
 *
1599
 * This method should not be called on a object returned with
1600
 * OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead, the
1601
 * OGRLayer::SetIgnoredFields() method should be called.
1602
 *
1603
 * @param ignore ignore state
1604
 */
1605
1606
/************************************************************************/
1607
/*                         OGR_Fld_SetIgnored()                         */
1608
/************************************************************************/
1609
1610
/**
1611
 * \brief Set whether this field should be omitted when fetching features
1612
 *
1613
 * This method is the same as the C++ method OGRFieldDefn::SetIgnored().
1614
 *
1615
 * This method should not be called on a object returned with
1616
 * OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead, the
1617
 * OGRLayer::SetIgnoredFields() method should be called.
1618
 *
1619
 * @param hDefn handle to the field definition
1620
 * @param ignore ignore state
1621
 */
1622
1623
void OGR_Fld_SetIgnored(OGRFieldDefnH hDefn, int ignore)
1624
0
{
1625
0
    OGRFieldDefn::FromHandle(hDefn)->SetIgnored(ignore);
1626
0
}
1627
1628
/************************************************************************/
1629
/*                               IsSame()                               */
1630
/************************************************************************/
1631
1632
/**
1633
 * \brief Test if the field definition is identical to the other one.
1634
 *
1635
 * @param poOtherFieldDefn the other field definition to compare to.
1636
 * @return TRUE if the field definition is identical to the other one.
1637
 */
1638
1639
int OGRFieldDefn::IsSame(const OGRFieldDefn *poOtherFieldDefn) const
1640
0
{
1641
0
    return strcmp(pszName, poOtherFieldDefn->pszName) == 0 &&
1642
0
           strcmp(pszAlternativeName, poOtherFieldDefn->pszAlternativeName) ==
1643
0
               0 &&
1644
0
           eType == poOtherFieldDefn->eType &&
1645
0
           eSubType == poOtherFieldDefn->eSubType &&
1646
0
           nWidth == poOtherFieldDefn->nWidth &&
1647
0
           nPrecision == poOtherFieldDefn->nPrecision &&
1648
0
           bNullable == poOtherFieldDefn->bNullable &&
1649
0
           m_osComment == poOtherFieldDefn->m_osComment &&
1650
0
           m_nTZFlag == poOtherFieldDefn->m_nTZFlag;
1651
0
}
1652
1653
/************************************************************************/
1654
/*                             IsNullable()                             */
1655
/************************************************************************/
1656
1657
/**
1658
 * \fn int OGRFieldDefn::IsNullable() const
1659
 *
1660
 * \brief Return whether this field can receive null values.
1661
 *
1662
 * By default, fields are nullable.
1663
 *
1664
 * Even if this method returns FALSE (i.e not-nullable field), it doesn't mean
1665
 * that OGRFeature::IsFieldSet() will necessary return TRUE, as fields can be
1666
 * temporary unset and null/not-null validation is usually done when
1667
 * OGRLayer::CreateFeature()/SetFeature() is called.
1668
 *
1669
 * This method is the same as the C function OGR_Fld_IsNullable().
1670
 *
1671
 * @return TRUE if the field is authorized to be null.
1672
 */
1673
1674
/************************************************************************/
1675
/*                         OGR_Fld_IsNullable()                         */
1676
/************************************************************************/
1677
1678
/**
1679
 * \brief Return whether this field can receive null values.
1680
 *
1681
 * By default, fields are nullable.
1682
 *
1683
 * Even if this method returns FALSE (i.e not-nullable field), it doesn't mean
1684
 * that OGRFeature::IsFieldSet() will necessary return TRUE, as fields can be
1685
 * temporary unset and null/not-null validation is usually done when
1686
 * OGRLayer::CreateFeature()/SetFeature() is called.
1687
 *
1688
 * This method is the same as the C++ method OGRFieldDefn::IsNullable().
1689
 *
1690
 * @param hDefn handle to the field definition
1691
 * @return TRUE if the field is authorized to be null.
1692
 */
1693
1694
int OGR_Fld_IsNullable(OGRFieldDefnH hDefn)
1695
0
{
1696
0
    return OGRFieldDefn::FromHandle(hDefn)->IsNullable();
1697
0
}
1698
1699
/************************************************************************/
1700
/*                            SetNullable()                             */
1701
/************************************************************************/
1702
1703
/**
1704
 * \fn void OGRFieldDefn::SetNullable( int bNullableIn );
1705
 *
1706
 * \brief Set whether this field can receive null values.
1707
 *
1708
 * By default, fields are nullable, so this method is generally called with
1709
 * FALSE to set a not-null constraint.
1710
 *
1711
 * Drivers that support writing not-null constraint will advertise the
1712
 * GDAL_DCAP_NOTNULL_FIELDS driver metadata item.
1713
 *
1714
 * This method is the same as the C function OGR_Fld_SetNullable().
1715
 *
1716
 * Note that once a OGRFieldDefn has been added to a layer definition with
1717
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1718
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1719
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1720
 * OGRFieldDefn, for drivers that support AlterFieldDefn().
1721
 *
1722
 * @param bNullableIn FALSE if the field must have a not-null constraint.
1723
 */
1724
void OGRFieldDefn::SetNullable(int bNullableIn)
1725
0
{
1726
0
    if (m_bSealed)
1727
0
    {
1728
0
        CPLError(CE_Failure, CPLE_AppDefined,
1729
0
                 "OGRFieldDefn::SetNullable() not allowed on a sealed object");
1730
0
        return;
1731
0
    }
1732
0
    bNullable = bNullableIn;
1733
0
}
1734
1735
/************************************************************************/
1736
/*                        OGR_Fld_SetNullable()                         */
1737
/************************************************************************/
1738
1739
/**
1740
 * \brief Set whether this field can receive null values.
1741
 *
1742
 * By default, fields are nullable, so this method is generally called with
1743
 * FALSE to set a not-null constraint.
1744
 *
1745
 * Drivers that support writing not-null constraint will advertise the
1746
 * GDAL_DCAP_NOTNULL_FIELDS driver metadata item.
1747
 *
1748
 * This method is the same as the C++ method OGRFieldDefn::SetNullable().
1749
 *
1750
 * Note that once a OGRFieldDefn has been added to a layer definition with
1751
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1752
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1753
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1754
 * OGRFieldDefn, for drivers that support AlterFieldDefn().
1755
 *
1756
 * @param hDefn handle to the field definition
1757
 * @param bNullableIn FALSE if the field must have a not-null constraint.
1758
 */
1759
1760
void OGR_Fld_SetNullable(OGRFieldDefnH hDefn, int bNullableIn)
1761
0
{
1762
0
    OGRFieldDefn::FromHandle(hDefn)->SetNullable(bNullableIn);
1763
0
}
1764
1765
/************************************************************************/
1766
/*                        OGR_Fld_SetGenerated()                        */
1767
/************************************************************************/
1768
1769
/**
1770
 * \brief Set whether this field is a generated field.
1771
 *
1772
 * By default, fields are not generated, so this method is generally called with
1773
 * TRUE to set a generated field.
1774
 *
1775
 * This method is the same as the C++ method OGRFieldDefn::SetGenerated().
1776
 *
1777
 * Note that once a OGRFieldDefn has been added to a layer definition with
1778
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1779
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1780
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1781
 * OGRFieldDefn, for drivers that support AlterFieldDefn().
1782
 *
1783
 * @param hDefn handle to the field definition
1784
 * @param bGeneratedIn FALSE if the field is a generated field.
1785
 * @since GDAL 3.11
1786
 */
1787
1788
void OGR_Fld_SetGenerated(OGRFieldDefnH hDefn, int bGeneratedIn)
1789
0
{
1790
0
    OGRFieldDefn::FromHandle(hDefn)->SetGenerated(bGeneratedIn);
1791
0
}
1792
1793
/************************************************************************/
1794
/*                        OGR_Fld_IsGenerated()                         */
1795
/************************************************************************/
1796
1797
/**
1798
 * \brief Return whether this field is a generated field.
1799
 *
1800
 * By default, fields are not generated.
1801
 *
1802
 * This method is the same as the C++ method OGRFieldDefn::IsGenerated().
1803
 *
1804
 * @param hDefn handle to the field definition
1805
 * @return TRUE if the field is a generated field.
1806
 * @since GDAL 3.11
1807
 */
1808
1809
int OGR_Fld_IsGenerated(OGRFieldDefnH hDefn)
1810
0
{
1811
0
    return OGRFieldDefn::FromHandle(hDefn)->IsGenerated();
1812
0
}
1813
1814
/************************************************************************/
1815
/*                              IsUnique()                              */
1816
/************************************************************************/
1817
1818
/**
1819
 * \fn int OGRFieldDefn::IsUnique() const
1820
 *
1821
 * \brief Return whether this field has a unique constraint.
1822
 *
1823
 * By default, fields have no unique constraint.
1824
 *
1825
 * This method is the same as the C function OGR_Fld_IsUnique().
1826
 *
1827
 * @return TRUE if the field has a unique constraint.
1828
 * @since GDAL 3.2
1829
 */
1830
1831
/************************************************************************/
1832
/*                          OGR_Fld_IsUnique()                          */
1833
/************************************************************************/
1834
1835
/**
1836
 * \brief Return whether this field has a unique constraint.
1837
 *
1838
 * By default, fields have no unique constraint.
1839
 *
1840
 * This method is the same as the C++ method OGRFieldDefn::IsUnique().
1841
 *
1842
 * @param hDefn handle to the field definition
1843
 * @return TRUE if the field has a unique constraint.
1844
 * @since GDAL 3.2
1845
 */
1846
1847
int OGR_Fld_IsUnique(OGRFieldDefnH hDefn)
1848
0
{
1849
0
    return OGRFieldDefn::FromHandle(hDefn)->IsUnique();
1850
0
}
1851
1852
/*********************************************************  ***************/
1853
/*                            SetUnique()                             */
1854
/************************************************************************/
1855
1856
/**
1857
 * \brief Set whether this field has a unique constraint.
1858
 *
1859
 * By default, fields have no unique constraint, so this method is generally
1860
 * called with TRUE to set a unique constraint.
1861
 *
1862
 * Drivers that support writing unique constraint will advertise the
1863
 * GDAL_DCAP_UNIQUE_FIELDS driver metadata item.
1864
 *
1865
 * This method is the same as the C function OGR_Fld_SetUnique().
1866
 *
1867
 * Note that once a OGRFieldDefn has been added to a layer definition with
1868
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1869
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1870
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1871
 * OGRFieldDefn, for drivers that support AlterFieldDefn().
1872
 *
1873
 * @param bUniqueIn TRUE if the field must have a unique constraint.
1874
 * @since GDAL 3.2
1875
 */
1876
void OGRFieldDefn::SetUnique(int bUniqueIn)
1877
0
{
1878
0
    if (m_bSealed)
1879
0
    {
1880
0
        CPLError(CE_Failure, CPLE_AppDefined,
1881
0
                 "OGRFieldDefn::SetUnique() not allowed on a sealed object");
1882
0
        return;
1883
0
    }
1884
0
    bUnique = bUniqueIn;
1885
0
}
1886
1887
/************************************************************************/
1888
/*                         OGR_Fld_SetUnique()                          */
1889
/************************************************************************/
1890
1891
/**
1892
 * \brief Set whether this field has a unique constraint.
1893
 *
1894
 * By default, fields have no unique constraint, so this method is generally
1895
 *called with TRUE to set a unique constraint.
1896
 *
1897
 * Drivers that support writing unique constraint will advertise the
1898
 * GDAL_DCAP_UNIQUE_FIELDS driver metadata item.
1899
 *field can receive null values.
1900
 *
1901
 * This method is the same as the C++ method OGRFieldDefn::SetUnique().
1902
 *
1903
 * Note that once a OGRFieldDefn has been added to a layer definition with
1904
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1905
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1906
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1907
 * OGRFieldDefn, for drivers that support AlterFieldDefn().
1908
 *
1909
 * @param hDefn handle to the field definition
1910
 * @param bUniqueIn TRUE if the field must have a unique constraint.
1911
 * @since GDAL 3.2
1912
 */
1913
1914
void OGR_Fld_SetUnique(OGRFieldDefnH hDefn, int bUniqueIn)
1915
0
{
1916
0
    OGRFieldDefn::FromHandle(hDefn)->SetUnique(bUniqueIn);
1917
0
}
1918
1919
/************************************************************************/
1920
/*                           GetDomainName()                            */
1921
/************************************************************************/
1922
1923
/**
1924
 * \fn const std::string& OGRFieldDefn::GetDomainName() const
1925
 *
1926
 * \brief Return the name of the field domain for this field.
1927
 *
1928
 * By default, none (empty string) is returned.
1929
 *
1930
 * Field domains (OGRFieldDomain class) are attached at the GDALDataset level
1931
 * and should be retrieved with GDALDataset::GetFieldDomain().
1932
 *
1933
 * This method is the same as the C function OGR_Fld_GetDomainName().
1934
 *
1935
 * @return the field domain name, or an empty string if there is none.
1936
 * @since GDAL 3.3
1937
 */
1938
1939
/************************************************************************/
1940
/*                       OGR_Fld_GetDomainName()                        */
1941
/************************************************************************/
1942
1943
/**
1944
 * \brief Return the name of the field domain for this field.
1945
 *
1946
 * By default, none (empty string) is returned.
1947
 *
1948
 * Field domains (OGRFieldDomain class) are attached at the GDALDataset level
1949
 * and should be retrieved with GDALDatasetGetFieldDomain().
1950
 *
1951
 * This method is the same as the C++ method OGRFieldDefn::GetDomainName().
1952
 *
1953
 * @param hDefn handle to the field definition
1954
 * @return the field domain name, or an empty string if there is none.
1955
 * @since GDAL 3.3
1956
 */
1957
1958
const char *OGR_Fld_GetDomainName(OGRFieldDefnH hDefn)
1959
0
{
1960
0
    return OGRFieldDefn::FromHandle(hDefn)->GetDomainName().c_str();
1961
0
}
1962
1963
/************************************************************************/
1964
/*                           SetDomainName()                            */
1965
/************************************************************************/
1966
1967
/**
1968
 * \brief Set the name of the field domain for this field.
1969
 *
1970
 * Field domains (OGRFieldDomain) are attached at the GDALDataset level.
1971
 *
1972
 * This method is the same as the C function OGR_Fld_SetDomainName().
1973
 *
1974
 * Note that once a OGRFieldDefn has been added to a layer definition with
1975
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
1976
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
1977
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
1978
 * OGRFieldDefn, for drivers that support AlterFieldDefn().
1979
 *
1980
 * @param osDomainName Field domain name.
1981
 * @since GDAL 3.3
1982
 */
1983
void OGRFieldDefn::SetDomainName(const std::string &osDomainName)
1984
0
{
1985
0
    if (m_bSealed)
1986
0
    {
1987
0
        CPLError(
1988
0
            CE_Failure, CPLE_AppDefined,
1989
0
            "OGRFieldDefn::SetDomainName() not allowed on a sealed object");
1990
0
        return;
1991
0
    }
1992
0
    m_osDomainName = osDomainName;
1993
0
}
1994
1995
/************************************************************************/
1996
/*                       OGR_Fld_SetDomainName()                        */
1997
/************************************************************************/
1998
1999
/**
2000
 * \brief Set the name of the field domain for this field.
2001
 *
2002
 * Field domains (OGRFieldDomain) are attached at the GDALDataset level.
2003
 *
2004
 * This method is the same as the C++ method OGRFieldDefn::SetDomainName().
2005
 *
2006
 * Note that once a OGRFieldDefn has been added to a layer definition with
2007
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
2008
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
2009
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
2010
 * OGRFieldDefn, for drivers that support AlterFieldDefn().
2011
 *
2012
 * @param hDefn handle to the field definition
2013
 * @param pszFieldName Field domain name.
2014
 * @since GDAL 3.3
2015
 */
2016
2017
void OGR_Fld_SetDomainName(OGRFieldDefnH hDefn, const char *pszFieldName)
2018
0
{
2019
0
    OGRFieldDefn::FromHandle(hDefn)->SetDomainName(pszFieldName ? pszFieldName
2020
0
                                                                : "");
2021
0
}
2022
2023
/************************************************************************/
2024
/*                             GetComment()                             */
2025
/************************************************************************/
2026
2027
/**
2028
 * \fn const std::string& OGRFieldDefn::GetComment() const
2029
 *
2030
 * \brief Return the (optional) comment for this field.
2031
 *
2032
 * By default, none (empty string) is returned.
2033
 *
2034
 * This method is the same as the C function OGR_Fld_GetComment().
2035
 *
2036
 * @return the field comment, or an empty string if there is none.
2037
 * @since GDAL 3.7
2038
 */
2039
2040
/************************************************************************/
2041
/*                         OGR_Fld_GetComment()                         */
2042
/************************************************************************/
2043
2044
/**
2045
 * \brief Return the (optional) comment for this field.
2046
 *
2047
 * By default, none (empty string) is returned.
2048
 *
2049
 * This method is the same as the C++ method OGRFieldDefn::GetComment().
2050
 *
2051
 * @param hDefn handle to the field definition
2052
 * @return the comment, or an empty string if there is none.
2053
 * @since GDAL 3.7
2054
 */
2055
2056
const char *OGR_Fld_GetComment(OGRFieldDefnH hDefn)
2057
0
{
2058
0
    return OGRFieldDefn::FromHandle(hDefn)->GetComment().c_str();
2059
0
}
2060
2061
/************************************************************************/
2062
/*                             SetComment()                             */
2063
/************************************************************************/
2064
2065
/**
2066
 * \brief Set the comment for this field.
2067
 *
2068
 * This method is the same as the C function OGR_Fld_SetComment().
2069
 *
2070
 * Note that once a OGRFieldDefn has been added to a layer definition with
2071
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
2072
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
2073
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
2074
 * OGRFieldDefn, for drivers that support AlterFieldDefn().
2075
 *
2076
 * @param osComment Field comment.
2077
 * @since GDAL 3.7
2078
 */
2079
void OGRFieldDefn::SetComment(const std::string &osComment)
2080
0
{
2081
0
    if (m_bSealed)
2082
0
    {
2083
0
        CPLError(CE_Failure, CPLE_AppDefined,
2084
0
                 "OGRFieldDefn::SetComment() not allowed on a sealed object");
2085
0
        return;
2086
0
    }
2087
0
    m_osComment = osComment;
2088
0
}
2089
2090
/************************************************************************/
2091
/*                         OGR_Fld_SetComment()                         */
2092
/************************************************************************/
2093
2094
/**
2095
 * \brief Set the comment for this field.
2096
 *
2097
 * This method is the same as the C++ method OGRFieldDefn::SetComment().
2098
 *
2099
 * Note that once a OGRFieldDefn has been added to a layer definition with
2100
 * OGRLayer::AddFieldDefn(), its setter methods should not be called on the
2101
 * object returned with OGRLayer::GetLayerDefn()->GetFieldDefn(). Instead,
2102
 * OGRLayer::AlterFieldDefn() should be called on a new instance of
2103
 * OGRFieldDefn, for drivers that support AlterFieldDefn().
2104
 *
2105
 * @param hDefn handle to the field definition
2106
 * @param pszComment Field comment.
2107
 * @since GDAL 3.7
2108
 */
2109
2110
void OGR_Fld_SetComment(OGRFieldDefnH hDefn, const char *pszComment)
2111
0
{
2112
0
    OGRFieldDefn::FromHandle(hDefn)->SetComment(pszComment ? pszComment : "");
2113
0
}
2114
2115
/************************************************************************/
2116
/*                         OGRUpdateFieldType()                         */
2117
/************************************************************************/
2118
2119
/**
2120
 * \brief Update the type of a field definition by "merging" its existing type
2121
 * with a new type.
2122
 *
2123
 * The update is done such as broadening the type. For example a OFTInteger
2124
 * updated with OFTInteger64 will be promoted to OFTInteger64.
2125
 *
2126
 * @param poFDefn the field definition whose type must be updated.
2127
 * @param eNewType the new field type to merge into the existing type.
2128
 * @param eNewSubType the new field subtype to merge into the existing subtype.
2129
 */
2130
2131
void OGRUpdateFieldType(OGRFieldDefn *poFDefn, OGRFieldType eNewType,
2132
                        OGRFieldSubType eNewSubType)
2133
0
{
2134
0
    OGRFieldType eType = poFDefn->GetType();
2135
0
    if (eType == OFTInteger)
2136
0
    {
2137
0
        if (eNewType == OFTInteger && poFDefn->GetSubType() == OFSTBoolean &&
2138
0
            eNewSubType != OFSTBoolean)
2139
0
        {
2140
0
            poFDefn->SetSubType(OFSTNone);
2141
0
        }
2142
0
        else if (eNewType == OFTInteger64 || eNewType == OFTReal)
2143
0
        {
2144
0
            poFDefn->SetSubType(OFSTNone);
2145
0
            poFDefn->SetType(eNewType);
2146
0
        }
2147
0
        else if (eNewType == OFTIntegerList || eNewType == OFTInteger64List ||
2148
0
                 eNewType == OFTRealList || eNewType == OFTStringList)
2149
0
        {
2150
0
            if (eNewType != OFTIntegerList || eNewSubType != OFSTBoolean)
2151
0
                poFDefn->SetSubType(OFSTNone);
2152
0
            poFDefn->SetType(eNewType);
2153
0
        }
2154
0
        else if (eNewType != OFTInteger)
2155
0
        {
2156
0
            poFDefn->SetSubType(OFSTNone);
2157
0
            poFDefn->SetType(OFTString);
2158
0
        }
2159
0
    }
2160
0
    else if (eType == OFTInteger64)
2161
0
    {
2162
0
        if (eNewType == OFTReal)
2163
0
        {
2164
0
            poFDefn->SetSubType(OFSTNone);
2165
0
            poFDefn->SetType(eNewType);
2166
0
        }
2167
0
        else if (eNewType == OFTIntegerList)
2168
0
        {
2169
0
            poFDefn->SetSubType(OFSTNone);
2170
0
            poFDefn->SetType(OFTInteger64List);
2171
0
        }
2172
0
        else if (eNewType == OFTInteger64List || eNewType == OFTRealList ||
2173
0
                 eNewType == OFTStringList)
2174
0
        {
2175
0
            poFDefn->SetSubType(OFSTNone);
2176
0
            poFDefn->SetType(eNewType);
2177
0
        }
2178
0
        else if (eNewType != OFTInteger && eNewType != OFTInteger64)
2179
0
        {
2180
0
            poFDefn->SetSubType(OFSTNone);
2181
0
            poFDefn->SetType(OFTString);
2182
0
        }
2183
0
    }
2184
0
    else if (eType == OFTReal)
2185
0
    {
2186
0
        if (eNewType == OFTIntegerList || eNewType == OFTInteger64List ||
2187
0
            eNewType == OFTRealList)
2188
0
        {
2189
0
            poFDefn->SetType(OFTRealList);
2190
0
        }
2191
0
        else if (eNewType == OFTStringList)
2192
0
        {
2193
0
            poFDefn->SetType(OFTStringList);
2194
0
        }
2195
0
        else if (eNewType != OFTInteger && eNewType != OFTInteger64 &&
2196
0
                 eNewType != OFTReal)
2197
0
        {
2198
0
            poFDefn->SetSubType(OFSTNone);
2199
0
            poFDefn->SetType(OFTString);
2200
0
        }
2201
0
    }
2202
0
    else if (eType == OFTIntegerList)
2203
0
    {
2204
0
        if (eNewType == OFTIntegerList &&
2205
0
            poFDefn->GetSubType() == OFSTBoolean && eNewSubType != OFSTBoolean)
2206
0
        {
2207
0
            poFDefn->SetSubType(OFSTNone);
2208
0
        }
2209
0
        else if (eNewType == OFTInteger64 || eNewType == OFTInteger64List)
2210
0
        {
2211
0
            poFDefn->SetSubType(OFSTNone);
2212
0
            poFDefn->SetType(OFTInteger64List);
2213
0
        }
2214
0
        else if (eNewType == OFTReal || eNewType == OFTRealList)
2215
0
        {
2216
0
            poFDefn->SetSubType(OFSTNone);
2217
0
            poFDefn->SetType(OFTRealList);
2218
0
        }
2219
0
        else if (eNewType != OFTInteger && eNewType != OFTIntegerList)
2220
0
        {
2221
0
            poFDefn->SetSubType(OFSTNone);
2222
0
            poFDefn->SetType(OFTStringList);
2223
0
        }
2224
0
    }
2225
0
    else if (eType == OFTInteger64List)
2226
0
    {
2227
0
        if (eNewType == OFTReal || eNewType == OFTRealList)
2228
0
            poFDefn->SetType(OFTRealList);
2229
0
        else if (eNewType != OFTInteger && eNewType != OFTInteger64 &&
2230
0
                 eNewType != OFTIntegerList && eNewType != OFTInteger64List)
2231
0
        {
2232
0
            poFDefn->SetSubType(OFSTNone);
2233
0
            poFDefn->SetType(OFTStringList);
2234
0
        }
2235
0
    }
2236
0
    else if (eType == OFTRealList)
2237
0
    {
2238
0
        if (eNewType != OFTInteger && eNewType != OFTInteger64 &&
2239
0
            eNewType != OFTReal && eNewType != OFTIntegerList &&
2240
0
            eNewType != OFTInteger64List && eNewType != OFTRealList)
2241
0
        {
2242
0
            poFDefn->SetSubType(OFSTNone);
2243
0
            poFDefn->SetType(OFTStringList);
2244
0
        }
2245
0
    }
2246
0
    else if (eType == OFTDateTime)
2247
0
    {
2248
0
        if (eNewType != OFTDateTime && eNewType != OFTDate)
2249
0
        {
2250
0
            poFDefn->SetType(OFTString);
2251
0
        }
2252
0
    }
2253
0
    else if (eType == OFTDate || eType == OFTTime)
2254
0
    {
2255
0
        if (eNewType == OFTDateTime)
2256
0
            poFDefn->SetType(OFTDateTime);
2257
0
        else if (eNewType != eType)
2258
0
            poFDefn->SetType(OFTString);
2259
0
    }
2260
0
    else if (eType == OFTString && eNewType == OFTStringList)
2261
0
    {
2262
0
        poFDefn->SetType(OFTStringList);
2263
0
    }
2264
0
}
2265
2266
/************************************************************************/
2267
/*                         OGRFieldDefn::Seal()                         */
2268
/************************************************************************/
2269
2270
/** Seal a OGRFieldDefn.
2271
 *
2272
 * A sealed OGRFieldDefn can not be modified while it is sealed.
2273
 *
2274
 * This method should only be called by driver implementations.
2275
 *
2276
 * @since GDAL 3.9
2277
 */
2278
void OGRFieldDefn::Seal()
2279
0
{
2280
0
    m_bSealed = true;
2281
0
}
2282
2283
/************************************************************************/
2284
/*                        OGRFieldDefn::Unseal()                        */
2285
/************************************************************************/
2286
2287
/** Unseal a OGRFieldDefn.
2288
 *
2289
 * Undo OGRFieldDefn::Seal()
2290
 *
2291
 * Using GetTemporaryUnsealer() is recommended for most use cases.
2292
 *
2293
 * This method should only be called by driver implementations.
2294
 *
2295
 * @since GDAL 3.9
2296
 */
2297
void OGRFieldDefn::Unseal()
2298
0
{
2299
0
    m_bSealed = false;
2300
0
}
2301
2302
/************************************************************************/
2303
/*                 OGRFieldDefn::GetTemporaryUnsealer()                 */
2304
/************************************************************************/
2305
2306
/** Return an object that temporary unseals the OGRFieldDefn
2307
 *
2308
 * The returned object calls Unseal() initially, and when it is destroyed
2309
 * it calls Seal().
2310
 *
2311
 * This method should only be called by driver implementations.
2312
 *
2313
 * It is also possible to use the helper method whileUnsealing(). Example:
2314
 * whileUnsealing(poFieldDefn)->some_method()
2315
 *
2316
 * @since GDAL 3.9
2317
 */
2318
OGRFieldDefn::TemporaryUnsealer OGRFieldDefn::GetTemporaryUnsealer()
2319
0
{
2320
0
    return TemporaryUnsealer(this);
2321
0
}
2322
2323
/************************************************************************/
2324
/*                           OGRFieldDomain()                           */
2325
/************************************************************************/
2326
2327
/*! @cond Doxygen_Suppress */
2328
OGRFieldDomain::OGRFieldDomain(const std::string &osName,
2329
                               const std::string &osDescription,
2330
                               OGRFieldDomainType eDomainType,
2331
                               OGRFieldType eFieldType,
2332
                               OGRFieldSubType eFieldSubType)
2333
0
    : m_osName(osName), m_osDescription(osDescription),
2334
0
      m_eDomainType(eDomainType), m_eFieldType(eFieldType),
2335
0
      m_eFieldSubType(eFieldSubType)
2336
0
{
2337
0
}
2338
2339
/*! @endcond */
2340
2341
/************************************************************************/
2342
/*                          ~OGRFieldDomain()                           */
2343
/************************************************************************/
2344
2345
0
OGRFieldDomain::~OGRFieldDomain() = default;
2346
2347
/************************************************************************/
2348
/*                          ~OGRFieldDomain()                           */
2349
/************************************************************************/
2350
2351
/** Destroy a field domain.
2352
 *
2353
 * This is the same as the C++ method OGRFieldDomain::~OGRFieldDomain()
2354
 *
2355
 * @param hFieldDomain the field domain.
2356
 * @since GDAL 3.3
2357
 */
2358
void OGR_FldDomain_Destroy(OGRFieldDomainH hFieldDomain)
2359
0
{
2360
0
    delete OGRFieldDomain::FromHandle(hFieldDomain);
2361
0
}
2362
2363
/************************************************************************/
2364
/*                        OGRCodedFieldDomain()                         */
2365
/************************************************************************/
2366
2367
OGRCodedFieldDomain::OGRCodedFieldDomain(const std::string &osName,
2368
                                         const std::string &osDescription,
2369
                                         OGRFieldType eFieldType,
2370
                                         OGRFieldSubType eFieldSubType,
2371
                                         std::vector<OGRCodedValue> &&asValues)
2372
0
    : OGRFieldDomain(osName, osDescription, OFDT_CODED, eFieldType,
2373
0
                     eFieldSubType),
2374
0
      m_asValues(std::move(asValues))
2375
0
{
2376
    // Guard
2377
0
    if (m_asValues.empty() || m_asValues.back().pszCode != nullptr)
2378
0
    {
2379
0
        OGRCodedValue cv;
2380
0
        cv.pszCode = nullptr;
2381
0
        cv.pszValue = nullptr;
2382
0
        m_asValues.emplace_back(cv);
2383
0
    }
2384
0
}
2385
2386
/************************************************************************/
2387
/*                     OGR_CodedFldDomain_Create()                      */
2388
/************************************************************************/
2389
2390
/** Creates a new coded field domain.
2391
 *
2392
 * This is the same as the C++ method OGRCodedFieldDomain::OGRCodedFieldDomain()
2393
 * (except that the C function copies the enumeration, whereas the C++
2394
 * method moves it)
2395
 *
2396
 * @param pszName        Domain name. Should not be NULL.
2397
 * @param pszDescription Domain description (can be NULL)
2398
 * @param eFieldType     Field type. Generally numeric. Potentially OFTDateTime
2399
 * @param eFieldSubType  Field subtype.
2400
 * @param enumeration    Enumeration as (code, value) pairs. Should not be
2401
 *                       NULL. The end of the enumeration is marked by a code
2402
 *                       set to NULL. The enumeration will be copied.
2403
 *                       Each code should appear only once, but it is the
2404
 *                       responsibility of the user to check it.
2405
 * @return a new handle that should be freed with OGR_FldDomain_Destroy(),
2406
 *         or NULL in case of error.
2407
 * @since GDAL 3.3
2408
 */
2409
OGRFieldDomainH OGR_CodedFldDomain_Create(const char *pszName,
2410
                                          const char *pszDescription,
2411
                                          OGRFieldType eFieldType,
2412
                                          OGRFieldSubType eFieldSubType,
2413
                                          const OGRCodedValue *enumeration)
2414
0
{
2415
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
2416
0
    VALIDATE_POINTER1(enumeration, __func__, nullptr);
2417
0
    size_t count = 0;
2418
0
    for (int i = 0; enumeration[i].pszCode != nullptr; ++i)
2419
0
    {
2420
0
        ++count;
2421
0
    }
2422
0
    std::vector<OGRCodedValue> asValues;
2423
0
    try
2424
0
    {
2425
0
        asValues.reserve(count + 1);
2426
0
    }
2427
0
    catch (const std::exception &e)
2428
0
    {
2429
0
        CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what());
2430
0
        return nullptr;
2431
0
    }
2432
0
    bool error = false;
2433
0
    for (int i = 0; enumeration[i].pszCode != nullptr; ++i)
2434
0
    {
2435
0
        OGRCodedValue cv;
2436
0
        cv.pszCode = VSI_STRDUP_VERBOSE(enumeration[i].pszCode);
2437
0
        if (cv.pszCode == nullptr)
2438
0
        {
2439
0
            error = true;
2440
0
            break;
2441
0
        }
2442
0
        if (enumeration[i].pszValue)
2443
0
        {
2444
0
            cv.pszValue = VSI_STRDUP_VERBOSE(enumeration[i].pszValue);
2445
0
            if (cv.pszValue == nullptr)
2446
0
            {
2447
0
                VSIFree(cv.pszCode);
2448
0
                error = true;
2449
0
                break;
2450
0
            }
2451
0
        }
2452
0
        else
2453
0
        {
2454
0
            cv.pszValue = nullptr;
2455
0
        }
2456
0
        asValues.emplace_back(cv);
2457
0
    }
2458
0
    if (error)
2459
0
    {
2460
0
        for (auto &cv : asValues)
2461
0
        {
2462
0
            VSIFree(cv.pszCode);
2463
0
            VSIFree(cv.pszValue);
2464
0
        }
2465
0
        return nullptr;
2466
0
    }
2467
    // Add guard
2468
0
    {
2469
0
        OGRCodedValue cv;
2470
0
        cv.pszCode = nullptr;
2471
0
        cv.pszValue = nullptr;
2472
0
        asValues.emplace_back(cv);
2473
0
    }
2474
0
    return OGRFieldDomain::ToHandle(new OGRCodedFieldDomain(
2475
0
        pszName, pszDescription ? pszDescription : "", eFieldType,
2476
0
        eFieldSubType, std::move(asValues)));
2477
0
}
2478
2479
/************************************************************************/
2480
/*                     OGRCodedFieldDomain::Clone()                     */
2481
/************************************************************************/
2482
2483
OGRCodedFieldDomain *OGRCodedFieldDomain::Clone() const
2484
0
{
2485
0
    auto poDomain = cpl::down_cast<OGRCodedFieldDomain *>(
2486
0
        OGRFieldDomain::FromHandle(OGR_CodedFldDomain_Create(
2487
0
            m_osName.c_str(), m_osDescription.c_str(), m_eFieldType,
2488
0
            m_eFieldSubType, m_asValues.data())));
2489
0
    poDomain->SetMergePolicy(m_eMergePolicy);
2490
0
    poDomain->SetSplitPolicy(m_eSplitPolicy);
2491
0
    return poDomain;
2492
0
}
2493
2494
/************************************************************************/
2495
/*                        ~OGRCodedFieldDomain()                        */
2496
/************************************************************************/
2497
2498
OGRCodedFieldDomain::~OGRCodedFieldDomain()
2499
0
{
2500
0
    for (auto &cv : m_asValues)
2501
0
    {
2502
0
        CPLFree(cv.pszCode);
2503
0
        CPLFree(cv.pszValue);
2504
0
    }
2505
0
}
2506
2507
/************************************************************************/
2508
/*                        OGRRangeFieldDomain()                         */
2509
/************************************************************************/
2510
2511
// cppcheck-suppress uninitMemberVar
2512
OGRRangeFieldDomain::OGRRangeFieldDomain(
2513
    const std::string &osName, const std::string &osDescription,
2514
    OGRFieldType eFieldType, OGRFieldSubType eFieldSubType,
2515
    const OGRField &sMin, bool bMinIsInclusive, const OGRField &sMax,
2516
    bool bMaxIsInclusive)
2517
0
    : OGRFieldDomain(osName, osDescription, OFDT_RANGE, eFieldType,
2518
0
                     eFieldSubType),
2519
0
      m_sMin(sMin), m_sMax(sMax), m_bMinIsInclusive(bMinIsInclusive),
2520
0
      m_bMaxIsInclusive(bMaxIsInclusive)
2521
0
{
2522
0
    CPLAssert(eFieldType == OFTInteger || eFieldType == OFTInteger64 ||
2523
0
              eFieldType == OFTReal || eFieldType == OFTDateTime);
2524
0
}
2525
2526
/************************************************************************/
2527
/*                     OGR_RangeFldDomain_Create()                      */
2528
/************************************************************************/
2529
2530
static OGRField GetUnsetField()
2531
0
{
2532
0
    OGRField sUnset;
2533
0
    OGR_RawField_SetUnset(&sUnset);
2534
0
    return sUnset;
2535
0
}
2536
2537
/** Creates a new range field domain.
2538
 *
2539
 * This is the same as the C++ method
2540
 * OGRRangeFieldDomain::OGRRangeFieldDomain().
2541
 *
2542
 * @param pszName        Domain name. Should not be NULL.
2543
 * @param pszDescription Domain description (can be NULL)
2544
 * @param eFieldType     Field type. Among OFTInteger, OFTInteger64, OFTReal
2545
 *                       and OFTDateTime.
2546
 * @param eFieldSubType  Field subtype.
2547
 * @param psMin          Minimum value (can be NULL). The member in the union
2548
 *                       that is read is consistent with eFieldType
2549
 * @param bMinIsInclusive Whether the minimum value is included in the range.
2550
 * @param psMax          Maximum value (can be NULL). The member in the union
2551
 *                       that is read is consistent with eFieldType
2552
 * @param bMaxIsInclusive Whether the maximum value is included in the range.
2553
 * @return a new handle that should be freed with OGR_FldDomain_Destroy()
2554
 * @since GDAL 3.3
2555
 */
2556
OGRFieldDomainH OGR_RangeFldDomain_Create(
2557
    const char *pszName, const char *pszDescription, OGRFieldType eFieldType,
2558
    OGRFieldSubType eFieldSubType, const OGRField *psMin, bool bMinIsInclusive,
2559
    const OGRField *psMax, bool bMaxIsInclusive)
2560
0
{
2561
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
2562
0
    if (eFieldType != OFTInteger && eFieldType != OFTInteger64 &&
2563
0
        eFieldType != OFTReal && eFieldType != OFTDateTime)
2564
0
    {
2565
0
        CPLError(CE_Failure, CPLE_NotSupported, "Unsupported field type");
2566
0
        return nullptr;
2567
0
    }
2568
0
    const OGRField unsetField = GetUnsetField();
2569
0
    return OGRFieldDomain::ToHandle(new OGRRangeFieldDomain(
2570
0
        pszName, pszDescription ? pszDescription : "", eFieldType,
2571
0
        eFieldSubType, psMin ? *psMin : unsetField, bMinIsInclusive,
2572
0
        psMax ? *psMax : unsetField, bMaxIsInclusive));
2573
0
}
2574
2575
/************************************************************************/
2576
/*                         OGRGlobFieldDomain()                         */
2577
/************************************************************************/
2578
2579
OGRGlobFieldDomain::OGRGlobFieldDomain(const std::string &osName,
2580
                                       const std::string &osDescription,
2581
                                       OGRFieldType eFieldType,
2582
                                       OGRFieldSubType eFieldSubType,
2583
                                       const std::string &osGlob)
2584
0
    : OGRFieldDomain(osName, osDescription, OFDT_GLOB, eFieldType,
2585
0
                     eFieldSubType),
2586
0
      m_osGlob(osGlob)
2587
0
{
2588
0
}
2589
2590
/************************************************************************/
2591
/*                      OGR_GlobFldDomain_Create()                      */
2592
/************************************************************************/
2593
2594
/** Creates a new glob field domain.
2595
 *
2596
 * This is the same as the C++ method OGRGlobFieldDomain::OGRGlobFieldDomain()
2597
 *
2598
 * @param pszName        Domain name. Should not be NULL.
2599
 * @param pszDescription Domain description (can be NULL)
2600
 * @param eFieldType     Field type.
2601
 * @param eFieldSubType  Field subtype.
2602
 * @param pszGlob        Glob expression. Should not be NULL.
2603
 * @return a new handle that should be freed with OGR_FldDomain_Destroy()
2604
 * @since GDAL 3.3
2605
 */
2606
OGRFieldDomainH OGR_GlobFldDomain_Create(const char *pszName,
2607
                                         const char *pszDescription,
2608
                                         OGRFieldType eFieldType,
2609
                                         OGRFieldSubType eFieldSubType,
2610
                                         const char *pszGlob)
2611
0
{
2612
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
2613
0
    VALIDATE_POINTER1(pszGlob, __func__, nullptr);
2614
0
    return OGRFieldDomain::ToHandle(
2615
0
        new OGRGlobFieldDomain(pszName, pszDescription ? pszDescription : "",
2616
0
                               eFieldType, eFieldSubType, pszGlob));
2617
0
}
2618
2619
/************************************************************************/
2620
/*                       OGR_FldDomain_GetName()                        */
2621
/************************************************************************/
2622
2623
/** Get the name of the field domain.
2624
 *
2625
 * This is the same as the C++ method OGRFieldDomain::GetName()
2626
 *
2627
 * @param hFieldDomain Field domain handle.
2628
 * @return the field domain name.
2629
 * @since GDAL 3.3
2630
 */
2631
const char *OGR_FldDomain_GetName(OGRFieldDomainH hFieldDomain)
2632
0
{
2633
0
    return OGRFieldDomain::FromHandle(hFieldDomain)->GetName().c_str();
2634
0
}
2635
2636
/************************************************************************/
2637
/*                    OGR_FldDomain_GetDescription()                    */
2638
/************************************************************************/
2639
2640
/** Get the description of the field domain.
2641
 *
2642
 * This is the same as the C++ method OGRFieldDomain::GetDescription()
2643
 *
2644
 * @param hFieldDomain Field domain handle.
2645
 * @return the field domain description (might be empty string).
2646
 * @since GDAL 3.3
2647
 */
2648
const char *OGR_FldDomain_GetDescription(OGRFieldDomainH hFieldDomain)
2649
0
{
2650
0
    return OGRFieldDomain::FromHandle(hFieldDomain)->GetDescription().c_str();
2651
0
}
2652
2653
/************************************************************************/
2654
/*                    OGR_FldDomain_GetDomainType()                     */
2655
/************************************************************************/
2656
2657
/** Get the type of the field domain.
2658
 *
2659
 * This is the same as the C++ method OGRFieldDomain::GetDomainType()
2660
 *
2661
 * @param hFieldDomain Field domain handle.
2662
 * @return the type of the field domain.
2663
 * @since GDAL 3.3
2664
 */
2665
OGRFieldDomainType OGR_FldDomain_GetDomainType(OGRFieldDomainH hFieldDomain)
2666
0
{
2667
0
    return OGRFieldDomain::FromHandle(hFieldDomain)->GetDomainType();
2668
0
}
2669
2670
/************************************************************************/
2671
/*                     OGR_FldDomain_GetFieldType()                     */
2672
/************************************************************************/
2673
2674
/** Get the field type of the field domain.
2675
 *
2676
 * This is the same as the C++ method OGRFieldDomain::GetFieldType()
2677
 *
2678
 * @param hFieldDomain Field domain handle.
2679
 * @return the field type of the field domain.
2680
 * @since GDAL 3.3
2681
 */
2682
OGRFieldType OGR_FldDomain_GetFieldType(OGRFieldDomainH hFieldDomain)
2683
0
{
2684
0
    return OGRFieldDomain::FromHandle(hFieldDomain)->GetFieldType();
2685
0
}
2686
2687
/************************************************************************/
2688
/*                   OGR_FldDomain_GetFieldSubType()                    */
2689
/************************************************************************/
2690
2691
/** Get the field subtype of the field domain.
2692
 *
2693
 * This is the same as OGRFieldDomain::GetFieldSubType()
2694
 *
2695
 * @param hFieldDomain Field domain handle.
2696
 * @return the field subtype of the field domain.
2697
 * @since GDAL 3.3
2698
 */
2699
OGRFieldSubType OGR_FldDomain_GetFieldSubType(OGRFieldDomainH hFieldDomain)
2700
0
{
2701
0
    return OGRFieldDomain::FromHandle(hFieldDomain)->GetFieldSubType();
2702
0
}
2703
2704
/************************************************************************/
2705
/*                    OGR_FldDomain_GetSplitPolicy()                    */
2706
/************************************************************************/
2707
2708
/** Get the split policy of the field domain.
2709
 *
2710
 * This is the same as the C++ method OGRFieldDomain::GetSplitPolicy()
2711
 *
2712
 * @param hFieldDomain Field domain handle.
2713
 * @return the split policy of the field domain.
2714
 * @since GDAL 3.3
2715
 */
2716
2717
OGRFieldDomainSplitPolicy
2718
OGR_FldDomain_GetSplitPolicy(OGRFieldDomainH hFieldDomain)
2719
0
{
2720
0
    return OGRFieldDomain::FromHandle(hFieldDomain)->GetSplitPolicy();
2721
0
}
2722
2723
/************************************************************************/
2724
/*                    OGR_FldDomain_SetSplitPolicy()                    */
2725
/************************************************************************/
2726
2727
/** Set the split policy of the field domain.
2728
 *
2729
 * This is the same as the C++ method OGRFieldDomain::SetSplitPolicy()
2730
 *
2731
 * @param hFieldDomain Field domain handle.
2732
 * @param policy the split policy of the field domain.
2733
 * @since GDAL 3.3
2734
 */
2735
2736
void OGR_FldDomain_SetSplitPolicy(OGRFieldDomainH hFieldDomain,
2737
                                  OGRFieldDomainSplitPolicy policy)
2738
0
{
2739
0
    OGRFieldDomain::FromHandle(hFieldDomain)->SetSplitPolicy(policy);
2740
0
}
2741
2742
/************************************************************************/
2743
/*                    OGR_FldDomain_GetMergePolicy()                    */
2744
/************************************************************************/
2745
2746
/** Get the merge policy of the field domain.
2747
 *
2748
 * This is the same as the C++ method OGRFieldDomain::GetMergePolicy()
2749
 *
2750
 * @param hFieldDomain Field domain handle.
2751
 * @return the merge policy of the field domain.
2752
 * @since GDAL 3.3
2753
 */
2754
2755
OGRFieldDomainMergePolicy
2756
OGR_FldDomain_GetMergePolicy(OGRFieldDomainH hFieldDomain)
2757
0
{
2758
0
    return OGRFieldDomain::FromHandle(hFieldDomain)->GetMergePolicy();
2759
0
}
2760
2761
/************************************************************************/
2762
/*                    OGR_FldDomain_SetMergePolicy()                    */
2763
/************************************************************************/
2764
2765
/** Set the merge policy of the field domain.
2766
 *
2767
 * This is the same as the C++ method OGRFieldDomain::SetMergePolicy()
2768
 *
2769
 * @param hFieldDomain Field domain handle.
2770
 * @param policy the merge policy of the field domain.
2771
 * @since GDAL 3.3
2772
 */
2773
2774
void OGR_FldDomain_SetMergePolicy(OGRFieldDomainH hFieldDomain,
2775
                                  OGRFieldDomainMergePolicy policy)
2776
0
{
2777
0
    OGRFieldDomain::FromHandle(hFieldDomain)->SetMergePolicy(policy);
2778
0
}
2779
2780
/************************************************************************/
2781
/*                 OGR_CodedFldDomain_GetEnumeration()                  */
2782
/************************************************************************/
2783
2784
/** Get the enumeration as (code, value) pairs.
2785
 *
2786
 * The end of the enumeration is signaled by code == NULL
2787
 *
2788
 * This is the same as the C++ method OGRCodedFieldDomain::GetEnumeration()
2789
 *
2790
 * @param hFieldDomain Field domain handle.
2791
 * @return the (code, value) pairs, or nullptr in case of error.
2792
 * @since GDAL 3.3
2793
 */
2794
const OGRCodedValue *
2795
OGR_CodedFldDomain_GetEnumeration(OGRFieldDomainH hFieldDomain)
2796
0
{
2797
    // The user should normally only call us with the right object type, but
2798
    // it doesn't hurt to check.
2799
0
    auto poFieldDomain = dynamic_cast<const OGRCodedFieldDomain *>(
2800
0
        OGRFieldDomain::FromHandle(hFieldDomain));
2801
0
    if (!poFieldDomain)
2802
0
    {
2803
0
        CPLError(
2804
0
            CE_Failure, CPLE_AppDefined,
2805
0
            "This function should be called with a coded field domain object");
2806
0
        return nullptr;
2807
0
    }
2808
0
    return poFieldDomain->GetEnumeration();
2809
0
}
2810
2811
/************************************************************************/
2812
/*                     OGR_RangeFldDomain_GetMin()                      */
2813
/************************************************************************/
2814
2815
/** Get the minimum value.
2816
 *
2817
 * Which member in the returned OGRField enum must be read depends on the field
2818
 * type.
2819
 *
2820
 * If no minimum value is set, the OGR_RawField_IsUnset() will return true when
2821
 * called on the result.
2822
 *
2823
 * This is the same as the C++ method OGRRangeFieldDomain::GetMin()
2824
 *
2825
 * @param hFieldDomain Field domain handle.
2826
 * @param pbIsInclusiveOut set to true if the minimum is included in the range.
2827
 * @return the minimum value.
2828
 * @since GDAL 3.3
2829
 */
2830
const OGRField *OGR_RangeFldDomain_GetMin(OGRFieldDomainH hFieldDomain,
2831
                                          bool *pbIsInclusiveOut)
2832
0
{
2833
    // The user should normally only call us with the right object type, but
2834
    // it doesn't hurt to check.
2835
0
    auto poFieldDomain = dynamic_cast<const OGRRangeFieldDomain *>(
2836
0
        OGRFieldDomain::FromHandle(hFieldDomain));
2837
0
    if (!poFieldDomain)
2838
0
    {
2839
0
        CPLError(
2840
0
            CE_Failure, CPLE_AppDefined,
2841
0
            "This function should be called with a range field domain object");
2842
0
        static const OGRField dummyField = GetUnsetField();
2843
0
        return &dummyField;
2844
0
    }
2845
0
    bool bIsInclusive = false;
2846
0
    const auto &ret = poFieldDomain->GetMin(bIsInclusive);
2847
0
    if (pbIsInclusiveOut)
2848
0
        *pbIsInclusiveOut = bIsInclusive;
2849
0
    return &ret;
2850
0
}
2851
2852
/************************************************************************/
2853
/*                     OGR_RangeFldDomain_GetMax()                      */
2854
/************************************************************************/
2855
2856
/** Get the maximum value.
2857
 *
2858
 * Which member in the returned OGRField enum must be read depends on the field
2859
 * type.
2860
 *
2861
 * If no maximum value is set, the OGR_RawField_IsUnset() will return true when
2862
 * called on the result.
2863
 *
2864
 * This is the same as the C++ method OGRRangeFieldDomain::GetMax()
2865
 *
2866
 * @param hFieldDomain Field domain handle.
2867
 * @param pbIsInclusiveOut set to true if the maximum is included in the range.
2868
 * @return the maximum value.
2869
 * @since GDAL 3.3
2870
 */
2871
const OGRField *OGR_RangeFldDomain_GetMax(OGRFieldDomainH hFieldDomain,
2872
                                          bool *pbIsInclusiveOut)
2873
0
{
2874
    // The user should normally only call us with the right object type, but
2875
    // it doesn't hurt to check.
2876
0
    auto poFieldDomain = dynamic_cast<const OGRRangeFieldDomain *>(
2877
0
        OGRFieldDomain::FromHandle(hFieldDomain));
2878
0
    if (!poFieldDomain)
2879
0
    {
2880
0
        CPLError(
2881
0
            CE_Failure, CPLE_AppDefined,
2882
0
            "This function should be called with a range field domain object");
2883
0
        static const OGRField dummyField = GetUnsetField();
2884
0
        return &dummyField;
2885
0
    }
2886
0
    bool bIsInclusive = false;
2887
0
    const auto &ret = poFieldDomain->GetMax(bIsInclusive);
2888
0
    if (pbIsInclusiveOut)
2889
0
        *pbIsInclusiveOut = bIsInclusive;
2890
0
    return &ret;
2891
0
}
2892
2893
/************************************************************************/
2894
/*                     OGR_GlobFldDomain_GetGlob()                      */
2895
/************************************************************************/
2896
2897
/** Get the glob expression.
2898
 *
2899
 * This is the same as the C++ method OGRGlobFieldDomain::GetGlob()
2900
 *
2901
 * @param hFieldDomain Field domain handle.
2902
 * @return the glob expression, or nullptr in case of error
2903
 * @since GDAL 3.3
2904
 */
2905
const char *OGR_GlobFldDomain_GetGlob(OGRFieldDomainH hFieldDomain)
2906
0
{
2907
    // The user should normally only call us with the right object type, but
2908
    // it doesn't hurt to check.
2909
0
    auto poFieldDomain = dynamic_cast<const OGRGlobFieldDomain *>(
2910
0
        OGRFieldDomain::FromHandle(hFieldDomain));
2911
0
    if (!poFieldDomain)
2912
0
    {
2913
0
        CPLError(
2914
0
            CE_Failure, CPLE_AppDefined,
2915
0
            "This function should be called with a glob field domain object");
2916
0
        return nullptr;
2917
0
    }
2918
0
    return poFieldDomain->GetGlob().c_str();
2919
0
}