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