The R Project SVN R-packages

Rev

Rev 3308 | Rev 3358 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3251 mrmanese 1
#include <stdio.h>
2
#include <string.h>
3252 mrmanese 3
#include "sqlite_dataframe.h"
3251 mrmanese 4
 
3255 mrmanese 5
/****************************************************************************
6
 * UTILITY FUNCTIONS
7
 ****************************************************************************/
3251 mrmanese 8
 
3255 mrmanese 9
/* if user supplied a name, return that name. otherwise, use the 
10
 * default "data" */
3307 mrmanese 11
static int _check_sdf_name(SEXP name, char **rname, char **iname, int *file_idx) {
3255 mrmanese 12
    int namelen = 0;
13
 
3252 mrmanese 14
    /* check if arg name is supplied */
3251 mrmanese 15
    if (IS_CHARACTER(name)) {
3255 mrmanese 16
        *rname = CHAR_ELT(name,0);
17
        if (!_is_r_sym(*rname)) { 
18
            Rprintf("Error: supplied name \"%s\"is not a valid R symbol.\n", 
19
                    *rname); 
20
            return FALSE; 
21
        }
22
        namelen = strlen(*rname);
23
        *iname = (char*)R_alloc(namelen + 9, sizeof(char)); /* <name>10000.db\0 */
24
        sprintf(*iname, "%s.db", *rname);
3251 mrmanese 25
    } else if (name == R_NilValue) {
3255 mrmanese 26
        *rname = "data";
3252 mrmanese 27
        namelen = 5;
3255 mrmanese 28
        *iname = (char*)R_alloc(13, sizeof(char)); /* data10000.db\0 */
29
        *file_idx = 1;
30
        sprintf(*iname, "%s%d.db", *rname, *file_idx);
3251 mrmanese 31
    } else {
3255 mrmanese 32
        Rprintf("Error: the supplied value for arg name is not a string.\n");
3251 mrmanese 33
    }
3255 mrmanese 34
    return namelen;
35
}
3251 mrmanese 36
 
3307 mrmanese 37
static int _find_free_filename(char *rname, char **iname, int *namelen, int *file_idx) {
3251 mrmanese 38
    do {
3255 mrmanese 39
        if (!_file_exists(*iname)) break;
40
        *namelen = sprintf(*iname, "%s%d.db", rname, ++(*file_idx)) - 3;
41
    } while (*file_idx < 10000);
42
 
43
    return *file_idx;
44
}
45
 
46
/* creates an sdf attribute table */
47
static int _create_sdf_attribute2(const char *iname) {
48
    sprintf(g_sql_buf[2], "create table [%s].sdf_attributes(attr text, "
49
            "value text, primary key (attr))", iname);
3284 mrmanese 50
    int res;
51
    res = _sqlite_exec(g_sql_buf[2]);
3255 mrmanese 52
    if (res == SQLITE_OK) {
53
        sprintf(g_sql_buf[2], "insert into [%s].sdf_attributes values ('name',"
54
                "'%s');", iname, iname);
55
        res = _sqlite_exec(g_sql_buf[2]);
56
    }
57
    return res;
58
}
59
 
3307 mrmanese 60
char *_create_sdf_skeleton2(SEXP name, int *onamelen) {
61
    char *iname, *rname;
62
    int namelen, file_idx = 0, res;
63
 
64
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
65
 
66
    if (!namelen) return NULL;
67
 
68
    _find_free_filename(rname, &iname, &namelen, &file_idx);
69
 
70
    if (file_idx >= 10000) { 
71
        Rprintf("Error: cannot find free SDF name.\n");
72
        return NULL;
73
    }
74
 
3308 mrmanese 75
    /* add to workspace */
76
    strcpy(g_sql_buf[3], iname);
3307 mrmanese 77
    iname[namelen] = 0; /* remove ".db" */
3308 mrmanese 78
    res = _add_sdf1(g_sql_buf[3], iname);
79
    if (_sqlite_error(res)) return NULL;
3307 mrmanese 80
 
3308 mrmanese 81
    /* detach SDF's if necessary to attach this one. if file does not
82
     * exist, then a sqlite db will be created after we make our 1st table */
3324 mrmanese 83
    if (!USE_SDF(iname, FALSE)) { /* _delete_sdf2(iname); */ return NULL; }
3308 mrmanese 84
 
85
 
3307 mrmanese 86
    /* create attributes table */
87
    res = _create_sdf_attribute2(iname);
88
    if (_sqlite_error(res)) {
89
        sprintf(g_sql_buf[2], "detach '%s'", iname);
3308 mrmanese 90
        _delete_sdf2(iname);
3307 mrmanese 91
        return NULL; 
92
    }
93
 
94
    *onamelen = namelen;
95
    return iname;
96
}
3255 mrmanese 97
/* checks if a column has a corresponding factor|ordered table */
98
static int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
3281 mrmanese 99
    sqlite3_stmt *stmt;
3255 mrmanese 100
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
101
            colname);
3284 mrmanese 102
    int res;
103
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
3282 mrmanese 104
    sqlite3_finalize(stmt);
3255 mrmanese 105
    return res == SQLITE_OK;
106
}
107
 
