The R Project SVN R-packages

Rev

Rev 3446 | Rev 3471 | 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 */
3426 mrmanese 15
    if (name == R_NilValue) {
16
        *rname = "data";
17
        namelen = 5;
18
        *iname = (char*)R_alloc(13, sizeof(char)); /* data10000.db\0 */
19
        *file_idx = 1;
20
        sprintf(*iname, "%s%d.db", *rname, *file_idx);
21
    } else if (IS_CHARACTER(name)) {
3255 mrmanese 22
        *rname = CHAR_ELT(name,0);
23
        if (!_is_r_sym(*rname)) { 
24
            Rprintf("Error: supplied name \"%s\"is not a valid R symbol.\n", 
25
                    *rname); 
26
            return FALSE; 
27
        }
28
        namelen = strlen(*rname);
29
        *iname = (char*)R_alloc(namelen + 9, sizeof(char)); /* <name>10000.db\0 */
30
        sprintf(*iname, "%s.db", *rname);
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
 
3419 mrmanese 60
char *_create_sdf_skeleton1(SEXP name, int *onamelen, int protect) {
3307 mrmanese 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 */
3419 mrmanese 83
    if (!USE_SDF1(iname, FALSE, protect)) { /* _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
 
3397 mrmanese 94
    if (onamelen) *onamelen = namelen;
3307 mrmanese 95
    return iname;
96
}
3255 mrmanese 97
/* checks if a column has a corresponding factor|ordered table */
3419 mrmanese 98
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 */
3397 mrmanese 109
int _create_factor_table2(const char *iname, const char *factor_type, 
3255 mrmanese 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) */
3358 mrmanese 122
int _copy_factor_levels2(const char *factor_type, const char *iname_src,
3255 mrmanese 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
 
3457 mrmanese 210
        if (type == STRSXP || 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
 
3457 mrmanese 223
/* set ordinary df row name as numbers */
3281 mrmanese 224
static void _set_rownames2(SEXP df) {
225
    SEXP value = VECTOR_ELT(df, 0);
226
    int len = LENGTH(value), i;
227
    PROTECT(value = NEW_CHARACTER(len));
228
    for (i = 0; i < len; i++) {
229
        sprintf(g_sql_buf[2], "%d", i+1);
230
        SET_STRING_ELT(value, i, mkChar(g_sql_buf[2]));
231
    }
232
 
233
    SET_ROWNAMES(df, value);
234
    UNPROTECT(1);
235
}
236
 
237
 
3255 mrmanese 238
/****************************************************************************
239
 * SDF FUNCTIONS
240
 ****************************************************************************/
241
 
242
SEXP sdf_create_sdf(SEXP df, SEXP name) {
243
    SEXP ret;
3307 mrmanese 244
    char *iname; 
245
    int namelen, res, i, j;
3255 mrmanese 246
 
3307 mrmanese 247
    /* find free name, attach sdf, create sdf_attributes */
3419 mrmanese 248
    iname = _create_sdf_skeleton1(name, &namelen, FALSE);
3251 mrmanese 249
 
3307 mrmanese 250
    if (iname != NULL) {
3255 mrmanese 251
        int sql_len, sql_len2;
3252 mrmanese 252
        sqlite3_stmt *stmt;
253
 
3255 mrmanese 254
        /* create sdf_data table */
3426 mrmanese 255
        SEXP names = GET_NAMES(df), variable, levels, var_class;
3252 mrmanese 256
        int ncols = GET_LENGTH(names), type, *types;
257
        char *col_name, *class, *factor;
258
 
3324 mrmanese 259
        /* variables for adding rownames */
260
        SEXP rownames;
261
        int nrows;
262
        char *row_name;
263
 
3252 mrmanese 264
        /* TODO: put constraints on table after inserting everything? */
265
 
266
 
267
        /* 
268
         * create the create table and insert sql scripts by looping through
269
         * the columns of df
270
         */
271
        types = (int *)R_alloc(ncols, sizeof(int));
272
        sql_len = sprintf(g_sql_buf[0], "create table [%s].sdf_data ([row name] text", iname);
273
        sql_len2 = sprintf(g_sql_buf[1], "insert into [%s].sdf_data values(?", iname);
274
 
275
        for (i = 0; i < ncols; i++) {
276
            col_name = CHAR(STRING_ELT(names, i));
277
 
278
            /* add column definition to the create table sql */
279
            _expand_buf(0, sql_len+strlen(col_name)+10);
280
 
281
            variable = _getListElement(df, col_name);
3426 mrmanese 282
            var_class = GET_CLASS(variable);
283
            class = (var_class == R_NilValue) ? NULL : CHAR(STRING_ELT(var_class, 0));
3252 mrmanese 284
            type = TYPEOF(variable);
285
            types[i] = type;
286
 
287
            sql_len += sprintf(g_sql_buf[0]+sql_len, ", [%s] %s", col_name, 
288
                    _get_column_type(class, type));
289
 
290
            /* add handler to insert table sql */
291
            _expand_buf(1, sql_len+5);
292
            strcpy(g_sql_buf[1]+sql_len2, ",?");
293
            sql_len2 += 2; 
294
 
295
            /* create separate table for factors decode */
3426 mrmanese 296
            if (class != NULL && (strcmp(class, "factor") == 0 || strcmp(class, "ordered") == 0)){
3255 mrmanese 297
                if (_create_factor_table2(iname, class, col_name)) 
298
                    return R_NilValue; /* dup tbl name? */
3252 mrmanese 299
 
3397 mrmanese 300
                _sqlite_exec("begin");
3252 mrmanese 301
                levels = GET_LEVELS(variable);
3255 mrmanese 302
                sprintf(g_sql_buf[2], "insert into [%s].[%s %s] values(?, ?);",
303
                        iname, class, col_name);
304
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
3252 mrmanese 305
                if (_sqlite_error(res)) return R_NilValue; /* dup tbl name? */
306
 
307
                for (j = 0; j < GET_LENGTH(levels); j++) {
308
                    sqlite3_reset(stmt);
309
                    factor = CHAR(STRING_ELT(levels, j));
310
                    sqlite3_bind_int(stmt, 1, j+1);
3307 mrmanese 311
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
3252 mrmanese 312
                    sqlite3_step(stmt);
313
                }
314
                sqlite3_finalize(stmt);
3397 mrmanese 315
                _sqlite_exec("commit");
3252 mrmanese 316
            }
317
        }
318
 
319
        _expand_buf(0,sql_len+35);
3419 mrmanese 320
        sql_len += sprintf(g_sql_buf[0]+sql_len, ", primary key([row name]));");
321
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ");"); */
3252 mrmanese 322
        res = _sqlite_exec(g_sql_buf[0]);
323
        if (_sqlite_error(res)) return R_NilValue; /* why? */
324
 
325
        /*
326
         * add the data in df to the sdf
327
         */
3324 mrmanese 328
        rownames = getAttrib(df, R_RowNamesSymbol);
329
        nrows = GET_LENGTH(rownames);
3252 mrmanese 330
 
3397 mrmanese 331
        _sqlite_error(_sqlite_exec("begin"));
3255 mrmanese 332
        sprintf(g_sql_buf[1]+sql_len2, ")");
333
        res = sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
3252 mrmanese 334
 
335
        for (i = 0; i < nrows; i++) {
336
            sqlite3_reset(stmt);
337
 
3324 mrmanese 338
            /* since this is coming from a real dataframe, we're assured that
339
             * the rownames are unique */
340
            if (IS_CHARACTER(rownames)) {
341
                row_name = CHAR(STRING_ELT(rownames, i));
342
                if (*row_name) /* if not empty string */
343
                    sqlite3_bind_text(stmt, 1, row_name, strlen(row_name), SQLITE_STATIC);
344
                else sqlite3_bind_int(stmt, 1, i);
345
            } else if (IS_INTEGER(rownames)) {
346
                sqlite3_bind_int(stmt, 1, INTEGER(rownames)[i]);
347
            } else sqlite3_bind_int(stmt, 1, i);
3252 mrmanese 348
 
349
            for (j = 0; j < ncols; j++) {
350
                variable = VECTOR_ELT(df, j);
351
                switch(types[j]) {
352
                    case INTSXP : 
353
                        sqlite3_bind_int(stmt, j+2, INTEGER(variable)[i]);
354
                        break;
355
                    case REALSXP:
356
                        sqlite3_bind_double(stmt, j+2, REAL(variable)[i]);
357
                        break;
358
                    case CHARSXP:
3457 mrmanese 359
                    case STRSXP:
360
                        sqlite3_bind_text(stmt, j+2, CHAR_ELT(variable, i), -1, SQLITE_STATIC);
3252 mrmanese 361
                }
362
                /* TODO: handle NA's & NULL's */
363
            }
364
 
365
            res = sqlite3_step(stmt);
366
            if (res != SQLITE_DONE) { 
3397 mrmanese 367
                _sqlite_exec("ROLLBACK");
3252 mrmanese 368
                sqlite3_finalize(stmt);
3397 mrmanese 369
                Rprintf("SQLITE ERROR: %s\n", sqlite3_errmsg(g_workspace));
3252 mrmanese 370
                return R_NilValue; /* why? */
371
            }
372
        }
373
 
374
        sqlite3_finalize(stmt);
3397 mrmanese 375
        _sqlite_error(_sqlite_exec("COMMIT"));
3252 mrmanese 376
 
3308 mrmanese 377
        /* create a new object representing the sdf */
3252 mrmanese 378
        ret = _create_sdf_sexp(iname);
379
 
380
    } else {
3308 mrmanese 381
        Rprintf("ERROR: unable to create a sqlite data frame.\n");
3252 mrmanese 382
        ret = R_NilValue;
3251 mrmanese 383
    }
384
 
3252 mrmanese 385
    return ret;
386
}
387
 
388
SEXP sdf_get_names(SEXP sdf) {
3308 mrmanese 389
    char *iname;
390
    iname = SDF_INAME(sdf);
3419 mrmanese 391
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
3252 mrmanese 392
 
3308 mrmanese 393
    int len;
394
    len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
395
 
3252 mrmanese 396
    sqlite3_stmt *stmt;
3284 mrmanese 397
    int res, i;
3252 mrmanese 398
 
399
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
400
    if (_sqlite_error(res)) return R_NilValue;
401
 
402
    SEXP ret;
403
 
404
    len = sqlite3_column_count(stmt)-1;
405
    PROTECT(ret = NEW_CHARACTER(len));
406
 
3284 mrmanese 407
    for (i = 0; i < len; i++) {
3252 mrmanese 408
        SET_STRING_ELT(ret, i, mkChar(sqlite3_column_name(stmt, i+1)));
409
    }
410
 
411
    sqlite3_finalize(stmt);
3251 mrmanese 412
    UNPROTECT(1);
413
    return ret;
414
}
415
 
3252 mrmanese 416
SEXP sdf_get_length(SEXP sdf) {
3308 mrmanese 417
    char *iname;
3446 mrmanese 418
    int len;
419
    sqlite3_stmt *stmt;
420
    int res;
421
 
3308 mrmanese 422
    iname  = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
3446 mrmanese 423
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
3419 mrmanese 424
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
3308 mrmanese 425
 
3446 mrmanese 426
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
3252 mrmanese 427
    if (_sqlite_error(res)) return R_NilValue;
428
 
429
    len = sqlite3_column_count(stmt)-1;
430
    sqlite3_finalize(stmt);
3446 mrmanese 431
    return ScalarInteger(len);
3252 mrmanese 432
}
433
 
3255 mrmanese 434
/* get row count */
435
SEXP sdf_get_row_count(SEXP sdf) {
3252 mrmanese 436
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
3419 mrmanese 437
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
3308 mrmanese 438
 
3358 mrmanese 439
    int nrow;
3252 mrmanese 440
    SEXP ret;
441
 
3358 mrmanese 442
    nrow = _get_row_count2(iname, TRUE);
3252 mrmanese 443
 
3358 mrmanese 444
    if (nrow < 1) ret = R_NilValue;
3446 mrmanese 445
    else ret = ScalarInteger(nrow);
3252 mrmanese 446
 
447
    return ret;
448
}
449
 
450
 
3255 mrmanese 451
SEXP sdf_import_table(SEXP _filename, SEXP _name, SEXP _sep, SEXP _quote, 
452
        SEXP _rownames, SEXP _colnames) {
453
    char *filename = CHAR_ELT(_filename, 0);
454
    FILE *f = fopen(filename, "r");
3252 mrmanese 455
 
3255 mrmanese 456
    if (f == NULL) {
457
        Rprintf("Error: File %s does not exist.", filename);
458
        return R_NilValue;
459
    }
3252 mrmanese 460
 
3282 mrmanese 461
    /*char *sep = CHAR_ELT(_sep, 0), *quote = CHAR_ELT(_quote,0);
462
    char *name = CHAR_ELT(_name, 0);*/
3252 mrmanese 463
 
3255 mrmanese 464
    /* create the table */
465
    /* insert the stuffs */
466
    /* register to workspace */
467
 
468
    return R_NilValue;
469
}
470
 
471
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
3282 mrmanese 472
    SEXP ret = R_NilValue;
3255 mrmanese 473
    char *iname = SDF_INAME(sdf);
3419 mrmanese 474
    if (!USE_SDF1(iname, TRUE, TRUE)) return R_NilValue;
3308 mrmanese 475
 
3282 mrmanese 476
    sqlite3_stmt *stmt;
3281 mrmanese 477
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
3255 mrmanese 478
    int i, j,res;
479
    int *col_indices, col_index_len, *dup_indices;
3282 mrmanese 480
    int row_index_len = 0;
3255 mrmanese 481
 
3281 mrmanese 482
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
3255 mrmanese 483
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
484
    if (_sqlite_error(res)) return R_NilValue;
485
 
486
    buflen = sprintf(g_sql_buf[0], "select [row name]");
487
    idxlen = LENGTH(col);
488
    col_cnt = sqlite3_column_count(stmt) - 1;
489
 
490
    /*
3457 mrmanese 491
     * PROCESS COLUMN INDEX: set columns in the select statement
3255 mrmanese 492
     */
3281 mrmanese 493
    if (col == R_NilValue) {
3255 mrmanese 494
        for (i = 1; i < col_cnt+1; i++) {
3281 mrmanese 495
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
3255 mrmanese 496
                    sqlite3_column_name(stmt, i)); 
497
        }
3281 mrmanese 498
        _expand_buf(0, buflen+20+strlen(iname));
499
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
500
        col_index_len = col_cnt;
501
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
502
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
503
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
3255 mrmanese 504
    } else if (col == R_NilValue || idxlen < 1) {
505
        sqlite3_finalize(stmt);
506
        return R_NilValue;
507
    } else if (IS_NUMERIC(col)) {
508
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
509
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
510
        col_index_len = 0;
511
 
512
        for (i = 0; i < idxlen; i++) {
513
            /* no need to correct for 0 base because col 0 is [row name] */
514
            index = ((int) REAL(col)[i]); 
515
            if (index > col_cnt) {
516
                sqlite3_finalize(stmt);
517
                Rprintf("Error: undefined columns selected\n");
518
                return R_NilValue;
519
            } else if (index > 0) {
3281 mrmanese 520
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
3255 mrmanese 521
                        sqlite3_column_name(stmt,index));
522
                dup_indices[col_index_len] = 0;
523
                for (j = 0; j < col_index_len; j++) {
524
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
525
                }
526
                col_indices[col_index_len++] = index;
527
            } else if (index < 0) {
528
                sqlite3_finalize(stmt);
529
                Rprintf("Error: negative indices not supported.\n");
530
                return R_NilValue;
531
            }
532
        }
533
 
534
        if (col_index_len == 0) {
535
            sqlite3_finalize(stmt);
536
            Rprintf("Error: no indices detected??\n");
537
            return R_NilValue;
3281 mrmanese 538
        } else { 
539
            _expand_buf(0, buflen+20+strlen(iname));
540
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
3255 mrmanese 541
        }
542
    } else if (IS_INTEGER(col)) {
543
        /* identical logic with IS_NUMERIC, except that we don't have to cast
544
         * stuffs from SEXP col. the alternative is doing if idxlen times. */
545
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
546
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
547
        col_index_len = 0;
548
 
549
        for (i = 0; i < idxlen; i++) {
550
            /* no need to correct for 0 base because col 0 is [row name] */
551
            index = INTEGER(col)[i];
552
            if (index > col_cnt) {
553
                sqlite3_finalize(stmt);
554
                Rprintf("Error: undefined columns selected\n");
555
                return R_NilValue;
556
            } else if (index > 0) {
3281 mrmanese 557
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
3255 mrmanese 558
                        sqlite3_column_name(stmt,index));
559
                dup_indices[col_index_len] = 0;
560
                for (j = 0; j < col_index_len; j++) {
561
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
562
                }
563
                col_indices[col_index_len++] = index;
564
            } else if (index < 0) {
565
                sqlite3_finalize(stmt);
566
                Rprintf("Error: negative indices not supported.\n");
567
                return R_NilValue;
568
            }
569
        }
570
 
571
        if (col_index_len == 0) {
572
            sqlite3_finalize(stmt);
573
            Rprintf("Error: no indices detected??\n");
574
            return R_NilValue;
3281 mrmanese 575
        } else { 
576
            _expand_buf(0, buflen+20+strlen(iname));
577
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
3255 mrmanese 578
        }
579
    } else if (IS_LOGICAL(col)) {
580
        /* recycling stuff, so max column output is the # of cols in the df */
581
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
582
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
583
        col_index_len = 0;
584
        for (i = 0; i < col_cnt; i++) {
585
            if (LOGICAL(col)[i%idxlen]) {
3281 mrmanese 586
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
3255 mrmanese 587
                        sqlite3_column_name(stmt,i+1));
588
                dup_indices[col_index_len] = 0;
589
                col_indices[col_index_len++] = i;
590
            }
591
        }
592
 
593
        if (col_index_len == 0) {
594
            sqlite3_finalize(stmt);
595
            Rprintf("Warning: no column selected.\n");
596
            return R_NilValue;
3281 mrmanese 597
        } else { 
598
            _expand_buf(0, buflen+20+strlen(iname));
599
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
3255 mrmanese 600
        }
601
    } else {
602
        sqlite3_finalize(stmt);
603
        Rprintf("Error: don't know how to handle column index.\n");
604
        return R_NilValue;
605
    }
