The R Project SVN R-packages

Rev

Rev 4160 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4160 Rev 4590
Line 82... Line 82...
82
        if (added) UNPROTECT(1);
82
        if (added) UNPROTECT(1);
83
        *coltype = ctype;
83
        *coltype = ctype;
84
    } else if (stmt != NULL) {
84
    } else if (stmt != NULL) {
85
        int ctype = *coltype;
85
        int ctype = *coltype;
86
 
86
 
87
        /* first row must have been NULL so that *coltype was not set */
87
        /* first row/previous rows must have been NULL so that *coltype 
-
 
88
         * was not set */
88
        if (ctype == SQLITE_NULL) {
89
        if (ctype == SQLITE_NULL) {
89
            ctype = *coltype = sqlite3_column_type(stmt, colidx);
90
            ctype = *coltype = sqlite3_column_type(stmt, colidx);
90
            /* distinguish int or logical */
91
            /* distinguish int or logical */
91
            if (ctype == SQLITE_INTEGER) 
92
            if (ctype == SQLITE_INTEGER) 
92
                ctype = (strcmp(sqlite3_column_decltype(stmt, colidx), "bit") == 0) ?
93
                ctype = (strcmp(sqlite3_column_decltype(stmt, colidx), "bit") == 0) ?
Line 202... Line 203...
202
    
203
    
203
SEXP sdf_get_variable_index(SEXP svec, SEXP idx) {
204
SEXP sdf_get_variable_index(SEXP svec, SEXP idx) {
204
    SEXP ret = R_NilValue, tmp;
205
    SEXP ret = R_NilValue, tmp;
205
    char *iname = SDF_INAME(svec), *tblname = SVEC_TBLNAME(svec),
206
    char *iname = SDF_INAME(svec), *tblname = SVEC_TBLNAME(svec),
206
         *varname = SVEC_VARNAME(svec);
207
         *varname = SVEC_VARNAME(svec);
207
    int index, idxlen, i, retlen=0, res, coltype;
208
    int *index, _idx, nrows, i, init=FALSE, retlen=0, res, coltype;
208
    sqlite3_stmt *stmt;
209
    sqlite3_stmt *stmt;
209
 
210
 
210
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
211
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
211
 
212
 
212
    idxlen = LENGTH(idx);
213
    /* get # of rows in svec */
-
 
214
    nrows = _get_row_count2(iname, TRUE);
213
    if (idxlen < 1) return ret;
215
    if (nrows < 1) return ret;
214
 
216
 
215
    sprintf(g_sql_buf[0], "select [%s] from [%s].[%s] where rowid=?",
217
    sprintf(g_sql_buf[0], "select [%s] from [%s].[%s] where rowid=?",
216
            varname, iname, tblname);
218
            varname, iname, tblname);
217
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
219
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
218
    if (_sqlite_error(res)) error("cannot complete request");
220
    if (_sqlite_error(res)) error("cannot complete request");
219
 
221
 
220
    /* get data based on index */
-
 
221
    if (IS_NUMERIC(idx)) {
-
 
222
        /* special handling of 1st index, bec this is where we create the
-
 
223
         * SEXP to be used to hold the result */
-
 
224
        index = (int) REAL(idx)[0];
-
 
225
        if (index < 1 && idxlen == 1) return ret;
-
 
226
 
-
 
227
        if (index >= 1) {
-
 
228
            sqlite3_bind_int(stmt, 1, index);
222
    index = _make_row_index(idx, &nrows);
229
            res = sqlite3_step(stmt);
-
 
230
            if (res == SQLITE_ROW) { 
-
 
231
                retlen = _get_vector_index_typed_result(stmt, &ret, 0, idxlen, &coltype);
-
 
232
            } 
-
 
233
        } 
-
 
234
        
-
 
235
        if (index < 1 || res != SQLITE_ROW) {
-
 
236
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
-
 
237
             * a "dummy" call to setup the SEXP */
-
 
238
            sqlite3_reset(stmt);
-
 
239
            sqlite3_bind_int(stmt, 1, 0);
-
 
240
            res = sqlite3_step(stmt);
-
 
241
            if (res == SQLITE_ROW) {
-
 
242
                /* populate with 1st row. this should be overwritten by 
-
 
243
                 * succeeding calls. if there are no valid result, then 
-
 
244
                 * everything will be removed by _shrink_vector().
-
 
245
                 */
-
 
246
                _get_vector_index_typed_result(stmt, &ret, 0, idxlen - 1, &coltype);
-
 
247
            }
-
 
248
            retlen = 0;
-
 
249
        }
-
 
250
 
-
 
251
 
223
 
252
        if (idxlen > 1) {
-
 
253
            for (i = 1; i < idxlen; i++) {
224
    for (i = 0; i < nrows; i++) {
254
                index = (int) REAL(idx)[i];
225
        _idx = index[i];
255
                if (index < 1) continue;
226
        if (_idx < 1 || _idx == NA_INTEGER) continue;
256
                sqlite3_reset(stmt);
227
        sqlite3_reset(stmt);
257
                sqlite3_bind_int(stmt, 1, index);
228
        sqlite3_bind_int(stmt, 1, _idx);
258
                res = sqlite3_step(stmt);
229
        res = sqlite3_step(stmt);
259
                if (res == SQLITE_ROW) {
-
 
260
                    retlen += _get_vector_index_typed_result(stmt, &ret, 0, retlen, &coltype); 
-
 
261
                } else {
-
 
262
                    retlen += _get_vector_index_typed_result(NULL, &ret, 0, retlen, &coltype); 
-
 
263
                }
-
 
264
            }
-
 
265
        }
-
 
266
    } else if (IS_INTEGER(idx)) {
-
 
267
        /* similar to REAL (IS_NUMERIC) above, except that we don't have
-
 
268
         * to cast idx to int. can't refactor this out, sucks. */
-
 
269
        index = INTEGER(idx)[0];
-
 
270
        if (index < 1 && idxlen == 1) return ret;
-
 
271
 
-
 
272
        if (index >= 1) {
230
        if (!init) {
273
            sqlite3_bind_int(stmt, 1, index);
-
 
274
            res = sqlite3_step(stmt);
-
 
275
            if (res == SQLITE_ROW) { 
231
            if (res == SQLITE_ROW) { 
276
                retlen = _get_vector_index_typed_result(stmt, &ret, 0, idxlen, &coltype);
-
 
277
            } 
-
 
278
        } 
-
 
279
        
-
 
280
        if (index < 1 || res != SQLITE_ROW) {
-
 
281
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
232
                /* valid 1st row. initialize the returned vector with length
282
             * a "dummy" call to setup the SEXP */
233
                 * nrows. retlen holds the length of vector ret. 
283
            sqlite3_reset(stmt);
-
 
284
            sqlite3_bind_int(stmt, 1, 0);
-
 
285
            res = sqlite3_step(stmt);
-
 
286
            if (res == SQLITE_ROW) {
234
                 */ 
287
                _get_vector_index_typed_result(stmt, &ret, 0, idxlen - 1, &coltype);
235
                retlen = _get_vector_index_typed_result(stmt, &ret, 0, nrows, &coltype);
288
            }
236
            } else {
289
            retlen = 0;
237
                /* invalid 1st row, initalize anyway but now with length 
290
        }
-
 
291
 
-
 
292
        if (idxlen > 1) {
-
 
293
            for (i = 1; i < idxlen; i++) {
238
                 * nrows-1. we put dummy results in 1st element of vector which
294
                index = INTEGER(idx)[i];
239
                 * should be overwritten in succeeding calls
295
                if (index < 1) continue;
240
                 */
296
                sqlite3_reset(stmt);
241
                sqlite3_reset(stmt);
297
                sqlite3_bind_int(stmt, 1, index);
242
                sqlite3_bind_int(stmt, 1, 0);  /* get 1st row */
298
                res = sqlite3_step(stmt);
243
                res = sqlite3_step(stmt);
299
                if (res == SQLITE_ROW) {
-
 
300
                    retlen += _get_vector_index_typed_result(stmt, &ret, 0, retlen, &coltype); 
-
 
301
                } else {
-
 
302
                    retlen += _get_vector_index_typed_result(NULL, &ret, 0, retlen, &coltype);
-
 
303
                }
-
 
304
            }
-
 
305
        }
-
 
306
 
-
 
307
    } else if (IS_LOGICAL(idx)) {
-
 
308
        /* have to deal with recycling */
-
 
309
        int veclen = _get_row_count2(iname, 1);
-
 
310
 
-
 
311
        /* in this indexing, idx[i] == TRUE is equiv to vector[i]. therefore
-
 
312
         * idxlen <= veclen. if idxlen < veclen, then idx is recycled. */
-
 
313
 
-
 
314
        /* find if there is any TRUE element in the vector */
-
 
315
        for (i = 1; i < idxlen && i < veclen; i++) {
-
 
316
            if (LOGICAL(idx)[i]) {
-
 
317
                sqlite3_bind_int(stmt, 1, i+1);
-
 
318
                sqlite3_step(stmt);
-
 
319
                /* there are at least (idxlen-i) TRUE per cycle of the LOGICAL
-
 
320
                 * index. there are at least (veclen/idxlen) cycles (int div).
-
 
321
                 * at the last cycle, if (veclen%idxlen > 0), there will be
-
 
322
                 * at least (veclen%idxlen - i) if veclen%idxlen > i */
-
 
323
                retlen = (idxlen-i) * (veclen/idxlen);
-
 
324
                if (veclen%idxlen > i) retlen += (veclen%idxlen - i);
-
 
325
 
-
 
326
                /* create the vector */
-
 
327
                retlen = _get_vector_index_typed_result(stmt, &ret, 0, retlen, &coltype);
244
                _get_vector_index_typed_result(stmt, &ret, 0, nrows - 1, &coltype);
328
                break;
245
                retlen = 0;
329
            }
-
 
330
        }
-
 
331
 
-
 
332
        if (i < idxlen && i < veclen) {
-
 
333
            for (i++; i < veclen; i++) {
-
 
334
                if (LOGICAL(idx)[i%idxlen]) {
-
 
335
                    sqlite3_reset(stmt);
-
 
336
                    sqlite3_bind_int(stmt, 1, i+1);
-
 
337
                    res = sqlite3_step(stmt);
-
 
338
                    if (res == SQLITE_ROW) {
-
 
339
                        retlen += _get_vector_index_typed_result(stmt, &ret, 0, retlen, &coltype);
-
 
340
                    } else {
-
 
341
                        retlen += _get_vector_index_typed_result(NULL, &ret, 0, retlen, &coltype);
-
 
342
                    }
-
 
343
                }
-
 
344
            }
246
            }
-
 
247
            init = TRUE;
-
 
248
        } else {
-
 
249
            retlen += _get_vector_index_typed_result((res == SQLITE_ROW) ? stmt : NULL,
-
 
250
                            &ret, 0, retlen, &coltype);
345
        }
251
        }
346
    }
252
    }
347
 
253
 
348
    sqlite3_finalize(stmt);
254
    sqlite3_finalize(stmt);
349
 
255
 
Line 366... Line 272...
366
    return ret;
272
    return ret;
367
}
273
}
368
 
274
 
369
/* sqlite.vector.[<- */
275
/* sqlite.vector.[<- */
370
SEXP sdf_set_variable_index(SEXP svec, SEXP idx, SEXP value) {
276
SEXP sdf_set_variable_index(SEXP svec, SEXP idx, SEXP value) {
-
 
277
    int *index;
371
    int idx_len, val_len, svec_len, i, res;
278
    int coltype, valtype, idx_len, val_len, svec_len, i, i2, res;
372
    char *iname = SDF_INAME(svec), *tblname = SVEC_TBLNAME(svec),
279
    char *iname = SDF_INAME(svec), *tblname = SVEC_TBLNAME(svec),
373
         *varname = SVEC_VARNAME(svec);
280
         *varname = SVEC_VARNAME(svec);
-
 
281
    const char *decltype;
374
    sqlite3_stmt *stmt;
282
    sqlite3_stmt *stmt;
375
 
283
 
376
    /* match levels if factor or ordered */
-
 
377
    if (inherits(value, "ordered")) {
-
 
378
    } else if (inherits(value, "factor")) {
-
 
379
    }
-
 
380
 
284
 
-
 
285
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
-
 
286
 
381
    idx_len = LENGTH(idx);
287
    /* find the type of the svec */
-
 
288
    sprintf(g_sql_buf[0], "select [%s] from [%s].[%s] where not [%s] is null limit 1", 
-
 
289
                varname, iname, tblname, varname);
-
 
290
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
-
 
291
    if (_sqlite_error(res)) error("cannot complete request");
382
    val_len = LENGTH(value);
292
    sqlite3_step(stmt);
-
 
293
    decltype = sqlite3_column_decltype(stmt, 0);
-
 
294
    Rprintf("decltype: %s\n", decltype); 
383
    svec_len = _get_row_count2(iname, TRUE);
295
    coltype = _get_r_type(decltype);
-
 
296
    sqlite3_finalize(stmt);
384
 
297
 
-
 
298
    /* get index */
-
 
299
    val_len = LENGTH(value);
-
 
300
    idx_len = svec_len = _get_row_count2(iname, TRUE);
-
 
301
    /*Rprintf("idx_len: %d\n", idx_len);*/
-
 
302
    index = _make_row_index(idx, &idx_len);
385
    if (idx_len % val_len != 0) 
303
    if (idx_len % val_len != 0) 
386
        warning("number of items to replace is not a multiple of replacement length");
304
        warning("number of items to replace is not a multiple of replacement length");
387
 
305
 
-
 
306
    /* find the type of value to be assigned */
-
 
307
    valtype = TYPEOF(value);
-
 
308
 
-
 
309
    /* we will use the style similar to R's */
-
 
310
    coltype = coltype * 100 + valtype;
-
 
311
 
388
    sprintf(g_sql_buf[0], "update [%s].[%s] set [%s]=? where rowid=?",
312
    sprintf(g_sql_buf[0], "update [%s].[%s] set [%s]=? where rowid=?",
389
            iname, tblname, varname);
313
            iname, tblname, varname);
390
    _sqlite_begin;
314
    _sqlite_begin;
391
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
315
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
392
    if (_sqlite_error(res)) {
-
 
393
        error("cannot complete request");
316
    if (_sqlite_error(res)) error("cannot complete request");
394
        _sqlite_commit;
317
    /*Rprintf("idx_len: %d\n", idx_len);*/
395
    }
-
 
396
 
318
 
-
 
319
    Rprintf("coltype: %d\n", coltype);
397
    if (IS_NUMERIC(idx)) {
320
    switch (coltype) {
-
 
321
        case 1010: /* logical <- logical */
-
 
322
        case 1310: /* int <- logical */
-
 
323
        case 1313: /* int <- int */
-
 
324
            /* TODO: check if factor */
-
 
325
            for (i = 0; i < idx_len; i++) { 
-
 
326
                sqlite3_reset(stmt);
-
 
327
                i2 = i % val_len;
-
 
328
                sqlite3_bind_int(stmt, 1, INTEGER(value)[i2]);
-
 
329
                sqlite3_bind_int(stmt, 2, index[i]);
-
 
330
                sqlite3_step(stmt);
-
 
331
            }
-
 
332
            break;
-
 
333
        case 1410: /* real <- logical */
-
 
334
        case 1413: /* real <- int */
-
 
335
            for (i = 0; i < idx_len; i++) { 
-
 
336
                sqlite3_reset(stmt);
-
 
337
                i2 = i % val_len;
-
 
338
                sqlite3_bind_double(stmt, 1, (double) INTEGER(value)[i2]);
-
 
339
                sqlite3_bind_int(stmt, 2, index[i]);
-
 
340
                sqlite3_step(stmt);
-
 
341
            }
-
 
342
            break;
-
 
343
        case 1414: /* real <- real */
398
        for (i = 0; i < idx_len; i++) {
344
            for (i = 0; i < idx_len; i++) { 
-
 
345
                sqlite3_reset(stmt);
-
 
346
                i2 = i % val_len;
399
            /* determine type of svec, cast value to type of it */
347
                sqlite3_bind_double(stmt, 1, REAL(value)[i2]);
-
 
348
                sqlite3_bind_int(stmt, 2, index[i]);
-
 
349
                res = sqlite3_step(stmt);
-
 
350
            }
-
 
351
            break;
-
 
352
        case 1610: /* character <- logical */
-
 
353
            for (i = 0; i < idx_len; i++) { 
-
 
354
                sqlite3_reset(stmt);
-
 
355
                i2 = i % val_len;
-
 
356
                sqlite3_bind_text(stmt, 1, 
-
 
357
                        (INTEGER(value)[i2]) ? "TRUE" : "FALSE", -1,
-
 
358
                        SQLITE_STATIC);
-
 
359
                sqlite3_bind_int(stmt, 2, index[i]);
-
 
360
                sqlite3_step(stmt);
-
 
361
            }
-
 
362
            break;
-
 
363
        case 1613: /* character <- integer */
400
            /* value can only be of a single type, because it is a vector! */
364
            /* value could be a factor! */
-
 
365
            for (i = 0; i < idx_len; i++) { 
-
 
366
                sqlite3_reset(stmt);
-
 
367
                i2 = i % val_len;
401
            /* if svec is a factor, value is a string, must check with
368
                res = sprintf(g_sql_buf[1], "%d", INTEGER(value)[i2], 
-
 
369
                        SQLITE_STATIC);
-
 
370
                sqlite3_bind_text(stmt, 1, g_sql_buf[1], res, SQLITE_STATIC);
-
 
371
                sqlite3_bind_int(stmt, 2, index[i]);
-
 
372
                sqlite3_step(stmt);
-
 
373
            }
-
 
374
            break;
-
 
375
        case 1614: { /* character <- real */
-
 
376
            SEXP chvalue;
-
 
377
            PROTECT(chvalue = AS_CHARACTER(value));
-
 
378
            for (i = 0; i < idx_len; i++) { 
-
 
379
                sqlite3_reset(stmt);
402
             * factor levels */
380
                i2 = i % val_len;
-
 
381
                sqlite3_bind_text(stmt, 1, CHAR(STRING_ELT(chvalue, i2)), 
-
 
382
                        -1, SQLITE_STATIC);
-
 
383
                sqlite3_bind_int(stmt, 2, index[i]);
-
 
384
                sqlite3_step(stmt);
-
 
385
            }
-
 
386
            UNPROTECT(1);
403
        }
387
          }
-
 
388
            break;
-
 
389
        case 1616: /* character <- character */
404
    } else if (IS_INTEGER(idx)) {
390
            for (i = 0; i < idx_len; i++) { 
-
 
391
                sqlite3_reset(stmt);
-
 
392
                i2 = i % val_len;
-
 
393
                sqlite3_bind_text(stmt, 1, CHAR(STRING_ELT(value, i2)), 
-
 
394
                        -1, SQLITE_STATIC);
-
 
395
                sqlite3_bind_int(stmt, 2, index[i]);
-
 
396
                sqlite3_step(stmt);
-
 
397
            }
-
 
398
            break;
405
    } else if (IS_LOGICAL(idx)) {
399
        case 1314: /* int <- real */
-
 
400
            _sqlite_commit;
-
 
401
            error("cannot promote vector from integer to real");
-
 
402
        case 1014: /* logical <- real */
-
 
403
            _sqlite_commit;
-
 
404
            error("cannot promote vector from logical to real");
-
 
405
        case 1013: /* logical <- int */
-
 
406
            _sqlite_commit;
-
 
407
            error("cannot promote vector from logical to integer");
-
 
408
        case 1016: /* logical <- character */
-
 
409
            _sqlite_commit;
-
 
410
            error("cannot promote vector from logical to character");
-
 
411
        case 1316: /* integer <- character */
-
 
412
            /* TODO: this could be factors */
-
 
413
            _sqlite_commit;
-
 
414
            error("cannot promote vector from integer to character");
-
 
415
        case 1416: /* real <- character */
-
 
416
            _sqlite_commit;
-
 
417
            error("cannot promote vector from real to character");
-
 
418
        default:
-
 
419
            _sqlite_commit;
-
 
420
            error("don't know what to do with coltype=%d", coltype);
406
    }
421
    }
407
 
-
 
408
    _sqlite_commit;
422
    _sqlite_commit;
-
 
423
 
409
    return R_NilValue;
424
    return svec;
410
}
425
}
411
 
426
 
412
SEXP sdf_variable_summary(SEXP svec, SEXP maxsum) {
427
SEXP sdf_variable_summary(SEXP svec, SEXP maxsum) {
413
    char *iname, *tblname, *varname, *type;
428
    char *iname, *tblname, *varname, *type;
414
    sqlite3_stmt *stmt;
429
    sqlite3_stmt *stmt;