108
/* create a factor|ordered table */
109
static int _create_factor_table2(const char *iname, const char *factor_type, 
110
        const char *colname) {
111
    sqlite3_stmt *stmt;
112
    sprintf(g_sql_buf[2], "create table [%s].[%s %s] (level int, label text, "
113
            "primary key(level), unique(label));", iname, factor_type, colname);
3284 mrmanese 114
    int res;
115
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
3282 mrmanese 116
    if (res == SQLITE_OK) sqlite3_step(stmt);
3255 mrmanese 117
    sqlite3_finalize(stmt);
118
    return res; /* error on dup name? */
119
}
120
 
121
/* copy a factor|ordered table from a sdf(db) to another sdf(db) */
122
static int _copy_factor_levels2(const char *factor_type, const char *iname_src,
123
        const char *colname_src, const char *iname_dst, const char *colname_dst) {
124
    sqlite3_stmt *stmt;
125
    int res;
126
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
127
    if (res == SQLITE_OK) {
3281 mrmanese 128
        sprintf(g_sql_buf[2], "insert into [%s].[%s %s] select * from [%s].[%s %s]",
3255 mrmanese 129
                iname_src, factor_type, colname_src, iname_dst, factor_type,
130
                colname_dst);
131
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
3282 mrmanese 132
        if (res == SQLITE_OK) sqlite3_step(stmt);
3255 mrmanese 133
        sqlite3_finalize(stmt);
3281 mrmanese 134
    }
3255 mrmanese 135
    return res; /* error on dup name? */
136
}
137
 
138
 
139
/* assumes stmt has col_cnt + 1 columns, col 0 is [row name]. returned SEXP is 
140
 * not UNPROTECT-ed, user will have to do that. will create the names &
141
 * attach factor infos */
3281 mrmanese 142
static SEXP _setup_df_sexp1(sqlite3_stmt *stmt, const char *iname, 
143
        int col_cnt, int row_cnt, int *dup_indices) {
144
    SEXP ret, names, value, class = R_NilValue;
145
    int i, type;
146
    const char *colname, *coltype;
3255 mrmanese 147
 
148
    PROTECT(ret = NEW_LIST(col_cnt));
149
 
3281 mrmanese 150
    /* set up names. */
151
    PROTECT(names = NEW_CHARACTER(col_cnt));
3255 mrmanese 152
    for (i = 0; i < col_cnt; i++) {
3281 mrmanese 153
        colname = sqlite3_column_name(stmt, i+1);  /* +1 bec [row name is 0] */
154
        coltype = sqlite3_column_decltype(stmt, i+1);
155
 
3255 mrmanese 156
        if (dup_indices[i] == 0) {
157
            strcpy(g_sql_buf[1], colname);
158
        } else {
159
            sprintf(g_sql_buf[1], "%s.%d", colname, dup_indices[i]);
160
        }
161
 
3281 mrmanese 162
        SET_STRING_ELT(names, i, mkChar(g_sql_buf[1]));
163
 
164
        if (strcmp(coltype, "text") == 0) {
165
            PROTECT(value = NEW_CHARACTER(row_cnt));
166
        } else if (strcmp(coltype, "double") == 0) {
167
            PROTECT(value = NEW_NUMERIC(row_cnt));
168
        } else if (strcmp(coltype, "bit") == 0) {
169
            PROTECT(value = NEW_LOGICAL(row_cnt));
170
        } else if (strcmp(coltype, "integer") == 0 || 
171
                   strcmp(coltype, "int") == 0) {
172
            PROTECT(value = NEW_INTEGER(row_cnt));
173
            type = _get_factor_levels1(iname, colname, value);
174
            if (type == VAR_FACTOR) {
175
                PROTECT(class = mkString("factor"));
176
                SET_CLASS(value, class);
177
                UNPROTECT(1);
178
            } else if (type == VAR_ORDERED) {
179
                PROTECT(class = NEW_CHARACTER(2));
180
                SET_STRING_ELT(class, 0, mkChar("ordered"));
181
                SET_STRING_ELT(class, 1, mkChar("factor"));
182
                SET_CLASS(value, class);
183
                UNPROTECT(1);
184
            }
185
        } else {
186
            Rprintf("Error: not supported type %s for %s\n", coltype, colname);
187
            UNPROTECT(2); /* unprotect ret, names */
188
            return R_NilValue;
189
        }
190
 
191
        SET_VECTOR_ELT(ret, i, value);
192
        UNPROTECT(1); /* unprotect value */
3255 mrmanese 193
 
194
    }
3281 mrmanese 195
    SET_NAMES(ret, names);
196
    UNPROTECT(1); /* unprotect names only */
3255 mrmanese 197
    return ret;
198
}
199
 
3281 mrmanese 200
/* expected that 1st col of stmt is [row name], so we start with index 1 */
201
static int _add_row_to_df(SEXP df, sqlite3_stmt *stmt, int row, int ncols) {
202
    SEXP vec;
3282 mrmanese 203
    int type = -1, is_null, i;
3281 mrmanese 204
    for (i = 1; i <= ncols; i++) {
205
        vec = VECTOR_ELT(df, i-1);
206
        type = TYPEOF(vec);
207
 
208
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
209
 
210
        if (type == CHARSXP) {
3284 mrmanese 211
            SET_STRING_ELT(vec, row, mkChar((char *)sqlite3_column_text(stmt, i)));
3281 mrmanese 212
        } else if (type == INTSXP) {
213
            INTEGER(vec)[row] = sqlite3_column_int(stmt, i);
214
        } else if (type == REALSXP) {
215
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
216
        } else if (type == LGLSXP) {
217
            LOGICAL(vec)[row] = sqlite3_column_int(stmt, i);
218
        }
219
    }
3282 mrmanese 220
    return type;
3281 mrmanese 221
}
222
 