606
 
607
    /* 
3457 mrmanese 608
     * PROCESS ROW INDEX: setup limit or where clause
3255 mrmanese 609
     */
610
    idxlen = LENGTH(row);
3281 mrmanese 611
    if (row == R_NilValue) {
612
        if (col_index_len == 1) {
613
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
614
            sqlite3_finalize(stmt);
615
            return ret;
616
        } if (col_index_len > 1) {
3255 mrmanese 617
            /* create a new SDF, logic similar to sdf_create_sdf */
618
 
619
            /* find a new name. data<n> ? */
3307 mrmanese 620
            char *iname2;
3281 mrmanese 621
            const char *colname;
3307 mrmanese 622
            int namelen, sql_len, sql_len2;
3255 mrmanese 623
 
3419 mrmanese 624
            iname2 = _create_sdf_skeleton1(R_NilValue, &namelen, TRUE);
3255 mrmanese 625
 
626
            /* create sdf_data table */
3281 mrmanese 627
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
628
            sql_len2 = 0;
3255 mrmanese 629
 
630
            for (i = 0; i < col_index_len; i++) {
631
                colname = sqlite3_column_name(stmt, col_indices[i]);
632
                if (dup_indices[i] == 0) {
3281 mrmanese 633
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s] %s", colname,
3255 mrmanese 634
                            sqlite3_column_decltype(stmt, col_indices[i]));
635
                } else {
636
                    /* un-duplicate col names by appending num, just like R */
3281 mrmanese 637
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
3255 mrmanese 638
                            dup_indices[i], 
639
                            sqlite3_column_decltype(stmt, col_indices[i]));
640
                }
