Debian Codegraph @

ogr/ogrsf_frmts/oci/ogrociwritablelayer.cpp

raw
1 /******************************************************************************
2 *
3 * Project: Oracle Spatial Driver
4 * Purpose: Implementation of the OGROCIWritableLayer class. This provides
5 * some services for converting OGRGeometries into Oracle structures
6 * that is shared between OGROCITableLayer and OGROCILoaderLayer.
7 * Author: Frank Warmerdam, [email protected]
8 *
9 ******************************************************************************
10 * Copyright (c) 2002, Frank Warmerdam <[email protected]>
11 *
12 * SPDX-License-Identifier: MIT
13 ****************************************************************************/
14
15 #include "ogr_oci.h"
16 #include "cpl_conv.h"
17 #include "cpl_string.h"
18
19 #include <algorithm>
20
21 /************************************************************************/
22 /* OGROCIWritableLayer() */
23 /************************************************************************/
24
25 OGROCIWritableLayer::OGROCIWritableLayer(OGROCIDataSource *poDSIn)
26 : OGROCILayer(poDSIn)
27
28 {
29 nDimension =
30 std::clamp(atoi(CPLGetConfigOption("OCI_DEFAULT_DIM", "3")), 2, 3);
31 nSRID = -1;
32
33 nOrdinalCount = 0;
34 nOrdinalMax = 0;
35 padfOrdinals = nullptr;
36
37 nElemInfoCount = 0;
38 nElemInfoMax = 0;
39 panElemInfo = nullptr;
40
41 bLaunderColumnNames = TRUE;
42 bPreservePrecision = FALSE;
43 nDefaultStringSize = DEFAULT_STRING_SIZE;
44 bTruncationReported = FALSE;
45 poSRS = nullptr;
46
47 papszOptions = nullptr;
48 }
49
50 /************************************************************************/
51 /* ~OGROCIWritableLayer() */
52 /************************************************************************/
53
54 OGROCIWritableLayer::~OGROCIWritableLayer()
55
56 {
57 CPLFree(padfOrdinals);
58 CPLFree(panElemInfo);
59
60 CSLDestroy(papszOptions);
61 }
62
63 /************************************************************************/
64 /* PushOrdinal() */
65 /************************************************************************/
66
67 void OGROCIWritableLayer::PushOrdinal(double dfOrd)
68
69 {
70 if (nOrdinalCount == nOrdinalMax)
71 {
72 nOrdinalMax = nOrdinalMax * 2 + 100;
73 padfOrdinals =
74 (double *)CPLRealloc(padfOrdinals, sizeof(double) * nOrdinalMax);
75 }
76
77 padfOrdinals[nOrdinalCount++] = dfOrd;
78 }
79
80 /************************************************************************/
81 /* PushElemInfo() */
82 /************************************************************************/
83
84 void OGROCIWritableLayer::PushElemInfo(int nOffset, int nEType, int nInterp)
85
86 {
87 if (nElemInfoCount + 3 >= nElemInfoMax)
88 {
89 nElemInfoMax = nElemInfoMax * 2 + 100;
90 panElemInfo =
91 (int *)CPLRealloc(panElemInfo, sizeof(int) * nElemInfoMax);
92 }
93
94 panElemInfo[nElemInfoCount++] = nOffset;
95 panElemInfo[nElemInfoCount++] = nEType;
96 panElemInfo[nElemInfoCount++] = nInterp;
97 }
98
99 /************************************************************************/
100 /* TranslateElementGroup() */
101 /* */
102 /* Append one or more element groups to the existing element */
103 /* info and ordinates lists for the passed geometry. */
104 /************************************************************************/
105
106 OGRErr OGROCIWritableLayer::TranslateElementGroup(OGRGeometry *poGeometry)
107
108 {
109 switch (wkbFlatten(poGeometry->getGeometryType()))
110 {
111 case wkbPoint:
112 {
113 OGRPoint *poPoint = poGeometry->toPoint();
114
115 PushElemInfo(nOrdinalCount + 1, 1, 1);
116
117 PushOrdinal(poPoint->getX());
118 PushOrdinal(poPoint->getY());
119 if (nDimension == 3)
120 PushOrdinal(poPoint->getZ());
121
122 return OGRERR_NONE;
123 }
124
125 case wkbLineString:
126 {
127 OGRLineString *poLine = poGeometry->toLineString();
128 int iVert;
129
130 PushElemInfo(nOrdinalCount + 1, 2, 1);
131
132 for (iVert = 0; iVert < poLine->getNumPoints(); iVert++)
133 {
134 PushOrdinal(poLine->getX(iVert));
135 PushOrdinal(poLine->getY(iVert));
136 if (nDimension == 3)
137 PushOrdinal(poLine->getZ(iVert));
138 }
139 return OGRERR_NONE;
140 }
141
142 case wkbPolygon:
143 {
144 OGRPolygon *poPoly = poGeometry->toPolygon();
145 int iRing;
146
147 for (iRing = -1; iRing < poPoly->getNumInteriorRings(); iRing++)
148 {
149 OGRLinearRing *poRing;
150 int iVert;
151
152 if (iRing == -1)
153 poRing = poPoly->getExteriorRing();
154 else
155 poRing = poPoly->getInteriorRing(iRing);
156
157 if (iRing == -1)
158 PushElemInfo(nOrdinalCount + 1, 1003, 1);
159 else
160 PushElemInfo(nOrdinalCount + 1, 2003, 1);
161
162 if ((iRing == -1 && poRing->isClockwise()) ||
163 (iRing != -1 && !poRing->isClockwise()))
164 {
165 for (iVert = poRing->getNumPoints() - 1; iVert >= 0;
166 iVert--)
167 {
168 PushOrdinal(poRing->getX(iVert));
169 PushOrdinal(poRing->getY(iVert));
170 if (nDimension == 3)
171 PushOrdinal(poRing->getZ(iVert));
172 }
173 }
174 else
175 {
176 for (iVert = 0; iVert < poRing->getNumPoints(); iVert++)
177 {
178 PushOrdinal(poRing->getX(iVert));
179 PushOrdinal(poRing->getY(iVert));
180 if (nDimension == 3)
181 PushOrdinal(poRing->getZ(iVert));
182 }
183 }
184 }
185
186 return OGRERR_NONE;
187 }
188
189 default:
190 {
191 return OGRERR_FAILURE;
192 }
193 }
194 }
195
196 /************************************************************************/
197 /* ReportTruncation() */
198 /************************************************************************/
199
200 void OGROCIWritableLayer::ReportTruncation(OGRFieldDefn *psFldDefn)
201
202 {
203 if (bTruncationReported)
204 return;
205
206 CPLError(CE_Warning, CPLE_AppDefined,
207 "The value for the field %s is being truncated to fit the\n"
208 "declared width/precision of the field. No more truncations\n"
209 "for table %s will be reported.",
210 psFldDefn->GetNameRef(), poFeatureDefn->GetName());
211
212 bTruncationReported = TRUE;
213 }
214
215 /************************************************************************/
216 /* SetOptions() */
217 /* */
218 /* Set layer creation or other options. */
219 /************************************************************************/
220
221 void OGROCIWritableLayer::SetOptions(CSLConstList papszOptionsIn)
222
223 {
224 CSLDestroy(papszOptions);
225 papszOptions = CSLDuplicate(papszOptionsIn);
226 }
227
228 /************************************************************************/
229 /* CreateField() */
230 /************************************************************************/
231
232 OGRErr OGROCIWritableLayer::CreateField(const OGRFieldDefn *poFieldIn,
233 int bApproxOK)
234
235 {
236 OGROCISession *poSession = poDS->GetSession();
237 char szFieldType[256];
238 char szFieldName[128]; // 12.2 max identifier name
239 OGRFieldDefn oField(poFieldIn);
240
241 /* -------------------------------------------------------------------- */
242 /* Do we want to "launder" the column names into Oracle */
243 /* friendly format? */
244 /* -------------------------------------------------------------------- */
245 if (bLaunderColumnNames)
246 {
247 char *pszSafeName = CPLStrdup(oField.GetNameRef());
248
249 poSession->CleanName(pszSafeName);
250 oField.SetName(pszSafeName);
251 CPLFree(pszSafeName);
252 }
253
254 /* -------------------------------------------------------------------- */
255 /* Work out the Oracle type. */
256 /* -------------------------------------------------------------------- */
257 if (oField.GetType() == OFTInteger)
258 {
259 if (bPreservePrecision && oField.GetWidth() != 0)
260 snprintf(szFieldType, sizeof(szFieldType), "NUMBER(%d)",
261 oField.GetWidth());
262 else
263 strcpy(szFieldType, "INTEGER");
264 }
265 else if (oField.GetType() == OFTInteger64)
266 {
267 if (bPreservePrecision && oField.GetWidth() != 0)
268 snprintf(szFieldType, sizeof(szFieldType), "NUMBER(%d)",
269 oField.GetWidth());
270 else
271 strcpy(szFieldType, "NUMBER(20)");
272 }
273 else if (oField.GetType() == OFTReal)
274 {
275 if (bPreservePrecision && oField.GetWidth() != 0)
276 snprintf(szFieldType, sizeof(szFieldType), "NUMBER(%d,%d)",
277 oField.GetWidth(), oField.GetPrecision());
278 else
279 strcpy(szFieldType, "FLOAT(126)");
280 }
281 else if (oField.GetType() == OFTString)
282 {
283 if (oField.GetWidth() == 0 || !bPreservePrecision)
284 snprintf(szFieldType, sizeof(szFieldType), "VARCHAR2(%d)",
285 nDefaultStringSize);
286 else
287 snprintf(szFieldType, sizeof(szFieldType), "VARCHAR2(%d CHAR)",
288 oField.GetWidth());
289 }
290 else if (oField.GetType() == OFTDate)
291 {
292 snprintf(szFieldType, sizeof(szFieldType), "DATE");
293 }
294 else if (oField.GetType() == OFTDateTime)
295 {
296 const char *pszTIMESTAMP_WITH_TIME_ZONE =
297 CSLFetchNameValue(papszOptions, "TIMESTAMP_WITH_TIME_ZONE");
298 if ((!pszTIMESTAMP_WITH_TIME_ZONE &&
299 oField.GetTZFlag() >= OGR_TZFLAG_MIXED_TZ) ||
300 (pszTIMESTAMP_WITH_TIME_ZONE &&
301 CPLTestBool(pszTIMESTAMP_WITH_TIME_ZONE)))
302 {
303 setFieldIndexWithTimeStampWithTZ.insert(
304 poFeatureDefn->GetFieldCount());
305 snprintf(szFieldType, sizeof(szFieldType),
306 "TIMESTAMP(3) WITH TIME ZONE");
307 }
308 else
309 snprintf(szFieldType, sizeof(szFieldType), "TIMESTAMP(3)");
310 }
311 else if (bApproxOK)
312 {
313 oField.SetDefault(nullptr);
314 CPLError(CE_Warning, CPLE_NotSupported,
315 "Can't create field %s with type %s on Oracle layers. "
316 "Creating as VARCHAR.",
317 oField.GetNameRef(),
318 OGRFieldDefn::GetFieldTypeName(oField.GetType()));
319 snprintf(szFieldType, sizeof(szFieldType), "VARCHAR2(%d)",
320 nDefaultStringSize);
321 }
322 else
323 {
324 CPLError(CE_Failure, CPLE_NotSupported,
325 "Can't create field %s with type %s on Oracle layers.",
326 oField.GetNameRef(),
327 OGRFieldDefn::GetFieldTypeName(oField.GetType()));
328
329 return OGRERR_FAILURE;
330 }
331
332 /* -------------------------------------------------------------------- */
333 /* Create the new field. */
334 /* -------------------------------------------------------------------- */
335 OGROCIStringBuf oCommand;
336 OGROCIStatement oAddField(poSession);
337
338 const int nCommandSize = static_cast<int>(
339 70 + strlen(poFeatureDefn->GetName()) + strlen(oField.GetNameRef()) +
340 strlen(szFieldType) +
341 (oField.GetDefault() ? strlen(oField.GetDefault()) : 0));
342 oCommand.MakeRoomFor(nCommandSize);
343
344 snprintf(szFieldName, sizeof(szFieldName), "%s", oField.GetNameRef());
345 szFieldName[sizeof(szFieldName) - 1] = '\0';
346 if (strlen(oField.GetNameRef()) > sizeof(szFieldName))
347 {
348 szFieldName[sizeof(szFieldName) - 1] = '_';
349 CPLError(CE_Warning, CPLE_AppDefined,
350 "Column %s is too long (at most 30 characters). Using %s.",
351 oField.GetNameRef(), szFieldName);
352 oField.SetName(szFieldName);
353 }
354 snprintf(oCommand.GetString(), nCommandSize, "ALTER TABLE %s ADD \"%s\" %s",
355 poFeatureDefn->GetName(), szFieldName, szFieldType);
356 if (oField.GetDefault() != nullptr && !oField.IsDefaultDriverSpecific())
357 {
358 snprintf(oCommand.GetString() + strlen(oCommand.GetString()),
359 nCommandSize - strlen(oCommand.GetString()), " DEFAULT %s",
360 oField.GetDefault());
361 }
362 if (!oField.IsNullable())
363 strcat(oCommand.GetString(), " NOT NULL");
364
365 if (oAddField.Execute(oCommand.GetString()) != CE_None)
366 return OGRERR_FAILURE;
367
368 poFeatureDefn->AddFieldDefn(&oField);
369
370 return OGRERR_NONE;
371 }
372
373 /************************************************************************/
374 /* SetDimension() */
375 /************************************************************************/
376
377 void OGROCIWritableLayer::SetDimension(int nNewDim)
378
379 {
380 nDimension = nNewDim;
381 }
382
383 /************************************************************************/
384 /* ParseDIMINFO() */
385 /************************************************************************/
386
387 void OGROCIWritableLayer::ParseDIMINFO(const char *pszOptionName,
388 double *pdfMin, double *pdfMax,
389 double *pdfRes)
390
391 {
392 const char *pszUserDIMINFO;
393 char **papszTokens;
394
395 pszUserDIMINFO = CSLFetchNameValue(papszOptions, pszOptionName);
396 if (pszUserDIMINFO == nullptr)
397 return;
398
399 papszTokens = CSLTokenizeStringComplex(pszUserDIMINFO, ",", FALSE, FALSE);
400 if (CSLCount(papszTokens) != 3)
401 {
402 CSLDestroy(papszTokens);
403 CPLError(
404 CE_Warning, CPLE_AppDefined,
405 "Ignoring %s, it does not contain three comma separated values.",
406 pszOptionName);
407 return;
408 }
409
410 *pdfMin = CPLAtof(papszTokens[0]);
411 *pdfMax = CPLAtof(papszTokens[1]);
412 *pdfRes = CPLAtof(papszTokens[2]);
413
414 CSLDestroy(papszTokens);
415 }
416
417 /************************************************************************/
418 /* TranslateToSDOGeometry() */
419 /************************************************************************/
420
421 OGRErr OGROCIWritableLayer::TranslateToSDOGeometry(OGRGeometry *poGeometry,
422 int *pnGType)
423
424 {
425 nOrdinalCount = 0;
426 nElemInfoCount = 0;
427
428 if (poGeometry == nullptr)
429 return OGRERR_FAILURE;
430
431 /* ==================================================================== */
432 /* Handle a point geometry. */
433 /* ==================================================================== */
434 if (wkbFlatten(poGeometry->getGeometryType()) == wkbPoint)
435 {
436 #ifdef notdef
437 char szResult[1024];
438 OGRPoint *poPoint = poGeometry->toPoint();
439
440 if (nDimension == 2)
441 CPLsprintf(
442 szResult,
443 "%s(%d,%s,MDSYS.SDO_POINT_TYPE(%.16g,%.16g,0.0),NULL,NULL)",
444 SDO_GEOMETRY, 2001, szSRID, poPoint->getX(), poPoint->getY());
445 else
446 CPLsprintf(
447 szResult,
448 "%s(%d,%s,MDSYS.SDO_POINT_TYPE(%.16g,%.16g,%.16g),NULL,NULL)",
449 SDO_GEOMETRY, 3001, szSRID, poPoint->getX(), poPoint->getY(),
450 poPoint->getZ());
451
452 return CPLStrdup(szResult);
453 #endif
454 }
455
456 /* ==================================================================== */
457 /* Handle a line string geometry. */
458 /* ==================================================================== */
459 else if (wkbFlatten(poGeometry->getGeometryType()) == wkbLineString)
460 {
461 *pnGType = nDimension * 1000 + 2;
462 TranslateElementGroup(poGeometry);
463 return OGRERR_NONE;
464 }
465
466 /* ==================================================================== */
467 /* Handle a polygon geometry. */
468 /* ==================================================================== */
469 else if (wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon)
470 {
471 *pnGType = nDimension == 2 ? 2003 : 3003;
472 TranslateElementGroup(poGeometry);
473 return OGRERR_NONE;
474 }
475
476 /* ==================================================================== */
477 /* Handle a multi point geometry. */
478 /* ==================================================================== */
479 else if (wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPoint)
480 {
481 OGRMultiPoint *poMP = poGeometry->toMultiPoint();
482
483 *pnGType = nDimension * 1000 + 5;
484 PushElemInfo(1, 1, poMP->getNumGeometries());
485
486 for (auto &&poPoint : *poMP)
487 {
488 PushOrdinal(poPoint->getX());
489 PushOrdinal(poPoint->getY());
490 if (nDimension == 3)
491 PushOrdinal(poPoint->getZ());
492 }
493
494 return OGRERR_NONE;
495 }
496
497 /* ==================================================================== */
498 /* Handle other geometry collections. */
499 /* ==================================================================== */
500 else
501 {
502 /* --------------------------------------------------------------------
503 */
504 /* Identify the GType. */
505 /* --------------------------------------------------------------------
506 */
507 if (wkbFlatten(poGeometry->getGeometryType()) == wkbMultiLineString)
508 *pnGType = nDimension * 1000 + 6;
509 else if (wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon)
510 *pnGType = nDimension * 1000 + 7;
511 else if (wkbFlatten(poGeometry->getGeometryType()) ==
512 wkbGeometryCollection)
513 *pnGType = nDimension * 1000 + 4;
514 else
515 {
516 CPLError(CE_Failure, CPLE_AppDefined,
517 "Unexpected geometry type (%d/%s) in "
518 "OGROCIWritableLayer::TranslateToSDOGeometry()",
519 poGeometry->getGeometryType(),
520 poGeometry->getGeometryName());
521 return OGRERR_FAILURE;
522 }
523
524 /* --------------------------------------------------------------------
525 */
526 /* Translate each child in turn. */
527 /* --------------------------------------------------------------------
528 */
529 OGRGeometryCollection *poGC = poGeometry->toGeometryCollection();
530 for (auto &&poMember : *poGC)
531 TranslateElementGroup(poMember);
532
533 return OGRERR_NONE;
534 }
535
536 return OGRERR_FAILURE;
537 }
538
539 int OGROCIWritableLayer::FindFieldIndex(const char *pszFieldName,
540 int bExactMatch)
541 {
542 int iField = GetLayerDefn()->GetFieldIndex(pszFieldName);
543
544 if (!bExactMatch && iField < 0)
545 {
546 // try laundered version
547 OGROCISession *poSession = poDS->GetSession();
548 char *pszSafeName = CPLStrdup(pszFieldName);
549
550 poSession->CleanName(pszSafeName);
551
552 iField = GetLayerDefn()->GetFieldIndex(pszSafeName);
553
554 CPLFree(pszSafeName);
555 }
556
557 return iField;
558 }