223
static void _set_rownames2(SEXP df) {
224
    SEXP value = VECTOR_ELT(df, 0);
225
    int len = LENGTH(value), i;
226
    PROTECT(value = NEW_CHARACTER(len));
227
    for (i = 0; i < len; i++) {
228
        sprintf(g_sql_buf[2], "%d", i+1);
229
        SET_STRING_ELT(value, i, mkChar(g_sql_buf[2]));
230
    }
231
 
232
    SET_ROWNAMES(df, value);
233
    UNPROTECT(1);
234
}
235
 
236
 
3255 mrmanese 237
/****************************************************************************
238
 * SDF FUNCTIONS
239
 ****************************************************************************/
240
 
241
SEXP sdf_create_sdf(SEXP df, SEXP name) {
242
    SEXP ret;
3307 mrmanese 243
    char *iname; 
244
    int namelen, res, i, j;
3255 mrmanese 245
 
3307 mrmanese 246
    /* find free name, attach sdf, create sdf_attributes */
247
    iname = _create_sdf_skeleton2(name, &namelen);
3251 mrmanese 248
 
3307 mrmanese 249
    if (iname != NULL) {
3255 mrmanese 250
        int sql_len, sql_len2;
3252 mrmanese 251
        sqlite3_stmt *stmt;
252
 
3255 mrmanese 253
        /* create sdf_data table */
3252 mrmanese 254
        SEXP names = GET_NAMES(df), variable, levels;
255
        int ncols = GET_LENGTH(names), type, *types;
256
        char *col_name, *class, *factor;
257
 
3324 mrmanese 258
        /* variables for adding rownames */
259
        SEXP rownames;
260
        int nrows;
261
        char *row_name;
262
 
3252 mrmanese 263
        /* TODO: put constraints on table after inserting everything? */
264
 
265
 
266
        /* 
267
         * create the create table and insert sql scripts by looping through
268
         * the columns of df
269
         */
270
        types = (int *)R_alloc(ncols, sizeof(int));
271
        sql_len = sprintf(g_sql_buf[0], "create table [%s].sdf_data ([row name] text", iname);
272
        sql_len2 = sprintf(g_sql_buf[1], "insert into [%s].sdf_data values(?", iname);
273
 
274
        for (i = 0; i < ncols; i++) {
275
            col_name = CHAR(STRING_ELT(names, i));
276
 
277
            /* add column definition to the create table sql */
278
            _expand_buf(0, sql_len+strlen(col_name)+10);
279
 
280
            variable = _getListElement(df, col_name);
281
            class = CHAR(STRING_ELT(GET_CLASS(variable), 0));
282
            type = TYPEOF(variable);
283
            types[i] = type;
284
 
285
            sql_len += sprintf(g_sql_buf[0]+sql_len, ", [%s] %s", col_name, 
286
                    _get_column_type(class, type));
287
 
288
            /* add handler to insert table sql */
289
            _expand_buf(1, sql_len+5);
290
            strcpy(g_sql_buf[1]+sql_len2, ",?");
291
            sql_len2 += 2; 
292
 
293
            /* create separate table for factors decode */
294
            if (strcmp(class, "factor") == 0 || strcmp(class, "ordered") == 0){
3255 mrmanese 295
                if (_create_factor_table2(iname, class, col_name)) 
296
                    return R_NilValue; /* dup tbl name? */
3252 mrmanese 297
 
298
                levels = GET_LEVELS(variable);
3255 mrmanese 299
                sprintf(g_sql_buf[2], "insert into [%s].[%s %s] values(?, ?);",
300
                        iname, class, col_name);
301
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
3252 mrmanese 302
                if (_sqlite_error(res)) return R_NilValue; /* dup tbl name? */
303
 
304
                for (j = 0; j < GET_LENGTH(levels); j++) {
305
                    sqlite3_reset(stmt);
306
                    factor = CHAR(STRING_ELT(levels, j));
307
                    sqlite3_bind_int(stmt, 1, j+1);
3307 mrmanese 308
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
3252 mrmanese 309
                    sqlite3_step(stmt);
310
                }
311
                sqlite3_finalize(stmt);
312
            }
313
        }
314
 
315
        _expand_buf(0,sql_len+35);
3324 mrmanese 316
        sql_len += sprintf(g_sql_buf[0]+sql_len, ", primary key([row name]));");
317
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ");"); */
3252 mrmanese 318
        res = _sqlite_exec(g_sql_buf[0]);
319
        if (_sqlite_error(res)) return R_NilValue; /* why? */
320
 
321
        /*
322
         * add the data in df to the sdf
323
         */
3324 mrmanese 324
        rownames = getAttrib(df, R_RowNamesSymbol);
325
        nrows = GET_LENGTH(rownames);
3252 mrmanese 326
 
3255 mrmanese 327
        sprintf(g_sql_buf[1]+sql_len2, ")");
328
        res = sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
3252 mrmanese 329
 
330
        for (i = 0; i < nrows; i++) {
331
            sqlite3_reset(stmt);
332
 
3324 mrmanese 333
            /* since this is coming from a real dataframe, we're assured that
334
             * the rownames are unique */
335
            if (IS_CHARACTER(rownames)) {
336
                row_name = CHAR(STRING_ELT(rownames, i));
337
                if (*row_name) /* if not empty string */
338
                    sqlite3_bind_text(stmt, 1, row_name, strlen(row_name), SQLITE_STATIC);
339
                else sqlite3_bind_int(stmt, 1, i);
340
            } else if (IS_INTEGER(rownames)) {
341
                sqlite3_bind_int(stmt, 1, INTEGER(rownames)[i]);
342
            } else sqlite3_bind_int(stmt, 1, i);
3252 mrmanese 343
 
344
            for (j = 0; j < ncols; j++) {
345
                variable = VECTOR_ELT(df, j);
346
                switch(types[j]) {
347
                    case INTSXP : 
348
                        sqlite3_bind_int(stmt, j+2, INTEGER(variable)[i]);
349
                        break;
350
                    case REALSXP:
351
                        sqlite3_bind_double(stmt, j+2, REAL(variable)[i]);
352
                        break;
353
                    case CHARSXP:
354
                        col_name = CHAR(STRING_ELT(variable,i));
355
                        sqlite3_bind_text(stmt, j+2, col_name, strlen(col_name), SQLITE_STATIC);
356
                }
357
                /* TODO: handle NA's & NULL's */
358
            }
359
 
360
            res = sqlite3_step(stmt);
361
            if (res != SQLITE_DONE) { 
362
                sqlite3_finalize(stmt);
363
                Rprintf("ERROR: %s\n", sqlite3_errmsg(g_workspace));
364
                return R_NilValue; /* why? */
365
            }
366
        }
367
 
368
        sqlite3_finalize(stmt);
369
 
3308 mrmanese 370
        /* create a new object representing the sdf */
3252 mrmanese 371
        ret = _create_sdf_sexp(iname);
372
 
373
    } else {
3308 mrmanese 374
        Rprintf("ERROR: unable to create a sqlite data frame.\n");
3252 mrmanese 375
        ret = R_NilValue;
3251 mrmanese 376
    }
