| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: OpenGIS Simple Features Reference Implementation |
| 4 |
* Purpose: Implements OGRIDBDataSource class |
| 5 |
* (based on ODBC and PG drivers). |
| 6 |
* Author: Oleg Semykin, [email protected] |
| 7 |
* |
| 8 |
****************************************************************************** |
| 9 |
* Copyright (c) 2006, Oleg Semykin |
| 10 |
* |
| 11 |
* SPDX-License-Identifier: MIT |
| 12 |
****************************************************************************/ |
| 13 |
|
| 14 |
#include "ogr_idb.h" |
| 15 |
#include "cpl_conv.h" |
| 16 |
#include "cpl_string.h" |
| 17 |
|
| 18 |
/************************************************************************/ |
| 19 |
/* OGRIDBDataSource() */ |
| 20 |
/************************************************************************/ |
| 21 |
|
| 22 |
OGRIDBDataSource::OGRIDBDataSource() |
| 23 |
|
| 24 |
{ |
| 25 |
papoLayers = nullptr; |
| 26 |
nLayers = 0; |
| 27 |
poConn = nullptr; |
| 28 |
bDSUpdate = FALSE; |
| 29 |
} |
| 30 |
|
| 31 |
/************************************************************************/ |
| 32 |
/* ~OGRIDBDataSource() */ |
| 33 |
/************************************************************************/ |
| 34 |
|
| 35 |
OGRIDBDataSource::~OGRIDBDataSource() |
| 36 |
|
| 37 |
{ |
| 38 |
int i; |
| 39 |
|
| 40 |
for (i = 0; i < nLayers; i++) |
| 41 |
delete papoLayers[i]; |
| 42 |
|
| 43 |
CPLFree(papoLayers); |
| 44 |
|
| 45 |
if (poConn != nullptr && poConn->IsOpen()) |
| 46 |
{ |
| 47 |
poConn->Close(); |
| 48 |
CPLDebug("OGR_IDB", "Closing connection"); |
| 49 |
} |
| 50 |
|
| 51 |
delete poConn; |
| 52 |
} |
| 53 |
|
| 54 |
/************************************************************************/ |
| 55 |
/* Open() */ |
| 56 |
/************************************************************************/ |
| 57 |
|
| 58 |
int OGRIDBDataSource::Open(const char *pszNewName, int bUpdate, |
| 59 |
int /* bTestOpen */) |
| 60 |
|
| 61 |
{ |
| 62 |
CPLAssert(poConn == nullptr); |
| 63 |
|
| 64 |
/* -------------------------------------------------------------------- */ |
| 65 |
/* URL: DBI:dbname=.. server=.. user=.. pass=.. table=.. */ |
| 66 |
/* Any of params is optional, table param can repeat more than once */ |
| 67 |
/* -------------------------------------------------------------------- */ |
| 68 |
char *pszDbName = nullptr; |
| 69 |
char *pszServer = nullptr; |
| 70 |
char *pszUser = nullptr; |
| 71 |
char *pszPass = nullptr; |
| 72 |
char **papszTables = nullptr; |
| 73 |
char **papszGeomCol = nullptr; |
| 74 |
|
| 75 |
char **papszTokens = CSLTokenizeString2(pszNewName + 4, " ", 0); |
| 76 |
char *pszToken = nullptr; |
| 77 |
int i = 0; |
| 78 |
|
| 79 |
while ((pszToken = papszTokens[i++]) != nullptr) |
| 80 |
{ |
| 81 |
if (STARTS_WITH_CI(pszToken, "dbname=")) |
| 82 |
pszDbName = CPLStrdup(pszToken + 7); |
| 83 |
else if (STARTS_WITH_CI(pszToken, "server=")) |
| 84 |
pszServer = CPLStrdup(pszToken + 7); |
| 85 |
else if (STARTS_WITH_CI(pszToken, "user=")) |
| 86 |
pszUser = CPLStrdup(pszToken + 5); |
| 87 |
else if (STARTS_WITH_CI(pszToken, "pass=")) |
| 88 |
pszPass = CPLStrdup(pszToken + 5); |
| 89 |
else if (STARTS_WITH_CI(pszToken, "table=")) |
| 90 |
{ |
| 91 |
papszTables = CSLAddString(papszTables, pszToken + 6); |
| 92 |
papszGeomCol = CSLAddString(papszGeomCol, ""); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/* -------------------------------------------------------------------- */ |
| 97 |
/* Initialize based on the DSN. */ |
| 98 |
/* -------------------------------------------------------------------- */ |
| 99 |
ITDBInfo oDbInfo(pszDbName, pszUser, pszServer, pszPass); |
| 100 |
CPLDebug("OGR_IDB", "Connect to: db:'%s' server:'%s', user:'%s', pass:'%s'", |
| 101 |
pszDbName ? pszDbName : "", pszServer ? pszServer : "", |
| 102 |
pszUser ? pszUser : "", pszPass ? pszPass : ""); |
| 103 |
|
| 104 |
CPLFree(pszDbName); |
| 105 |
CPLFree(pszServer); |
| 106 |
CPLFree(pszUser); |
| 107 |
CPLFree(pszPass); |
| 108 |
|
| 109 |
poConn = new ITConnection(oDbInfo); |
| 110 |
|
| 111 |
poConn->AddCallback(IDBErrorHandler, nullptr); |
| 112 |
|
| 113 |
if (!poConn->Open()) |
| 114 |
{ |
| 115 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 116 |
"Unable to initialize IDB connection to %s", pszNewName + 4); |
| 117 |
CSLDestroy(papszTables); |
| 118 |
return FALSE; |
| 119 |
} |
| 120 |
|
| 121 |
bDSUpdate = bUpdate; |
| 122 |
|
| 123 |
/* -------------------------------------------------------------------- */ |
| 124 |
/* If no explicit list of tables was given, check for a list in */ |
| 125 |
/* a geometry_columns table. */ |
| 126 |
/* -------------------------------------------------------------------- */ |
| 127 |
if (papszTables == nullptr) |
| 128 |
{ |
| 129 |
ITCursor oCurr(*poConn); |
| 130 |
|
| 131 |
if (oCurr.Prepare(" SELECT f_table_name, f_geometry_column," |
| 132 |
" geometry_type FROM geometry_columns") && |
| 133 |
oCurr.Open(ITCursor::ReadOnly)) |
| 134 |
{ |
| 135 |
ITRow *row = nullptr; |
| 136 |
while ((row = oCurr.NextRow())) |
| 137 |
{ |
| 138 |
papszTables = |
| 139 |
CSLAddString(papszTables, row->Column(0)->Printable()); |
| 140 |
papszGeomCol = |
| 141 |
CSLAddString(papszGeomCol, row->Column(1)->Printable()); |
| 142 |
row->Release(); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/* -------------------------------------------------------------------- */ |
| 148 |
/* Otherwise our final resort is to return all tables as */ |
| 149 |
/* non-spatial tables. */ |
| 150 |
/* -------------------------------------------------------------------- */ |
| 151 |
if (papszTables == nullptr) |
| 152 |
{ |
| 153 |
ITCursor oTableList(*poConn); |
| 154 |
|
| 155 |
if (oTableList.Prepare("select tabname from systables where " |
| 156 |
"tabtype='T' and tabid > 99") && |
| 157 |
oTableList.Open(ITCursor::ReadOnly)) |
| 158 |
{ |
| 159 |
ITRow *row = nullptr; |
| 160 |
while ((row = oTableList.NextRow())) |
| 161 |
{ |
| 162 |
papszTables = |
| 163 |
CSLAddString(papszTables, row->Column(0)->Printable()); |
| 164 |
papszGeomCol = CSLAddString(papszGeomCol, ""); |
| 165 |
row->Release(); |
| 166 |
} |
| 167 |
} |
| 168 |
else |
| 169 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 170 |
"Unable to open cursor for '%s'", |
| 171 |
oTableList.QueryText().Data()); |
| 172 |
} |
| 173 |
|
| 174 |
/* -------------------------------------------------------------------- */ |
| 175 |
/* If we have an explicit list of requested tables, use them */ |
| 176 |
/* (non-spatial). */ |
| 177 |
/* -------------------------------------------------------------------- */ |
| 178 |
for (int iTable = 0; |
| 179 |
papszTables != nullptr && papszTables[iTable] != nullptr; iTable++) |
| 180 |
{ |
| 181 |
char *pszGeomCol = nullptr; |
| 182 |
|
| 183 |
if (strlen(papszGeomCol[iTable]) > 0) |
| 184 |
pszGeomCol = papszGeomCol[iTable]; |
| 185 |
|
| 186 |
OpenTable(papszTables[iTable], pszGeomCol, bUpdate); |
| 187 |
} |
| 188 |
|
| 189 |
CSLDestroy(papszTables); |
| 190 |
CSLDestroy(papszGeomCol); |
| 191 |
|
| 192 |
return TRUE; |
| 193 |
} |
| 194 |
|
| 195 |
/************************************************************************/ |
| 196 |
/* OpenTable() */ |
| 197 |
/************************************************************************/ |
| 198 |
|
| 199 |
int OGRIDBDataSource::OpenTable(const char *pszNewName, const char *pszGeomCol, |
| 200 |
int bUpdate) |
| 201 |
|
| 202 |
{ |
| 203 |
/* -------------------------------------------------------------------- */ |
| 204 |
/* Create the layer object. */ |
| 205 |
/* -------------------------------------------------------------------- */ |
| 206 |
OGRIDBTableLayer *poLayer; |
| 207 |
|
| 208 |
poLayer = new OGRIDBTableLayer(this); |
| 209 |
|
| 210 |
if (poLayer->Initialize(pszNewName, pszGeomCol, bUpdate)) |
| 211 |
{ |
| 212 |
delete poLayer; |
| 213 |
return FALSE; |
| 214 |
} |
| 215 |
|
| 216 |
/* -------------------------------------------------------------------- */ |
| 217 |
/* Add layer to data source layer list. */ |
| 218 |
/* -------------------------------------------------------------------- */ |
| 219 |
papoLayers = (OGRIDBLayer **)CPLRealloc(papoLayers, sizeof(OGRIDBLayer *) * |
| 220 |
(nLayers + 1)); |
| 221 |
papoLayers[nLayers++] = poLayer; |
| 222 |
|
| 223 |
return TRUE; |
| 224 |
} |
| 225 |
|
| 226 |
/************************************************************************/ |
| 227 |
/* TestCapability() */ |
| 228 |
/************************************************************************/ |
| 229 |
|
| 230 |
int OGRIDBDataSource::TestCapability(const char *pszCap) const |
| 231 |
|
| 232 |
{ |
| 233 |
if (EQUAL(pszCap, ODsCCreateLayer)) |
| 234 |
return TRUE; |
| 235 |
else |
| 236 |
return FALSE; |
| 237 |
} |
| 238 |
|
| 239 |
/************************************************************************/ |
| 240 |
/* GetLayer() */ |
| 241 |
/************************************************************************/ |
| 242 |
|
| 243 |
const OGRLayer *OGRIDBDataSource::GetLayer(int iLayer) const |
| 244 |
|
| 245 |
{ |
| 246 |
if (iLayer < 0 || iLayer >= nLayers) |
| 247 |
return nullptr; |
| 248 |
else |
| 249 |
return papoLayers[iLayer]; |
| 250 |
} |
| 251 |
|
| 252 |
/************************************************************************/ |
| 253 |
/* ExecuteSQL() */ |
| 254 |
/************************************************************************/ |
| 255 |
|
| 256 |
OGRLayer *OGRIDBDataSource::ExecuteSQL(const char *pszSQLCommand, |
| 257 |
OGRGeometry *poSpatialFilter, |
| 258 |
const char *pszDialect) |
| 259 |
|
| 260 |
{ |
| 261 |
/* -------------------------------------------------------------------- */ |
| 262 |
/* Use generic implementation for recognized dialects */ |
| 263 |
/* -------------------------------------------------------------------- */ |
| 264 |
if (IsGenericSQLDialect(pszDialect)) |
| 265 |
return GDALDataset::ExecuteSQL(pszSQLCommand, poSpatialFilter, |
| 266 |
pszDialect); |
| 267 |
|
| 268 |
/* -------------------------------------------------------------------- */ |
| 269 |
/* Execute statement. */ |
| 270 |
/* -------------------------------------------------------------------- */ |
| 271 |
ITCursor *poCurr = new ITCursor(*poConn); |
| 272 |
|
| 273 |
poCurr->Prepare(pszSQLCommand); |
| 274 |
if (!poCurr->Open(ITCursor::ReadOnly)) |
| 275 |
{ |
| 276 |
CPLError(CE_Failure, CPLE_AppDefined, "Error execute SQL: %s", |
| 277 |
pszSQLCommand); |
| 278 |
return nullptr; |
| 279 |
} |
| 280 |
|
| 281 |
/* -------------------------------------------------------------------- */ |
| 282 |
/* Are there result columns for this statement? */ |
| 283 |
/* -------------------------------------------------------------------- */ |
| 284 |
if (poCurr->RowType()->ColumnCount() == 0) |
| 285 |
{ |
| 286 |
delete poCurr; |
| 287 |
CPLErrorReset(); |
| 288 |
return nullptr; |
| 289 |
} |
| 290 |
|
| 291 |
/* -------------------------------------------------------------------- */ |
| 292 |
/* Create a results layer. It will take ownership of the */ |
| 293 |
/* statement. */ |
| 294 |
/* -------------------------------------------------------------------- */ |
| 295 |
OGRIDBSelectLayer *poLayer = new OGRIDBSelectLayer(this, poCurr); |
| 296 |
|
| 297 |
if (poSpatialFilter != nullptr) |
| 298 |
poLayer->SetSpatialFilter(poSpatialFilter); |
| 299 |
|
| 300 |
return poLayer; |
| 301 |
} |
| 302 |
|
| 303 |
/************************************************************************/ |
| 304 |
/* ReleaseResultSet() */ |
| 305 |
/************************************************************************/ |
| 306 |
|
| 307 |
void OGRIDBDataSource::ReleaseResultSet(OGRLayer *poLayer) |
| 308 |
|
| 309 |
{ |
| 310 |
delete poLayer; |
| 311 |
} |
| 312 |
|
| 313 |
ITCallbackResult IDBErrorHandler(const ITErrorManager &err, void *, long) |
| 314 |
{ |
| 315 |
if (err.Error()) |
| 316 |
CPLError(CE_Failure, CPLE_AppDefined, "IDB Error: %s", |
| 317 |
err.ErrorText().Data()); |
| 318 |
|
| 319 |
/*if ( err.Warn() ) |
| 320 |
CPLError( CE_Warning, CPLE_AppDefined, |
| 321 |
"IDB Warning: %s", err.WarningText().Data() ); |
| 322 |
*/ |
| 323 |
return IT_NOTHANDLED; |
| 324 |
} |