-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.cpp
228 lines (200 loc) · 8.05 KB
/
interface.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
223
224
225
226
227
228
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2009 Whit Armstrong //
// //
// This program 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. //
// //
// This program 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 <iostream>
#include "interface.hpp"
#include "database.errors.hpp"
#include "postgres.connection.hpp"
#include "query.results.hpp"
using std::cout;
using std::cerr;
using std::endl;
static void connFinalizer(SEXP dbi_conn_sexp) {
DatabaseConnection* conn = reinterpret_cast<DatabaseConnection*>(R_ExternalPtrAddr(dbi_conn_sexp));
if(conn) {
// call c++ destructor
delete conn;
R_ClearExternalPtr(dbi_conn_sexp);
}
}
static void queryResultsFinalizer(SEXP query_results_sexp) {
QueryResults* query_results = reinterpret_cast<QueryResults*>(R_ExternalPtrAddr(query_results_sexp));
if(query_results) {
// call c++ destructor
delete query_results;
R_ClearExternalPtr(query_results_sexp);
}
}
SEXP dbClearResult(SEXP dbi_query_results_sexp) {
SEXP ans;
PROTECT(ans = allocVector(LGLSXP,1));
QueryResults* query_results = reinterpret_cast<QueryResults*>(R_ExternalPtrAddr(dbi_query_results_sexp));
if(query_results) {
// call c++ destructor
delete query_results;
R_ClearExternalPtr(dbi_query_results_sexp);
LOGICAL(ans)[0] = static_cast<int>(true);
} else {
LOGICAL(ans)[0] = static_cast<int>(false);
}
UNPROTECT(1);
return ans;
}
SEXP dbDisconnect(SEXP dbi_conn_sexp) {
SEXP ans;
PROTECT(ans = allocVector(LGLSXP,1));
DatabaseConnection* conn = reinterpret_cast<DatabaseConnection*>(R_ExternalPtrAddr(dbi_conn_sexp));
if(conn) {
// call c++ destructor
delete conn;
R_ClearExternalPtr(dbi_conn_sexp);
LOGICAL(ans)[0] = static_cast<int>(true);
}
LOGICAL(ans)[0] = static_cast<int>(false);
UNPROTECT(1);
return ans;
}
SEXP dbConnect(SEXP dbType_sexp,
SEXP connection_string_sexp,
SEXP user_sexp,
SEXP pass_sexp,
SEXP host_sexp,
SEXP port_sexp,
SEXP tty_sexp,
SEXP dbName_sexp,
SEXP options_sexp) {
SEXP dbi_conn_sexp;
DatabaseConnection* conn = NULL;
const char* dbType = CHAR(STRING_PTR(dbType_sexp)[0]);
const char* connection_string = (connection_string_sexp == R_NilValue) ? NULL : CHAR(STRING_PTR(connection_string_sexp)[0]);
const char* user = (user_sexp == R_NilValue) ? NULL : CHAR(STRING_PTR(user_sexp)[0]);
const char* pass = (pass_sexp == R_NilValue) ? NULL : CHAR(STRING_PTR(pass_sexp)[0]);
const char* host = (host_sexp == R_NilValue) ? NULL : CHAR(STRING_PTR(host_sexp)[0]);
const char* port = (port_sexp == R_NilValue) ? NULL : CHAR(STRING_PTR(port_sexp)[0]);
const char* tty = (tty_sexp == R_NilValue) ? NULL : CHAR(STRING_PTR(tty_sexp)[0]);
const char* dbName = (dbName_sexp == R_NilValue) ? NULL : CHAR(STRING_PTR(dbName_sexp)[0]);
const char* options = (options_sexp == R_NilValue) ? NULL : CHAR(STRING_PTR(options_sexp)[0]);
// this test is to check whether the package was compiled with support
// for this specific dbType
try {
conn = DatabaseConnection::init(dbType);
} catch (DriverNotSupported& e) {
cerr << e.what() << endl;
return R_NilValue;
}
// if we succeed then return a wrapped connection, otherwise return null
try {
// if user provides connection_string, then use it, otherwise try traditional args
if(connection_string) {
conn->connect(connection_string);
} else {
conn->connect(user,pass,host,port,tty,dbName,options);
}
} catch(BadDatabaseConnection& e) {
cerr << e.what() << endl;
return R_NilValue;
}
PROTECT(dbi_conn_sexp = R_MakeExternalPtr(reinterpret_cast<void*>(conn),install("DBI_conn_pointer"),R_NilValue));
R_RegisterCFinalizerEx(dbi_conn_sexp, connFinalizer, TRUE);
UNPROTECT(1);
return dbi_conn_sexp;
}
SEXP dbSendQuery(SEXP dbi_conn_sexp, SEXP qry_sexp) {
SEXP dbi_query_results_sexp;
DatabaseConnection* conn = reinterpret_cast<DatabaseConnection*>(R_ExternalPtrAddr(dbi_conn_sexp));
if(!conn) {
// throw bad_connection_object
return R_NilValue;
}
const char* qry = CHAR(STRING_PTR(qry_sexp)[0]);
QueryResults* query_results = conn->sendQuery(qry);
//query_results->getStatus();
PROTECT(dbi_query_results_sexp = R_MakeExternalPtr(reinterpret_cast<void*>(query_results),install("DBI_results_pointer"),R_NilValue));
R_RegisterCFinalizerEx(dbi_query_results_sexp, queryResultsFinalizer, TRUE);
UNPROTECT(1);
return dbi_query_results_sexp;
}
SEXP dbfetch(SEXP dbi_query_results_sexp, SEXP nrows_sexp) {
if(!R_ExternalPtrAddr(dbi_query_results_sexp)) {
return R_NilValue;
}
const int nrows = INTEGER(nrows_sexp)[0];
QueryResults* query_results = reinterpret_cast<QueryResults*>(R_ExternalPtrAddr(dbi_query_results_sexp));
// FIXME: try/catch instead?
if(query_results) {
return query_results->fetch(nrows);
} else {
return R_NilValue;
}
}
// return number of rows written to database
// using both overwrite and append is a bad design decision
// there should just be overwrite, which will drop the table if it exists
// append should be automatic if the table exists, and should fail if the row formats don't match up
// having both overwrite and append just complicates the logic of this function
SEXP dbWriteTable(SEXP dbi_conn_sexp, SEXP tableName_sexp, SEXP value_sexp, SEXP writeRowNames_sexp, SEXP overWrite_sexp, SEXP append_sexp) {
cout << "ignoring append argument" << endl;
SEXP ans;
int rows;
DatabaseConnection* conn = reinterpret_cast<DatabaseConnection*>(R_ExternalPtrAddr(dbi_conn_sexp));
if(!conn) {
// throw bad_connection_object
cerr << "bad database connection." << endl;
return ScalarInteger(0);
}
const char* tableName = CHAR(STRING_ELT(tableName_sexp,0));
const bool writeRowNames = static_cast<bool>(LOGICAL(writeRowNames_sexp)[0]);
const bool overWrite = static_cast<bool>(LOGICAL(overWrite_sexp)[0]);
if(conn->existsTable(tableName) && overWrite) {
if(!conn->removeTable(tableName)) {
cerr << "could not remove existing table (aborting)." << endl;
return ScalarInteger(0);
}
}
try {
rows = conn->writeTable(tableName, value_sexp, writeRowNames);
} catch (MapToTypeNotImplemented& e) {
cerr << e.what() << endl;
return R_NilValue;
}
PROTECT(ans = allocVector(INTSXP,1));
INTEGER(ans)[0] = rows;
UNPROTECT(1);
return ans;
}
SEXP dbExistsTable(SEXP dbi_conn_sexp, SEXP tableName_sexp) {
SEXP ans;
DatabaseConnection* conn = reinterpret_cast<DatabaseConnection*>(R_ExternalPtrAddr(dbi_conn_sexp));
if(!conn) {
// throw bad_connection_object
cerr << "bad database connection." << endl;
return R_NilValue;
}
const char* tableName = CHAR(STRING_ELT(tableName_sexp,0));
PROTECT(ans = allocVector(LGLSXP,1));
LOGICAL(ans)[0] = static_cast<int>(conn->existsTable(tableName));
UNPROTECT(1);
return ans;
}
SEXP dbListTables(SEXP dbi_conn_sexp) {
DatabaseConnection* conn = reinterpret_cast<DatabaseConnection*>(R_ExternalPtrAddr(dbi_conn_sexp));
if(!conn) {
// throw bad_connection_object
cerr << "bad database connection." << endl;
return R_NilValue;
}
return conn->listTables();
}