377
 
3252 mrmanese 378
    return ret;
379
}
380
 
381
SEXP sdf_get_names(SEXP sdf) {
3308 mrmanese 382
    char *iname;
383
    iname = SDF_INAME(sdf);
3324 mrmanese 384
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
3252 mrmanese 385
 
3308 mrmanese 386
    int len;
387
    len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
388
 
3252 mrmanese 389
    sqlite3_stmt *stmt;
3284 mrmanese 390
    int res, i;
3252 mrmanese 391
 
392
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
393
    if (_sqlite_error(res)) return R_NilValue;
394
 
395
    SEXP ret;
396
 
397
    len = sqlite3_column_count(stmt)-1;
398
    PROTECT(ret = NEW_CHARACTER(len));
399
 
3284 mrmanese 400
    for (i = 0; i < len; i++) {
3252 mrmanese 401
        SET_STRING_ELT(ret, i, mkChar(sqlite3_column_name(stmt, i+1)));
402
    }
403
 
404
    sqlite3_finalize(stmt);
3251 mrmanese 405
    UNPROTECT(1);
406
    return ret;
407
}
408
 
3252 mrmanese 409
SEXP sdf_get_length(SEXP sdf) {
3308 mrmanese 410
    char *iname;
411
    iname  = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
3324 mrmanese 412
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
3308 mrmanese 413
 
3252 mrmanese 414
    int len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
3251 mrmanese 415
 
3252 mrmanese 416
    sqlite3_stmt *stmt;
417
    int res;
418
 
419
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
420
    if (_sqlite_error(res)) return R_NilValue;
421
 
422
    SEXP ret;
423
 
424
    len = sqlite3_column_count(stmt)-1;
425
    PROTECT(ret = NEW_INTEGER(1));
426
    INTEGER(ret)[0] = len;
427
 
428
    sqlite3_finalize(stmt);
429
    UNPROTECT(1);
430
    return ret;
431
}
432
 
3255 mrmanese 433
/* get row count */
434
SEXP sdf_get_row_count(SEXP sdf) {
3252 mrmanese 435
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
3324 mrmanese 436
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
3308 mrmanese 437
 
3252 mrmanese 438
    char **out;
439
    int res, ncol, nrow;
440
    SEXP ret;
441
 
442
    sprintf(g_sql_buf[0], "select count(*) from [%s].sdf_data;", iname);
443
    res = sqlite3_get_table(g_workspace, g_sql_buf[0], &out, &nrow, &ncol, NULL);
444
    if (_sqlite_error(res)) return R_NilValue;
445
 
446
 
447
    if (nrow != 1) ret = R_NilValue;
448
    else {
449
        PROTECT(ret = NEW_INTEGER(1));
450
        INTEGER(ret)[0] = atoi(out[1]);
451
        UNPROTECT(1);
452
    }
453
 
454
    sqlite3_free_table(out);
455
    return ret;
456
}
457
 
458
 
3255 mrmanese 459
SEXP sdf_import_table(SEXP _filename, SEXP _name, SEXP _sep, SEXP _quote, 
460
        SEXP _rownames, SEXP _colnames) {
461
    char *filename = CHAR_ELT(_filename, 0);
462
    FILE *f = fopen(filename, "r");
3252 mrmanese 463
 
3255 mrmanese 464
    if (f == NULL) {
465
        Rprintf("Error: File %s does not exist.", filename);
466
        return R_NilValue;
467
    }
3252 mrmanese 468
 
3282 mrmanese 469
    /*char *sep = CHAR_ELT(_sep, 0), *quote = CHAR_ELT(_quote,0);
470
    char *name = CHAR_ELT(_name, 0);*/
3252 mrmanese 471
 
3255 mrmanese 472
    /* create the table */
473
    /* insert the stuffs */
474
    /* register to workspace */
475
 
476
    return R_NilValue;
477
}
478
 
