| 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Project: VFK Reader (SQLite) |
| 4 | * Purpose: Implements VFKReaderSQLite class. |
| 5 | * Author: Martin Landa, landa.martin gmail.com |
| 6 | * |
| 7 | ****************************************************************************** |
| 8 | * Copyright (c) 2012-2018, Martin Landa <landa.martin gmail.com> |
| 9 | * Copyright (c) 2012-2018, Even Rouault <even dot rouault at spatialys.com> |
| 10 | * |
| 11 | * SPDX-License-Identifier: MIT |
| 12 | ****************************************************************************/ |
| 13 | |
| 14 | #include "cpl_vsi.h"cxx . . $ `<file>/port/cpl_vsi.h`/ |
| 15 | |
| 16 | #include "vfkreader.h"cxx . . $ `<file>/ogr/ogrsf_frmts/vfk/vfkreader.h`/ |
| 17 | #include "vfkreaderp.h"cxx . . $ `<file>/ogr/ogrsf_frmts/vfk/vfkreaderp.h`/ |
| 18 | |
| 19 | #include "cpl_conv.h"cxx . . $ `<file>/port/cpl_conv.h`/ |
| 20 | #include "cpl_error.h"cxx . . $ `<file>/port/cpl_error.h`/ |
| 21 | |
| 22 | #include <cstring> |
| 23 | |
| 24 | #include "ogr_geometry.h"cxx . . $ `<file>/ogr/ogr_geometry.h`/ |
| 25 | |
| 26 | /*! |
| 27 | \brief VFKReaderSQLite constructor |
| 28 | */ |
| 29 | VFKReaderSQLitecxx . . $ VFKReaderSQLite#::VFKReaderSQLitecxx . . $ VFKReaderSQLite#VFKReaderSQLite(d08e26ba0925c6b4).(const GDALOpenInfocxx . . $ GDALOpenInfo# *poOpenInfo) |
| 30 | : VFKReadercxx . . $ VFKReader#VFKReader(d08e26ba0925c6b4).(poOpenInfo), m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname.(nullptr), m_poDBcxx . . $ VFKReaderSQLite#m_poDB.(nullptr), |
| 31 | // True - build geometry from DB |
| 32 | // False - store also geometry in DB |
| 33 | m_bSpatialcxx . . $ VFKReaderSQLite#m_bSpatial.(CPLTestBool(CPLGetConfigOption("OGR_VFK_DB_SPATIAL", "YES"))), |
| 34 | m_bNewDbcxx . . $ VFKReaderSQLite#m_bNewDb.(false), m_bDbSourcecxx . . $ VFKReaderSQLite#m_bDbSource.(false) |
| 35 | { |
| 36 | size_t nLen = 0; |
| 37 | VSIStatBufL sStatBufDb; |
| 38 | |
| 39 | m_bDbSourcecxx . . $ VFKReaderSQLite#m_bDbSource. = |
| 40 | poOpenInfo->nHeaderBytescxx . . $ GDALOpenInfo#nHeaderBytes. >= 16 && |
| 41 | STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!((const char *)poOpenInfo->pabyHeader, "SQLite format 3"); |
| 42 | |
| 43 | const char *pszDbNameConf = CPLGetConfigOption("OGR_VFK_DB_NAME", nullptr); |
| 44 | CPLString osDbName; |
| 45 | |
| 46 | if (!m_bDbSourcecxx . . $ VFKReaderSQLite#m_bDbSource.) |
| 47 | { |
| 48 | m_bNewDbcxx . . $ VFKReaderSQLite#m_bNewDb. = true; |
| 49 | |
| 50 | /* open tmp SQLite DB (re-use DB file if already exists) */ |
| 51 | if (pszDbNameConf) |
| 52 | { |
| 53 | osDbName = pszDbNameConf; |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | osDbName = CPLResetExtensionSafe(m_pszFilenamecxx . . $ VFKReader#m_pszFilename., "db"); |
| 58 | } |
| 59 | nLen = osDbName.length(); |
| 60 | if (nLen > 2048) |
| 61 | { |
| 62 | nLen = 2048; |
| 63 | osDbName.resize(nLen); |
| 64 | } |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | // m_bNewDb = false; |
| 69 | nLen = strlen(m_pszFilenamecxx . . $ VFKReader#m_pszFilename.); |
| 70 | osDbName = m_pszFilenamecxx . . $ VFKReader#m_pszFilename.; |
| 71 | } |
| 72 | |
| 73 | m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname. = new char[nLen + 1]; |
| 74 | std::strncpy(m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname., osDbName.c_str(), nLen); |
| 75 | m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname.[nLen] = 0; |
| 76 | |
| 77 | CPLDebug("OGR-VFK", "Using internal DB: %s", m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname.); |
| 78 | |
| 79 | if (!m_bDbSourcecxx . . $ VFKReaderSQLite#m_bDbSource. && VSIStatL(osDbName, &sStatBufDb) == 0) |
| 80 | { |
| 81 | /* Internal DB exists */ |
| 82 | if (CPLTestBool(CPLGetConfigOption("OGR_VFK_DB_OVERWRITE", "NO"))) |
| 83 | { |
| 84 | m_bNewDbcxx . . $ VFKReaderSQLite#m_bNewDb. = true; // Overwrite existing DB. |
| 85 | CPLDebug("OGR-VFK", |
| 86 | "Internal DB (%s) already exists and will be overwritten", |
| 87 | m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname.); |
| 88 | VSIUnlink(osDbName); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | if (pszDbNameConf == nullptr && |
| 93 | m_poFStatcxx . . $ VFKReader#m_poFStat.->st_mtimecxx . . $ `<external>/16045971866008360577/struct_stat.h:78:11`! > sStatBufDb.st_mtimecxx . . $ `<external>/16045971866008360577/struct_stat.h:78:11`!) |
| 94 | { |
| 95 | CPLDebug("OGR-VFK", |
| 96 | "Found %s but ignoring because it appears\n" |
| 97 | "be older than the associated VFK file.", |
| 98 | osDbName.c_str()); |
| 99 | m_bNewDbcxx . . $ VFKReaderSQLite#m_bNewDb. = true; |
| 100 | VSIUnlink(osDbName); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | m_bNewDbcxx . . $ VFKReaderSQLite#m_bNewDb. = false; /* re-use existing DB */ |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | CPLDebug("OGR-VFK", "New DB: %s Spatial: %s", m_bNewDbcxx . . $ VFKReaderSQLite#m_bNewDb. ? "yes" : "no", |
| 110 | m_bSpatialcxx . . $ VFKReaderSQLite#m_bSpatial. ? "yes" : "no"); |
| 111 | |
| 112 | if (SQLITE_OKcxx . . $ `<external>/2400532043262193228/sqlite3.h:449:9`! != sqlite3_open(osDbName, &m_poDBcxx . . $ VFKReaderSQLite#m_poDB.)) |
| 113 | { |
| 114 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Creating SQLite DB failed: %s", |
| 115 | sqlite3_errmsg(m_poDBcxx . . $ VFKReaderSQLite#m_poDB.)); |
| 116 | } |
| 117 | |
| 118 | CPLString osCommand; |
| 119 | if (m_bDbSourcecxx . . $ VFKReaderSQLite#m_bDbSource.) |
| 120 | { |
| 121 | /* check if it is really VFK DB datasource */ |
| 122 | char *pszErrMsg = nullptr; |
| 123 | char **papszResult = nullptr; |
| 124 | int nRowCount = 0; |
| 125 | int nColCount = 0; |
| 126 | |
| 127 | osCommand.Printf( |
| 128 | "SELECT * FROM sqlite_master WHERE type='table' AND name='%s'", |
| 129 | VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!); |
| 130 | sqlite3_get_table(m_poDBcxx . . $ VFKReaderSQLite#m_poDB., osCommand.c_str(), &papszResult, &nRowCount, |
| 131 | &nColCount, &pszErrMsg); |
| 132 | sqlite3_free_table(papszResult); |
| 133 | sqlite3_free(pszErrMsg); |
| 134 | |
| 135 | if (nRowCount != 1) |
| 136 | { |
| 137 | /* DB is not valid VFK datasource */ |
| 138 | sqlite3_close(m_poDBcxx . . $ VFKReaderSQLite#m_poDB.); |
| 139 | m_poDBcxx . . $ VFKReaderSQLite#m_poDB. = nullptr; |
| 140 | return; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (!m_bNewDbcxx . . $ VFKReaderSQLite#m_bNewDb.) |
| 145 | { |
| 146 | /* check if DB is up-to-date datasource */ |
| 147 | char *pszErrMsg = nullptr; |
| 148 | char **papszResult = nullptr; |
| 149 | int nRowCount = 0; |
| 150 | int nColCount = 0; |
| 151 | |
| 152 | osCommand.Printf("SELECT * FROM %s LIMIT 1", VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!); |
| 153 | sqlite3_get_table(m_poDBcxx . . $ VFKReaderSQLite#m_poDB., osCommand.c_str(), &papszResult, &nRowCount, |
| 154 | &nColCount, &pszErrMsg); |
| 155 | sqlite3_free_table(papszResult); |
| 156 | sqlite3_free(pszErrMsg); |
| 157 | |
| 158 | if (nColCount != 7) |
| 159 | { |
| 160 | /* it seems that DB is outdated, let's create new DB from |
| 161 | * scratch */ |
| 162 | if (m_bDbSourcecxx . . $ VFKReaderSQLite#m_bDbSource.) |
| 163 | { |
| 164 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, |
| 165 | "Invalid VFK DB datasource"); |
| 166 | } |
| 167 | |
| 168 | if (SQLITE_OKcxx . . $ `<external>/2400532043262193228/sqlite3.h:449:9`! != sqlite3_close(m_poDBcxx . . $ VFKReaderSQLite#m_poDB.)) |
| 169 | { |
| 170 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, |
| 171 | "Closing SQLite DB failed: %s", |
| 172 | sqlite3_errmsg(m_poDBcxx . . $ VFKReaderSQLite#m_poDB.)); |
| 173 | } |
| 174 | VSIUnlink(osDbName); |
| 175 | if (SQLITE_OKcxx . . $ `<external>/2400532043262193228/sqlite3.h:449:9`! != sqlite3_open(osDbName, &m_poDBcxx . . $ VFKReaderSQLite#m_poDB.)) |
| 176 | { |
| 177 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, |
| 178 | "Creating SQLite DB failed: %s", |
| 179 | sqlite3_errmsg(m_poDBcxx . . $ VFKReaderSQLite#m_poDB.)); |
| 180 | } |
| 181 | CPLDebug("OGR-VFK", |
| 182 | "Internal DB (%s) is invalid - will be re-created", |
| 183 | m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname.); |
| 184 | |
| 185 | m_bNewDbcxx . . $ VFKReaderSQLite#m_bNewDb. = true; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | char *pszErrMsg = nullptr; |
| 190 | CPL_IGNORE_RET_VAL(sqlite3_exec(m_poDBcxx . . $ VFKReaderSQLite#m_poDB., "PRAGMA synchronous = OFF", nullptr, |
| 191 | nullptr, &pszErrMsg)); |
| 192 | sqlite3_free(pszErrMsg); |
| 193 | |
| 194 | if (m_bNewDbcxx . . $ VFKReaderSQLite#m_bNewDb.) |
| 195 | { |
| 196 | OGRSpatialReferencecxx . . $ OGRSpatialReference# *poSRS; |
| 197 | |
| 198 | /* new DB, create support metadata tables */ |
| 199 | osCommand.Printf( |
| 200 | "CREATE TABLE %s (file_name text, file_size integer, " |
| 201 | "table_name text, num_records integer, " |
| 202 | "num_features integer, num_geometries integer, table_defn text)", |
| 203 | VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!); |
| 204 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osCommand.c_str()); |
| 205 | |
| 206 | /* header table */ |
| 207 | osCommand.Printf("CREATE TABLE %s (key text, value text)", |
| 208 | VFK_DB_HEADER_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:39:9`!); |
| 209 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osCommand.c_str()); |
| 210 | |
| 211 | /* geometry_columns */ |
| 212 | osCommand.Printf( |
| 213 | "CREATE TABLE %s (f_table_name text, f_geometry_column text, " |
| 214 | "geometry_type integer, coord_dimension integer, " |
| 215 | "srid integer, geometry_format text)", |
| 216 | VFK_DB_GEOMETRY_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:41:9`!); |
| 217 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osCommand.c_str()); |
| 218 | |
| 219 | /* spatial_ref_sys */ |
| 220 | osCommand.Printf( |
| 221 | "CREATE TABLE %s (srid interer, auth_name text, auth_srid text, " |
| 222 | "srtext text)", |
| 223 | VFK_DB_SPATIAL_REF_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:42:9`!); |
| 224 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osCommand.c_str()); |
| 225 | |
| 226 | /* insert S-JTSK into spatial_ref_sys table */ |
| 227 | poSRS = new OGRSpatialReferencecxx . . $ OGRSpatialReference#OGRSpatialReference(85c52e162fed56f9).(); |
| 228 | poSRS->SetAxisMappingStrategycxx . . $ OGRSpatialReference#SetAxisMappingStrategy(3d39f43974738d6a).(OAMS_TRADITIONAL_GIS_ORDER); |
| 229 | if (poSRS->importFromEPSGcxx . . $ OGRSpatialReference#importFromEPSG(7864480464b09eea).(5514) != OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!) |
| 230 | { |
| 231 | char *pszWKT = nullptr; |
| 232 | poSRS->exportToWktcxx . . $ OGRSpatialReference#exportToWkt(7572283d65580925).(&pszWKT); |
| 233 | osCommand.Printf("INSERT INTO %s (srid, auth_name, auth_srid, " |
| 234 | "srtext) VALUES (5514, 'EPSG', 5514, '%s')", |
| 235 | VFK_DB_SPATIAL_REF_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:42:9`!, pszWKT); |
| 236 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osCommand.c_str()); |
| 237 | CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszWKT); |
| 238 | } |
| 239 | delete poSRS; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /*! |
| 244 | \brief VFKReaderSQLite destructor |
| 245 | */ |
| 246 | VFKReaderSQLitecxx . . $ VFKReaderSQLite#::~cxx . . $ VFKReaderSQLite#`~VFKReaderSQLite`(ced63f7c635d850d).VFKReaderSQLitecxx . . $ VFKReaderSQLite#() |
| 247 | { |
| 248 | /* clean loaded properties */ |
| 249 | for (int i = 0; i < m_nDataBlockCountcxx . . $ VFKReader#m_nDataBlockCount.; i++) |
| 250 | m_papoDataBlockcxx . . $ VFKReader#m_papoDataBlock.[i]->CleanPropertiescxx . . $ IVFKDataBlock#CleanProperties(b126dc7c1de90089).(); |
| 251 | |
| 252 | /* close backend SQLite DB */ |
| 253 | if (SQLITE_OKcxx . . $ `<external>/2400532043262193228/sqlite3.h:449:9`! != sqlite3_close(m_poDBcxx . . $ VFKReaderSQLite#m_poDB.)) |
| 254 | { |
| 255 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Closing SQLite DB failed: %s", |
| 256 | sqlite3_errmsg(m_poDBcxx . . $ VFKReaderSQLite#m_poDB.)); |
| 257 | } |
| 258 | CPLDebug("OGR-VFK", "Internal DB (%s) closed", m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname.); |
| 259 | |
| 260 | /* delete backend SQLite DB if requested */ |
| 261 | if (CPLTestBool(CPLGetConfigOption("OGR_VFK_DB_DELETE", "NO"))) |
| 262 | { |
| 263 | CPLDebug("OGR-VFK", "Internal DB (%s) deleted", m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname.); |
| 264 | VSIUnlink(m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname.); |
| 265 | } |
| 266 | delete[] m_pszDBnamecxx . . $ VFKReaderSQLite#m_pszDBname.; |
| 267 | } |
| 268 | |
| 269 | /*! |
| 270 | \brief Load data block definitions (&B) |
| 271 | |
| 272 | Call VFKReader::OpenFile() before this function. |
| 273 | |
| 274 | \return number of data blocks or -1 on error |
| 275 | */ |
| 276 | int VFKReaderSQLitecxx . . $ VFKReaderSQLite#::ReadDataBlockscxx . . $ VFKReaderSQLite#ReadDataBlocks(73da94e76e6bd037).(bool bSuppressGeometry) |
| 277 | { |
| 278 | CPLString osSQL; |
| 279 | osSQL.Printf("SELECT table_name, table_defn FROM %s", VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!); |
| 280 | sqlite3_stmt *hStmt = PrepareStatementcxx . . $ VFKReaderSQLite#PrepareStatement(c9d29368b06b7377).(osSQL.c_str()); |
| 281 | while (ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(22e4417d6f03bbbb).(hStmt) == OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!) |
| 282 | { |
| 283 | const char *pszName = (const char *)sqlite3_column_text(hStmt, 0); |
| 284 | const char *pszDefn = (const char *)sqlite3_column_text(hStmt, 1); |
| 285 | if (pszName && pszDefn) |
| 286 | { |
| 287 | IVFKDataBlockcxx . . $ IVFKDataBlock# *poNewDataBlock = |
| 288 | (IVFKDataBlockcxx . . $ IVFKDataBlock# *)CreateDataBlockcxx . . $ VFKReaderSQLite#CreateDataBlock(18454ff126c3ddbe).(pszName); |
| 289 | poNewDataBlock->SetGeometryTypecxx . . $ IVFKDataBlock#SetGeometryType(7dc970dea2893676).(bSuppressGeometry); |
| 290 | if (poNewDataBlock->GetGeometryTypecxx . . $ IVFKDataBlock#GetGeometryType(5a40c54c5ff210ce).() != wkbNone) |
| 291 | { |
| 292 | /* may happen: |
| 293 | * first open with "-oo SUPPRESS_GEOMETRY=YES --config |
| 294 | * OGR_VFK_DB_READ_ALL_BLOCKS NO" than attempt to fetch feature |
| 295 | * from geometry-included layer: SOBR -fid 1 |
| 296 | */ |
| 297 | cpl::down_cast<VFKDataBlockSQLitecxx . . $ VFKDataBlockSQLite# *>(poNewDataBlock) |
| 298 | ->AddGeometryColumncxx . . $ VFKDataBlockSQLite#AddGeometryColumn(455f465bc33b4cdf).(); |
| 299 | } |
| 300 | poNewDataBlock->SetPropertiescxx . . $ IVFKDataBlock#SetProperties(85c52e162fed56f9).(pszDefn); |
| 301 | VFKReadercxx . . $ VFKReader#::AddDataBlockcxx . . $ VFKReader#AddDataBlock(44ba7e8bfce4db83).(poNewDataBlock, nullptr); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | CPL_IGNORE_RET_VAL( |
| 306 | sqlite3_exec(m_poDBcxx . . $ VFKReaderSQLite#m_poDB., "BEGIN", nullptr, nullptr, nullptr)); |
| 307 | /* Read data from VFK file */ |
| 308 | const int nDataBlocks = VFKReadercxx . . $ VFKReader#::ReadDataBlockscxx . . $ VFKReader#ReadDataBlocks(73da94e76e6bd037).(bSuppressGeometry); |
| 309 | CPL_IGNORE_RET_VAL( |
| 310 | sqlite3_exec(m_poDBcxx . . $ VFKReaderSQLite#m_poDB., "COMMIT", nullptr, nullptr, nullptr)); |
| 311 | |
| 312 | return nDataBlocks; |
| 313 | } |
| 314 | |
| 315 | /*! |
| 316 | \brief Load data records (&D) |
| 317 | |
| 318 | Call VFKReader::OpenFile() before this function. |
| 319 | |
| 320 | \param poDataBlock limit to selected data block or NULL for all |
| 321 | |
| 322 | \return number of data records or -1 on error |
| 323 | */ |
| 324 | int64_t VFKReaderSQLitecxx . . $ VFKReaderSQLite#::ReadDataRecordscxx . . $ VFKReaderSQLite#ReadDataRecords(6764505ea3a507ec).(IVFKDataBlockcxx . . $ IVFKDataBlock# *poDataBlock) |
| 325 | { |
| 326 | CPLString osSQL; |
| 327 | IVFKDataBlockcxx . . $ IVFKDataBlock# *poDataBlockCurrent = nullptr; |
| 328 | sqlite3_stmt *hStmt = nullptr; |
| 329 | const char *pszName = nullptr; |
| 330 | int64_t nDataRecords = 0; |
| 331 | bool bReadVfk = !m_bDbSourcecxx . . $ VFKReaderSQLite#m_bDbSource.; |
| 332 | bool bReadDb = false; |
| 333 | |
| 334 | if (poDataBlock) |
| 335 | { /* read records only for selected data block */ |
| 336 | /* table name */ |
| 337 | pszName = poDataBlock->GetNamecxx . . $ IVFKDataBlock#GetName(a9f41ea0e82d88cf).(); |
| 338 | |
| 339 | /* check for existing records (re-use already inserted data) */ |
| 340 | osSQL.Printf("SELECT num_records FROM %s WHERE " |
| 341 | "table_name = '%s'", |
| 342 | VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!, pszName); |
| 343 | hStmt = PrepareStatementcxx . . $ VFKReaderSQLite#PrepareStatement(c9d29368b06b7377).(osSQL.c_str()); |
| 344 | if (ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(22e4417d6f03bbbb).(hStmt) == OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!) |
| 345 | { |
| 346 | nDataRecords = sqlite3_column_int64(hStmt, 0); |
| 347 | if (nDataRecords > 0) |
| 348 | bReadDb = true; /* -> read from DB */ |
| 349 | else |
| 350 | nDataRecords = 0; |
| 351 | } |
| 352 | sqlite3_finalize(hStmt); |
| 353 | } |
| 354 | else |
| 355 | { /* read all data blocks */ |
| 356 | /* check for existing records (re-use already inserted data) */ |
| 357 | osSQL.Printf("SELECT COUNT(*) FROM %s WHERE num_records > 0", |
| 358 | VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!); |
| 359 | hStmt = PrepareStatementcxx . . $ VFKReaderSQLite#PrepareStatement(c9d29368b06b7377).(osSQL.c_str()); |
| 360 | if (ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(22e4417d6f03bbbb).(hStmt) == OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`! && |
| 361 | sqlite3_column_int(hStmt, 0) != 0) |
| 362 | bReadDb = true; /* -> read from DB */ |
| 363 | sqlite3_finalize(hStmt); |
| 364 | |
| 365 | /* check if file is already registered in DB (requires file_size column) |
| 366 | */ |
| 367 | osSQL.Printf("SELECT COUNT(*) FROM %s WHERE file_name = '%s' AND " |
| 368 | "file_size = " CPL_FRMT_GUIBcxx . . $ `port/cpl_port.h:259:9`! " AND num_records > 0", |
| 369 | VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!, CPLGetFilename(m_pszFilenamecxx . . $ VFKReader#m_pszFilename.), |
| 370 | (GUIntBigcxx . . $ GUIntBig#)m_poFStatcxx . . $ VFKReader#m_poFStat.->st_size); |
| 371 | hStmt = PrepareStatementcxx . . $ VFKReaderSQLite#PrepareStatement(c9d29368b06b7377).(osSQL.c_str()); |
| 372 | if (ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(22e4417d6f03bbbb).(hStmt) == OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`! && |
| 373 | sqlite3_column_int(hStmt, 0) > 0) |
| 374 | { |
| 375 | /* -> file already registered (filename & size is the same) */ |
| 376 | CPLDebug("OGR-VFK", "VFK file %s already loaded in DB", |
| 377 | m_pszFilenamecxx . . $ VFKReader#m_pszFilename.); |
| 378 | bReadVfk = false; |
| 379 | } |
| 380 | sqlite3_finalize(hStmt); |
| 381 | } |
| 382 | |
| 383 | if (bReadDb) |
| 384 | { /* read records from DB */ |
| 385 | /* read from DB */ |
| 386 | VFKFeatureSQLitecxx . . $ VFKFeatureSQLite# *poNewFeature = nullptr; |
| 387 | |
| 388 | poDataBlockCurrent = nullptr; |
| 389 | for (int iDataBlock = 0; iDataBlock < GetDataBlockCountcxx . . $ VFKReader#GetDataBlockCount(455f465bc33b4cdf).(); iDataBlock++) |
| 390 | { |
| 391 | poDataBlockCurrent = GetDataBlockcxx . . $ VFKReader#GetDataBlock(84679fbf55c3a6dc).(iDataBlock); |
| 392 | |
| 393 | if (poDataBlock && poDataBlock != poDataBlockCurrent) |
| 394 | continue; |
| 395 | |
| 396 | poDataBlockCurrent->SetFeatureCountcxx . . $ IVFKDataBlock#SetFeatureCount(918ebd27a9ddbad3).(0); /* avoid recursive call */ |
| 397 | |
| 398 | pszName = poDataBlockCurrent->GetNamecxx . . $ IVFKDataBlock#GetName(a9f41ea0e82d88cf).(); |
| 399 | CPLAssertcxx . . $ `port/cpl_error.h:353:9`!(nullptr != pszName); |
| 400 | |
| 401 | osSQL.Printf("SELECT %s,_rowid_ FROM %s ", FID_COLUMNcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:35:9`!, pszName); |
| 402 | if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszName, "SBP") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszName, "SBPG")) |
| 403 | osSQL += "WHERE PORADOVE_CISLO_BODU = 1 "; |
| 404 | osSQL += "ORDER BY "; |
| 405 | osSQL += FID_COLUMNcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:35:9`!; |
| 406 | hStmt = PrepareStatementcxx . . $ VFKReaderSQLite#PrepareStatement(c9d29368b06b7377).(osSQL.c_str()); |
| 407 | nDataRecords = 0; |
| 408 | while (ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(22e4417d6f03bbbb).(hStmt) == OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!) |
| 409 | { |
| 410 | const long iFID = sqlite3_column_int(hStmt, 0); |
| 411 | int iRowId = sqlite3_column_int(hStmt, 1); |
| 412 | poNewFeature = |
| 413 | new VFKFeatureSQLitecxx . . $ VFKFeatureSQLite#VFKFeatureSQLite(892b3eadfc176bb7).(poDataBlockCurrent, iRowId, iFID); |
| 414 | poDataBlockCurrent->AddFeaturecxx . . $ IVFKDataBlock#AddFeature(9b6d40346934b7dd).(poNewFeature); |
| 415 | nDataRecords++; |
| 416 | } |
| 417 | |
| 418 | /* check DB consistency */ |
| 419 | osSQL.Printf("SELECT num_features FROM %s WHERE table_name = '%s'", |
| 420 | VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!, pszName); |
| 421 | hStmt = PrepareStatementcxx . . $ VFKReaderSQLite#PrepareStatement(c9d29368b06b7377).(osSQL.c_str()); |
| 422 | if (ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(22e4417d6f03bbbb).(hStmt) == OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!) |
| 423 | { |
| 424 | const int nFeatDB = sqlite3_column_int(hStmt, 0); |
| 425 | if (nFeatDB > 0 && |
| 426 | nFeatDB != poDataBlockCurrent->GetFeatureCountcxx . . $ IVFKDataBlock#GetFeatureCount(a03497b3070eedac).()) |
| 427 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, |
| 428 | "%s: Invalid number of features " CPL_FRMT_GIBcxx . . $ `port/cpl_port.h:257:9`! |
| 429 | " (should be %d)", |
| 430 | pszName, poDataBlockCurrent->GetFeatureCountcxx . . $ IVFKDataBlock#GetFeatureCount(a03497b3070eedac).(), |
| 431 | nFeatDB); |
| 432 | } |
| 433 | sqlite3_finalize(hStmt); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (bReadVfk) |
| 438 | { /* read from VFK file and insert records into DB */ |
| 439 | /* begin transaction */ |
| 440 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).("BEGIN"); |
| 441 | |
| 442 | /* Store VFK header to DB */ |
| 443 | StoreInfo2DBcxx . . $ VFKReaderSQLite#StoreInfo2DB(49f6e7a06ebc5aa8).(); |
| 444 | |
| 445 | /* Insert VFK data records into DB */ |
| 446 | const int64_t nExtraRecords = VFKReadercxx . . $ VFKReader#::ReadDataRecordscxx . . $ VFKReader#ReadDataRecords(6764505ea3a507ec).(poDataBlock); |
| 447 | if (nExtraRecords >= 0) |
| 448 | { |
| 449 | nDataRecords += nExtraRecords; |
| 450 | |
| 451 | /* update VFK_DB_TABLE table */ |
| 452 | poDataBlockCurrent = nullptr; |
| 453 | for (int iDataBlock = 0; iDataBlock < GetDataBlockCountcxx . . $ VFKReader#GetDataBlockCount(455f465bc33b4cdf).(); |
| 454 | iDataBlock++) |
| 455 | { |
| 456 | poDataBlockCurrent = GetDataBlockcxx . . $ VFKReader#GetDataBlock(84679fbf55c3a6dc).(iDataBlock); |
| 457 | |
| 458 | if (poDataBlock && poDataBlock != poDataBlockCurrent) |
| 459 | continue; |
| 460 | |
| 461 | /* update number of records in metadata table */ |
| 462 | osSQL.Printf("UPDATE %s SET num_records = %d WHERE " |
| 463 | "table_name = '%s'", |
| 464 | VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!, poDataBlockCurrent->GetRecordCountcxx . . $ IVFKDataBlock#GetRecordCount(fbb8d3eed9710374).(), |
| 465 | poDataBlockCurrent->GetNamecxx . . $ IVFKDataBlock#GetName(a9f41ea0e82d88cf).()); |
| 466 | |
| 467 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osSQL); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /* create indices if not exist */ |
| 472 | CreateIndicescxx . . $ VFKReaderSQLite#CreateIndices(49f6e7a06ebc5aa8).(); |
| 473 | |
| 474 | /* commit transaction */ |
| 475 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).("COMMIT"); |
| 476 | } |
| 477 | |
| 478 | return nDataRecords; |
| 479 | } |
| 480 | |
| 481 | /*! |
| 482 | \brief Store header info to VFK_DB_HEADER |
| 483 | */ |
| 484 | void VFKReaderSQLitecxx . . $ VFKReaderSQLite#::StoreInfo2DBcxx . . $ VFKReaderSQLite#StoreInfo2DB(49f6e7a06ebc5aa8).() |
| 485 | { |
| 486 | for (std::mapcxx . . $ std/map#<CPLString, CPLString>::iteratorcxx . . $ std/map#iterator# i = poInfocxx . . $ VFKReader#poInfo..begincxx . . $ std/map#begin(7e4de33083b6e738).(); |
| 487 | i !=cxx . . $ std/`operator!=`(be6a71a859368080). poInfocxx . . $ VFKReader#poInfo..endcxx . . $ std/map#end(7e4de33083b6e738).(); ++cxx . . $ std/_Rb_tree_iterator#operator++(5b825426e5b44d2c).i) |
| 488 | { |
| 489 | const char *value = i->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).second.c_str(); |
| 490 | |
| 491 | const char q = (value[0] == '"') ? ' ' : '"'; |
| 492 | |
| 493 | CPLString osSQL; |
| 494 | osSQL.Printf("INSERT INTO %s VALUES(\"%s\", %c%s%c)", |
| 495 | VFK_DB_HEADER_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:39:9`!, i->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).first.c_str(), q, value, q); |
| 496 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osSQL); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /*! |
| 501 | \brief Create indices for newly added db tables (datablocks) |
| 502 | */ |
| 503 | void VFKReaderSQLitecxx . . $ VFKReaderSQLite#::CreateIndicescxx . . $ VFKReaderSQLite#CreateIndices(49f6e7a06ebc5aa8).() |
| 504 | { |
| 505 | const char *pszBlockName; |
| 506 | CPLString osIndexName, osSQL; |
| 507 | VFKDataBlockSQLitecxx . . $ VFKDataBlockSQLite# *poDataBlock; |
| 508 | |
| 509 | sqlite3_stmt *hStmt; |
| 510 | |
| 511 | for (int iLayer = 0; iLayer < GetDataBlockCountcxx . . $ VFKReader#GetDataBlockCount(455f465bc33b4cdf).(); iLayer++) |
| 512 | { |
| 513 | poDataBlock = (VFKDataBlockSQLitecxx . . $ VFKDataBlockSQLite# *)GetDataBlockcxx . . $ VFKReader#GetDataBlock(84679fbf55c3a6dc).(iLayer); |
| 514 | pszBlockName = poDataBlock->GetNamecxx . . $ IVFKDataBlock#GetName(a9f41ea0e82d88cf).(); |
| 515 | |
| 516 | /* ogr_fid */ |
| 517 | osIndexName.Printf("%s_%s", pszBlockName, FID_COLUMNcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:35:9`!); |
| 518 | |
| 519 | /* check if index on ogr_fid column exists */ |
| 520 | osSQL.Printf("SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' " |
| 521 | "AND name = '%s'", |
| 522 | osIndexName.c_str()); |
| 523 | hStmt = PrepareStatementcxx . . $ VFKReaderSQLite#PrepareStatement(c9d29368b06b7377).(osSQL.c_str()); |
| 524 | |
| 525 | if (ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(22e4417d6f03bbbb).(hStmt) == OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`! && |
| 526 | sqlite3_column_int(hStmt, 0) > 0) |
| 527 | { |
| 528 | /* index on ogr_fid column exists, skip creating indices |
| 529 | for current datablock */ |
| 530 | sqlite3_finalize(hStmt); |
| 531 | continue; |
| 532 | } |
| 533 | sqlite3_finalize(hStmt); |
| 534 | |
| 535 | /* create index on ogr_fid */ |
| 536 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).( |
| 537 | osIndexName.c_str(), pszBlockName, FID_COLUMNcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:35:9`!, |
| 538 | !(EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "SBP") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "SBPG"))); |
| 539 | |
| 540 | if (poDataBlock->GetGeometryTypecxx . . $ IVFKDataBlock#GetGeometryType(5a40c54c5ff210ce).() == wkbNone) |
| 541 | { |
| 542 | /* skip geometry-related indices */ |
| 543 | continue; |
| 544 | } |
| 545 | |
| 546 | if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "SOBR") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "OBBP") || |
| 547 | EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "SPOL") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "OB") || |
| 548 | EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "OP") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "OBPEJ") || |
| 549 | EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "SBP") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "SBPG") || |
| 550 | EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "HP") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "DPM") || |
| 551 | EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "ZVB") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "PAR") || |
| 552 | EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "BUD")) |
| 553 | { |
| 554 | const char *pszKey = |
| 555 | cpl::down_cast<VFKDataBlockSQLitecxx . . $ VFKDataBlockSQLite# *>(poDataBlock)->GetKeycxx . . $ VFKDataBlockSQLite#GetKey(a9f41ea0e82d88cf).(); |
| 556 | if (pszKey) |
| 557 | { |
| 558 | /* ID */ |
| 559 | osIndexName.Printf("%s_%s", pszBlockName, pszKey); |
| 560 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).(osIndexName.c_str(), pszBlockName, pszKey, |
| 561 | !m_bAmendmentcxx . . $ VFKReader#m_bAmendment.); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /* create other indices used for building geometry */ |
| 566 | if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "SBP")) |
| 567 | { |
| 568 | /* SBP */ |
| 569 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).("SBP_OB", pszBlockName, "OB_ID", false); |
| 570 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).("SBP_HP", pszBlockName, "HP_ID", false); |
| 571 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).("SBP_DPM", pszBlockName, "DPM_ID", false); |
| 572 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).("SBP_OB_HP_DPM", pszBlockName, "OB_ID,HP_ID,DPM_ID", |
| 573 | true); |
| 574 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).("SBP_OB_POR", pszBlockName, "OB_ID,PORADOVE_CISLO_BODU", |
| 575 | false); |
| 576 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).("SBP_HP_POR", pszBlockName, "HP_ID,PORADOVE_CISLO_BODU", |
| 577 | false); |
| 578 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).("SBP_DPM_POR", pszBlockName, |
| 579 | "DPM_ID,PORADOVE_CISLO_BODU", false); |
| 580 | } |
| 581 | else if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "HP")) |
| 582 | { |
| 583 | /* HP */ |
| 584 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).("HP_PAR1", pszBlockName, "PAR_ID_1", false); |
| 585 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).("HP_PAR2", pszBlockName, "PAR_ID_2", false); |
| 586 | } |
| 587 | else if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "OB")) |
| 588 | { |
| 589 | /* OP */ |
| 590 | CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).("OB_BUD", pszBlockName, "BUD_ID", false); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /*! |
| 596 | \brief Create index |
| 597 | |
| 598 | If creating unique index fails, then non-unique index is created instead. |
| 599 | |
| 600 | \param name index name |
| 601 | \param table table name |
| 602 | \param column column(s) name |
| 603 | \param unique true to create unique index |
| 604 | */ |
| 605 | void VFKReaderSQLitecxx . . $ VFKReaderSQLite#::CreateIndexcxx . . $ VFKReaderSQLite#CreateIndex(39b2f20f6acb9928).(const char *name, const char *table, |
| 606 | const char *column, bool unique) |
| 607 | { |
| 608 | CPLString osSQL; |
| 609 | |
| 610 | if (unique) |
| 611 | { |
| 612 | osSQL.Printf("CREATE UNIQUE INDEX %s ON %s (%s)", name, table, column); |
| 613 | if (ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osSQL.c_str()) == OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!) |
| 614 | { |
| 615 | return; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | osSQL.Printf("CREATE INDEX %s ON %s (%s)", name, table, column); |
| 620 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osSQL.c_str()); |
| 621 | } |
| 622 | |
| 623 | /*! |
| 624 | \brief Create new data block |
| 625 | |
| 626 | \param pszBlockName name of the block to be created |
| 627 | |
| 628 | \return pointer to VFKDataBlockSQLite instance |
| 629 | */ |
| 630 | IVFKDataBlockcxx . . $ IVFKDataBlock# *VFKReaderSQLitecxx . . $ VFKReaderSQLite#::CreateDataBlockcxx . . $ VFKReaderSQLite#CreateDataBlock(18454ff126c3ddbe).(const char *pszBlockName) |
| 631 | { |
| 632 | /* create new data block, i.e. table in DB */ |
| 633 | return new VFKDataBlockSQLitecxx . . $ VFKDataBlockSQLite#(pszBlockName, (IVFKReadercxx . . $ IVFKReader# *)this); |
| 634 | } |
| 635 | |
| 636 | /*! |
| 637 | \brief Create DB table from VFKDataBlock (SQLITE only) |
| 638 | |
| 639 | \param poDataBlock pointer to VFKDataBlock instance |
| 640 | */ |
| 641 | void VFKReaderSQLitecxx . . $ VFKReaderSQLite#::AddDataBlockcxx . . $ VFKReaderSQLite#AddDataBlock(44ba7e8bfce4db83).(IVFKDataBlockcxx . . $ IVFKDataBlock# *poDataBlock, |
| 642 | const char *pszDefn) |
| 643 | { |
| 644 | CPLString osColumn; |
| 645 | |
| 646 | const char *pszBlockName = poDataBlock->GetNamecxx . . $ IVFKDataBlock#GetName(a9f41ea0e82d88cf).(); |
| 647 | |
| 648 | /* register table in VFK_DB_TABLE */ |
| 649 | CPLString osCommand; |
| 650 | osCommand.Printf("SELECT COUNT(*) FROM %s WHERE " |
| 651 | "table_name = '%s'", |
| 652 | VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!, pszBlockName); |
| 653 | sqlite3_stmt *hStmt = PrepareStatementcxx . . $ VFKReaderSQLite#PrepareStatement(c9d29368b06b7377).(osCommand.c_str()); |
| 654 | |
| 655 | if (ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(22e4417d6f03bbbb).(hStmt) == OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!) |
| 656 | { |
| 657 | if (sqlite3_column_int(hStmt, 0) == 0) |
| 658 | { |
| 659 | |
| 660 | osCommand.Printf("CREATE TABLE IF NOT EXISTS '%s' (", pszBlockName); |
| 661 | for (int i = 0; i < poDataBlock->GetPropertyCountcxx . . $ IVFKDataBlock#GetPropertyCount(455f465bc33b4cdf).(); i++) |
| 662 | { |
| 663 | VFKPropertyDefncxx . . $ VFKPropertyDefn# *poPropertyDefn = poDataBlock->GetPropertycxx . . $ IVFKDataBlock#GetProperty(96c67b7a13b97e99).(i); |
| 664 | if (i > 0) |
| 665 | osCommand += ","; |
| 666 | osColumn.Printf("%s %s", poPropertyDefn->GetNamecxx . . $ VFKPropertyDefn#GetName(a9f41ea0e82d88cf).(), |
| 667 | poPropertyDefn->GetTypeSQLcxx . . $ VFKPropertyDefn#GetTypeSQL(54570bbf7b35bc1a).().c_str()); |
| 668 | osCommand += osColumn; |
| 669 | } |
| 670 | osColumn.Printf(",%s integer", FID_COLUMNcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:35:9`!); |
| 671 | osCommand += osColumn; |
| 672 | if (poDataBlock->GetGeometryTypecxx . . $ IVFKDataBlock#GetGeometryType(5a40c54c5ff210ce).() != wkbNone) |
| 673 | { |
| 674 | osColumn.Printf(",%s blob", GEOM_COLUMNcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:36:9`!); |
| 675 | osCommand += osColumn; |
| 676 | } |
| 677 | osCommand += ")"; |
| 678 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osCommand.c_str()); /* CREATE TABLE */ |
| 679 | |
| 680 | /* update VFK_DB_TABLE meta-table */ |
| 681 | osCommand.Printf( |
| 682 | "INSERT INTO %s (file_name, file_size, table_name, " |
| 683 | "num_records, num_features, num_geometries, table_defn) VALUES " |
| 684 | "('%s', " CPL_FRMT_GUIBcxx . . $ `port/cpl_port.h:259:9`! ", '%s', -1, 0, 0, '%s')", |
| 685 | VFK_DB_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:40:9`!, CPLGetFilename(m_pszFilenamecxx . . $ VFKReader#m_pszFilename.), |
| 686 | (GUIntBigcxx . . $ GUIntBig#)m_poFStatcxx . . $ VFKReader#m_poFStat.->st_size, pszBlockName, pszDefn); |
| 687 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osCommand.c_str()); |
| 688 | |
| 689 | int geom_type = cpl::down_cast<VFKDataBlockSQLitecxx . . $ VFKDataBlockSQLite# *>(poDataBlock) |
| 690 | ->GetGeometrySQLTypecxx . . $ VFKDataBlockSQLite#GetGeometrySQLType(455f465bc33b4cdf).(); |
| 691 | /* update VFK_DB_GEOMETRY_TABLE */ |
| 692 | osCommand.Printf("INSERT INTO %s (f_table_name, f_geometry_column, " |
| 693 | "geometry_type, " |
| 694 | "coord_dimension, srid, geometry_format) VALUES " |
| 695 | "('%s', '%s', %d, 2, 5514, 'WKB')", |
| 696 | VFK_DB_GEOMETRY_TABLEcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:41:9`!, pszBlockName, GEOM_COLUMNcxx . . $ `ogr/ogrsf_frmts/vfk/vfkreader.h:36:9`!, |
| 697 | geom_type); |
| 698 | ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osCommand.c_str()); |
| 699 | } |
| 700 | sqlite3_finalize(hStmt); |
| 701 | } |
| 702 | |
| 703 | return VFKReadercxx . . $ VFKReader#::AddDataBlockcxx . . $ VFKReader#AddDataBlock(44ba7e8bfce4db83).(poDataBlock, nullptr); |
| 704 | } |
| 705 | |
| 706 | /*! |
| 707 | \brief Prepare SQL statement |
| 708 | |
| 709 | \param pszSQLCommand SQL statement to be prepared |
| 710 | |
| 711 | \return pointer to sqlite3_stmt instance or NULL on error |
| 712 | */ |
| 713 | sqlite3_stmt *VFKReaderSQLitecxx . . $ VFKReaderSQLite#::PrepareStatementcxx . . $ VFKReaderSQLite#PrepareStatement(c9d29368b06b7377).(const char *pszSQLCommand) |
| 714 | { |
| 715 | CPLDebug("OGR-VFK", "VFKReaderSQLite::PrepareStatement(): %s", |
| 716 | pszSQLCommand); |
| 717 | |
| 718 | sqlite3_stmt *hStmt = nullptr; |
| 719 | const int rc = |
| 720 | sqlite3_prepare_v2(m_poDBcxx . . $ VFKReaderSQLite#m_poDB., pszSQLCommand, -1, &hStmt, nullptr); |
| 721 | |
| 722 | // TODO(schwehr): if( rc == SQLITE_OK ) return NULL; |
| 723 | if (rc != SQLITE_OKcxx . . $ `<external>/2400532043262193228/sqlite3.h:449:9`!) |
| 724 | { |
| 725 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, |
| 726 | "In PrepareStatement(): sqlite3_prepare_v2(%s):\n %s", |
| 727 | pszSQLCommand, sqlite3_errmsg(m_poDBcxx . . $ VFKReaderSQLite#m_poDB.)); |
| 728 | |
| 729 | if (hStmt != nullptr) |
| 730 | { |
| 731 | sqlite3_finalize(hStmt); |
| 732 | } |
| 733 | |
| 734 | return nullptr; |
| 735 | } |
| 736 | |
| 737 | return hStmt; |
| 738 | } |
| 739 | |
| 740 | /*! |
| 741 | \brief Execute prepared SQL statement |
| 742 | |
| 743 | \param hStmt pointer to sqlite3_stmt |
| 744 | |
| 745 | \return OGRERR_NONE on success |
| 746 | */ |
| 747 | OGRErr VFKReaderSQLitecxx . . $ VFKReaderSQLite#::ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(22e4417d6f03bbbb).(sqlite3_stmt *&hStmt) |
| 748 | { |
| 749 | const int rc = sqlite3_step(hStmt); |
| 750 | if (rc != SQLITE_ROWcxx . . $ `<external>/2400532043262193228/sqlite3.h:479:9`!) |
| 751 | { |
| 752 | if (rc == SQLITE_DONEcxx . . $ `<external>/2400532043262193228/sqlite3.h:480:9`!) |
| 753 | { |
| 754 | sqlite3_finalize(hStmt); |
| 755 | hStmt = nullptr; |
| 756 | return OGRERR_NOT_ENOUGH_DATAcxx . . $ `ogr/ogr_core.h:391:9`!; |
| 757 | } |
| 758 | |
| 759 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, |
| 760 | "In ExecuteSQL(): sqlite3_step:\n %s", |
| 761 | sqlite3_errmsg(m_poDBcxx . . $ VFKReaderSQLite#m_poDB.)); |
| 762 | if (hStmt) |
| 763 | { |
| 764 | sqlite3_finalize(hStmt); |
| 765 | hStmt = nullptr; |
| 766 | } |
| 767 | return OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!; |
| 768 | } |
| 769 | |
| 770 | return OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!; |
| 771 | } |
| 772 | |
| 773 | /*! |
| 774 | \brief Execute SQL statement (SQLITE only) |
| 775 | |
| 776 | \param pszSQLCommand SQL command to execute |
| 777 | \param eErrLevel if equal to CE_None, no error message will be emitted on |
| 778 | failure. |
| 779 | |
| 780 | \return OGRERR_NONE on success or OGRERR_FAILURE on failure |
| 781 | */ |
| 782 | OGRErr VFKReaderSQLitecxx . . $ VFKReaderSQLite#::ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(const char *pszSQLCommand, CPLErr eErrLevel) |
| 783 | { |
| 784 | char *pszErrMsg = nullptr; |
| 785 | |
| 786 | if (SQLITE_OKcxx . . $ `<external>/2400532043262193228/sqlite3.h:449:9`! != |
| 787 | sqlite3_exec(m_poDBcxx . . $ VFKReaderSQLite#m_poDB., pszSQLCommand, nullptr, nullptr, &pszErrMsg)) |
| 788 | { |
| 789 | if (eErrLevel != CE_None) |
| 790 | { |
| 791 | CPLError(eErrLevel, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "In ExecuteSQL(%s): %s", |
| 792 | pszSQLCommand, pszErrMsg ? pszErrMsg : "(null)"); |
| 793 | } |
| 794 | sqlite3_free(pszErrMsg); |
| 795 | |
| 796 | return OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!; |
| 797 | } |
| 798 | |
| 799 | return OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!; |
| 800 | } |
| 801 | |
| 802 | /*! |
| 803 | \brief Add feature |
| 804 | |
| 805 | \param poDataBlock pointer to VFKDataBlock instance |
| 806 | \param poFeature pointer to VFKFeature instance |
| 807 | */ |
| 808 | OGRErr VFKReaderSQLitecxx . . $ VFKReaderSQLite#::AddFeaturecxx . . $ VFKReaderSQLite#AddFeature(97b87c3b656f4cc0).(IVFKDataBlockcxx . . $ IVFKDataBlock# *poDataBlock, |
| 809 | VFKFeaturecxx . . $ VFKFeature# *poFeature) |
| 810 | { |
| 811 | CPLString osValue; |
| 812 | |
| 813 | const char *pszBlockName = poDataBlock->GetNamecxx . . $ IVFKDataBlock#GetName(a9f41ea0e82d88cf).(); |
| 814 | CPLString osCommand; |
| 815 | osCommand.Printf("INSERT INTO '%s' VALUES(", pszBlockName); |
| 816 | |
| 817 | for (int i = 0; i < poDataBlock->GetPropertyCountcxx . . $ IVFKDataBlock#GetPropertyCount(455f465bc33b4cdf).(); i++) |
| 818 | { |
| 819 | const OGRFieldType ftype = poDataBlock->GetPropertycxx . . $ IVFKDataBlock#GetProperty(96c67b7a13b97e99).(i)->GetTypecxx . . $ VFKPropertyDefn#GetType(b365ab774e05458e).(); |
| 820 | const VFKPropertycxx . . $ VFKProperty# *poProperty = poFeature->GetPropertycxx . . $ VFKFeature#GetProperty(d552f75970f2dfc4).(i); |
| 821 | if (i > 0) |
| 822 | osCommand += ","; |
| 823 | |
| 824 | if (!poProperty || poProperty->IsNullcxx . . $ VFKProperty#IsNull(50ce9a9e25b4a850).()) |
| 825 | { |
| 826 | osValue.Printf("NULL"); |
| 827 | } |
| 828 | else |
| 829 | { |
| 830 | switch (ftype) |
| 831 | { |
| 832 | case OFTInteger: |
| 833 | osValue.Printf("%d", poProperty->GetValueIcxx . . $ VFKProperty#GetValueI(455f465bc33b4cdf).()); |
| 834 | break; |
| 835 | case OFTInteger64: |
| 836 | osValue.Printf(CPL_FRMT_GIBcxx . . $ `port/cpl_port.h:257:9`!, poProperty->GetValueI64cxx . . $ VFKProperty#GetValueI64(98833e9ab7de59a4).()); |
| 837 | break; |
| 838 | case OFTReal: |
| 839 | osValue.Printf("%f", poProperty->GetValueDcxx . . $ VFKProperty#GetValueD(21b6c25e127938e2).()); |
| 840 | break; |
| 841 | case OFTString: |
| 842 | osValue.Printf("'%s'", poProperty->GetValueScxx . . $ VFKProperty#GetValueS(50cd6a058ec5c72c).(true)); |
| 843 | break; |
| 844 | default: |
| 845 | osValue.Printf("'%s'", poProperty->GetValueScxx . . $ VFKProperty#GetValueS(50cd6a058ec5c72c).(true)); |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | osCommand += osValue; |
| 850 | } |
| 851 | osValue.Printf("," CPL_FRMT_GIBcxx . . $ `port/cpl_port.h:257:9`!, poFeature->GetFIDcxx . . $ IVFKFeature#GetFID(98833e9ab7de59a4).()); |
| 852 | if (poDataBlock->GetGeometryTypecxx . . $ IVFKDataBlock#GetGeometryType(5a40c54c5ff210ce).() != wkbNone) |
| 853 | { |
| 854 | osValue += ",NULL"; |
| 855 | } |
| 856 | osCommand += osValue; |
| 857 | osCommand += ")"; |
| 858 | |
| 859 | if (ExecuteSQLcxx . . $ VFKReaderSQLite#ExecuteSQL(e02f33c2abca146b).(osCommand.c_str(), CE_Warning) != OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!) |
| 860 | return OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!; |
| 861 | |
| 862 | if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "SBP") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszBlockName, "SBPG")) |
| 863 | { |
| 864 | const VFKPropertycxx . . $ VFKProperty# *poProperty = |
| 865 | poFeature->GetPropertycxx . . $ VFKFeature#GetProperty(cd9e7c5de95cbd03).("PORADOVE_CISLO_BODU"); |
| 866 | if (poProperty == nullptr) |
| 867 | { |
| 868 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, |
| 869 | "Cannot find property PORADOVE_CISLO_BODU"); |
| 870 | return OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!; |
| 871 | } |
| 872 | if (poProperty->GetValueI64cxx . . $ VFKProperty#GetValueI64(98833e9ab7de59a4).() != 1) |
| 873 | return OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!; |
| 874 | } |
| 875 | |
| 876 | VFKFeatureSQLitecxx . . $ VFKFeatureSQLite# *poNewFeature = new VFKFeatureSQLitecxx . . $ VFKFeatureSQLite#VFKFeatureSQLite(892b3eadfc176bb7).( |
| 877 | poDataBlock, poDataBlock->GetRecordCountcxx . . $ IVFKDataBlock#GetRecordCount(fbb8d3eed9710374).(RecordValidcxx . . $ RecordValid.) + 1, |
| 878 | poFeature->GetFIDcxx . . $ IVFKFeature#GetFID(98833e9ab7de59a4).()); |
| 879 | poDataBlock->AddFeaturecxx . . $ IVFKDataBlock#AddFeature(9b6d40346934b7dd).(poNewFeature); |
| 880 | |
| 881 | return OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!; |
| 882 | } |