Debian Codegraph @

ogr/ogrsf_frmts/oci/ogrociselectlayer.cpp

raw
1 /******************************************************************************
2 *
3 * Project: Oracle Spatial Driver
4 * Purpose: Implementation of the OGROCISelectLayer class. This class
5 * provides read semantics on the result of a SELECT 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 #include "cpl_string.h"
17
18 /************************************************************************/
19 /* OGROCISelectLayer() */
20 /************************************************************************/
21
22 OGROCISelectLayer::OGROCISelectLayer(OGROCIDataSource *poDSIn,
23 const char *pszQuery,
24 OGROCIStatement *poDescribedCommand)
25 : OGROCILayer(poDSIn)
26
27 {
28 iNextShapeId = 0;
29
30 poFeatureDefn = ReadTableDefinition(poDescribedCommand);
31 SetDescription(poFeatureDefn->GetName());
32
33 pszQueryStatement = CPLStrdup(pszQuery);
34
35 ResetReading();
36 }
37
38 /************************************************************************/
39 /* ~OGROCISelectLayer() */
40 /************************************************************************/
41
42 OGROCISelectLayer::~OGROCISelectLayer()
43
44 {
45 }
46
47 /************************************************************************/
48 /* ReadTableDefinition() */
49 /* */
50 /* Build layer definition from the described information about */
51 /* the command. */
52 /************************************************************************/
53
54 OGRFeatureDefn *
55 OGROCISelectLayer::ReadTableDefinition(OGROCIStatement *poCommand)
56
57 {
58 OGROCISession *poSession = poDS->GetSession();
59
60 /* -------------------------------------------------------------------- */
61 /* Parse the returned table information. */
62 /* -------------------------------------------------------------------- */
63 for (int iParam = 0; true; iParam++)
64 {
65 OGRFieldDefn oField("", OFTString);
66 int nStatus;
67 OCIParam *hParamDesc;
68 ub2 nOCIType;
69 ub4 nOCILen;
70
71 nStatus = OCIParamGet(
72 poCommand->GetStatement(), OCI_HTYPE_STMT, poSession->hError,
73 reinterpret_cast<dvoid **>(&hParamDesc), (ub4)iParam + 1);
74
75 if (nStatus == OCI_ERROR)
76 break;
77
78 if (poSession->GetParamInfo(hParamDesc, &oField, &nOCIType, &nOCILen) !=
79 CE_None)
80 break;
81
82 if (oField.GetType() == OFTBinary && nOCIType == 108)
83 {
84 CPLFree(pszGeomName);
85 pszGeomName = CPLStrdup(oField.GetNameRef());
86 iGeomColumn = iParam;
87 break;
88 }
89 }
90
91 /* -------------------------------------------------------------------- */
92 /* Use the schema off the statement. */
93 /* -------------------------------------------------------------------- */
94 OGRFeatureDefn *poDefn;
95
96 poDefn = poCommand->GetResultDefn();
97 if (iGeomColumn >= 0)
98 poDefn->SetGeomType(wkbUnknown);
99 poDefn->Reference();
100
101 /* -------------------------------------------------------------------- */
102 /* Do we have an FID? */
103 /* -------------------------------------------------------------------- */
104 const char *pszExpectedFIDName = CPLGetConfigOption("OCI_FID", "OGR_FID");
105 if (poDefn->GetFieldIndex(pszExpectedFIDName) > -1)
106 {
107 iFIDColumn = poDefn->GetFieldIndex(pszExpectedFIDName);
108 pszFIDName = CPLStrdup(poDefn->GetFieldDefn(iFIDColumn)->GetNameRef());
109 }
110
111 if (EQUAL(pszExpectedFIDName, "OGR_FID") && pszFIDName)
112 {
113 for (int i = 0; i < poDefn->GetFieldCount(); i++)
114 {
115 // This is presumably a Integer since we always create Integer64
116 // with a defined precision
117 if (poDefn->GetFieldDefn(i)->GetType() == OFTInteger64 &&
118 poDefn->GetFieldDefn(i)->GetWidth() == 0)
119 {
120 poDefn->GetFieldDefn(i)->SetType(OFTInteger);
121 }
122 }
123 }
124
125 return poDefn;
126 }