479
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
3282 mrmanese 480
    SEXP ret = R_NilValue;
3255 mrmanese 481
    char *iname = SDF_INAME(sdf);
3324 mrmanese 482
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
3308 mrmanese 483
 
3282 mrmanese 484
    sqlite3_stmt *stmt;
3281 mrmanese 485
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
3255 mrmanese 486
    int i, j,res;
487
    int *col_indices, col_index_len, *dup_indices;
3282 mrmanese 488
    int row_index_len = 0;
3255 mrmanese 489
 
3281 mrmanese 490
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
3255 mrmanese 491
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
492
    if (_sqlite_error(res)) return R_NilValue;
493
 
494
    buflen = sprintf(g_sql_buf[0], "select [row name]");
495
    idxlen = LENGTH(col);
496
    col_cnt = sqlite3_column_count(stmt) - 1;
497
 
498
    /*
499
     * PROCESS COLUMN INDEX
500
     */
3281 mrmanese 501
    if (col == R_NilValue) {
3255 mrmanese 502
        for (i = 1; i < col_cnt+1; i++) {
3281 mrmanese 503
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
3255 mrmanese 504
                    sqlite3_column_name(stmt, i)); 
505
        }
3281 mrmanese 506
        _expand_buf(0, buflen+20+strlen(iname));
507
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
508
        col_index_len = col_cnt;
509
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
510
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
511
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
3255 mrmanese 512
    } else if (col == R_NilValue || idxlen < 1) {
513
        sqlite3_finalize(stmt);
514
        return R_NilValue;
515
    } else if (IS_NUMERIC(col)) {
516
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
517
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
518
        col_index_len = 0;
519
 
520
        for (i = 0; i < idxlen; i++) {
521
            /* no need to correct for 0 base because col 0 is [row name] */
522
            index = ((int) REAL(col)[i]); 
523
            if (index > col_cnt) {
524
                sqlite3_finalize(stmt);
525
                Rprintf("Error: undefined columns selected\n");
526
                return R_NilValue;
527
            } else if (index > 0) {
3281 mrmanese 528
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
3255 mrmanese 529
                        sqlite3_column_name(stmt,index));
530
                dup_indices[col_index_len] = 0;
531
                for (j = 0; j < col_index_len; j++) {
532
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
533
                }
534
                col_indices[col_index_len++] = index;
535
            } else if (index < 0) {
536
                sqlite3_finalize(stmt);
537
                Rprintf("Error: negative indices not supported.\n");
538
                return R_NilValue;
539
            }
540
        }
541
 
542
        if (col_index_len == 0) {
543
            sqlite3_finalize(stmt);
544
            Rprintf("Error: no indices detected??\n");
545
            return R_NilValue;
3281 mrmanese 546
        } else { 
547
            _expand_buf(0, buflen+20+strlen(iname));
548
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
3255 mrmanese 549
        }
550
    } else if (IS_INTEGER(col)) {
551
        /* identical logic with IS_NUMERIC, except that we don't have to cast
552
         * stuffs from SEXP col. the alternative is doing if idxlen times. */
553
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
554
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
555
        col_index_len = 0;
556
 
557
        for (i = 0; i < idxlen; i++) {
558
            /* no need to correct for 0 base because col 0 is [row name] */
559
            index = INTEGER(col)[i];
560
            if (index > col_cnt) {
561
                sqlite3_finalize(stmt);
562
                Rprintf("Error: undefined columns selected\n");
563
                return R_NilValue;
564
            } else if (index > 0) {
3281 mrmanese 565
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
3255 mrmanese 566
                        sqlite3_column_name(stmt,index));
567
                dup_indices[col_index_len] = 0;
568
                for (j = 0; j < col_index_len; j++) {
569
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
570
                }
571
                col_indices[col_index_len++] = index;
572
            } else if (index < 0) {
573
                sqlite3_finalize(stmt);
574
                Rprintf("Error: negative indices not supported.\n");
575
                return R_NilValue;
576
            }
577
        }
578
 
579
        if (col_index_len == 0) {
580
            sqlite3_finalize(stmt);
581
            Rprintf("Error: no indices detected??\n");
582
            return R_NilValue;
3281 mrmanese 583
        } else { 
584
            _expand_buf(0, buflen+20+strlen(iname));
585
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
3255 mrmanese 586
        }
587
    } else if (IS_LOGICAL(col)) {
588
        /* recycling stuff, so max column output is the # of cols in the df */
589
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
590
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
591
        col_index_len = 0;
592
        for (i = 0; i < col_cnt; i++) {
593
            if (LOGICAL(col)[i%idxlen]) {
3281 mrmanese 594
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
3255 mrmanese 595
                        sqlite3_column_name(stmt,i+1));
596
                dup_indices[col_index_len] = 0;
597
                col_indices[col_index_len++] = i;
598
            }
599
        }
600
 
601
        if (col_index_len == 0) {
602
            sqlite3_finalize(stmt);
603
            Rprintf("Warning: no column selected.\n");
604
            return R_NilValue;
3281 mrmanese 605
        } else { 
606
            _expand_buf(0, buflen+20+strlen(iname));
607
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
3255 mrmanese 608
        }
