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