-
Notifications
You must be signed in to change notification settings - Fork 156
/
GdaCartoDB.cpp
222 lines (176 loc) · 5.78 KB
/
GdaCartoDB.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* GeoDa TM, Copyright (C) 2011-2015 by Luc Anselin - all rights reserved
*
* This file is part of GeoDa.
*
* GeoDa is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeoDa is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <boost/thread/thread.hpp>
#include <curl/curl.h>
#include <wx/string.h>
#include "GdaCartoDB.h"
CartoDBProxy::CartoDBProxy()
{
user_name = "";
api_key = "";
}
CartoDBProxy::CartoDBProxy(const wxString& _user_name, const wxString& _api_key)
{
user_name = _user_name;
api_key = _api_key;
}
CartoDBProxy::~CartoDBProxy() {
}
wxString CartoDBProxy::GetKey() const {
return api_key;
}
wxString CartoDBProxy::GetUserName() const {
return user_name;
}
void CartoDBProxy::Close() {
}
void CartoDBProxy::SetKey(const wxString& key) {
api_key = key;
}
void CartoDBProxy::SetUserName(const wxString& name) {
user_name = name;
}
wxString CartoDBProxy::buildBaseUrl()
{
wxString url;
url << "https://" << user_name << ".carto.com/api/v2/sql";
return url;
}
wxString CartoDBProxy::buildUpdateSQL(const wxString& table_name, const wxString& col_name, const wxString &new_table)
{
/**
update test as t set
column_a = c.column_a
from (values
('123', 1),
('345', 2)
) as c(column_b, column_a)
where c.column_b = t.column_b;
*/
wxString sql;
sql << "UPDATE " << table_name << " t "
<< "SET " << col_name << " = c.val FROM (VALUES"
<< new_table.substr(0, new_table.length()-1)
<< ") AS c(id,val) "
<< "WHERE c.id = t.cartodb_id ";
return sql;
}
void CartoDBProxy::UpdateColumn(const wxString& table_name, const wxString& col_name, std::vector<wxString>& vals)
{
wxString ss_newtable;
for (size_t i=0, n=vals.size(); i<n; i++) {
ss_newtable << "(" << i+1 << ", '" << vals[i] << "'),";
}
wxString sql = buildUpdateSQL(table_name, col_name, ss_newtable);
sql = "q=" + sql;
_doPost(sql);
}
void CartoDBProxy::UpdateColumn(const wxString& table_name, const wxString& col_name, std::vector<double>& vals)
{
std::ostringstream ss_newtable;
ss_newtable.precision(std::numeric_limits<double>::digits10);
for (size_t i=0, n=vals.size(); i<n; i++) {
ss_newtable << "(" << i+1 << ", " << vals[i] << "),";
}
wxString sql = buildUpdateSQL(table_name, col_name, ss_newtable.str());
sql = "q=" + sql;
_doPost(sql);
}
void CartoDBProxy::UpdateColumn(const wxString& table_name, const wxString& col_name, std::vector<long long>& vals)
{
std::ostringstream ss_newtable;
for (size_t i=0, n=vals.size(); i<n; i++) {
ss_newtable << "(" << i+1 << ", " << vals[i] << "),";
}
wxString sql = buildUpdateSQL(table_name, col_name, ss_newtable.str());
sql = "q=" + sql;
_doPost(sql);
}
void CartoDBProxy::doGet(wxString parameter)
{
boost::thread t(boost::bind(&CartoDBProxy::_doGet, this, parameter));
t.join();
}
void CartoDBProxy::doPost(wxString parameter)
{
boost::thread t(boost::bind(&CartoDBProxy::_doPost, this, parameter));
t.join();
}
void CartoDBProxy::_doGet(wxString parameter)
{
CURL* curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
wxString url = buildBaseUrl() + "?api_key=" + api_key +"&" + parameter;
curl_easy_setopt(curl, CURLOPT_URL, (const char*)url.mb_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
// Grab image
res = curl_easy_perform(curl);
if( res ) {
printf("Cannot connect carto.com!\n");
}
int res_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res_code);
if (!((res_code == 200 || res_code == 201) && res != CURLE_ABORTED_BY_CALLBACK))
{
printf("!!! Response code: %d\n", res_code);
}
}
// Clean up the resources
curl_easy_cleanup(curl);
curl_global_cleanup();
}
void CartoDBProxy::_doPost(wxString parameter)
{
CURL* curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
wxString url = buildBaseUrl();
parameter = "api_key=" + api_key + "&" + parameter;
curl_easy_setopt(curl, CURLOPT_URL, (const char*)url.mb_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (const char*)parameter.mb_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
//curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L);
//curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
// Grab image
res = curl_easy_perform(curl);
if( res ) {
printf("Cannot connect carto.com!\n");
}
int res_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res_code);
if (!((res_code == 200 || res_code == 201) && res != CURLE_ABORTED_BY_CALLBACK))
{
printf("!!! Response code: %d\n", res_code);
}
// Clean up the resources
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}