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