1 : /******************************************************************************
2 : * $Id: vfkfeaturesqlite.cpp 24217 2012-04-11 14:25:09Z 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, 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 : #ifdef HAVE_SQLITE
39 :
40 : /*!
41 : \brief VFKFeatureSQLite constructor
42 :
43 : Read VFK feature from DB
44 : */
45 53 : VFKFeatureSQLite::VFKFeatureSQLite(IVFKDataBlock *poDataBlock) : IVFKFeature(poDataBlock)
46 : {
47 53 : m_hStmt = NULL;
48 53 : m_nIndex = m_poDataBlock->GetFeatureCount();
49 :
50 : /* set FID from DB */
51 53 : SetFIDFromDB(); /* -> m_nFID */
52 53 : }
53 :
54 : /*!
55 : \brief Read FID from DB
56 : */
57 53 : OGRErr VFKFeatureSQLite::SetFIDFromDB()
58 : {
59 53 : CPLString osSQL;
60 :
61 : osSQL.Printf("SELECT ogr_fid FROM '%s' WHERE _rowid_ = %d",
62 53 : m_poDataBlock->GetName(), m_nIndex + 1);
63 53 : if (ExecuteSQL(osSQL.c_str()) != OGRERR_NONE)
64 0 : return OGRERR_FAILURE;
65 :
66 53 : m_nFID = sqlite3_column_int(m_hStmt, 0);
67 :
68 53 : FinalizeSQL();
69 :
70 53 : return OGRERR_NONE;
71 : }
72 :
73 : /*!
74 : \brief Finalize SQL statement
75 : */
76 83 : void VFKFeatureSQLite::FinalizeSQL()
77 : {
78 83 : sqlite3_finalize(m_hStmt);
79 83 : m_hStmt = NULL;
80 83 : }
81 :
82 : /*!
83 : \brief Execute SQL (select) statement
84 :
85 : \param pszSQLCommand SQL command string
86 :
87 : \return OGRERR_NONE on success
88 : \return OGRERR_FAILURE on error
89 : */
90 83 : OGRErr VFKFeatureSQLite::ExecuteSQL(const char *pszSQLCommand)
91 : {
92 : int rc;
93 :
94 : sqlite3 *poDB;
95 :
96 83 : VFKReaderSQLite *poReader = (VFKReaderSQLite *) m_poDataBlock->GetReader();
97 83 : poDB = poReader->m_poDB;
98 :
99 : rc = sqlite3_prepare(poDB, pszSQLCommand, strlen(pszSQLCommand),
100 83 : &m_hStmt, NULL);
101 83 : if (rc != SQLITE_OK) {
102 : CPLError(CE_Failure, CPLE_AppDefined,
103 : "In LoadProperties(): sqlite3_prepare(%s):\n %s",
104 0 : pszSQLCommand, sqlite3_errmsg(poDB));
105 :
106 0 : if(m_hStmt != NULL) {
107 0 : FinalizeSQL();
108 : }
109 0 : return OGRERR_FAILURE;
110 : }
111 83 : rc = sqlite3_step(m_hStmt);
112 83 : if (rc != SQLITE_ROW) {
113 : CPLError(CE_Failure, CPLE_AppDefined,
114 : "In ExecuteSQL(): sqlite3_step(%s):\n %s",
115 0 : pszSQLCommand, sqlite3_errmsg(poDB));
116 :
117 0 : if (m_hStmt) {
118 0 : FinalizeSQL();
119 : }
120 :
121 0 : return OGRERR_FAILURE;
122 : }
123 :
124 83 : return OGRERR_NONE;
125 : }
126 :
127 : /*!
128 : \brief VFKFeatureSQLite constructor (derived from VFKFeature)
129 :
130 : Read VFK feature from VFK file and insert it into DB
131 : */
132 53 : VFKFeatureSQLite::VFKFeatureSQLite(const VFKFeature *poVFKFeature) : IVFKFeature(poVFKFeature->m_poDataBlock)
133 : {
134 53 : m_nFID = poVFKFeature->m_nFID;
135 53 : m_hStmt = NULL;
136 53 : m_nIndex = m_poDataBlock->GetFeatureCount();
137 53 : }
138 :
139 : /*!
140 : \brief Load geometry (point layers)
141 :
142 : \todo Implement (really needed?)
143 :
144 : \return TRUE on success
145 : \return FALSE on failure
146 : */
147 0 : bool VFKFeatureSQLite::LoadGeometryPoint()
148 : {
149 0 : return FALSE;
150 : }
151 :
152 : /*!
153 : \brief Load geometry (linestring SBP layer)
154 :
155 : \todo Implement (really needed?)
156 :
157 : \return TRUE on success
158 : \return FALSE on failure
159 : */
160 0 : bool VFKFeatureSQLite::LoadGeometryLineStringSBP()
161 : {
162 0 : return FALSE;
163 : }
164 :
165 : /*!
166 : \brief Load geometry (linestring HP/DPM layer)
167 :
168 : \todo Implement (really needed?)
169 :
170 : \return TRUE on success
171 : \return FALSE on failure
172 : */
173 0 : bool VFKFeatureSQLite::LoadGeometryLineStringHP()
174 : {
175 0 : return FALSE;
176 : }
177 :
178 : /*!
179 : \brief Load geometry (polygon BUD/PAR layers)
180 :
181 : \todo Implement (really needed?)
182 :
183 : \return TRUE on success
184 : \return FALSE on failure
185 : */
186 0 : bool VFKFeatureSQLite::LoadGeometryPolygon()
187 : {
188 0 : return FALSE;
189 : }
190 :
191 : /*!
192 : \brief Load feature properties from DB
193 :
194 : \param poFeature pointer to OGR feature
195 :
196 : \return OGRERR_NONE on success
197 : \return OGRERR_FAILURE on failure
198 : */
199 30 : OGRErr VFKFeatureSQLite::LoadProperties(OGRFeature *poFeature)
200 : {
201 30 : CPLString osSQL;
202 :
203 : osSQL.Printf("SELECT * FROM '%s' WHERE _rowid_ = %d",
204 30 : m_poDataBlock->GetName(), m_nIndex + 1);
205 30 : if (ExecuteSQL(osSQL.c_str()) != OGRERR_NONE)
206 0 : return OGRERR_FAILURE;
207 :
208 374 : for (int iField = 0; iField < m_poDataBlock->GetPropertyCount(); iField++) {
209 344 : OGRFieldType fType = poFeature->GetDefnRef()->GetFieldDefn(iField)->GetType();
210 344 : if (fType == OFTInteger)
211 : poFeature->SetField(iField,
212 162 : sqlite3_column_int(m_hStmt, iField));
213 182 : else if (fType == OFTReal)
214 : poFeature->SetField(iField,
215 54 : sqlite3_column_double(m_hStmt, iField));
216 : else
217 : poFeature->SetField(iField,
218 128 : (const char *) sqlite3_column_text(m_hStmt, iField));
219 : }
220 :
221 30 : FinalizeSQL();
222 :
223 30 : return OGRERR_NONE;
224 : }
225 :
226 : #endif // HAVE_SQLITE
|