641
 
642
                /* deal with possibly factor columns */
643
                if (_is_factor2(iname, "factor", colname)) {
644
                    /* found a factor column */
3281 mrmanese 645
 
3255 mrmanese 646
                    if (dup_indices[i] == 0)  {
647
                        _copy_factor_levels2("factor", iname, colname, iname2, colname);
648
                    } else {
649
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
650
                        _copy_factor_levels2("factor", iname, colname, iname2, 
651
                                g_sql_buf[3]);
652
                    }
653
                }
654
 
655
                /* and deal with ordered factors too... life is hard, then you die */
656
                if (_is_factor2(iname, "ordered", colname)) {
3281 mrmanese 657
 
3255 mrmanese 658
                    if (dup_indices[i] == 0)  {
659
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
660
                                colname);
661
                    } else {
662
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
663
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
664
                                g_sql_buf[3]);
665
                    }
666
                }
667
 
668
            }
669
 
670
            /* don't need it anymore */
671
            sqlite3_finalize(stmt);
672
 
673
            /* no table index created, that's for v2 I hope*/
674
            sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
675
            res = _sqlite_exec(g_sql_buf[1]);
3281 mrmanese 676
            if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
3255 mrmanese 677
 
678
            /* insert data (with row names). buf[0] contains a select */
