Debian Codegraph @

ogr/ogrsf_frmts/oci/ogrocistatement.cpp

raw
1 /******************************************************************************
2 *
3 * Project: Oracle Spatial Driver
4 * Purpose: Implementation of OGROCIStatement, which encapsulates the
5 * preparation, execution and fetching from an SQL statement.
6 * Author: Frank Warmerdam, [email protected]
7 *
8 ******************************************************************************
9 * Copyright (c) 2002, Frank Warmerdam <[email protected]>
10 *
11 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14 #include "ogr_oci.h"
15 #include "cpl_conv.h"
16
17 /************************************************************************/
18 /* OGROCIStatement() */
19 /************************************************************************/
20
21 OGROCIStatement::OGROCIStatement(OGROCISession *poSessionIn)
22
23 {
24 poSession = poSessionIn;
25 hStatement = nullptr;
26 poDefn = nullptr;
27
28 nRawColumnCount = 0;
29 papszCurColumn = nullptr;
30 papszCurImage = nullptr;
31 panCurColumnInd = nullptr;
32 panFieldMap = nullptr;
33
34 pszCommandText = nullptr;
35 nAffectedRows = 0;
36 }
37
38 /************************************************************************/
39 /* ~OGROCIStatement() */
40 /************************************************************************/
41
42 OGROCIStatement::~OGROCIStatement()
43
44 {
45 Clean();
46 }
47
48 /************************************************************************/
49 /* Clean() */
50 /************************************************************************/
51
52 void OGROCIStatement::Clean()
53
54 {
55 int i;
56
57 CPLFree(pszCommandText);
58 pszCommandText = nullptr;
59
60 if (papszCurColumn != nullptr)
61 {
62 for (i = 0; papszCurColumn[i] != nullptr; i++)
63 CPLFree(papszCurColumn[i]);
64 }
65 CPLFree(papszCurColumn);
66 papszCurColumn = nullptr;
67
68 CPLFree(papszCurImage);
69 papszCurImage = nullptr;
70
71 CPLFree(panCurColumnInd);
72 panCurColumnInd = nullptr;
73
74 CPLFree(panFieldMap);
75 panFieldMap = nullptr;
76
77 if (poDefn != nullptr)
78 {
79 poDefn->Release();
80 poDefn = nullptr;
81 }
82
83 if (hStatement != nullptr)
84 {
85 OCIHandleFree(static_cast<dvoid *>(hStatement), (ub4)OCI_HTYPE_STMT);
86 hStatement = nullptr;
87 }
88 }
89
90 /************************************************************************/
91 /* Prepare() */
92 /************************************************************************/
93
94 CPLErr OGROCIStatement::Prepare(const char *pszSQLStatement)
95
96 {
97 Clean();
98
99 CPLDebug("OCI", "Prepare(%s)", pszSQLStatement);
100
101 pszCommandText = CPLStrdup(pszSQLStatement);
102
103 if (hStatement != nullptr)
104 {
105 CPLError(CE_Failure, CPLE_AppDefined,
106 "Statement already executed once on this OGROCIStatement.");
107 return CE_Failure;
108 }
109
110 /* -------------------------------------------------------------------- */
111 /* Allocate a statement handle. */
112 /* -------------------------------------------------------------------- */
113 if (poSession->Failed(
114 OCIHandleAlloc(poSession->hEnv,
115 reinterpret_cast<dvoid **>(&hStatement),
116 (ub4)OCI_HTYPE_STMT, 0, nullptr),
117 "OCIHandleAlloc(Statement)"))
118 return CE_Failure;
119
120 /* -------------------------------------------------------------------- */
121 /* Prepare the statement. */
122 /* -------------------------------------------------------------------- */
123 if (poSession->Failed(
124 OCIStmtPrepare(
125 hStatement, poSession->hError,
126 reinterpret_cast<text *>(const_cast<char *>(pszSQLStatement)),
127 static_cast<unsigned int>(strlen(pszSQLStatement)),
128 (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT),
129 "OCIStmtPrepare"))
130 return CE_Failure;
131
132 return CE_None;
133 }
134
135 /************************************************************************/
136 /* BindObject() */
137 /************************************************************************/
138
139 CPLErr OGROCIStatement::BindObject(const char *pszPlaceName, void *pahObjects,
140 OCIType *hTDO, void **papIndicators)
141
142 {
143 OCIBind *hBindOrd = nullptr;
144
145 if (poSession->Failed(
146 OCIBindByName(
147 hStatement, &hBindOrd, poSession->hError,
148 reinterpret_cast<text *>(const_cast<char *>(pszPlaceName)),
149 (sb4)strlen(pszPlaceName), nullptr, 0, SQLT_NTY, nullptr,
150 nullptr, nullptr, (ub4)0, nullptr, (ub4)OCI_DEFAULT),
151 "OCIBindByName()"))
152 return CE_Failure;
153
154 if (poSession->Failed(
155 OCIBindObject(hBindOrd, poSession->hError, hTDO,
156 reinterpret_cast<dvoid **>(pahObjects), nullptr,
157 reinterpret_cast<dvoid **>(papIndicators), nullptr),
158 "OCIBindObject()"))
159 return CE_Failure;
160
161 return CE_None;
162 }
163
164 /************************************************************************/
165 /* BindScalar() */
166 /************************************************************************/
167
168 CPLErr OGROCIStatement::BindScalar(const char *pszPlaceName, void *pData,
169 int nDataLen, int nSQLType, sb2 *paeInd)
170
171 {
172 OCIBind *hBindOrd = nullptr;
173
174 if (poSession->Failed(
175 OCIBindByName(
176 hStatement, &hBindOrd, poSession->hError,
177 reinterpret_cast<text *>(const_cast<char *>(pszPlaceName)),
178 (sb4)strlen(pszPlaceName), static_cast<dvoid *>(pData),
179 (sb4)nDataLen, (ub2)nSQLType, static_cast<dvoid *>(paeInd),
180 nullptr, nullptr, 0, nullptr, (ub4)OCI_DEFAULT),
181 "OCIBindByName()"))
182 return CE_Failure;
183 else
184 return CE_None;
185 }
186
187 /************************************************************************/
188 /* BindString() */
189 /************************************************************************/
190
191 CPLErr OGROCIStatement::BindString(const char *pszPlaceName,
192 const char *pszData, sb2 *paeInd)
193
194 {
195 return BindScalar(
196 pszPlaceName,
197 const_cast<void *>(reinterpret_cast<const void *>(pszData)),
198 static_cast<int>(strlen(pszData)) + 1, SQLT_STR, paeInd);
199 }
200
201 /************************************************************************/
202 /* Execute() */
203 /************************************************************************/
204
205 CPLErr OGROCIStatement::Execute(const char *pszSQLStatement, int nMode)
206
207 {
208 /* -------------------------------------------------------------------- */
209 /* Prepare the statement if it is being passed in. */
210 /* -------------------------------------------------------------------- */
211 if (pszSQLStatement != nullptr)
212 {
213 CPLErr eErr = Prepare(pszSQLStatement);
214 if (eErr != CE_None)
215 return eErr;
216 }
217
218 if (hStatement == nullptr)
219 {
220 CPLError(
221 CE_Failure, CPLE_AppDefined,
222 "No prepared statement in call to OGROCIStatement::Execute(NULL)");
223 return CE_Failure;
224 }
225
226 /* -------------------------------------------------------------------- */
227 /* Determine if this is a SELECT statement. */
228 /* -------------------------------------------------------------------- */
229 ub2 nStmtType;
230
231 if (poSession->Failed(OCIAttrGet(hStatement, OCI_HTYPE_STMT, &nStmtType,
232 nullptr, OCI_ATTR_STMT_TYPE,
233 poSession->hError),
234 "OCIAttrGet(ATTR_STMT_TYPE)"))
235 return CE_Failure;
236
237 int bSelect = (nStmtType == OCI_STMT_SELECT);
238
239 /* -------------------------------------------------------------------- */
240 /* Work out some details about execution mode. */
241 /* -------------------------------------------------------------------- */
242 if (nMode == -1)
243 {
244 if (bSelect)
245 nMode = OCI_DEFAULT;
246 else
247 nMode = OCI_COMMIT_ON_SUCCESS;
248 }
249
250 /* -------------------------------------------------------------------- */
251 /* Execute the statement. */
252 /* -------------------------------------------------------------------- */
253 if (poSession->Failed(
254 OCIStmtExecute(poSession->hSvcCtx, hStatement, poSession->hError,
255 (ub4)bSelect ? 0 : 1, (ub4)0, (OCISnapshot *)nullptr,
256 (OCISnapshot *)nullptr, nMode),
257 pszCommandText))
258 return CE_Failure;
259
260 if (!bSelect)
261 {
262 ub4 row_count;
263 if (poSession->Failed(OCIAttrGet(hStatement, OCI_HTYPE_STMT, &row_count,
264 nullptr, OCI_ATTR_ROW_COUNT,
265 poSession->hError),
266 "OCIAttrGet(OCI_ATTR_ROW_COUNT)"))
267 return CE_Failure;
268 nAffectedRows = row_count;
269
270 return CE_None;
271 }
272
273 /* -------------------------------------------------------------------- */
274 /* Count the columns. */
275 /* -------------------------------------------------------------------- */
276 for (nRawColumnCount = 0; true; nRawColumnCount++)
277 {
278 OCIParam *hParamDesc;
279
280 if (OCIParamGet(hStatement, OCI_HTYPE_STMT, poSession->hError,
281 reinterpret_cast<dvoid **>(&hParamDesc),
282 (ub4)nRawColumnCount + 1) != OCI_SUCCESS)
283 break;
284 }
285
286 panFieldMap = (int *)CPLCalloc(sizeof(int), nRawColumnCount);
287
288 papszCurColumn = (char **)CPLCalloc(sizeof(char *), nRawColumnCount + 1);
289 panCurColumnInd = (sb2 *)CPLCalloc(sizeof(sb2), nRawColumnCount + 1);
290
291 /* ==================================================================== */
292 /* Establish result column definitions, and setup parameter */
293 /* defines. */
294 /* ==================================================================== */
295 poDefn = new OGRFeatureDefn(pszCommandText);
296 poDefn->SetGeomType(wkbNone);
297 poDefn->Reference();
298
299 for (int iParam = 0; iParam < nRawColumnCount; iParam++)
300 {
301 OGRFieldDefn oField("", OFTString);
302 OCIParam *hParamDesc;
303 ub2 nOCIType;
304 ub4 nOCILen;
305
306 /* --------------------------------------------------------------------
307 */
308 /* Get parameter definition. */
309 /* --------------------------------------------------------------------
310 */
311 if (poSession->Failed(
312 OCIParamGet(hStatement, OCI_HTYPE_STMT, poSession->hError,
313 reinterpret_cast<dvoid **>(&hParamDesc),
314 (ub4)iParam + 1),
315 "OCIParamGet"))
316 return CE_Failure;
317
318 if (poSession->GetParamInfo(hParamDesc, &oField, &nOCIType, &nOCILen) !=
319 CE_None)
320 return CE_Failure;
321
322 if (oField.GetType() == OFTBinary)
323 {
324 /* We could probably generalize that, but at least it works in that
325 */
326 /* use case */
327 if (EQUAL(oField.GetNameRef(), "DATA_DEFAULT") &&
328 nOCIType == SQLT_LNG)
329 {
330 oField.SetType(OFTString);
331 }
332 else
333 {
334 panFieldMap[iParam] = -1;
335 continue;
336 }
337 }
338
339 poDefn->AddFieldDefn(&oField);
340 panFieldMap[iParam] = poDefn->GetFieldCount() - 1;
341
342 /* --------------------------------------------------------------------
343 */
344 /* Prepare a binding. */
345 /* --------------------------------------------------------------------
346 */
347 int nBufWidth = 256, nOGRField = panFieldMap[iParam];
348 OCIDefine *hDefn = nullptr;
349
350 if (oField.GetWidth() > 0)
351 /* extra space needed for the decimal separator the string
352 terminator and the negative sign (Tamas Szekeres)*/
353 nBufWidth = oField.GetWidth() + 3;
354 else if (oField.GetType() == OFTInteger)
355 nBufWidth = 22;
356 else if (oField.GetType() == OFTReal)
357 nBufWidth = 36;
358 else if (oField.GetType() == OFTDateTime)
359 nBufWidth = 40;
360 else if (oField.GetType() == OFTDate)
361 nBufWidth = 20;
362
363 papszCurColumn[nOGRField] = (char *)CPLMalloc(nBufWidth + 2);
364 CPLAssert(((long)papszCurColumn[nOGRField]) % 2 == 0);
365
366 if (poSession->Failed(
367 OCIDefineByPos(
368 hStatement, &hDefn, poSession->hError, iParam + 1,
369 reinterpret_cast<ub1 *>(papszCurColumn[nOGRField]),
370 nBufWidth, SQLT_STR, panCurColumnInd + nOGRField, nullptr,
371 nullptr, OCI_DEFAULT),
372 "OCIDefineByPos"))
373 return CE_Failure;
374 }
375
376 return CE_None;
377 }
378
379 /************************************************************************/
380 /* SimpleFetchRow() */
381 /************************************************************************/
382
383 char **OGROCIStatement::SimpleFetchRow()
384
385 {
386 int nStatus, i;
387
388 if (papszCurImage == nullptr)
389 {
390 papszCurImage = (char **)CPLCalloc(sizeof(char *), nRawColumnCount + 1);
391 }
392
393 nStatus = OCIStmtFetch(hStatement, poSession->hError, 1, OCI_FETCH_NEXT,
394 OCI_DEFAULT);
395
396 if (nStatus == OCI_NO_DATA)
397 return nullptr;
398 else if (poSession->Failed(nStatus, "OCIStmtFetch"))
399 return nullptr;
400
401 for (i = 0; papszCurColumn[i] != nullptr; i++)
402 {
403 if (panCurColumnInd[i] == OCI_IND_NULL)
404 papszCurImage[i] = nullptr;
405 else
406 papszCurImage[i] = papszCurColumn[i];
407 }
408
409 return papszCurImage;
410 }