609
    } else {
610
        sqlite3_finalize(stmt);
611
        Rprintf("Error: don't know how to handle column index.\n");
612
        return R_NilValue;
613
    }
614
 
615
    /* 
616
     * PROCESS ROW INDEX
617
     */
618
    idxlen = LENGTH(row);
3281 mrmanese 619
    if (row == R_NilValue) {
620
        if (col_index_len == 1) {
621
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
622
            sqlite3_finalize(stmt);
623
            return ret;
624
        } if (col_index_len > 1) {
3255 mrmanese 625
            /* create a new SDF, logic similar to sdf_create_sdf */
626
 
627
            /* find a new name. data<n> ? */
3307 mrmanese 628
            char *iname2;
3281 mrmanese 629
            const char *colname;
3307 mrmanese 630
            int namelen, sql_len, sql_len2;
3255 mrmanese 631
 
3307 mrmanese 632
            iname2 = _create_sdf_skeleton2(R_NilValue, &namelen);
3255 mrmanese 633
 
634
            /* create sdf_data table */
3281 mrmanese 635
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
636
            sql_len2 = 0;
3255 mrmanese 637
 
638
            for (i = 0; i < col_index_len; i++) {
639
                colname = sqlite3_column_name(stmt, col_indices[i]);
640
                if (dup_indices[i] == 0) {
3281 mrmanese 641
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s] %s", colname,
3255 mrmanese 642
                            sqlite3_column_decltype(stmt, col_indices[i]));
643
                } else {
644
                    /* un-duplicate col names by appending num, just like R */
3281 mrmanese 645
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
3255 mrmanese 646
                            dup_indices[i], 
647
                            sqlite3_column_decltype(stmt, col_indices[i]));
648
                }
649
 
650
                /* deal with possibly factor columns */
651
                if (_is_factor2(iname, "factor", colname)) {
652
                    /* found a factor column */
3281 mrmanese 653
 
3255 mrmanese 654
                    if (dup_indices[i] == 0)  {
655
                        _copy_factor_levels2("factor", iname, colname, iname2, colname);
656
                    } else {
657
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
658
                        _copy_factor_levels2("factor", iname, colname, iname2, 
659
                                g_sql_buf[3]);
660
                    }
661
                }
662
 
663
                /* and deal with ordered factors too... life is hard, then you die */
664
                if (_is_factor2(iname, "ordered", colname)) {
3281 mrmanese 665
 
3255 mrmanese 666
                    if (dup_indices[i] == 0)  {
667
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
668
                                colname);
669
                    } else {
670
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
671
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
672
                                g_sql_buf[3]);
673
                    }
674
                }
675
 
676
            }
677
 
678
            /* don't need it anymore */
679
            sqlite3_finalize(stmt);
680
 
681
            /* no table index created, that's for v2 I hope*/
682
            sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
683
            res = _sqlite_exec(g_sql_buf[1]);
3281 mrmanese 684
            if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
3255 mrmanese 685
 
686
            /* insert data (with row names). buf[0] contains a select */
687
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
688
                    g_sql_buf[0]);
689
            res = _sqlite_exec(g_sql_buf[1]);
690
            if (_sqlite_error(res)) return R_NilValue;
691
 
692
            /* add new sdf to workspace */
693
            sprintf(g_sql_buf[0], "%s.db", iname2);
3281 mrmanese 694
            res = _add_sdf1(g_sql_buf[0], iname2);
3255 mrmanese 695
            if (_sqlite_error(res)) return R_NilValue;
696
 
697
            /* create SEXP for the SDF */
698
            ret = _create_sdf_sexp(iname2);
3281 mrmanese 699
            return ret;
3255 mrmanese 700
        } else { sqlite3_finalize(stmt); return R_NilValue; }