679
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
680
                    g_sql_buf[0]);
681
            res = _sqlite_exec(g_sql_buf[1]);
682
            if (_sqlite_error(res)) return R_NilValue;
683
 
684
            /* add new sdf to workspace */
685
            sprintf(g_sql_buf[0], "%s.db", iname2);
3281 mrmanese 686
            res = _add_sdf1(g_sql_buf[0], iname2);
3255 mrmanese 687
            if (_sqlite_error(res)) return R_NilValue;
688
 
3419 mrmanese 689
            /* remove protection */
690
            UNUSE_SDF2(iname2);
691
 
3255 mrmanese 692
            /* create SEXP for the SDF */
693
            ret = _create_sdf_sexp(iname2);
3281 mrmanese 694
            return ret;
3255 mrmanese 695
        } else { sqlite3_finalize(stmt); return R_NilValue; }
696
    } else if (row == R_NilValue || idxlen < 1) {
697
        sqlite3_finalize(stmt);
698
        return R_NilValue;
699
    } else if (IS_NUMERIC(row)) {
700
        sqlite3_finalize(stmt);
701
 
3281 mrmanese 702
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
3358 mrmanese 703
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
3281 mrmanese 704
 
3255 mrmanese 705
        /* append " limit ?,1" to the formed select statement */
706
        _expand_buf(0, buflen+10);
3281 mrmanese 707
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
708
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
3255 mrmanese 709
 
3281 mrmanese 710
        index = ((int) REAL(row)[0]) - 1;
3255 mrmanese 711
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
712
 
713
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
714
        if (_sqlite_error(res)) return R_NilValue;
715
 
716
        sqlite3_bind_int(stmt, 1, index);
717
        res = sqlite3_step(stmt);
718
 
719
        /* create data frame */
3281 mrmanese 720
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
721
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
3255 mrmanese 722
 
3281 mrmanese 723
        /* put data in it */
724
        if (index >= 0 && index < row_cnt) {
725
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
726
        }
3255 mrmanese 727
 
3281 mrmanese 728
        for (i = 1; i < idxlen; i++) {
729
            sqlite3_reset(stmt);
730
            index = ((int) REAL(row)[i]) - 1;
731
            if (index >= 0 && index < row_cnt) {
732
                sqlite3_bind_int(stmt, 1, index);
733
                res = sqlite3_step(stmt);
734
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
735
            }
736
        }
737
        sqlite3_finalize(stmt);
738
 
739
        /* shrink vectors */
740
        if (row_index_len < idxlen) {
741
            for (i = 0; i < col_index_len; i++) {
742
                SET_VECTOR_ELT(ret, i, 
743
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
744
            }
745
        }
746
 
747
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
748
    } else if (IS_INTEGER(row)) {
749
        /* same stuff as with IS_INTEGER, except for setting of var index*/
750
        sqlite3_finalize(stmt);
751
 
752
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
3358 mrmanese 753
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
3281 mrmanese 754
 
755
        /* append " limit ?,1" to the formed select statement */
756
        _expand_buf(0, buflen+10);
757
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
758
 
759
        index = INTEGER(row)[0] - 1;
760
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
761
 
762
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
763
        if (_sqlite_error(res)) return R_NilValue;
764
 
765
        sqlite3_bind_int(stmt, 1, index);
766
        res = sqlite3_step(stmt);
767
 
768
        /* create data frame */
769
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
770
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
771
 
3255 mrmanese 772
        /* put data in it */
3281 mrmanese 773
        if (index >= 0 && index < row_cnt) {
774
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
775
        }
776
 
777
        for (i = 1; i < idxlen; i++) {
778
            sqlite3_reset(stmt);
779
            index = INTEGER(row)[i] - 1;
780
            if (index >= 0 && index < row_cnt) {
781
                sqlite3_bind_int(stmt, 1, index);
782
                res = sqlite3_step(stmt); 
783
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
784
            }
785
        }
786
        sqlite3_finalize(stmt);
787
 
788
        /* shrink vectors */
789
        if (row_index_len < idxlen) {
790
            for (i = 0; i < col_index_len; i++) {
791
                SET_VECTOR_ELT(ret, i, 
792
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
793
            }
794
        }
795
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
3255 mrmanese 796
    } else if (IS_LOGICAL(row)) {
797
        /* */
3281 mrmanese 798
        sqlite3_finalize(stmt);
799
        int est_row_cnt = 0;
800
 
801
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
3358 mrmanese 802
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
3281 mrmanese 803
 
804
        /* append " limit ?,1" to the formed select statement */
805
        _expand_buf(0, buflen+10);
806
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
807
 
808
        /* find if there is any TRUE element in the vector */
3282 mrmanese 809
        for (i = 0; i < idxlen && i < row_cnt; i++) {
3281 mrmanese 810
            if (LOGICAL(row)[i]) break;
811
        }
812
 
3282 mrmanese 813
        if (i < idxlen && i < row_cnt) {
3281 mrmanese 814
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
815
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
816
 
817
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
818
            if (_sqlite_error(res)) return R_NilValue;
819
 
820
            sqlite3_bind_int(stmt, 1, i);
821
            sqlite3_step(stmt);
822
            ret = _setup_df_sexp1(stmt, iname, col_index_len, est_row_cnt, dup_indices);
823
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
824
 
825
            for (i++ ; i < row_cnt; i++) {
826
                if (LOGICAL(row)[i%idxlen]) {
827
                    sqlite3_reset(stmt);
828
                    sqlite3_bind_int(stmt, 1, i);
829
                    sqlite3_step(stmt);
830
                    _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
831
                }
832
            }
833
        }
834
 
835
        sqlite3_finalize(stmt);
836
 
837
        /* shrink vectors */
838
        if (est_row_cnt > 0 && row_index_len < est_row_cnt) {
839
            for (i = 0; i < col_index_len; i++) {
840
                SET_VECTOR_ELT(ret, i, 
841
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
842
            }
843
        }
844
 
845
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
3255 mrmanese 846
    }
847
 
3419 mrmanese 848
    UNUSE_SDF2(iname);
849
 
3281 mrmanese 850
    if (ret != R_NilValue && col_index_len > 1) {
3457 mrmanese 851
        SET_CLASS(ret, mkString("data.frame"));
3281 mrmanese 852
        _set_rownames2(ret);
853
    } else if (ret != R_NilValue && col_index_len == 1) {
854
        ret = VECTOR_ELT(ret, 0);
855
    }
3255 mrmanese 856
 
857
    return ret;
858
}
859
 
3324 mrmanese 860
SEXP sdf_rbind(SEXP sdf, SEXP data) {
3457 mrmanese 861
    char *class;
3358 mrmanese 862
    char *iname = SDF_INAME(sdf), *colname, *rowname;
863
    SEXP ret = R_NilValue, col, names, levels, rownames;
3457 mrmanese 864
    int ncols, buflen, buflen2, i, j, res, nrows, *types, rownames_type;
3324 mrmanese 865
    sqlite3_stmt *stmt;
3358 mrmanese 866
    const char *type;
3457 mrmanese 867
    char *rn_str; int rn_int; double rn_dbl;
3281 mrmanese 868
 
3457 mrmanese 869
    class = CHAR_ELT(GET_CLASS(data), 0);
3324 mrmanese 870
    if (strcmp(class,"data.frame") == 0) {
871
        /* check table names, types. variables may not be in same order, as
872
         * long as names and types are identical. */
873
        names = GET_NAMES(data);
3358 mrmanese 874
        ncols = GET_LENGTH(names);
3324 mrmanese 875
 
876
        buflen = sprintf(g_sql_buf[0], "select 1");
3358 mrmanese 877
        for (i = 0; i < ncols; i++) {
3324 mrmanese 878
            _expand_buf(0, buflen + 100);
879
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
880
        }
881
        _expand_buf(0, buflen + 100);
3358 mrmanese 882
        sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
3324 mrmanese 883
 
884
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
885
        if (res != SQLITE_OK) { /* column name mismatch */
3457 mrmanese 886
            _sqlite_error(res);
3324 mrmanese 887
            Rprintf("Error: column names should exactly match the names of the SDF.\n");
888
            return ret;
889
        }
890
 
891
        /* now check the type */
3358 mrmanese 892
        types = (int *)R_alloc(ncols, sizeof(int));
893
        for (i = 0; i< ncols; i++) {
894
            type = sqlite3_column_decltype(stmt, i+1); /* +1 bec 1st col is "1" */
3324 mrmanese 895
 
896
            col = VECTOR_ELT(data, i);
3457 mrmanese 897
            /* class = CHAR_ELT(GET_CLASS(col),0); */
3324 mrmanese 898
 
3358 mrmanese 899
            if (strcmp(type, "double") == 0 && IS_NUMERIC(col)) types[i] = SQLITE_FLOAT;
900
            else if (strcmp(type, "text") == 0 && IS_CHARACTER(col)) types[i] = SQLITE_TEXT;
901
            else if (strcmp(type, "int") == 0) { 
902
                types[i] = SQLITE_INTEGER;
903
                if (isUnordered(col)) {  /* test if factor */
904
                    colname = CHAR_ELT(names, i);
3324 mrmanese 905
                    sqlite3_stmt *stmt2;
906
                    if (!_is_factor2(iname, "factor", colname)) break;
907
                    sprintf(g_sql_buf[1], "[%s].[factor %s]", iname, colname);
3358 mrmanese 908
                    nrows = _get_row_count2(g_sql_buf[1], 0);
3324 mrmanese 909
 
3358 mrmanese 910
                    /* levels in sdf must be >= levels in df */
3324 mrmanese 911
                    levels = GET_LEVELS(col);
912
                    if (nrows < GET_LENGTH(levels)) {
913
                        Rprintf("Error: The data frame variable %s has more levels than"
914
                                " its SDF counterpart.\n", colname);
915
                        break;
916
                    }
917
 
3358 mrmanese 918
                    sprintf(g_sql_buf[2], "select label from %s order by level", g_sql_buf[1]);
919
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
920
                    _sqlite_error(res);
3324 mrmanese 921
 
3358 mrmanese 922
                    for (j = 0; j < GET_LENGTH(levels); j++)  {
3324 mrmanese 923
                        sqlite3_step(stmt2);
3358 mrmanese 924
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) != 0) {
3324 mrmanese 925
                            Rprintf("Error: Level label mismatch in variable %s, [%s] in data frame"
926
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
927
                                    CHAR_ELT(levels, j));
3358 mrmanese 928
                            sqlite3_finalize(stmt2);
3324 mrmanese 929
                            break;
930
                        }
931
                    }
3358 mrmanese 932
                    sqlite3_finalize(stmt2);
933
                } else if (isOrdered(col)) { /* same with ordered */
934
                    colname = CHAR_ELT(names, i);
3324 mrmanese 935
                    sqlite3_stmt *stmt2;
936
                    if (!_is_factor2(iname, "ordered", colname)) break;
937
                    sprintf(g_sql_buf[1], "[%s].[ordered %s]", iname, colname);
3358 mrmanese 938
                    nrows = _get_row_count2(g_sql_buf[1], 0);
3324 mrmanese 939
 
3358 mrmanese 940
                    /* levels in sdf must be >= levels in df */
3324 mrmanese 941
                    levels = GET_LEVELS(col);
942
                    if (nrows < GET_LENGTH(levels)) {
943
                        Rprintf("Error: The data frame variable %s has more levels than"
944
                                " its SDF counterpart.\n", colname);
945
                        break;
946
                    }
947
 
3358 mrmanese 948
                    sprintf(g_sql_buf[2], "select label from %s order by level", g_sql_buf[1]);
3324 mrmanese 949
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
950
 
3358 mrmanese 951
                    for (j = 0; j < GET_LENGTH(levels); j++)  {
3324 mrmanese 952
                        sqlite3_step(stmt2);
3358 mrmanese 953
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) != 0) {
3324 mrmanese 954
                            Rprintf("Error: Level label mismatch in variable %s, [%s] in data frame"
955
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
956
                                    CHAR_ELT(levels, j));
3358 mrmanese 957
                            sqlite3_finalize(stmt2);
3324 mrmanese 958
                            break;
959
                        }
960
                    }
3358 mrmanese 961
                    sqlite3_finalize(stmt2);
3324 mrmanese 962
                }  /* else if class == "ordered" */
963
            } /* if integer */
964
        } /* for loop for column type checking */
