forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpl_vsil_ocilob.cpp
536 lines (438 loc) · 16.2 KB
/
cpl_vsil_ocilob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/**********************************************************************
* $Id: cpl_vsil_ocilob.cpp $
*
* Project: CPL - Common Portability Library
* Purpose: Implement VSI large file api for Oracle OCI LOB
* Author: Ivan Lucena, <ivan dot lucena at oracle dot com>
*
**********************************************************************
* Copyright (c) 2015, Ivan Lucena, <ivan dot lucena at oracle dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "cpl_error.h"
#include "cpl_vsi_virtual.h"
#include "georaster_priv.h"
// *****************************************************************************
// WSIOCILobFSHandle
// *****************************************************************************
class WSIOCILobFSHandle : public VSIFilesystemHandler
{
public:
VSIVirtualHandle *Open(const char *pszFilename, const char *pszAccess,
bool bSetError,
CSLConstList /* papszOptions */) override;
int Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
int nFlags) override;
int Unlink(const char *pszFilename) override;
private:
static char **ParseIdentificator(const char *pszFilename);
static OWConnection *GetConnection(char **papszParam);
static OWStatement *GetStatement(const char *tableName,
const char *rasterid, boolean bUpdate,
OWConnection *pConnection);
};
// *****************************************************************************
// VSIOCILobHandle
// *****************************************************************************
class VSIOCILobHandle : public VSIVirtualHandle
{
private:
OWConnection *poConnection;
OWStatement *poStatement;
OCILobLocator *phLocator;
GUIntBig nFileSize;
GUIntBig nCurOff;
boolean bUpdate;
public:
VSIOCILobHandle(OWConnection *poConnectionIn, OWStatement *poStatementIn,
OCILobLocator *phLocatorIn, boolean bUpdateIn);
~VSIOCILobHandle() override;
int Seek(vsi_l_offset nOffset, int nWhence) override;
vsi_l_offset Tell() override;
size_t Read(void *pBuffer, size_t nSize, size_t nMemb) override;
size_t Write(const void *pBuffer, size_t nSize, size_t nMemb) override;
int Eof() override;
int Close() override;
};
// ****************************************************************************
// Implementation WSIOCILobFSHandle
// ****************************************************************************
// -----------------------------------------------------------------------------
// ParseIdentificator()
// -----------------------------------------------------------------------------
char **WSIOCILobFSHandle::ParseIdentificator(const char *pszFilename)
{
if (strncmp(pszFilename, "/vsiocilob/", strlen("/vsiocilob/")) != 0)
{
return nullptr;
}
char **papszParam =
CSLTokenizeString2(&pszFilename[strlen("/vsiocilob/")], ",",
CSLT_HONOURSTRINGS | CSLT_ALLOWEMPTYTOKENS |
CSLT_STRIPLEADSPACES | CSLT_STRIPENDSPACES);
if (CSLCount(papszParam) < 6)
{
CSLDestroy(papszParam);
return nullptr;
}
return papszParam;
}
// -----------------------------------------------------------------------------
// GetConnection()
// -----------------------------------------------------------------------------
OWConnection *WSIOCILobFSHandle::GetConnection(char **papszParam)
{
OWConnection *poConnection = nullptr;
if (!papszParam)
{
return nullptr;
}
if (strlen(papszParam[0]) == 0 && strlen(papszParam[1]) == 0 &&
strlen(papszParam[2]) == 0)
{
/* In an external procedure environment, before opening any
* dataset, the caller must pass the with_context as an
* string metadata item OCI_CONTEXT_PTR to the driver. */
OCIExtProcContext *with_context = nullptr;
const char *pszContext = GDALGetMetadataItem(
GDALGetDriverByName("GEORASTER"), "OCI_CONTEXT_PTR", nullptr);
if (pszContext)
{
sscanf(pszContext, "%p", &with_context);
poConnection = new OWConnection(with_context);
}
else
{
return nullptr;
}
}
else
{
poConnection =
new OWConnection(papszParam[0], papszParam[1], papszParam[2]);
}
if (poConnection && !poConnection->Succeeded())
{
delete poConnection;
return nullptr;
}
return poConnection;
}
// -----------------------------------------------------------------------------
// GetStatement()
// -----------------------------------------------------------------------------
OWStatement *WSIOCILobFSHandle::GetStatement(const char *tableName,
const char *rasterid,
boolean bUpdate,
OWConnection *pConnection)
{
OWStatement *pStatement = nullptr;
const char *pszUpdate = bUpdate ? "for update" : "";
pStatement = pConnection->CreateStatement(CPLSPrintf(
"select rasterblock from %s where rasterid = %s and rownum = 1 %s",
tableName, rasterid, pszUpdate));
return pStatement;
}
// -----------------------------------------------------------------------------
// Open()
// -----------------------------------------------------------------------------
VSIVirtualHandle *WSIOCILobFSHandle::Open(const char *pszFilename,
const char *pszAccess,
bool /* bSetError*/,
CSLConstList /* papszOptions */)
{
char **papszParam = ParseIdentificator(pszFilename);
if (!papszParam)
{
return nullptr;
}
if (!EQUAL(papszParam[5], "noext"))
{
CSLDestroy(papszParam);
return nullptr;
}
// Get the connection
OWConnection *poConnection = GetConnection(papszParam);
if (!poConnection)
{
CSLDestroy(papszParam);
return nullptr;
}
boolean bUpdate = false;
if (strchr(pszAccess, 'w') != nullptr || strchr(pszAccess, '+') != nullptr)
{
bUpdate = true;
}
// Get the statement
OWStatement *poStatement =
GetStatement(papszParam[3], papszParam[4], bUpdate, poConnection);
if (!poStatement)
{
CSLDestroy(papszParam);
delete poConnection;
return nullptr;
}
// Get the lob locator
OCILobLocator *phLocator = nullptr;
poStatement->Define(&phLocator);
if (!poStatement->Execute() || !phLocator)
{
CSLDestroy(papszParam);
delete poConnection;
delete poStatement;
return nullptr;
}
CPLDebug("GEOR", "VSIOCILOB open successfully");
CSLDestroy(papszParam);
return new VSIOCILobHandle(poConnection, poStatement, phLocator, bUpdate);
}
// -----------------------------------------------------------------------------
// Unlink()
// -----------------------------------------------------------------------------
int WSIOCILobFSHandle::Unlink(const char *pszFilename)
{
char **papszParam = ParseIdentificator(pszFilename);
if (!papszParam)
{
return -1;
}
if (!EQUAL(papszParam[5], "noext"))
{
CSLDestroy(papszParam);
return -1;
}
CPLDebug("GEOR", "Unlink VSIOCILOB file");
// Get the connection
OWConnection *poConnection = GetConnection(papszParam);
if (!poConnection)
{
CSLDestroy(papszParam);
return -1;
}
// Get the statement
OWStatement *poStatement =
GetStatement(papszParam[3], papszParam[4], true, poConnection);
if (!poStatement)
{
CSLDestroy(papszParam);
delete poConnection;
return -1;
}
// Get the lob locator
OCILobLocator *phLocator = nullptr;
poStatement->Define(&phLocator);
if (!poStatement->Execute() || !phLocator)
{
CSLDestroy(papszParam);
delete poConnection;
delete poStatement;
return -1;
}
// Trim the lob
if (poStatement->GetBlobLength(phLocator) > 0)
{
CPLDebug("GEOR", "Trim the LOB");
poStatement->TrimLob(phLocator, 0);
poConnection->Commit();
CPLDebug("GEOR", "LOB trimmed");
}
// Destroy the objects
OWStatement::Free(&phLocator, 1);
delete poStatement;
delete poConnection;
CSLDestroy(papszParam);
return 0;
}
// -----------------------------------------------------------------------------
// Stat()
// -----------------------------------------------------------------------------
int WSIOCILobFSHandle::Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
int nFlags)
{
(void)nFlags;
memset(pStatBuf, 0, sizeof(VSIStatBufL));
char **papszParam = ParseIdentificator(pszFilename);
if (!papszParam)
{
return -1;
}
if (strcmp(papszParam[5], "noext") != 0)
{
CSLDestroy(papszParam);
return -1;
}
// Get the connection
OWConnection *poConnection = GetConnection(papszParam);
if (!poConnection)
{
CSLDestroy(papszParam);
return -1;
}
// Get the statement
OWStatement *poStatement =
GetStatement(papszParam[3], papszParam[4], false, poConnection);
if (!poStatement)
{
CSLDestroy(papszParam);
delete poConnection;
return -1;
}
// Get the lob locator
OCILobLocator *phLocator = nullptr;
poStatement->Define(&phLocator);
if (!poStatement->Execute() || !phLocator)
{
CSLDestroy(papszParam);
delete poConnection;
delete poStatement;
return -1;
}
// Get the lob length
pStatBuf->st_size = poStatement->GetBlobLength(phLocator);
pStatBuf->st_mode = S_IFREG;
// Destroy the objects
OWStatement::Free(&phLocator, 1);
delete poStatement;
delete poConnection;
CSLDestroy(papszParam);
return 0;
}
// ****************************************************************************
// Implementation VSIOCILobHandle
// ****************************************************************************
// ----------------------------------------------------------------------------
// VSIOCILobHandle()
// ----------------------------------------------------------------------------
VSIOCILobHandle::VSIOCILobHandle(OWConnection *poConnectionIn,
OWStatement *poStatementIn,
OCILobLocator *phLocatorIn, boolean bUpdateIn)
: poConnection(poConnectionIn), poStatement(poStatementIn),
phLocator(phLocatorIn), bUpdate(bUpdateIn)
{
nCurOff = 0;
nFileSize = poStatement->GetBlobLength(phLocator);
}
// ----------------------------------------------------------------------------
// ~VSIOCILobHandle()
// ----------------------------------------------------------------------------
VSIOCILobHandle::~VSIOCILobHandle()
{
CPLDebug("GEOR", "Destroy the vsiocilob handle");
if (phLocator)
{
OWStatement::Free(&phLocator, 1);
}
if (poStatement)
{
delete poStatement;
}
if (poConnection)
{
delete poConnection;
}
}
// ----------------------------------------------------------------------------
// Seek()
// ----------------------------------------------------------------------------
int VSIOCILobHandle::Seek(vsi_l_offset nOffset, int nWhence)
{
if (nWhence == SEEK_END)
{
nOffset = poStatement->GetBlobLength(phLocator);
}
if (nWhence == SEEK_CUR)
{
nOffset += nCurOff;
}
nCurOff = nOffset;
return 0;
}
// ----------------------------------------------------------------------------
// Tell()
// ----------------------------------------------------------------------------
vsi_l_offset VSIOCILobHandle::Tell()
{
return nCurOff;
}
// ----------------------------------------------------------------------------
// Read()
// ----------------------------------------------------------------------------
size_t VSIOCILobHandle::Read(void *pBuffer, size_t nSize, size_t nCount)
{
GUIntBig nBytes = (nSize * nCount);
if (nBytes == 0)
{
return 0;
}
GUIntBig nRead = poStatement->ReadBlob(
phLocator, pBuffer, static_cast<unsigned long>(nCurOff + 1),
static_cast<unsigned long>(nBytes));
nCurOff += (GUIntBig)nRead;
return (size_t)(nRead / nSize);
}
// ----------------------------------------------------------------------------
// Write()
// ----------------------------------------------------------------------------
size_t VSIOCILobHandle::Write(const void *pBuffer, size_t nSize, size_t nCount)
{
GUIntBig nBytes = (nSize * nCount);
if (nBytes == 0)
{
return 0;
}
GUIntBig nWrite = poStatement->WriteBlob(
phLocator, (void *)pBuffer, static_cast<unsigned long>(nCurOff + 1),
static_cast<unsigned long>(nBytes));
nCurOff += (GUIntBig)nWrite;
return (size_t)(nWrite / nSize);
}
// ----------------------------------------------------------------------------
// Eof()
// ----------------------------------------------------------------------------
int VSIOCILobHandle::Eof()
{
return (int)(nCurOff >= nFileSize);
}
// ----------------------------------------------------------------------------
// Close()
// ----------------------------------------------------------------------------
int VSIOCILobHandle::Close()
{
if (bUpdate)
{
poConnection->Commit();
}
return 0;
}
// -----------------------------------------------------------------------------
// VSIInstallStdinHandler()
// -----------------------------------------------------------------------------
/**
* \brief Install /vsiocilob/ virtual file system handler
*
* A special file handler that allows reading from Oracle's LOB objects.
*
* @since GDAL 2.0.0
*/
void VSIInstallOCILobHandler()
{
VSIFileManager::InstallHandler("/vsiocilob/", new WSIOCILobFSHandle);
}