Debian Codegraph @

ogr/ogrsf_frmts/oci/ogrociloaderlayer.cpp

raw
1 /******************************************************************************
2 *
3 * Project: Oracle Spatial Driver
4 * Purpose: Implementation of the OGROCILoaderLayer class. This implements
5 * an output only OGRLayer for writing an SQL*Loader file.
6 * Author: Frank Warmerdam, [email protected]
7 *
8 ******************************************************************************
9 * Copyright (c) 2003, Frank Warmerdam <[email protected]>
10 *
11 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14 #include "ogr_oci.h"
15 #include "cpl_conv.h"
16 #include "cpl_string.h"
17
18 /************************************************************************/
19 /* OGROCILoaderLayer() */
20 /************************************************************************/
21
22 OGROCILoaderLayer::OGROCILoaderLayer(OGROCIDataSource *poDSIn,
23 const char *pszTableName,
24 const char *pszGeomColIn, int nSRIDIn,
25 const char *pszLoaderFilenameIn)
26 : OGROCIWritableLayer(poDSIn)
27
28 {
29 iNextFIDToWrite = 1;
30
31 bTruncationReported = FALSE;
32 bHeaderWritten = FALSE;
33 nLDRMode = LDRM_UNKNOWN;
34
35 poFeatureDefn = new OGRFeatureDefn(pszTableName);
36 SetDescription(poFeatureDefn->GetName());
37 poFeatureDefn->Reference();
38
39 pszGeomName = CPLStrdup(pszGeomColIn);
40 pszFIDName = (char *)CPLGetConfigOption("OCI_FID", "OGR_FID");
41
42 nSRID = nSRIDIn;
43 poSRS = poDSIn->FetchSRS(nSRID);
44
45 /* -------------------------------------------------------------------- */
46 /* Open the loader file. */
47 /* -------------------------------------------------------------------- */
48 pszLoaderFilename = CPLStrdup(pszLoaderFilenameIn);
49
50 fpData = nullptr;
51 fpLoader = VSIFOpen(pszLoaderFilename, "wt");
52 if (fpLoader == nullptr)
53 {
54 CPLError(CE_Failure, CPLE_OpenFailed,
55 "Failed to open SQL*Loader control file:%s",
56 pszLoaderFilename);
57 return;
58 }
59 }
60
61 /************************************************************************/
62 /* ~OGROCILoaderLayer() */
63 /************************************************************************/
64
65 OGROCILoaderLayer::~OGROCILoaderLayer()
66
67 {
68 if (fpData != nullptr)
69 VSIFClose(fpData);
70
71 if (fpLoader != nullptr)
72 {
73 VSIFClose(fpLoader);
74 FinalizeNewLayer();
75 }
76
77 CPLFree(pszLoaderFilename);
78 }
79
80 /************************************************************************/
81 /* WriteLoaderHeader() */
82 /************************************************************************/
83
84 void OGROCILoaderLayer::WriteLoaderHeader()
85
86 {
87 if (bHeaderWritten)
88 return;
89
90 /* -------------------------------------------------------------------- */
91 /* Determine name of geometry column to use. */
92 /* -------------------------------------------------------------------- */
93 const char *pszGeometryName =
94 CSLFetchNameValue(papszOptions, "GEOMETRY_NAME");
95 if (pszGeometryName == nullptr)
96 pszGeometryName = "ORA_GEOMETRY";
97
98 /* -------------------------------------------------------------------- */
99 /* Determine our operation mode. */
100 /* -------------------------------------------------------------------- */
101 const char *pszLDRMode = CSLFetchNameValue(papszOptions, "LOADER_MODE");
102
103 if (pszLDRMode != nullptr && EQUAL(pszLDRMode, "VARIABLE"))
104 nLDRMode = LDRM_VARIABLE;
105 else if (pszLDRMode != nullptr && EQUAL(pszLDRMode, "BINARY"))
106 nLDRMode = LDRM_BINARY;
107 else
108 nLDRMode = LDRM_STREAM;
109
110 /* -------------------------------------------------------------------- */
111 /* Write loader header info. */
112 /* -------------------------------------------------------------------- */
113 VSIFPrintf(fpLoader, "LOAD DATA\n");
114 if (nLDRMode == LDRM_STREAM)
115 {
116 VSIFPrintf(fpLoader, "INFILE *\n");
117 VSIFPrintf(fpLoader, "CONTINUEIF NEXT(1:1) = '#'\n");
118 }
119 else if (nLDRMode == LDRM_VARIABLE)
120 {
121 const std::string osDataFilename =
122 CPLResetExtensionSafe(pszLoaderFilename, "dat");
123 fpData = VSIFOpen(osDataFilename.c_str(), "wb");
124 if (fpData == nullptr)
125 {
126 CPLError(CE_Failure, CPLE_OpenFailed,
127 "Unable to open data output file `%s'.",
128 osDataFilename.c_str());
129 return;
130 }
131
132 VSIFPrintf(fpLoader, "INFILE %s \"var 8\"\n", osDataFilename.c_str());
133 }
134 const char *pszExpectedFIDName = CPLGetConfigOption("OCI_FID", "OGR_FID");
135
136 VSIFPrintf(fpLoader, "INTO TABLE \"%s\" REPLACE\n",
137 poFeatureDefn->GetName());
138 VSIFPrintf(fpLoader, "FIELDS TERMINATED BY '|'\n");
139 VSIFPrintf(fpLoader, "TRAILING NULLCOLS (\n");
140 VSIFPrintf(fpLoader, " %s INTEGER EXTERNAL,\n", pszExpectedFIDName);
141 VSIFPrintf(fpLoader, " %s COLUMN OBJECT (\n", pszGeometryName);
142 VSIFPrintf(fpLoader, " SDO_GTYPE INTEGER EXTERNAL,\n");
143 VSIFPrintf(fpLoader, " SDO_ELEM_INFO VARRAY TERMINATED BY '|/'\n");
144 VSIFPrintf(fpLoader, " (elements INTEGER EXTERNAL),\n");
145 VSIFPrintf(fpLoader, " SDO_ORDINATES VARRAY TERMINATED BY '|/'\n");
146 VSIFPrintf(fpLoader, " (ordinates FLOAT EXTERNAL)\n");
147 VSIFPrintf(fpLoader, " ),\n");
148
149 /* -------------------------------------------------------------------- */
150 /* Write user field schema. */
151 /* -------------------------------------------------------------------- */
152 int iField;
153
154 for (iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++)
155 {
156 OGRFieldDefn *poFldDefn = poFeatureDefn->GetFieldDefn(iField);
157
158 if (poFldDefn->GetType() == OFTInteger)
159 {
160 VSIFPrintf(fpLoader, " \"%s\" INTEGER EXTERNAL",
161 poFldDefn->GetNameRef());
162 }
163 else if (poFldDefn->GetType() == OFTInteger64)
164 {
165 VSIFPrintf(fpLoader, " \"%s\" LONGINTEGER EXTERNAL",
166 poFldDefn->GetNameRef());
167 }
168 else if (poFldDefn->GetType() == OFTReal)
169 {
170 VSIFPrintf(fpLoader, " \"%s\" FLOAT EXTERNAL",
171 poFldDefn->GetNameRef());
172 }
173 else /* if( poFldDefn->GetType() == OFTString ) or default case */
174 {
175 VSIFPrintf(fpLoader, " \"%s\" VARCHARC(4)",
176 poFldDefn->GetNameRef());
177 }
178
179 if (iField < poFeatureDefn->GetFieldCount() - 1)
180 VSIFPrintf(fpLoader, ",");
181 VSIFPrintf(fpLoader, "\n");
182 }
183
184 VSIFPrintf(fpLoader, ")\n");
185
186 if (nLDRMode == LDRM_STREAM)
187 VSIFPrintf(fpLoader, "begindata\n");
188
189 bHeaderWritten = TRUE;
190 }
191
192 /************************************************************************/
193 /* GetNextFeature() */
194 /* */
195 /* We override the next feature method because we know that we */
196 /* implement the attribute query within the statement and so we */
197 /* don't have to test here. Eventually the spatial query will */
198 /* be fully tested within the statement as well. */
199 /************************************************************************/
200
201 OGRFeature *OGROCILoaderLayer::GetNextFeature()
202
203 {
204 CPLError(CE_Failure, CPLE_NotSupported,
205 "GetNextFeature() not supported for an OGROCILoaderLayer.");
206 return nullptr;
207 }
208
209 /************************************************************************/
210 /* ResetReading() */
211 /************************************************************************/
212
213 void OGROCILoaderLayer::ResetReading()
214
215 {
216 OGROCILayer::ResetReading();
217 }
218
219 /************************************************************************/
220 /* WriteFeatureStreamMode() */
221 /************************************************************************/
222
223 OGRErr OGROCILoaderLayer::WriteFeatureStreamMode(OGRFeature *poFeature)
224
225 {
226 /* -------------------------------------------------------------------- */
227 /* Write the FID. */
228 /* -------------------------------------------------------------------- */
229 VSIFPrintf(fpLoader, " " CPL_FRMT_GIB "|", poFeature->GetFID());
230
231 /* -------------------------------------------------------------------- */
232 /* Set the geometry */
233 /* -------------------------------------------------------------------- */
234 int nLineLen = 0;
235 if (poFeature->GetGeometryRef() != nullptr)
236 {
237 char szSRID[128];
238 int nGType;
239 int i;
240
241 if (nSRID == -1)
242 strcpy(szSRID, "NULL");
243 else
244 snprintf(szSRID, sizeof(szSRID), "%d", nSRID);
245
246 if (TranslateToSDOGeometry(poFeature->GetGeometryRef(), &nGType) ==
247 OGRERR_NONE)
248 {
249 VSIFPrintf(fpLoader, "%d|", nGType);
250 for (i = 0; i < nElemInfoCount; i++)
251 {
252 VSIFPrintf(fpLoader, "%d|", panElemInfo[i]);
253 if (++nLineLen > 18 && i < nElemInfoCount - 1)
254 {
255 VSIFPrintf(fpLoader, "\n#");
256 nLineLen = 0;
257 }
258 }
259 VSIFPrintf(fpLoader, "/");
260
261 for (i = 0; i < nOrdinalCount; i++)
262 {
263 VSIFPrintf(fpLoader, "%.16g|", padfOrdinals[i]);
264 if (++nLineLen > 6 && i < nOrdinalCount - 1)
265 {
266 VSIFPrintf(fpLoader, "\n#");
267 nLineLen = 0;
268 }
269 }
270 VSIFPrintf(fpLoader, "/");
271 }
272 else
273 {
274 VSIFPrintf(fpLoader, "0|/|/");
275 }
276 }
277 else
278 {
279 VSIFPrintf(fpLoader, "0|/|/");
280 }
281
282 /* -------------------------------------------------------------------- */
283 /* Set the other fields. */
284 /* -------------------------------------------------------------------- */
285 int i;
286
287 nLineLen = 0;
288 VSIFPrintf(fpLoader, "\n#");
289
290 for (i = 0; i < poFeatureDefn->GetFieldCount(); i++)
291 {
292 OGRFieldDefn *poFldDefn = poFeatureDefn->GetFieldDefn(i);
293
294 if (!poFeature->IsFieldSetAndNotNull(i))
295 {
296 if (poFldDefn->GetType() != OFTInteger &&
297 poFldDefn->GetType() != OFTInteger64 &&
298 poFldDefn->GetType() != OFTReal)
299 VSIFPrintf(fpLoader, "%04d", 0);
300 continue;
301 }
302
303 const char *pszStrValue = poFeature->GetFieldAsString(i);
304
305 if (nLineLen > 70)
306 {
307 VSIFPrintf(fpLoader, "\n#");
308 nLineLen = 0;
309 }
310
311 nLineLen += static_cast<int>(strlen(pszStrValue));
312
313 if (poFldDefn->GetType() == OFTInteger ||
314 poFldDefn->GetType() == OFTInteger64 ||
315 poFldDefn->GetType() == OFTReal)
316 {
317 if (poFldDefn->GetWidth() > 0 && bPreservePrecision &&
318 (int)strlen(pszStrValue) > poFldDefn->GetWidth())
319 {
320 ReportTruncation(poFldDefn);
321 VSIFPrintf(fpLoader, "|");
322 }
323 else
324 VSIFPrintf(fpLoader, "%s|", pszStrValue);
325 }
326 else
327 {
328 int nLength = static_cast<int>(strlen(pszStrValue));
329
330 if (poFldDefn->GetWidth() > 0 && nLength > poFldDefn->GetWidth())
331 {
332 ReportTruncation(poFldDefn);
333 nLength = poFldDefn->GetWidth();
334 }
335
336 VSIFPrintf(fpLoader, "%04d", nLength);
337 VSIFWrite((void *)pszStrValue, 1, nLength, fpLoader);
338 }
339 }
340
341 if (VSIFPrintf(fpLoader, "\n") == 0)
342 {
343 CPLError(CE_Failure, CPLE_FileIO,
344 "Write to loader file failed, likely out of disk space.");
345 return OGRERR_FAILURE;
346 }
347 else
348 return OGRERR_NONE;
349 }
350
351 /************************************************************************/
352 /* WriteFeatureVariableMode() */
353 /************************************************************************/
354
355 OGRErr OGROCILoaderLayer::WriteFeatureVariableMode(OGRFeature *poFeature)
356
357 {
358 OGROCIStringBuf oLine;
359
360 if (fpData == nullptr)
361 return OGRERR_FAILURE;
362
363 /* -------------------------------------------------------------------- */
364 /* Write the FID. */
365 /* -------------------------------------------------------------------- */
366 oLine.Append("00000000");
367 oLine.Appendf(32, " " CPL_FRMT_GIB "|", poFeature->GetFID());
368
369 /* -------------------------------------------------------------------- */
370 /* Set the geometry */
371 /* -------------------------------------------------------------------- */
372 if (poFeature->GetGeometryRef() != nullptr)
373 {
374 char szSRID[128];
375 int nGType;
376 int i;
377
378 if (nSRID == -1)
379 strcpy(szSRID, "NULL");
380 else
381 snprintf(szSRID, sizeof(szSRID), "%d", nSRID);
382
383 if (TranslateToSDOGeometry(poFeature->GetGeometryRef(), &nGType) ==
384 OGRERR_NONE)
385 {
386 oLine.Appendf(32, "%d|", nGType);
387 for (i = 0; i < nElemInfoCount; i++)
388 {
389 oLine.Appendf(32, "%d|", panElemInfo[i]);
390 }
391 oLine.Append("/");
392
393 for (i = 0; i < nOrdinalCount; i++)
394 {
395 oLine.Appendf(32, "%.16g|", padfOrdinals[i]);
396 }
397 oLine.Append("/");
398 }
399 else
400 {
401 oLine.Append("0|/|/");
402 }
403 }
404 else
405 {
406 oLine.Append("0|/|/");
407 }
408
409 /* -------------------------------------------------------------------- */
410 /* Set the other fields. */
411 /* -------------------------------------------------------------------- */
412 int i;
413
414 for (i = 0; i < poFeatureDefn->GetFieldCount(); i++)
415 {
416 OGRFieldDefn *poFldDefn = poFeatureDefn->GetFieldDefn(i);
417
418 if (!poFeature->IsFieldSetAndNotNull(i))
419 {
420 if (poFldDefn->GetType() != OFTInteger &&
421 poFldDefn->GetType() != OFTInteger64 &&
422 poFldDefn->GetType() != OFTReal)
423 oLine.Append("0000");
424 else
425 oLine.Append("|");
426 continue;
427 }
428
429 const char *pszStrValue = poFeature->GetFieldAsString(i);
430
431 if (poFldDefn->GetType() == OFTInteger ||
432 poFldDefn->GetType() == OFTInteger64 ||
433 poFldDefn->GetType() == OFTReal)
434 {
435 if (poFldDefn->GetWidth() > 0 && bPreservePrecision &&
436 (int)strlen(pszStrValue) > poFldDefn->GetWidth())
437 {
438 ReportTruncation(poFldDefn);
439 oLine.Append("|");
440 }
441 else
442 {
443 oLine.Append(pszStrValue);
444 oLine.Append("|");
445 }
446 }
447 else
448 {
449 int nLength = static_cast<int>(strlen(pszStrValue));
450
451 if (poFldDefn->GetWidth() > 0 && nLength > poFldDefn->GetWidth())
452 {
453 ReportTruncation(poFldDefn);
454 nLength = poFldDefn->GetWidth();
455 ((char *)pszStrValue)[nLength] = '\0';
456 }
457
458 oLine.Appendf(5, "%04d", nLength);
459 oLine.Append(pszStrValue);
460 }
461 }
462
463 oLine.Appendf(3, "\n");
464
465 /* -------------------------------------------------------------------- */
466 /* Update the line's length, and write to disk. */
467 /* -------------------------------------------------------------------- */
468 char szLength[9] = {};
469 size_t nStringLen = strlen(oLine.GetString());
470
471 snprintf(szLength, sizeof(szLength), "%08d", (int)(nStringLen - 8));
472 memcpy(oLine.GetString(), szLength, 8);
473
474 if (VSIFWrite(oLine.GetString(), 1, nStringLen, fpData) != nStringLen)
475 {
476 CPLError(CE_Failure, CPLE_FileIO,
477 "Write to loader file failed, likely out of disk space.");
478 return OGRERR_FAILURE;
479 }
480 else
481 return OGRERR_NONE;
482 }
483
484 /************************************************************************/
485 /* WriteFeatureBinaryMode() */
486 /************************************************************************/
487
488 OGRErr OGROCILoaderLayer::WriteFeatureBinaryMode(OGRFeature * /*poFeature*/)
489
490 {
491 return OGRERR_UNSUPPORTED_OPERATION;
492 }
493
494 /************************************************************************/
495 /* ICreateFeature() */
496 /************************************************************************/
497
498 OGRErr OGROCILoaderLayer::ICreateFeature(OGRFeature *poFeature)
499
500 {
501 WriteLoaderHeader();
502
503 /* -------------------------------------------------------------------- */
504 /* Set the FID. */
505 /* -------------------------------------------------------------------- */
506 if (poFeature->GetFID() == OGRNullFID)
507 poFeature->SetFID(iNextFIDToWrite++);
508
509 /* -------------------------------------------------------------------- */
510 /* Add extents of this geometry to the existing layer extents. */
511 /* -------------------------------------------------------------------- */
512 if (poFeature->GetGeometryRef() != nullptr)
513 {
514 OGREnvelope sThisExtent;
515
516 poFeature->GetGeometryRef()->getEnvelope(&sThisExtent);
517 sExtent.Merge(sThisExtent);
518 }
519
520 /* -------------------------------------------------------------------- */
521 /* Call the mode specific write function. */
522 /* -------------------------------------------------------------------- */
523 if (nLDRMode == LDRM_STREAM)
524 return WriteFeatureStreamMode(poFeature);
525 else if (nLDRMode == LDRM_VARIABLE)
526 return WriteFeatureVariableMode(poFeature);
527 else if (nLDRMode == LDRM_BINARY)
528 return WriteFeatureBinaryMode(poFeature);
529 else
530 return OGRERR_UNSUPPORTED_OPERATION;
531 }
532
533 /************************************************************************/
534 /* TestCapability() */
535 /************************************************************************/
536
537 int OGROCILoaderLayer::TestCapability(const char *pszCap) const
538
539 {
540 if (EQUAL(pszCap, OLCSequentialWrite))
541 return TRUE;
542
543 else if (EQUAL(pszCap, OLCCreateField))
544 return TRUE;
545
546 else
547 return OGROCILayer::TestCapability(pszCap);
548 }
549
550 /************************************************************************/
551 /* GetFeatureCount() */
552 /* */
553 /* If a spatial filter is in effect, we turn control over to */
554 /* the generic counter. Otherwise we return the total count. */
555 /* Eventually we should consider implementing a more efficient */
556 /* way of counting features matching a spatial query. */
557 /************************************************************************/
558
559 GIntBig OGROCILoaderLayer::GetFeatureCount(int /* bForce */)
560
561 {
562 return iNextFIDToWrite - 1;
563 }
564
565 /************************************************************************/
566 /* FinalizeNewLayer() */
567 /* */
568 /* Our main job here is to update the USER_SDO_GEOM_METADATA */
569 /* table to include the correct array of dimension object with */
570 /* the appropriate extents for this layer. We may also do */
571 /* spatial indexing at this point. */
572 /************************************************************************/
573
574 void OGROCILoaderLayer::FinalizeNewLayer()
575
576 {
577 OGROCIStringBuf sDimUpdate;
578
579 /* -------------------------------------------------------------------- */
580 /* If the dimensions are degenerate (all zeros) then we assume */
581 /* there were no geometries, and we don't bother setting the */
582 /* dimensions. */
583 /* -------------------------------------------------------------------- */
584 if (sExtent.MaxX == 0 && sExtent.MinX == 0 && sExtent.MaxY == 0 &&
585 sExtent.MinY == 0)
586 {
587 CPLError(CE_Warning, CPLE_AppDefined,
588 "Layer %s appears to have no geometry ... not setting SDO "
589 "DIMINFO metadata.",
590 poFeatureDefn->GetName());
591 return;
592 }
593
594 /* -------------------------------------------------------------------- */
595 /* Establish the extents and resolution to use. */
596 /* -------------------------------------------------------------------- */
597 double dfResSize;
598 double dfXMin, dfXMax, dfXRes;
599 double dfYMin, dfYMax, dfYRes;
600 double dfZMin, dfZMax, dfZRes;
601
602 if (sExtent.MaxX - sExtent.MinX > 400)
603 dfResSize = 0.001;
604 else
605 dfResSize = 0.0000001;
606
607 dfXMin = sExtent.MinX - dfResSize * 3;
608 dfXMax = sExtent.MaxX + dfResSize * 3;
609 dfXRes = dfResSize;
610 ParseDIMINFO("DIMINFO_X", &dfXMin, &dfXMax, &dfXRes);
611
612 dfYMin = sExtent.MinY - dfResSize * 3;
613 dfYMax = sExtent.MaxY + dfResSize * 3;
614 dfYRes = dfResSize;
615 ParseDIMINFO("DIMINFO_Y", &dfYMin, &dfYMax, &dfYRes);
616
617 dfZMin = -100000.0;
618 dfZMax = 100000.0;
619 dfZRes = 0.002;
620 ParseDIMINFO("DIMINFO_Z", &dfZMin, &dfZMax, &dfZRes);
621
622 /* -------------------------------------------------------------------- */
623 /* Prepare dimension update statement. */
624 /* -------------------------------------------------------------------- */
625 sDimUpdate.Append("UPDATE USER_SDO_GEOM_METADATA SET DIMINFO = ");
626 sDimUpdate.Append("MDSYS.SDO_DIM_ARRAY(");
627
628 sDimUpdate.Appendf(200, "MDSYS.SDO_DIM_ELEMENT('X',%.16g,%.16g,%.12g)",
629 dfXMin, dfXMax, dfXRes);
630 sDimUpdate.Appendf(200, ",MDSYS.SDO_DIM_ELEMENT('Y',%.16g,%.16g,%.12g)",
631 dfYMin, dfYMax, dfYRes);
632
633 if (nDimension == 3)
634 {
635 sDimUpdate.Appendf(200, ",MDSYS.SDO_DIM_ELEMENT('Z',%.16g,%.16g,%.12g)",
636 dfZMin, dfZMax, dfZRes);
637 }
638
639 sDimUpdate.Append(")");
640
641 sDimUpdate.Appendf(static_cast<int>(strlen(poFeatureDefn->GetName()) + 100),
642 " WHERE table_name = UPPER('%s')",
643 poFeatureDefn->GetName());
644
645 /* -------------------------------------------------------------------- */
646 /* Execute the metadata update. */
647 /* -------------------------------------------------------------------- */
648 OGROCIStatement oExecStatement(poDS->GetSession());
649
650 if (oExecStatement.Execute(sDimUpdate.GetString()) != CE_None)
651 return;
652 }