965
 
3358 mrmanese 966
        sqlite3_finalize(stmt);
967
        if (i < ncols) {
3324 mrmanese 968
            Rprintf("Error: type mismatch at variable %s\n", CHAR_ELT(names, i));
969
            return ret;
970
        }
971
 
972
        /* create the insert statement */
973
        buflen = sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name]", iname);
974
        buflen2 = sprintf(g_sql_buf[1], "values(?");
3358 mrmanese 975
        for (i = 0; i < ncols; i++) {
3324 mrmanese 976
            _expand_buf(0, buflen+100);
977
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
978
            _expand_buf(1, buflen2+5);
979
            buflen2 += sprintf(g_sql_buf[1]+buflen2, ",?");
980
        }
981
 
982
        _expand_buf(2, buflen + buflen2 + 10);
3358 mrmanese 983
        sprintf(g_sql_buf[2], "%s) %s)", g_sql_buf[0], g_sql_buf[1]);
3324 mrmanese 984
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
985
        if (_sqlite_error(res)) return ret;
986
 
987
        /* finally, add rows */
3457 mrmanese 988
        rownames = getAttrib(data, R_RowNamesSymbol); /* GET_ROWNAMES(data); */
989
        nrows = LENGTH(rownames);
990
        rownames_type = TYPEOF(rownames);
