LCOV - code coverage report
Current view: directory - ogr/ogrsf_frmts/vfk - vfkfeaturesqlite.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 67 32 47.8 %
Date: 2013-03-30 Functions: 14 4 28.6 %

       1                 : /******************************************************************************
       2                 :  * $Id: vfkfeaturesqlite.cpp 25721 2013-03-09 16:21:46Z martinl $
       3                 :  *
       4                 :  * Project:  VFK Reader - Feature definition (SQLite)
       5                 :  * Purpose:  Implements VFKFeatureSQLite class.
       6                 :  * Author:   Martin Landa, landa.martin gmail.com
       7                 :  *
       8                 :  ******************************************************************************
       9                 :  * Copyright (c) 2012-2013, Martin Landa <landa.martin gmail.com>
      10                 :  *
      11                 :  * Permission is hereby granted, free of charge, to any person
      12                 :  * obtaining a copy of this software and associated documentation
      13                 :  * files (the "Software"), to deal in the Software without
      14                 :  * restriction, including without limitation the rights to use, copy,
      15                 :  * modify, merge, publish, distribute, sublicense, and/or sell copies
      16                 :  * of the Software, and to permit persons to whom the Software is
      17                 :  * furnished to do so, subject to the following conditions:
      18                 :  *
      19                 :  * The above copyright notice and this permission notice shall be
      20                 :  * included in all copies or substantial portions of the Software.
      21                 :  *
      22                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      23                 :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      24                 :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
      25                 :  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
      26                 :  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
      27                 :  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
      28                 :  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      29                 :  * SOFTWARE.
      30                 :  ****************************************************************************/
      31                 : 
      32                 : #include "vfkreader.h"
      33                 : #include "vfkreaderp.h"
      34                 : 
      35                 : #include "cpl_conv.h"
      36                 : #include "cpl_error.h"
      37                 : 
      38                 : /*!
      39                 :   \brief VFKFeatureSQLite constructor (from DB)
      40                 : 
      41                 :   Read VFK feature from DB
      42                 : 
      43                 :   \param poDataBlock pointer to related IVFKDataBlock
      44                 : */
      45               0 : VFKFeatureSQLite::VFKFeatureSQLite(IVFKDataBlock *poDataBlock) : IVFKFeature(poDataBlock)
      46                 : {
      47               0 :     m_hStmt  = NULL;
      48               0 :     m_iRowId = m_poDataBlock->GetFeatureCount() + 1; /* starts at 1 */
      49                 : 
      50                 :     /* set FID from DB */
      51               0 :     SetFIDFromDB(); /* -> m_nFID */
      52               0 : }
      53                 : 
      54                 : /*!
      55                 :   \brief VFKFeatureSQLite constructor 
      56                 : 
      57                 :   \param poDataBlock pointer to related IVFKDataBlock
      58                 :   \param iRowId feature DB rowid (starts at 1)
      59                 :   \param nFID feature id
      60                 : */
      61              80 : VFKFeatureSQLite::VFKFeatureSQLite(IVFKDataBlock *poDataBlock, int iRowId, long nFID) : IVFKFeature(poDataBlock)
      62                 : {
      63              80 :     m_hStmt  = NULL;
      64              80 :     m_iRowId = iRowId;
      65              80 :     m_nFID   = nFID;
      66              80 : }
      67                 : 
      68                 : /*!
      69                 :   \brief Read FID from DB
      70                 : */
      71               0 : OGRErr VFKFeatureSQLite::SetFIDFromDB()
      72                 : {
      73               0 :     CPLString   osSQL;
      74                 :     
      75                 :     osSQL.Printf("SELECT %s FROM %s WHERE rowid = %d",
      76               0 :                  FID_COLUMN, m_poDataBlock->GetName(), m_iRowId);
      77               0 :     if (ExecuteSQL(osSQL.c_str()) != OGRERR_NONE)
      78               0 :         return OGRERR_FAILURE;
      79                 : 
      80               0 :     m_nFID = sqlite3_column_int(m_hStmt, 0);
      81                 :   
      82               0 :     FinalizeSQL();
      83                 :     
      84               0 :     return OGRERR_NONE;
      85                 : }
      86                 : 
      87                 : /*!
      88                 :   \brief Finalize SQL statement
      89                 : */
      90              15 : void VFKFeatureSQLite::FinalizeSQL()
      91                 : {
      92              15 :     sqlite3_finalize(m_hStmt);
      93              15 :     m_hStmt = NULL;
      94              15 : }
      95                 : 
      96                 : /*!
      97                 :   \brief Execute SQL (select) statement
      98                 : 
      99                 :   \param pszSQLCommand SQL command string
     100                 : 
     101                 :   \return OGRERR_NONE on success or OGRERR_FAILURE on error
     102                 : */
     103              15 : OGRErr VFKFeatureSQLite::ExecuteSQL(const char *pszSQLCommand)
     104                 : {
     105                 :     int rc;
     106                 : 
     107                 :     sqlite3  *poDB;
     108                 :     
     109              15 :     VFKReaderSQLite *poReader = (VFKReaderSQLite *) m_poDataBlock->GetReader();
     110              15 :     poDB = poReader->m_poDB;
     111                 :     
     112                 :     rc = sqlite3_prepare(poDB, pszSQLCommand, strlen(pszSQLCommand),
     113              15 :                          &m_hStmt, NULL);
     114              15 :     if (rc != SQLITE_OK) {
     115                 :         CPLError(CE_Failure, CPLE_AppDefined, 
     116                 :                  "In LoadProperties(): sqlite3_prepare(%s):\n  %s",
     117               0 :                  pszSQLCommand, sqlite3_errmsg(poDB));
     118                 :         
     119               0 :         if(m_hStmt != NULL) {
     120               0 :             FinalizeSQL();
     121                 :         }
     122               0 :         return OGRERR_FAILURE;
     123                 :     }
     124              15 :     rc = sqlite3_step(m_hStmt);
     125              15 :     if (rc != SQLITE_ROW) {
     126                 :         CPLError(CE_Failure, CPLE_AppDefined, 
     127                 :                  "In ExecuteSQL(): sqlite3_step(%s):\n  %s", 
     128               0 :                  pszSQLCommand, sqlite3_errmsg(poDB));
     129                 :         
     130               0 :         if (m_hStmt) {
     131               0 :             FinalizeSQL();
     132                 :         }
     133                 :         
     134               0 :         return OGRERR_FAILURE;
     135                 :     }
     136                 : 
     137              15 :     return OGRERR_NONE;
     138                 : }
     139                 : 
     140                 : /*!
     141                 :   \brief VFKFeatureSQLite constructor (derived from VFKFeature)
     142                 : 
     143                 :   Read VFK feature from VFK file and insert it into DB
     144                 : */
     145               0 : VFKFeatureSQLite::VFKFeatureSQLite(const VFKFeature *poVFKFeature) : IVFKFeature(poVFKFeature->m_poDataBlock)
     146                 : {
     147               0 :     m_nFID   = poVFKFeature->m_nFID;
     148               0 :     m_hStmt  = NULL;
     149               0 :     m_iRowId = m_poDataBlock->GetFeatureCount() + 1; /* starts at 1 */
     150               0 : }
     151                 : 
     152                 : /*!
     153                 :   \brief Load geometry (point layers)
     154                 : 
     155                 :   \todo Implement (really needed?)
     156                 :   
     157                 :   \return TRUE on success or FALSE on failure
     158                 : */
     159               0 : bool VFKFeatureSQLite::LoadGeometryPoint()
     160                 : {
     161               0 :     return FALSE;
     162                 : }
     163                 : 
     164                 : /*!
     165                 :   \brief Load geometry (linestring SBP layer)
     166                 : 
     167                 :   \todo Implement (really needed?)
     168                 : 
     169                 :   \return TRUE on success or FALSE on failure
     170                 : */
     171               0 : bool VFKFeatureSQLite::LoadGeometryLineStringSBP()
     172                 : {
     173               0 :     return FALSE;
     174                 : }
     175                 : 
     176                 : /*!
     177                 :   \brief Load geometry (linestring HP/DPM layer)
     178                 : 
     179                 :   \todo Implement (really needed?)
     180                 : 
     181                 :   \return TRUE on success or FALSE on failure
     182                 : */
     183               0 : bool VFKFeatureSQLite::LoadGeometryLineStringHP()
     184                 : {
     185               0 :     return FALSE;
     186                 : }
     187                 : 
     188                 : /*!
     189                 :   \brief Load geometry (polygon BUD/PAR layers)
     190                 : 
     191                 :   \todo Implement (really needed?)
     192                 : 
     193                 :   \return TRUE on success or FALSE on failure
     194                 : */
     195               0 : bool VFKFeatureSQLite::LoadGeometryPolygon()
     196                 : {
     197               0 :     return FALSE;
     198                 : }
     199                 : 
     200                 : /*!
     201                 :   \brief Load feature properties from DB
     202                 : 
     203                 :   \param poFeature pointer to OGR feature
     204                 : 
     205                 :   \return OGRERR_NONE on success or OGRERR_FAILURE on failure
     206                 : */
     207              15 : OGRErr VFKFeatureSQLite::LoadProperties(OGRFeature *poFeature)
     208                 : {
     209              15 :     CPLString   osSQL;
     210                 : 
     211                 :     osSQL.Printf("SELECT * FROM %s WHERE rowid = %d",
     212              15 :                  m_poDataBlock->GetName(), m_iRowId);
     213              15 :     if (ExecuteSQL(osSQL.c_str()) != OGRERR_NONE)
     214               0 :         return OGRERR_FAILURE;
     215                 : 
     216             187 :     for (int iField = 0; iField < m_poDataBlock->GetPropertyCount(); iField++) {
     217             172 :   if (sqlite3_column_type(m_hStmt, iField) == SQLITE_NULL) /* skip null values */
     218              29 :             continue;
     219             143 :         OGRFieldType fType = poFeature->GetDefnRef()->GetFieldDefn(iField)->GetType();
     220             143 :         if (fType == OFTInteger)
     221                 :             poFeature->SetField(iField,
     222              63 :                                 sqlite3_column_int(m_hStmt, iField));
     223              80 :         else if (fType == OFTReal)
     224                 :             poFeature->SetField(iField,
     225              26 :                                 sqlite3_column_double(m_hStmt, iField));
     226                 :         else
     227                 :             poFeature->SetField(iField,
     228              54 :                                 (const char *) sqlite3_column_text(m_hStmt, iField));
     229                 :     }
     230                 : 
     231              15 :     FinalizeSQL();
     232                 : 
     233              15 :     return OGRERR_NONE;
     234                 : }

Generated by: LCOV version 1.7