1 : /******************************************************************************
2 : * $Id: ogrcouchdbrowslayer.cpp 22252 2011-04-28 20:59:51Z rouault $
3 : *
4 : * Project: CouchDB Translator
5 : * Purpose: Implements OGRCouchDBRowsLayer class.
6 : * Author: Even Rouault, <even dot rouault at mines dash paris dot org>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2011, Even Rouault <even dot rouault at mines dash paris dot org>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "ogr_couchdb.h"
31 :
32 : CPL_CVSID("$Id: ogrcouchdbrowslayer.cpp 22252 2011-04-28 20:59:51Z rouault $");
33 :
34 : /************************************************************************/
35 : /* OGRCouchDBRowsLayer() */
36 : /************************************************************************/
37 :
38 1 : OGRCouchDBRowsLayer::OGRCouchDBRowsLayer(OGRCouchDBDataSource* poDS) :
39 1 : OGRCouchDBLayer(poDS)
40 :
41 : {
42 1 : poFeatureDefn = new OGRFeatureDefn( "rows" );
43 1 : poFeatureDefn->Reference();
44 :
45 1 : OGRFieldDefn oFieldId("_id", OFTString);
46 1 : poFeatureDefn->AddFieldDefn(&oFieldId);
47 :
48 1 : OGRFieldDefn oFieldRev("_rev", OFTString);
49 1 : poFeatureDefn->AddFieldDefn(&oFieldRev);
50 :
51 1 : bAllInOne = FALSE;
52 1 : }
53 :
54 : /************************************************************************/
55 : /* ~OGRCouchDBRowsLayer() */
56 : /************************************************************************/
57 :
58 1 : OGRCouchDBRowsLayer::~OGRCouchDBRowsLayer()
59 :
60 : {
61 1 : }
62 :
63 : /************************************************************************/
64 : /* ResetReading() */
65 : /************************************************************************/
66 :
67 1 : void OGRCouchDBRowsLayer::ResetReading()
68 :
69 : {
70 1 : OGRCouchDBLayer::ResetReading();
71 :
72 1 : if (!bAllInOne)
73 : {
74 0 : json_object_put(poFeatures);
75 0 : poFeatures = NULL;
76 0 : aoFeatures.resize(0);
77 : }
78 1 : }
79 :
80 : /************************************************************************/
81 : /* FetchNextRows() */
82 : /************************************************************************/
83 :
84 1 : int OGRCouchDBRowsLayer::FetchNextRows()
85 : {
86 1 : if (bAllInOne)
87 0 : return FALSE;
88 :
89 1 : json_object_put(poFeatures);
90 1 : poFeatures = NULL;
91 1 : aoFeatures.resize(0);
92 :
93 1 : int bHasEsperluet = (strstr(poDS->GetURL(), "?") != NULL);
94 :
95 1 : CPLString osURI;
96 1 : if (strstr(poDS->GetURL(), "limit=") == NULL &&
97 : strstr(poDS->GetURL(), "skip=") == NULL)
98 : {
99 1 : if (!bHasEsperluet)
100 : {
101 0 : bHasEsperluet = TRUE;
102 0 : osURI += "?";
103 : }
104 :
105 : osURI += CPLSPrintf("&limit=%d&skip=%d",
106 1 : GetFeaturesToFetch(), nOffset);
107 : }
108 1 : if (strstr(poDS->GetURL(), "reduce=") == NULL)
109 : {
110 1 : if (!bHasEsperluet)
111 : {
112 0 : bHasEsperluet = TRUE;
113 0 : osURI += "?";
114 : }
115 :
116 1 : osURI += "&reduce=false";
117 : }
118 1 : json_object* poAnswerObj = poDS->GET(osURI);
119 1 : return FetchNextRowsAnalyseDocs(poAnswerObj);
120 : }
121 :
122 : /************************************************************************/
123 : /* BuildFeatureDefn() */
124 : /************************************************************************/
125 :
126 1 : int OGRCouchDBRowsLayer::BuildFeatureDefn()
127 : {
128 1 : int bRet = FetchNextRows();
129 1 : if (!bRet)
130 0 : return FALSE;
131 :
132 1 : bRet = BuildFeatureDefnFromRows(poFeatures);
133 1 : if (!bRet)
134 0 : return FALSE;
135 :
136 1 : if ( bEOF )
137 1 : bAllInOne = TRUE;
138 :
139 1 : return TRUE;
140 : }
|