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