| 3255 |
mrmanese |
1 |
#include "sqlite_dataframe.h"
|
|
|
2 |
|
|
|
3 |
SEXP sdf_get_variable(SEXP sdf, SEXP name) {
|
|
|
4 |
if (!IS_CHARACTER(name)) {
|
|
|
5 |
Rprintf("ERROR: argument is not a string.\n");
|
|
|
6 |
return R_NilValue;
|
|
|
7 |
}
|
|
|
8 |
|
|
|
9 |
char *iname = SDF_INAME(sdf);
|
|
|
10 |
char *varname = CHAR_ELT(name, 0);
|
|
|
11 |
|
|
|
12 |
/* check if sdf & varname w/in that sdf exists */
|
|
|
13 |
sqlite3_stmt *stmt;
|
|
|
14 |
sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
|
|
|
15 |
|
|
|
16 |
int res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
|
|
|
17 |
|
| 3281 |
mrmanese |
18 |
if (_sqlite_error(res)) return R_NilValue;
|
| 3255 |
mrmanese |
19 |
|
|
|
20 |
const char *coltype = sqlite3_column_decltype(stmt, 0);
|
|
|
21 |
sqlite3_finalize(stmt);
|
|
|
22 |
|
|
|
23 |
|
| 3281 |
mrmanese |
24 |
SEXP ret, value, class = R_NilValue; int nprotected = 0;
|
| 3255 |
mrmanese |
25 |
PROTECT(ret = NEW_LIST(2)); nprotected++;
|
|
|
26 |
|
|
|
27 |
/* set list names */
|
|
|
28 |
PROTECT(value = NEW_CHARACTER(2)); nprotected++;
|
|
|
29 |
SET_STRING_ELT(value, 0, mkChar("iname"));
|
|
|
30 |
SET_STRING_ELT(value, 1, mkChar("varname"));
|
|
|
31 |
SET_NAMES(ret, value);
|
|
|
32 |
|
|
|
33 |
/* set list values */
|
|
|
34 |
SET_VECTOR_ELT(ret, 0, mkString(iname));
|
|
|
35 |
SET_VECTOR_ELT(ret, 1, mkString(varname));
|
|
|
36 |
|
|
|
37 |
/* set class */
|
|
|
38 |
int type = -1;
|
|
|
39 |
if (strcmp(coltype, "text") == 0) class = mkChar("character");
|
|
|
40 |
else if (strcmp(coltype, "double") == 0) class = mkChar("numeric");
|
|
|
41 |
else if (strcmp(coltype, "bit") == 0) class = mkChar("logical");
|
|
|
42 |
else if (strcmp(coltype, "integer") == 0 || strcmp(coltype, "int") == 0) {
|
|
|
43 |
/* determine if int, factor or ordered */
|
|
|
44 |
type = _get_factor_levels1(iname, varname, ret);
|
|
|
45 |
switch(type) {
|
|
|
46 |
case VAR_INTEGER: class = mkChar("numeric"); break;
|
|
|
47 |
case VAR_FACTOR: class = mkChar("factor"); break;
|
|
|
48 |
case VAR_ORDERED: class = mkChar("ordered");
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
if (type != VAR_ORDERED) {
|
|
|
54 |
PROTECT(value = NEW_CHARACTER(2)); nprotected++;
|
|
|
55 |
SET_STRING_ELT(value, 0, mkChar("sqlite.vector"));
|
|
|
56 |
SET_STRING_ELT(value, 1, class);
|
|
|
57 |
} else {
|
|
|
58 |
PROTECT(value = NEW_CHARACTER(3)); nprotected++;
|
|
|
59 |
SET_STRING_ELT(value, 0, mkChar("sqlite.vector"));
|
|
|
60 |
SET_STRING_ELT(value, 1, class);
|
|
|
61 |
SET_STRING_ELT(value, 2, mkChar("factor"));
|
|
|
62 |
}
|
|
|
63 |
SET_CLASS(ret, value);
|
|
|
64 |
|
|
|
65 |
UNPROTECT(nprotected);
|
|
|
66 |
return ret;
|
|
|
67 |
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
int _get_vector_index_typed_result(sqlite3_stmt *stmt, SEXP *ret, int idx_or_len) {
|
|
|
71 |
int added = 1;
|
|
|
72 |
if (*ret == NULL || *ret == R_NilValue) {
|
|
|
73 |
const char *coltype = sqlite3_column_decltype(stmt, 0);
|
|
|
74 |
if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
|
|
|
75 |
added = 0;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
if (strcmp(coltype, "text") == 0) {
|
|
|
79 |
PROTECT(*ret = NEW_CHARACTER(idx_or_len));
|
|
|
80 |
if (added)
|
|
|
81 |
SET_STRING_ELT(*ret, 0, mkChar(sqlite3_column_text(stmt, 0)));
|
|
|
82 |
} else if (strcmp(coltype, "double") == 0) {
|
|
|
83 |
PROTECT(*ret = NEW_NUMERIC(idx_or_len));
|
|
|
84 |
if (added) REAL(*ret)[0] = sqlite3_column_double(stmt, 0);
|
|
|
85 |
} else if (strcmp(coltype, "bit") == 0) {
|
|
|
86 |
PROTECT(*ret = NEW_LOGICAL(idx_or_len));
|
|
|
87 |
if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
|
|
|
88 |
} else if (strcmp(coltype, "integer") == 0 ||
|
|
|
89 |
strcmp(coltype, "int") == 0) {
|
|
|
90 |
/* caller should just copy off the vars level attr for factors */
|
|
|
91 |
PROTECT(*ret = NEW_INTEGER(idx_or_len));
|
|
|
92 |
if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
|
|
|
93 |
} else added = 0;
|
|
|
94 |
|
|
|
95 |
UNPROTECT(1);
|
|
|
96 |
} else {
|
|
|
97 |
const char *coltype = sqlite3_column_decltype(stmt, 0);
|
|
|
98 |
if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
|
|
|
99 |
added = 0;
|
|
|
100 |
} else if (strcmp(coltype, "text") == 0) {
|
|
|
101 |
SET_STRING_ELT(*ret, idx_or_len,
|
|
|
102 |
mkChar(sqlite3_column_text(stmt, 0)));
|
|
|
103 |
} else if (strcmp(coltype, "double") == 0) {
|
|
|
104 |
REAL(*ret)[idx_or_len] = sqlite3_column_double(stmt, 0);
|
|
|
105 |
} else if (strcmp(coltype, "bit") == 0) {
|
|
|
106 |
INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
|
|
|
107 |
} else if (strcmp(coltype, "integer") == 0 ||
|
|
|
108 |
strcmp(coltype, "int") == 0) {
|
|
|
109 |
/* caller should just copy off the vars level attr for factors */
|
|
|
110 |
INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
|
|
|
111 |
} else added = 0;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
return added;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
SEXP sdf_get_variable_length(SEXP svec) {
|
|
|
118 |
char *iname = SDF_INAME(svec);
|
|
|
119 |
sprintf(g_sql_buf[0], "[%s].sdf_data", iname);
|
|
|
120 |
|
|
|
121 |
SEXP ret;
|
|
|
122 |
PROTECT(ret = NEW_INTEGER(1));
|
|
|
123 |
INTEGER(ret)[0] = _get_row_count2(g_sql_buf[0]);
|
|
|
124 |
UNPROTECT(1);
|
|
|
125 |
return ret;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
SEXP sdf_get_variable_index(SEXP svec, SEXP idx) {
|
|
|
130 |
SEXP ret = R_NilValue, tmp;
|
|
|
131 |
char *iname = SDF_INAME(svec), *varname = SVEC_VARNAME(svec);
|
| 3282 |
mrmanese |
132 |
int index, idxlen, i, retlen=0, res;
|
| 3255 |
mrmanese |
133 |
|
|
|
134 |
/* check if sdf exists */
|
|
|
135 |
sqlite3_stmt *stmt;
|
|
|
136 |
sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
|
|
|
137 |
res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
|
|
|
138 |
sqlite3_finalize(stmt);
|
|
|
139 |
if (_sqlite_error(res)) { return ret; }
|
|
|
140 |
|
|
|
141 |
idxlen = LENGTH(idx);
|
|
|
142 |
if (idxlen < 1) return ret;
|
|
|
143 |
|
|
|
144 |
sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data limit ?,1",
|
|
|
145 |
varname, iname);
|
|
|
146 |
res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
|
|
|
147 |
|
|
|
148 |
/* get data based on index */
|
|
|
149 |
if (IS_NUMERIC(idx)) {
|
|
|
150 |
index = ((int) REAL(idx)[0]) - 1;
|
|
|
151 |
if (index < 0 && idxlen == 1) return ret;
|
|
|
152 |
|
| 3281 |
mrmanese |
153 |
if (index >= 0) {
|
| 3255 |
mrmanese |
154 |
sqlite3_bind_int(stmt, 1, index);
|
|
|
155 |
res = sqlite3_step(stmt);
|
|
|
156 |
if (res == SQLITE_ROW) {
|
|
|
157 |
retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
| 3281 |
mrmanese |
161 |
if (index < 0 || res != SQLITE_ROW) {
|
| 3255 |
mrmanese |
162 |
/* something wrong w/ 1st idx, and it is quietly ignored. we make
|
|
|
163 |
* a "dummy" call to setup the SEXP */
|
|
|
164 |
sqlite3_bind_int(stmt, 1, 0);
|
|
|
165 |
_get_vector_index_typed_result(stmt, &ret, idxlen - 1);
|
|
|
166 |
retlen = 0;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
if (idxlen > 1) {
|
|
|
170 |
for (i = 1; i < idxlen; i++) {
|
|
|
171 |
index = ((int) REAL(idx)[i]) - 1;
|
|
|
172 |
if (index < 0) continue;
|
|
|
173 |
sqlite3_reset(stmt);
|
|
|
174 |
sqlite3_bind_int(stmt, 1, index);
|
|
|
175 |
res = sqlite3_step(stmt);
|
|
|
176 |
if (res == SQLITE_ROW)
|
|
|
177 |
retlen += _get_vector_index_typed_result(stmt, &ret, retlen);
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
} else if (IS_INTEGER(idx)) {
|
|
|
181 |
/* similar to REAL (IS_NUMERIC) above, except that we don't have
|
|
|
182 |
* to cast idx to int. can't refactor this out, sucks. */
|
|
|
183 |
index = INTEGER(idx)[0] - 1;
|
|
|
184 |
if (index < 0 && idxlen == 1) return ret;
|
|
|
185 |
|
| 3281 |
mrmanese |
186 |
if (index >= 0) {
|
| 3255 |
mrmanese |
187 |
sqlite3_bind_int(stmt, 1, index);
|
| 3281 |
mrmanese |
188 |
res = sqlite3_step(stmt);
|
|
|
189 |
if (res == SQLITE_ROW) {
|
|
|
190 |
retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
if (index < 0 || res != SQLITE_ROW) {
|
|
|
195 |
/* something wrong w/ 1st idx, and it is quietly ignored. we make
|
|
|
196 |
* a "dummy" call to setup the SEXP */
|
| 3255 |
mrmanese |
197 |
sqlite3_bind_int(stmt, 1, 0);
|
|
|
198 |
_get_vector_index_typed_result(stmt, &ret, idxlen - 1);
|
|
|
199 |
retlen = 0;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
if (idxlen > 1) {
|
|
|
203 |
for (i = 1; i < idxlen; i++) {
|
|
|
204 |
index = INTEGER(idx)[i] - 1;
|
|
|
205 |
if (index < 0) continue;
|
|
|
206 |
sqlite3_reset(stmt);
|
|
|
207 |
sqlite3_bind_int(stmt, 1, index);
|
|
|
208 |
sqlite3_step(stmt);
|
|
|
209 |
retlen += _get_vector_index_typed_result(stmt, &ret, retlen);
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
} else if (IS_LOGICAL(idx)) {
|
|
|
214 |
/* have to deal with recycling */
|
|
|
215 |
sprintf(g_sql_buf[0], "[%s].sdf_data", iname);
|
|
|
216 |
int veclen = _get_row_count2(g_sql_buf[0]);
|
|
|
217 |
|
|
|
218 |
/* find if there is any TRUE element in the vector */
|
| 3281 |
mrmanese |
219 |
for (i = 0; i < idxlen && i < veclen; i++) {
|
| 3255 |
mrmanese |
220 |
if (LOGICAL(idx)[i]) {
|
|
|
221 |
sqlite3_bind_int(stmt, 1, i);
|
|
|
222 |
sqlite3_step(stmt);
|
|
|
223 |
/* there are at least (idxlen-i) TRUE per cycle of the LOGICAL
|
|
|
224 |
* index. there are at least (veclen/idxlen) cycles (int div).
|
|
|
225 |
* at the last cycle, if (veclen%idxlen > 0), there will be
|
|
|
226 |
* at least (veclen%idxlen - i) if veclen%idxlen > i */
|
|
|
227 |
retlen = (idxlen-i) * (veclen/idxlen);
|
|
|
228 |
if (veclen%idxlen > i) retlen += (veclen%idxlen - i);
|
|
|
229 |
|
|
|
230 |
/* create the vector */
|
|
|
231 |
retlen = _get_vector_index_typed_result(stmt, &ret, retlen);
|
|
|
232 |
break;
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
|
| 3281 |
mrmanese |
236 |
if (i < idxlen && i < veclen) {
|
| 3255 |
mrmanese |
237 |
for (i++; i < veclen; i++) {
|
|
|
238 |
if (LOGICAL(idx)[i%idxlen]) {
|
|
|
239 |
sqlite3_reset(stmt);
|
|
|
240 |
sqlite3_bind_int(stmt, 1, i);
|
|
|
241 |
sqlite3_step(stmt);
|
|
|
242 |
retlen += _get_vector_index_typed_result(stmt, &ret, retlen);
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
}
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
sqlite3_finalize(stmt);
|
|
|
249 |
|
|
|
250 |
if (ret != R_NilValue) {
|
|
|
251 |
ret = _shrink_vector(ret, retlen);
|
|
|
252 |
tmp = GET_LEVELS(svec);
|
|
|
253 |
if (tmp != R_NilValue) {
|
|
|
254 |
SET_LEVELS(ret, duplicate(tmp));
|
|
|
255 |
if (LENGTH(GET_CLASS(svec)) == 2) {
|
|
|
256 |
SET_CLASS(ret, mkString("factor"));
|
|
|
257 |
} else {
|
|
|
258 |
PROTECT(tmp = NEW_CHARACTER(2));
|
|
|
259 |
SET_STRING_ELT(tmp, 0, mkChar("ordered"));
|
|
|
260 |
SET_STRING_ELT(tmp, 1, mkChar("factor"));
|
|
|
261 |
UNPROTECT(1);
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
return ret;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
|
271 |
|