701
    } else if (row == R_NilValue || idxlen < 1) {
702
        sqlite3_finalize(stmt);
703
        return R_NilValue;
704
    } else if (IS_NUMERIC(row)) {
705
        sqlite3_finalize(stmt);
706
 
3281 mrmanese 707
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
708
        row_cnt = _get_row_count2(g_sql_buf[1]);
709
 
3255 mrmanese 710
        /* append " limit ?,1" to the formed select statement */
711
        _expand_buf(0, buflen+10);
3281 mrmanese 712
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
713
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
3255 mrmanese 714
 
3281 mrmanese 715
        index = ((int) REAL(row)[0]) - 1;
3255 mrmanese 716
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
717
 
718
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
719
        if (_sqlite_error(res)) return R_NilValue;
720
 
721
        sqlite3_bind_int(stmt, 1, index);
722
        res = sqlite3_step(stmt);
723
 
724
        /* create data frame */
3281 mrmanese 725
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
726
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
3255 mrmanese 727
 
3281 mrmanese 728
        /* put data in it */
729
        if (index >= 0 && index < row_cnt) {
730
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
731
        }
3255 mrmanese 732
 
3281 mrmanese 733
        for (i = 1; i < idxlen; i++) {
734
            sqlite3_reset(stmt);
735
            index = ((int) REAL(row)[i]) - 1;
736
            if (index >= 0 && index < row_cnt) {
737
                sqlite3_bind_int(stmt, 1, index);
738
                res = sqlite3_step(stmt);
739
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
740
            }
741
        }
742
        sqlite3_finalize(stmt);
743
 
744
        /* shrink vectors */
745
        if (row_index_len < idxlen) {
746
            for (i = 0; i < col_index_len; i++) {
747
                SET_VECTOR_ELT(ret, i, 
748
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
749
            }
750
        }
751
 
752
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
753
    } else if (IS_INTEGER(row)) {
754
        /* same stuff as with IS_INTEGER, except for setting of var index*/
755
        sqlite3_finalize(stmt);
756
 
757
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
758
        row_cnt = _get_row_count2(g_sql_buf[1]);
759
 
760
        /* append " limit ?,1" to the formed select statement */
761
        _expand_buf(0, buflen+10);
762
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
763
 
764
        index = INTEGER(row)[0] - 1;
765
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
766
 
767
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
768
        if (_sqlite_error(res)) return R_NilValue;
769
 
770
        sqlite3_bind_int(stmt, 1, index);
771
        res = sqlite3_step(stmt);
772
 
773
        /* create data frame */
774
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
775
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
776
 
3255 mrmanese 777
        /* put data in it */
3281 mrmanese 778
        if (index >= 0 && index < row_cnt) {
779
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
780
        }
781
 
782
        for (i = 1; i < idxlen; i++) {
783
            sqlite3_reset(stmt);
784
            index = INTEGER(row)[i] - 1;
785
            if (index >= 0 && index < row_cnt) {
786
                sqlite3_bind_int(stmt, 1, index);
787
                res = sqlite3_step(stmt); 
788
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
789
            }
790
        }
791
        sqlite3_finalize(stmt);
792
 
793
        /* shrink vectors */
794
        if (row_index_len < idxlen) {
795
            for (i = 0; i < col_index_len; i++) {
796
                SET_VECTOR_ELT(ret, i, 
797
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
798
            }
799
        }
800
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
3255 mrmanese 801
    } else if (IS_LOGICAL(row)) {
802
        /* */
3281 mrmanese 803
        sqlite3_finalize(stmt);
804
        int est_row_cnt = 0;
805
 
806
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
807
        row_cnt = _get_row_count2(g_sql_buf[1]);
808
 
809
        /* append " limit ?,1" to the formed select statement */
810
        _expand_buf(0, buflen+10);
811
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
812
 
813
        /* find if there is any TRUE element in the vector */
3282 mrmanese 814
        for (i = 0; i < idxlen && i < row_cnt; i++) {
3281 mrmanese 815
            if (LOGICAL(row)[i]) break;
816
        }
817
 
3282 mrmanese 818
        if (i < idxlen && i < row_cnt) {
3281 mrmanese 819
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
820
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
821
 
822
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
823
            if (_sqlite_error(res)) return R_NilValue;
824
 
825
            sqlite3_bind_int(stmt, 1, i);
826
            sqlite3_step(stmt);
827
            ret = _setup_df_sexp1(stmt, iname, col_index_len, est_row_cnt, dup_indices);
828
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
829
 
830
            for (i++ ; i < row_cnt; i++) {
831
                if (LOGICAL(row)[i%idxlen]) {
832
                    sqlite3_reset(stmt);
833
                    sqlite3_bind_int(stmt, 1, i);
834
                    sqlite3_step(stmt);
835
                    _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
836
                }
837
            }
838
        }
839
 
840
        sqlite3_finalize(stmt);
841
 
842
        /* shrink vectors */
843
        if (est_row_cnt > 0 && row_index_len < est_row_cnt) {
844
            for (i = 0; i < col_index_len; i++) {
845
                SET_VECTOR_ELT(ret, i, 
846
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
847
            }
848
        }
849
 
850
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
3255 mrmanese 851
    }
852
 
3281 mrmanese 853
    if (ret != R_NilValue && col_index_len > 1) {
854
        SEXP class = mkString("data.frame");
855
        SET_CLASS(ret, class);
856
        _set_rownames2(ret);
857
    } else if (ret != R_NilValue && col_index_len == 1) {
858
        ret = VECTOR_ELT(ret, 0);
859
    }
3255 mrmanese 860
 
861
    return ret;
862
}
863
 
3324 mrmanese 864
SEXP sdf_rbind(SEXP sdf, SEXP data) {
865
    char *class = CHAR_ELT(data, 0);
866
    char *iname = SDF_INAME(sdf), *colname;
867
    SEXP ret = R_NilValue, col, names, levels;
868
    int len, buflen, buflen2, i, j, res, type, nrows;
869
    sqlite3_stmt *stmt;
3281 mrmanese 870
 
3324 mrmanese 871
    if (strcmp(class,"data.frame") == 0) {
872
        /* check table names, types. variables may not be in same order, as
873
         * long as names and types are identical. */
874
        names = GET_NAMES(data);
875
        len = GET_LENGTH(names);
876
 
877
        buflen = sprintf(g_sql_buf[0], "select 1");
878
        for (i = 0; i < len; i++) {
879
            _expand_buf(0, buflen + 100);
880
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
881
        }
882
        _expand_buf(0, buflen + 100);
883
        sprintf(g_sql_buf[0]+buflen, " from %s", iname);
884
 
885
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
886
        if (res != SQLITE_OK) { /* column name mismatch */
887
            Rprintf("Error: column names should exactly match the names of the SDF.\n");
888
            return ret;
889
        }
890
 
891
        /* now check the type */
892
        sqlite3_step(stmt);
893
        for (i = 0; i< len; i++) {
894
            do { 
895
                type = sqlite3_column_type(stmt, i+1); 
896
            } while((type == SQLITE_NULL) && (sqlite3_step(stmt) != SQLITE_DONE));
897
 
898
            if (type == SQLITE_NULL) {
899
                Rprintf("Error: can't figure out type of col %d because all rows are NULL."
900
                        " There is still a way to figure out type, but I'm bailing out.", i);
901
                sqlite3_finalize(stmt);
902
                return ret;
903
            }
904
 
905
            col = VECTOR_ELT(data, i);
906
            class = CHAR_ELT(GET_CLASS(col),0);
907
            if ((type == SQLITE_FLOAT && !IS_NUMERIC(col)) ||
908
                (type == SQLITE_TEXT && !IS_CHARACTER(col)) ||
909
                (type == SQLITE_INTEGER && !IS_INTEGER(col))) break;
910
 
911
            /* additional tests for int */
912
            if (type == SQLITE_INTEGER) {
913
                colname = CHAR_ELT(names, i);
914
 
915
                /* test if the sdf column is indeed a factor */
916
                if (strcmp(class,"factor") == 0) {
917
                    sqlite3_stmt *stmt2;
918
                    if (!_is_factor2(iname, "factor", colname)) break;
919
                    sprintf(g_sql_buf[1], "[%s].[factor %s]", iname, colname);
920
                    nrows = _get_row_count2(g_sql_buf[1]);
921
 
922
                    levels = GET_LEVELS(col);
923
                    if (nrows < GET_LENGTH(levels)) {
924
                        Rprintf("Error: The data frame variable %s has more levels than"
925
                                " its SDF counterpart.\n", colname);
926
                        break;
927
                    }
928
 
929
                    sprintf(g_sql_buf[2], "select label from %s ordered by level", g_sql_buf[1]);
930
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
931
 
932
                    for (j = 0; j < nrows; j++)  {
933
                        sqlite3_step(stmt2);
934
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) == 0) {
935
                            Rprintf("Error: Level label mismatch in variable %s, [%s] in data frame"
936
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
937
                                    CHAR_ELT(levels, j));
938
                            break;
939
                        }
940
                    }
941
 
942
                    if (j < nrows); break;
943
 
944
                }  else if (strcmp(class,"ordered") == 0) { /* same with ordered */
945
                    sqlite3_stmt *stmt2;
946
                    if (!_is_factor2(iname, "ordered", colname)) break;
947
                    sprintf(g_sql_buf[1], "[%s].[ordered %s]", iname, colname);
948
                    nrows = _get_row_count2(g_sql_buf[1]);
949
 
950
                    levels = GET_LEVELS(col);
951
                    if (nrows < GET_LENGTH(levels)) {
952
                        Rprintf("Error: The data frame variable %s has more levels than"
953
                                " its SDF counterpart.\n", colname);
954
                        break;
955
                    }
956
 
957
                    sprintf(g_sql_buf[2], "select label from %s ordered by level", g_sql_buf[1]);
958
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
959
 
960
                    for (j = 0; j < nrows; j++)  {
961
                        sqlite3_step(stmt2);
962
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) == 0) {
963
                            Rprintf("Error: Level label mismatch in variable %s, [%s] in data frame"
964
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
965
                                    CHAR_ELT(levels, j));
966
                            break;
967
                        }
968
                    }
969
 
970
                    if (j < nrows); break;
971
 
972
                }  /* else if class == "ordered" */
973
            } /* if integer */
974
        } /* for loop for column type checking */