3324 mrmanese 991
 
3457 mrmanese 992
        _sqlite_begin;
3324 mrmanese 993
        for (i = 0; i < nrows; i++) {
3457 mrmanese 994
            sqlite3_reset(stmt);
995
            if (rownames_type == STRSXP) {
996
                rn_str = CHAR_ELT(rownames, i);
997
            } else if (rownames_type == INTSXP) {
998
                sprintf(g_sql_buf[1], "%d", INTEGER(rownames)[i]);
999
                rn_str = g_sql_buf[1];
1000
            } else if (rownames_type == REALSXP) {
1001
                sprintf(g_sql_buf[1], "%f", REAL(rownames)[i]);
1002
                rn_str = g_sql_buf[1];
1003
            } else {
1004
                sprintf(g_sql_buf[1], "%d", i);
1005
                rn_str = g_sql_buf[1];
1006
            }
1007
            sqlite3_bind_text(stmt, 1, rn_str, -1, SQLITE_STATIC);
1008
 
3358 mrmanese 1009
            for (j = 0; j < ncols; j++) {
1010
                col = VECTOR_ELT(data, j);
1011
                switch(types[j]) {
1012
                    case SQLITE_FLOAT:
1013
                        sqlite3_bind_double(stmt, j+2, REAL(col)[i]); break;
1014
                    case SQLITE_TEXT:
1015
                        sqlite3_bind_text(stmt, j+2, CHAR_ELT(col, i), -1, SQLITE_STATIC); break;
1016
                    case SQLITE_INTEGER:
1017
                    default:  /* runtime error if we're wrong */
1018
                        sqlite3_bind_int(stmt, j+2, INTEGER(col)[i]); break;
1019
                }
1020
            }
1021
 
1022
            res = sqlite3_step(stmt);
1023
            if (res != SQLITE_DONE) { /* row name pk conflict */
1024
                for (j = 1; res != SQLITE_DONE; j++) { /* _normally_, j shouldn't overflow */
1025
                    sqlite3_reset(stmt);
3457 mrmanese 1026
                    sprintf(g_sql_buf[2], "%s-%d", rn_str, j);
3358 mrmanese 1027
                    sqlite3_bind_text(stmt, 1, g_sql_buf[2], -1, SQLITE_STATIC);
1028
                    res = sqlite3_step(stmt);
1029
                }
3457 mrmanese 1030
            } 
3324 mrmanese 1031
        }
1032
 
3358 mrmanese 1033
        sqlite3_finalize(stmt);
3457 mrmanese 1034
        _sqlite_commit;
3358 mrmanese 1035
        ret = sdf;
3324 mrmanese 1036
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1037
    }
1038
 
1039
    return ret;
1040
}
1041
 
1042
 
3358 mrmanese 1043
SEXP sdf_get_iname(SEXP sdf) {
1044
    return _getListElement(sdf, "iname");
1045
}