Debian Codegraph @

ogr/ogrsf_frmts/idb/ogridblayer.cpp

raw
1 /******************************************************************************
2 *
3 * Project: OpenGIS Simple Features Reference Implementation
4 * Purpose: Implements OGRIDBLayer class, code shared between
5 * the direct table access, and the generic SQL results
6 * (based on ODBC and PG drivers).
7 * Author: Oleg Semykin, [email protected]
8 *
9 ******************************************************************************
10 * Copyright (c) 2006, Oleg Semykin
11 *
12 * SPDX-License-Identifier: MIT
13 ****************************************************************************/
14
15 #include "cpl_conv.h"
16 #include "ogr_idb.h"
17 #include "cpl_string.h"
18
19 /************************************************************************/
20 /* OGRIDBLayer() */
21 /************************************************************************/
22
23 OGRIDBLayer::OGRIDBLayer()
24
25 {
26 poDS = nullptr;
27
28 bGeomColumnWKB = FALSE;
29 pszFIDColumn = nullptr;
30 pszGeomColumn = nullptr;
31
32 m_poCurr = nullptr;
33
34 iNextShapeId = 0;
35
36 poSRS = nullptr;
37 nSRSId = -2; // we haven't even queried the database for it yet.
38 poFeatureDefn = nullptr;
39 }
40
41 /************************************************************************/
42 /* ~OGRIDBLayer() */
43 /************************************************************************/
44
45 OGRIDBLayer::~OGRIDBLayer()
46
47 {
48 if (m_poCurr != nullptr)
49 {
50 m_poCurr->Close();
51 delete m_poCurr;
52 m_poCurr = nullptr;
53 }
54
55 if (pszGeomColumn)
56 CPLFree(pszGeomColumn);
57
58 if (pszFIDColumn)
59 CPLFree(pszFIDColumn);
60
61 if (poFeatureDefn)
62 {
63 poFeatureDefn->Release();
64 poFeatureDefn = nullptr;
65 }
66
67 if (poSRS)
68 poSRS->Release();
69 }
70
71 /************************************************************************/
72 /* BuildFeatureDefn() */
73 /* */
74 /* Build feature definition from a set of column definitions */
75 /* set on a statement. Sift out geometry and FID fields. */
76 /************************************************************************/
77
78 CPLErr OGRIDBLayer::BuildFeatureDefn(const char *pszLayerName, ITCursor *poCurr)
79
80 {
81 poFeatureDefn = new OGRFeatureDefn(pszLayerName);
82 SetDescription(poFeatureDefn->GetName());
83 const ITTypeInfo *poInfo = poCurr->RowType();
84 int nRawColumns = poInfo->ColumnCount();
85
86 poFeatureDefn->Reference();
87
88 for (int iCol = 0; iCol < nRawColumns; iCol++)
89 {
90 const char *pszColName = poInfo->ColumnName(iCol);
91 const ITTypeInfo *poTI = poInfo->ColumnType(iCol);
92 const char *pszTypName = poTI->Name();
93
94 OGRFieldDefn oField(pszColName, OFTString);
95
96 oField.SetWidth(MAX(0, poTI->Bound()));
97
98 if (pszGeomColumn != nullptr && EQUAL(pszColName, pszGeomColumn))
99 continue;
100
101 if (STARTS_WITH_CI(pszTypName, "st_") && pszGeomColumn == nullptr)
102 {
103 // We found spatial column!
104 pszGeomColumn = CPLStrdup(pszColName);
105
106 if (EQUAL("st_point", pszTypName))
107 poFeatureDefn->SetGeomType(wkbPoint);
108 else if (EQUAL("st_linestring", pszTypName))
109 poFeatureDefn->SetGeomType(wkbLineString);
110 else if (EQUAL("st_polygon", pszTypName))
111 poFeatureDefn->SetGeomType(wkbPolygon);
112 else if (EQUAL("st_multipoint", pszTypName))
113 poFeatureDefn->SetGeomType(wkbMultiPoint);
114 else if (EQUAL("st_multilinestring", pszTypName))
115 poFeatureDefn->SetGeomType(wkbMultiLineString);
116 else if (EQUAL("st_multipolygon", pszTypName))
117 poFeatureDefn->SetGeomType(wkbMultiPolygon);
118
119 continue;
120 }
121
122 // Check other field types
123 if (EQUAL(pszTypName, "blob") || EQUAL(pszTypName, "byte") ||
124 EQUAL(pszTypName, "opaque") || EQUAL(pszTypName, "text") ||
125 STARTS_WITH_CI(pszTypName, "list") ||
126 STARTS_WITH_CI(pszTypName, "collection") ||
127 STARTS_WITH_CI(pszTypName, "row") ||
128 STARTS_WITH_CI(pszTypName, "set"))
129 {
130 CPLDebug("OGR_IDB",
131 "'%s' column type not supported yet. Column '%s'",
132 pszTypName, pszColName);
133 continue;
134 }
135
136 if (STARTS_WITH_CI(pszTypName, "st_"))
137 {
138 oField.SetType(OFTBinary);
139 }
140 else if (EQUAL(pszTypName, "date"))
141 {
142 oField.SetType(OFTDate);
143 }
144 else if (EQUAL(pszTypName, "datetime"))
145 {
146 oField.SetType(OFTDateTime);
147 }
148 else if (EQUAL(pszTypName, "decimal") || EQUAL(pszTypName, "money") ||
149 EQUAL(pszTypName, "float") || EQUAL(pszTypName, "smallfloat"))
150 {
151 oField.SetType(OFTReal);
152 oField.SetPrecision(MAX(0, poTI->Scale())); // -1 for numeric
153 }
154 else if (EQUAL(pszTypName, "integer") || EQUAL(pszTypName, "serial"))
155 {
156 oField.SetType(OFTInteger);
157 // 10 as hardcoded max int32 value length + 1 sig bit
158 oField.SetWidth(11);
159 }
160 else if (EQUAL(pszTypName, "smallint"))
161 {
162 oField.SetType(OFTInteger);
163 // 5 as hardcoded max int16 value length + 1 sig bit
164 oField.SetWidth(6);
165 }
166 else
167 {
168 // leave as string:
169 // *char, character, character varying, *varchar
170 // interval. int8, serial8
171 }
172
173 poFeatureDefn->AddFieldDefn(&oField);
174 }
175
176 /* -------------------------------------------------------------------- */
177 /* If we don't already have an FID, check if there is a special */
178 /* FID named column available. */
179 /* -------------------------------------------------------------------- */
180 if (pszFIDColumn == nullptr)
181 {
182 const char *pszOGR_FID = CPLGetConfigOption("IDB_OGR_FID", "OGR_FID");
183 if (poFeatureDefn->GetFieldIndex(pszOGR_FID) != -1)
184 pszFIDColumn = CPLStrdup(pszOGR_FID);
185 }
186
187 if (pszFIDColumn != nullptr)
188 CPLDebug("OGR_IDB", "Using column %s as FID for table %s.",
189 pszFIDColumn, poFeatureDefn->GetName());
190 else
191 CPLDebug("OGR_IDB", "Table %s has no identified FID column.",
192 poFeatureDefn->GetName());
193
194 return CE_None;
195 }
196
197 /************************************************************************/
198 /* ResetReading() */
199 /************************************************************************/
200
201 void OGRIDBLayer::ResetReading()
202
203 {
204 iNextShapeId = 0;
205 }
206
207 /************************************************************************/
208 /* GetNextFeature() */
209 /************************************************************************/
210
211 OGRFeature *OGRIDBLayer::GetNextFeature()
212
213 {
214 while (true)
215 {
216 OGRFeature *poFeature;
217
218 poFeature = GetNextRawFeature();
219 if (poFeature == nullptr)
220 return nullptr;
221
222 if ((m_poFilterGeom == nullptr ||
223 FilterGeometry(poFeature->GetGeometryRef())) &&
224 (m_poAttrQuery == nullptr || m_poAttrQuery->Evaluate(poFeature)))
225 return poFeature;
226
227 delete poFeature;
228 }
229 }
230
231 /************************************************************************/
232 /* GetNextRawFeature() */
233 /************************************************************************/
234
235 OGRFeature *OGRIDBLayer::GetNextRawFeature()
236
237 {
238 if (GetQuery() == nullptr)
239 return nullptr;
240
241 /* -------------------------------------------------------------------- */
242 /* If we are marked to restart then do so, and fetch a record. */
243 /* -------------------------------------------------------------------- */
244 ITRow *row = m_poCurr->NextRow();
245 if (!row)
246 {
247 delete m_poCurr;
248 m_poCurr = nullptr;
249 return nullptr;
250 }
251
252 iNextShapeId++;
253 m_nFeaturesRead++;
254
255 /* -------------------------------------------------------------------- */
256 /* Create a feature from the current result. */
257 /* -------------------------------------------------------------------- */
258 int iField;
259 OGRFeature *poFeature = new OGRFeature(poFeatureDefn);
260
261 const ITTypeInfo *poRowType = m_poCurr->RowType();
262 int nFieldCount = poRowType->ColumnCount();
263
264 for (iField = 0; iField < nFieldCount; iField++)
265 {
266 /* --------------------------------------------------------------------
267 */
268 /* Handle FID column */
269 /* --------------------------------------------------------------------
270 */
271 if (pszFIDColumn != nullptr &&
272 EQUAL(poRowType->ColumnName(iField), pszFIDColumn))
273 poFeature->SetFID(atoi(row->Column(iField)->Printable()));
274
275 /* --------------------------------------------------------------------
276 */
277 /* Handle geometry */
278 /* --------------------------------------------------------------------
279 */
280 if (pszGeomColumn != nullptr &&
281 EQUAL(poRowType->ColumnName(iField), pszGeomColumn))
282 {
283 OGRGeometry *poGeom = nullptr;
284 OGRErr eErr = OGRERR_NONE;
285
286 ITValue *v = row->Column(iField);
287
288 if (!v->IsNull() && !bGeomColumnWKB)
289 {
290 const char *pszGeomText = v->Printable();
291 if (pszGeomText != nullptr)
292 eErr = OGRGeometryFactory::createFromWkt(pszGeomText, poSRS,
293 &poGeom);
294 }
295 else if (!v->IsNull() && bGeomColumnWKB)
296 {
297 ITDatum *rv = nullptr;
298 if (v->QueryInterface(ITDatumIID, (void **)&rv) ==
299 IT_QUERYINTERFACE_SUCCESS)
300 {
301 int nLength = rv->DataLength();
302 unsigned char *wkb = (unsigned char *)rv->Data();
303
304 eErr = OGRGeometryFactory::createFromWkb(wkb, poSRS,
305 &poGeom, nLength);
306 rv->Release();
307 }
308 }
309
310 v->Release();
311
312 if (eErr != OGRERR_NONE)
313 {
314 const char *pszMessage;
315
316 switch (eErr)
317 {
318 case OGRERR_NOT_ENOUGH_DATA:
319 pszMessage = "Not enough data to deserialize";
320 break;
321 case OGRERR_UNSUPPORTED_GEOMETRY_TYPE:
322 pszMessage = "Unsupported geometry type";
323 break;
324 case OGRERR_CORRUPT_DATA:
325 pszMessage = "Corrupt data";
326 break;
327 default:
328 pszMessage = "Unrecognized error";
329 }
330 CPLError(CE_Failure, CPLE_AppDefined, "GetNextRawFeature(): %s",
331 pszMessage);
332 }
333
334 if (poGeom != nullptr)
335 {
336 poFeature->SetGeometryDirectly(poGeom);
337 }
338
339 continue;
340 }
341
342 /* --------------------------------------------------------------------
343 */
344 /* Transfer regular data fields. */
345 /* --------------------------------------------------------------------
346 */
347 int iOGRField =
348 poFeatureDefn->GetFieldIndex(poRowType->ColumnName(iField));
349
350 if (iOGRField < 0)
351 continue;
352
353 const char *pszColData = row->Column(iField)->Printable();
354
355 if (!pszColData)
356 continue;
357
358 if (poFeatureDefn->GetFieldDefn(iOGRField)->GetType() == OFTBinary)
359 poFeature->SetField(iOGRField,
360 poRowType->ColumnType(iField)->Size(),
361 (GByte *)pszColData);
362 else
363 poFeature->SetField(iOGRField, pszColData);
364 }
365
366 row->Release();
367 return poFeature;
368 }
369
370 /************************************************************************/
371 /* GetFeature() */
372 /************************************************************************/
373
374 OGRFeature *OGRIDBLayer::GetFeature(GIntBig nFeatureId)
375
376 {
377 /* This should be implemented directly! */
378
379 return OGRLayer::GetFeature(nFeatureId);
380 }
381
382 /************************************************************************/
383 /* TestCapability() */
384 /************************************************************************/
385
386 int OGRIDBLayer::TestCapability(const char * /*pszCap*/) const
387
388 {
389 return FALSE;
390 }
391
392 /************************************************************************/
393 /* GetSpatialRef() */
394 /************************************************************************/
395
396 const OGRSpatialReference *OGRIDBLayer::GetSpatialRef() const
397
398 {
399 return poSRS;
400 }
401
402 /************************************************************************/
403 /* GetFIDColumn() */
404 /************************************************************************/
405
406 const char *OGRIDBLayer::GetFIDColumn() const
407
408 {
409 if (pszFIDColumn != nullptr)
410 return pszFIDColumn;
411 else
412 return "";
413 }
414
415 /************************************************************************/
416 /* GetGeometryColumn() */
417 /************************************************************************/
418
419 const char *OGRIDBLayer::GetGeometryColumn() const
420
421 {
422 if (pszGeomColumn != nullptr)
423 return pszGeomColumn;
424 else
425 return "";
426 }
427
428 /* TODO Query to get layer extent */
429 /*
430 EXECUTE FUNCTION SE_BoundingBox ('table_name', 'geom_column' )
431 */