Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GeoPackage Translator
4 : * Purpose: Implements OGRGeoPackageSelectLayer class
5 : * Author: Even Rouault <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2014, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * Permission is hereby granted, free of charge, to any person obtaining a
11 : * copy of this software and associated documentation files (the "Software"),
12 : * to deal in the Software without restriction, including without limitation
13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 : * and/or sell copies of the Software, and to permit persons to whom the
15 : * Software is furnished to do so, subject to the following conditions:
16 : *
17 : * The above copyright notice and this permission notice shall be included
18 : * in all copies or substantial portions of the Software.
19 : *
20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 : * DEALINGS IN THE SOFTWARE.
27 : ****************************************************************************/
28 :
29 : #include "ogr_geopackage.h"
30 :
31 : /************************************************************************/
32 : /* OGRGeoPackageSelectLayer() */
33 : /************************************************************************/
34 :
35 735 : OGRGeoPackageSelectLayer::OGRGeoPackageSelectLayer(
36 : GDALGeoPackageDataset *poDS, const CPLString &osSQLIn,
37 : sqlite3_stmt *hStmtIn, bool bUseStatementForGetNextFeature,
38 735 : bool bEmptyLayer)
39 735 : : OGRGeoPackageLayer(poDS)
40 : {
41 : // Cannot be moved to initializer list because of use of this, which MSVC
42 : // 2008 doesn't like
43 735 : poBehavior = new OGRSQLiteSelectLayerCommonBehaviour(poDS, this, osSQLIn,
44 735 : bEmptyLayer);
45 :
46 735 : BuildFeatureDefn("SELECT", hStmtIn);
47 :
48 735 : if (bUseStatementForGetNextFeature)
49 : {
50 609 : m_poQueryStatement = hStmtIn;
51 609 : m_bDoStep = false;
52 : }
53 : else
54 : {
55 126 : sqlite3_finalize(hStmtIn);
56 : }
57 735 : }
58 :
59 : /************************************************************************/
60 : /* ~OGRGeoPackageSelectLayer() */
61 : /************************************************************************/
62 :
63 1470 : OGRGeoPackageSelectLayer::~OGRGeoPackageSelectLayer()
64 : {
65 735 : delete poBehavior;
66 1470 : }
67 :
68 : /************************************************************************/
69 : /* ResetReading() */
70 : /************************************************************************/
71 :
72 133 : void OGRGeoPackageSelectLayer::ResetReading()
73 : {
74 133 : poBehavior->ResetReading();
75 133 : }
76 :
77 : /************************************************************************/
78 : /* GetNextFeature() */
79 : /************************************************************************/
80 :
81 1972 : OGRFeature *OGRGeoPackageSelectLayer::GetNextFeature()
82 : {
83 1972 : return poBehavior->GetNextFeature();
84 : }
85 :
86 : /************************************************************************/
87 : /* GetNextFeature() */
88 : /************************************************************************/
89 :
90 257 : GIntBig OGRGeoPackageSelectLayer::GetFeatureCount(int bForce)
91 : {
92 257 : return poBehavior->GetFeatureCount(bForce);
93 : }
94 :
95 : /************************************************************************/
96 : /* ResetStatement() */
97 : /************************************************************************/
98 :
99 59 : OGRErr OGRGeoPackageSelectLayer::ResetStatement()
100 :
101 : {
102 59 : ClearStatement();
103 :
104 59 : m_iNextShapeId = 0;
105 59 : m_bDoStep = true;
106 :
107 : #ifdef DEBUG
108 59 : CPLDebug("OGR_GPKG", "prepare_v2(%s)", poBehavior->m_osSQLCurrent.c_str());
109 : #endif
110 :
111 : const int rc =
112 59 : sqlite3_prepare_v2(m_poDS->GetDB(), poBehavior->m_osSQLCurrent,
113 59 : static_cast<int>(poBehavior->m_osSQLCurrent.size()),
114 : &m_poQueryStatement, nullptr);
115 :
116 59 : if (rc == SQLITE_OK)
117 : {
118 59 : return OGRERR_NONE;
119 : }
120 : else
121 : {
122 0 : CPLError(CE_Failure, CPLE_AppDefined,
123 : "In ResetStatement(): sqlite3_prepare_v2(%s):\n %s",
124 0 : poBehavior->m_osSQLCurrent.c_str(),
125 0 : sqlite3_errmsg(m_poDS->GetDB()));
126 0 : m_poQueryStatement = nullptr;
127 0 : return OGRERR_FAILURE;
128 : }
129 : }
130 :
131 : /************************************************************************/
132 : /* SetAttributeFilter() */
133 : /************************************************************************/
134 :
135 34 : OGRErr OGRGeoPackageSelectLayer::SetAttributeFilter(const char *pszQuery)
136 : {
137 34 : return poBehavior->SetAttributeFilter(pszQuery);
138 : }
139 :
140 : /************************************************************************/
141 : /* SetSpatialFilter() */
142 : /************************************************************************/
143 :
144 47 : void OGRGeoPackageSelectLayer::SetSpatialFilter(int iGeomField,
145 : OGRGeometry *poGeomIn)
146 :
147 : {
148 47 : poBehavior->SetSpatialFilter(iGeomField, poGeomIn);
149 47 : }
150 :
151 : /************************************************************************/
152 : /* TestCapability() */
153 : /************************************************************************/
154 :
155 26 : int OGRGeoPackageSelectLayer::TestCapability(const char *pszCap)
156 : {
157 26 : return poBehavior->TestCapability(pszCap);
158 : }
159 :
160 : /************************************************************************/
161 : /* GetExtent() */
162 : /************************************************************************/
163 :
164 4 : OGRErr OGRGeoPackageSelectLayer::GetExtent(int iGeomField,
165 : OGREnvelope *psExtent, int bForce)
166 : {
167 4 : return poBehavior->GetExtent(iGeomField, psExtent, bForce);
168 : }
|