975
 
976
        if (i < len) {
977
            Rprintf("Error: type mismatch at variable %s\n", CHAR_ELT(names, i));
978
            return ret;
979
        }
980
 
981
        /* create the insert statement */
982
        buflen = sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name]", iname);
983
        buflen2 = sprintf(g_sql_buf[1], "values(?");
984
        for (i = 0; i < len; i++) {
985
            _expand_buf(0, buflen+100);
986
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
987
            _expand_buf(1, buflen2+5);
988
            buflen2 += sprintf(g_sql_buf[1]+buflen2, ",?");
989
        }
990
 
991
        _expand_buf(2, buflen + buflen2 + 10);
992
        sprintf(g_sql_buf[2], "%s) %s", g_sql_buf[0], g_sql_buf[1]);
993
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
994
        if (_sqlite_error(res)) return ret;
995
 
996
        /* finally, add rows */
997
        nrows = LENGTH(GET_ROWNAMES(data));
998
 
999
        for (i = 0; i < nrows; i++) {
1000
 
1001
        }
1002
 
1003
 
1004
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1005
    }
1006
 
1007
    return ret;
1008
}
1009
 
1010
 
3251 mrmanese 1011
SEXP sopen(SEXP name) {
1012
    char *filename;
1013
 
1014
    if (IS_CHARACTER(name)) {
1015
        filename = CHAR(STRING_ELT(name,0));
1016
        Rprintf("%s\n", filename);
1017
    }
1018
    /* sqlite3 *db;
1019
    int res = sqlite3_open(filename, &db); */
1020
    SEXP ret;
1021
    PROTECT(ret = NEW_LOGICAL(1));
1022
    LOGICAL(ret)[0] = IS_CHARACTER(name);
1023
    /* sqlite3_close(db); */ 
1024
    UNPROTECT(1);
1025
    return ret;
1026
}