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