The R Project SVN R-packages

Rev

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

Rev 3255 Rev 3281
Line 56... Line 56...
56
    return res;
56
    return res;
57
}
57
}
58
 
58
 
59
/* checks if a column has a corresponding factor|ordered table */
59
/* checks if a column has a corresponding factor|ordered table */
60
static int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
60
static int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
61
    sqlite3_stmt stmt;
61
    sqlite3_stmt *stmt;
62
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
62
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
63
            colname);
63
            colname);
64
    int res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
64
    int res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
65
    return res == SQLITE_OK;
65
    return res == SQLITE_OK;
66
}
66
}
Line 81... Line 81...
81
        const char *colname_src, const char *iname_dst, const char *colname_dst) {
81
        const char *colname_src, const char *iname_dst, const char *colname_dst) {
82
    sqlite3_stmt *stmt;
82
    sqlite3_stmt *stmt;
83
    int res;
83
    int res;
84
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
84
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
85
    if (res == SQLITE_OK) {
85
    if (res == SQLITE_OK) {
86
        sprintf(g_sql_buf[2], "insert into [%s].[%s %s] select * from [%s].[%s,%s]",
86
        sprintf(g_sql_buf[2], "insert into [%s].[%s %s] select * from [%s].[%s %s]",
87
                iname_src, factor_type, colname_src, iname_dst, factor_type,
87
                iname_src, factor_type, colname_src, iname_dst, factor_type,
88
                colname_dst);
88
                colname_dst);
89
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
89
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
90
        sqlite3_finalize(stmt);
90
        sqlite3_finalize(stmt);
-
 
91
    }
91
    return res; /* error on dup name? */
92
    return res; /* error on dup name? */
92
}
93
}
93
 
94
 
94
 
95
 
95
/* assumes stmt has col_cnt + 1 columns, col 0 is [row name]. returned SEXP is 
96
/* assumes stmt has col_cnt + 1 columns, col 0 is [row name]. returned SEXP is 
96
 * not UNPROTECT-ed, user will have to do that. will create the names &
97
 * not UNPROTECT-ed, user will have to do that. will create the names &
97
 * attach factor infos */
98
 * attach factor infos */
98
static SEXP _setup_df_sexp1(const char *iname_src, const char *iname_dst,
99
static SEXP _setup_df_sexp1(sqlite3_stmt *stmt, const char *iname, 
99
        sqlite3_stmt *stmt, int col_cnt, int *dup_indices) {
100
        int col_cnt, int row_cnt, int *dup_indices) {
100
    SEXP ret, tmp, value;
101
    SEXP ret, names, value, class = R_NilValue;
101
    int i, nprotect;
102
    int i, type;
102
    const char *colname;
103
    const char *colname, *coltype;
103
 
104
 
104
    PROTECT(ret = NEW_LIST(col_cnt));
105
    PROTECT(ret = NEW_LIST(col_cnt));
105
 
106
 
106
    /* set up names */
107
    /* set up names. */
107
    PROTECT(tmp = NEW_CHARACTER(col_cnt)); nprotect = 1;
108
    PROTECT(names = NEW_CHARACTER(col_cnt));
108
    for (i = 0; i < col_cnt; i++) {
109
    for (i = 0; i < col_cnt; i++) {
-
 
110
        colname = sqlite3_column_name(stmt, i+1);  /* +1 bec [row name is 0] */
109
        colname = sqlite3_column_name(stmt, i+1);
111
        coltype = sqlite3_column_decltype(stmt, i+1);
-
 
112
 
110
        if (dup_indices[i] == 0) {
113
        if (dup_indices[i] == 0) {
111
            strcpy(g_sql_buf[1], colname);
114
            strcpy(g_sql_buf[1], colname);
112
        } else {
115
        } else {
113
            sprintf(g_sql_buf[1], "%s.%d", colname, dup_indices[i]);
116
            sprintf(g_sql_buf[1], "%s.%d", colname, dup_indices[i]);
114
        }
117
        }
115
 
118
 
116
        SET_STRING_ELT(tmp, i, mkChar(g_sql_buf[1]));
119
        SET_STRING_ELT(names, i, mkChar(g_sql_buf[1]));
-
 
120
 
-
 
121
        if (strcmp(coltype, "text") == 0) {
-
 
122
            PROTECT(value = NEW_CHARACTER(row_cnt));
-
 
123
        } else if (strcmp(coltype, "double") == 0) {
-
 
124
            PROTECT(value = NEW_NUMERIC(row_cnt));
-
 
125
        } else if (strcmp(coltype, "bit") == 0) {
-
 
126
            PROTECT(value = NEW_LOGICAL(row_cnt));
-
 
127
        } else if (strcmp(coltype, "integer") == 0 || 
-
 
128
                   strcmp(coltype, "int") == 0) {
-
 
129
            PROTECT(value = NEW_INTEGER(row_cnt));
-
 
130
            type = _get_factor_levels1(iname, colname, value);
-
 
131
            if (type == VAR_FACTOR) {
-
 
132
                PROTECT(class = mkString("factor"));
-
 
133
                SET_CLASS(value, class);
-
 
134
                UNPROTECT(1);
-
 
135
            } else if (type == VAR_ORDERED) {
-
 
136
                PROTECT(class = NEW_CHARACTER(2));
-
 
137
                SET_STRING_ELT(class, 0, mkChar("ordered"));
-
 
138
                SET_STRING_ELT(class, 1, mkChar("factor"));
-
 
139
                SET_CLASS(value, class);
-
 
140
                UNPROTECT(1);
-
 
141
            }
-
 
142
        } else {
-
 
143
            Rprintf("Error: not supported type %s for %s\n", coltype, colname);
-
 
144
            UNPROTECT(2); /* unprotect ret, names */
-
 
145
            return R_NilValue;
-
 
146
        }
-
 
147
 
-
 
148
        SET_VECTOR_ELT(ret, i, value);
-
 
149
        UNPROTECT(1); /* unprotect value */
117
                    
150
                    
118
    }
151
    }
119
    SET_NAMES(ret, tmp);
152
    SET_NAMES(ret, names);
120
    UNPROTECT(nprotect);
153
    UNPROTECT(1); /* unprotect names only */
121
    return ret;
154
    return ret;
122
}
155
}
123
 
156
 
-
 
157
/* expected that 1st col of stmt is [row name], so we start with index 1 */
-
 
158
static int _add_row_to_df(SEXP df, sqlite3_stmt *stmt, int row, int ncols) {
-
 
159
    SEXP vec;
-
 
160
    int type, is_null, i;
-
 
161
    for (i = 1; i <= ncols; i++) {
-
 
162
        vec = VECTOR_ELT(df, i-1);
-
 
163
        type = TYPEOF(vec);
-
 
164
 
-
 
165
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
-
 
166
 
-
 
167
        if (type == CHARSXP) {
-
 
168
            SET_STRING_ELT(vec, row, mkChar(sqlite3_column_text(stmt, i)));
-
 
169
        } else if (type == INTSXP) {
-
 
170
            INTEGER(vec)[row] = sqlite3_column_int(stmt, i);
-
 
171
        } else if (type == REALSXP) {
-
 
172
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
-
 
173
        } else if (type == LGLSXP) {
-
 
174
            LOGICAL(vec)[row] = sqlite3_column_int(stmt, i);
-
 
175
        }
-
 
176
    }
-
 
177
}
-
 
178
 
-
 
179
static void _set_rownames2(SEXP df) {
-
 
180
    SEXP value = VECTOR_ELT(df, 0);
-
 
181
    int len = LENGTH(value), i;
-
 
182
    PROTECT(value = NEW_CHARACTER(len));
-
 
183
    for (i = 0; i < len; i++) {
-
 
184
        sprintf(g_sql_buf[2], "%d", i+1);
-
 
185
        SET_STRING_ELT(value, i, mkChar(g_sql_buf[2]));
-
 
186
    }
-
 
187
 
-
 
188
    SET_ROWNAMES(df, value);
-
 
189
    UNPROTECT(1);
-
 
190
}
-
 
191
 
-
 
192
 
124
/****************************************************************************
193
/****************************************************************************
125
 * SDF FUNCTIONS
194
 * SDF FUNCTIONS
126
 ****************************************************************************/
195
 ****************************************************************************/
127
 
196
 
128
SEXP sdf_create_sdf(SEXP df, SEXP name) {
197
SEXP sdf_create_sdf(SEXP df, SEXP name) {
129
    SEXP ret;
198
    SEXP ret;
130
    char *rname, *iname, *biname; 
199
    char *rname, *iname; 
131
    int file_idx = 0, namelen, res, i, j;
200
    int file_idx = 0, namelen, res, i, j;
132
 
201
 
133
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
202
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
134
    if (!namelen) return R_NilValue;
203
    if (!namelen) return R_NilValue;
135
 
204
 
Line 376... Line 445...
376
    /* register to workspace */
445
    /* register to workspace */
377
 
446
 
378
    return R_NilValue;
447
    return R_NilValue;
379
}
448
}
380
 
449
 
381
SEXP sdf_get_rownames(SEXP sdf) {
-
 
382
    return R_NilValue;
-
 
383
}
-
 
384
 
-
 
385
 
-
 
386
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
450
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
387
    SEXP ret, *df;
451
    SEXP ret, *df;
388
    char *iname = SDF_INAME(sdf);
452
    char *iname = SDF_INAME(sdf);
389
    sqlite3_stmt *stmt, *stmt2;
453
    sqlite3_stmt *stmt, *stmt2;
390
    int buflen = 0, idxlen, col_cnt, index;
454
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
391
    int i, j,res;
455
    int i, j,res;
392
    int *col_indices, col_index_len, *dup_indices;
456
    int *col_indices, col_index_len, *dup_indices;
-
 
457
    int row_index_len;
393
 
458
 
394
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data", iname);
459
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
395
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
460
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
396
    if (_sqlite_error(res)) return R_NilValue;
461
    if (_sqlite_error(res)) return R_NilValue;
397
 
462
 
398
    buflen = sprintf(g_sql_buf[0], "select [row name]");
463
    buflen = sprintf(g_sql_buf[0], "select [row name]");
399
    idxlen = LENGTH(col);
464
    idxlen = LENGTH(col);
400
    col_cnt = sqlite3_column_count(stmt) - 1;
465
    col_cnt = sqlite3_column_count(stmt) - 1;
401
 
466
 
402
    /*
467
    /*
403
     * PROCESS COLUMN INDEX
468
     * PROCESS COLUMN INDEX
404
     */
469
     */
405
    if (MISSING(col)) {
470
    if (col == R_NilValue) {
406
        for (i = 1; i < col_cnt+1; i++) {
471
        for (i = 1; i < col_cnt+1; i++) {
407
            buflen += sprintf(g_sql_buf[0]+buflen, "[%s],", 
472
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
408
                    sqlite3_column_name(stmt, i)); 
473
                    sqlite3_column_name(stmt, i)); 
409
        }
474
        }
410
        g_sql_buf[0][--buflen] = 0; /* remove trailing "," */
475
        _expand_buf(0, buflen+20+strlen(iname));
-
 
476
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
-
 
477
        col_index_len = col_cnt;
-
 
478
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
-
 
479
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
-
 
480
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
411
    } else if (col == R_NilValue || idxlen < 1) {
481
    } else if (col == R_NilValue || idxlen < 1) {
412
        sqlite3_finalize(stmt);
482
        sqlite3_finalize(stmt);
413
        return R_NilValue;
483
        return R_NilValue;
414
    } else if (IS_NUMERIC(col)) {
484
    } else if (IS_NUMERIC(col)) {
415
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
485
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
Line 422... Line 492...
422
            if (index > col_cnt) {
492
            if (index > col_cnt) {
423
                sqlite3_finalize(stmt);
493
                sqlite3_finalize(stmt);
424
                Rprintf("Error: undefined columns selected\n");
494
                Rprintf("Error: undefined columns selected\n");
425
                return R_NilValue;
495
                return R_NilValue;
426
            } else if (index > 0) {
496
            } else if (index > 0) {
427
                buflen += sprintf(g_sql_buf[0]+buflen, "[%s],", 
497
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
428
                        sqlite3_column_name(stmt,index));
498
                        sqlite3_column_name(stmt,index));
429
                dup_indices[col_index_len] = 0;
499
                dup_indices[col_index_len] = 0;
430
                for (j = 0; j < col_index_len; j++) {
500
                for (j = 0; j < col_index_len; j++) {
431
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
501
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
432
                }
502
                }
Line 440... Line 510...
440
 
510
 
441
        if (col_index_len == 0) {
511
        if (col_index_len == 0) {
442
            sqlite3_finalize(stmt);
512
            sqlite3_finalize(stmt);
443
            Rprintf("Error: no indices detected??\n");
513
            Rprintf("Error: no indices detected??\n");
444
            return R_NilValue;
514
            return R_NilValue;
445
        } else {
515
        } else { 
446
            /* remove trailing "," and add the from clause */
516
            _expand_buf(0, buflen+20+strlen(iname));
447
            sprintf(g_sql_buf[0] + (--buflen), " from [%s].sdf_data", iname);
517
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
448
        }
518
        }
449
    } else if (IS_INTEGER(col)) {
519
    } else if (IS_INTEGER(col)) {
450
        /* identical logic with IS_NUMERIC, except that we don't have to cast
520
        /* identical logic with IS_NUMERIC, except that we don't have to cast
451
         * stuffs from SEXP col. the alternative is doing if idxlen times. */
521
         * stuffs from SEXP col. the alternative is doing if idxlen times. */
452
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
522
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
Line 459... Line 529...
459
            if (index > col_cnt) {
529
            if (index > col_cnt) {
460
                sqlite3_finalize(stmt);
530
                sqlite3_finalize(stmt);
461
                Rprintf("Error: undefined columns selected\n");
531
                Rprintf("Error: undefined columns selected\n");
462
                return R_NilValue;
532
                return R_NilValue;
463
            } else if (index > 0) {
533
            } else if (index > 0) {
464
                buflen += sprintf(g_sql_buf[0]+buflen, "[%s],", 
534
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
465
                        sqlite3_column_name(stmt,index));
535
                        sqlite3_column_name(stmt,index));
466
                dup_indices[col_index_len] = 0;
536
                dup_indices[col_index_len] = 0;
467
                for (j = 0; j < col_index_len; j++) {
537
                for (j = 0; j < col_index_len; j++) {
468
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
538
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
469
                }
539
                }
Line 477... Line 547...
477
 
547
 
478
        if (col_index_len == 0) {
548
        if (col_index_len == 0) {
479
            sqlite3_finalize(stmt);
549
            sqlite3_finalize(stmt);
480
            Rprintf("Error: no indices detected??\n");
550
            Rprintf("Error: no indices detected??\n");
481
            return R_NilValue;
551
            return R_NilValue;
482
        } else {
552
        } else { 
483
            /* remove trailing "," and add the from clause */
553
            _expand_buf(0, buflen+20+strlen(iname));
484
            sprintf(g_sql_buf[0] + (--buflen), " from [%s].sdf_data", iname);
554
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
485
        }
555
        }
486
    } else if (IS_LOGICAL(col)) {
556
    } else if (IS_LOGICAL(col)) {
487
        /* recycling stuff, so max column output is the # of cols in the df */
557
        /* recycling stuff, so max column output is the # of cols in the df */
488
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
558
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
489
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
559
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
490
        col_index_len = 0;
560
        col_index_len = 0;
491
        for (i = 0; i < col_cnt; i++) {
561
        for (i = 0; i < col_cnt; i++) {
492
            if (LOGICAL(col)[i%idxlen]) {
562
            if (LOGICAL(col)[i%idxlen]) {
493
                buflen += sprintf(g_sql_buf[0]+buflen, "[%s],", 
563
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
494
                        sqlite3_column_name(stmt,i+1));
564
                        sqlite3_column_name(stmt,i+1));
495
                dup_indices[col_index_len] = 0;
565
                dup_indices[col_index_len] = 0;
496
                col_indices[col_index_len++] = i;
566
                col_indices[col_index_len++] = i;
497
            }
567
            }
498
        }
568
        }
499
 
569
 
500
        if (col_index_len == 0) {
570
        if (col_index_len == 0) {
501
            sqlite3_finalize(stmt);
571
            sqlite3_finalize(stmt);
502
            Rprintf("Warning: no column selected.\n");
572
            Rprintf("Warning: no column selected.\n");
503
            return R_NilValue;
573
            return R_NilValue;
-
 
574
        } else { 
-
 
575
            _expand_buf(0, buflen+20+strlen(iname));
-
 
576
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
504
        }
577
        }
505
    } else {
578
    } else {
506
        sqlite3_finalize(stmt);
579
        sqlite3_finalize(stmt);
507
        Rprintf("Error: don't know how to handle column index.\n");
580
        Rprintf("Error: don't know how to handle column index.\n");
508
        return R_NilValue;
581
        return R_NilValue;
Line 510... Line 583...
510
 
583
 
511
    /* 
584
    /* 
512
     * PROCESS ROW INDEX
585
     * PROCESS ROW INDEX
513
     */
586
     */
514
    idxlen = LENGTH(row);
587
    idxlen = LENGTH(row);
515
    if (MISSING(row)) {
588
    if (row == R_NilValue) {
-
 
589
        if (col_index_len == 1) {
-
 
590
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
-
 
591
            sqlite3_finalize(stmt);
-
 
592
            return ret;
516
        if (col_index_len > 0) {
593
        } if (col_index_len > 1) {
517
            /* create a new SDF, logic similar to sdf_create_sdf */
594
            /* create a new SDF, logic similar to sdf_create_sdf */
518
 
595
 
-
 
596
            /* we have to close the current statement so we can do
-
 
597
             * edits in the db */
-
 
598
            /* sqlite3_finalize(stmt); */
-
 
599
 
519
            /* find a new name. data<n> ? */
600
            /* find a new name. data<n> ? */
520
            char *iname2, *rname2;
601
            char *iname2, *rname2;
-
 
602
            const char *colname;
521
            int namelen, file_idx, sql_len, sql_len2, dup_idx;
603
            int namelen, file_idx = 0, sql_len, sql_len2, dup_idx;
522
            namelen = _check_sdf_name(R_NilValue, &rname2, &iname2, &file_idx);
604
            namelen = _check_sdf_name(R_NilValue, &rname2, &iname2, &file_idx);
523
 
605
 
524
            if (!namelen) { sqlite3_finalize(stmt); return R_NilValue; }
606
            if (!namelen) return R_NilValue; 
525
 
607
 
526
            _find_free_filename(rname2, &iname2, &namelen, &file_idx);
608
            _find_free_filename(rname2, &iname2, &namelen, &file_idx);
527
 
609
 
528
            if (file_idx >= 10000) { 
610
            if (file_idx >= 10000) { 
529
                Rprintf("Error: cannot find free SDF name.\n");
611
                Rprintf("Error: cannot find free SDF name.\n");
530
                sqlite3_finalize(stmt);
-
 
531
                return R_NilValue;
612
                return R_NilValue;
532
            }
613
            }
533
 
614
 
-
 
615
 
534
            /* create new db by attaching a non-existent file */
616
            /* create new db by attaching a non-existent file */
535
            iname[namelen] = 0; /* remove ".db" */
617
            iname2[namelen] = 0; /* remove ".db" */
536
            sprintf(g_sql_buf[1], "attach '%s.db' as %s", iname, iname);
618
            sprintf(g_sql_buf[1], "attach '%s.db' as %s", iname2, iname2);
537
            res = _sqlite_exec(g_sql_buf[1]);
619
            res = _sqlite_exec(g_sql_buf[1]);
538
            if (_sqlite_error(res)) return R_NilValue;
620
            if (_sqlite_error(res)) return R_NilValue; 
539
 
621
 
540
            /* create attributes table */
622
            /* create attributes table */
541
            res = _create_sdf_attribute2(iname);
623
            res = _create_sdf_attribute2(iname2);
542
            if (_sqlite_error(res)) { 
-
 
543
                sqlite3_finalize(stmt); return R_NilValue; 
624
            if (_sqlite_error(res)) return R_NilValue; 
544
            }
-
 
545
 
625
 
546
            /* create sdf_data table */
626
            /* create sdf_data table */
547
            sql_len = sprintf(g_sql_buf[1], "create table [%s],sdf_data ([row name] text", iname);
627
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
-
 
628
            sql_len2 = 0;
548
 
629
 
549
            for (i = 0; i < col_index_len; i++) {
630
            for (i = 0; i < col_index_len; i++) {
550
                colname = sqlite3_column_name(stmt, col_indices[i]);
631
                colname = sqlite3_column_name(stmt, col_indices[i]);
551
                if (dup_indices[i] == 0) {
632
                if (dup_indices[i] == 0) {
552
                    sql_len2 = sprintf(g_sql_buf[2], ", [%s] %s", colname,
633
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s] %s", colname,
553
                            sqlite3_column_decltype(stmt, col_indices[i]));
634
                            sqlite3_column_decltype(stmt, col_indices[i]));
554
                } else {
635
                } else {
555
                    /* un-duplicate col names by appending num, just like R */
636
                    /* un-duplicate col names by appending num, just like R */
556
                    sql_len2 = sprintf(g_sql_buf[2], ", [%s.%d] %s", colname,
637
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
557
                            dup_indices[i], 
638
                            dup_indices[i], 
558
                            sqlite3_column_decltype(stmt, col_indices[i]));
639
                            sqlite3_column_decltype(stmt, col_indices[i]));
559
                }
640
                }
560
 
641
 
561
                /* append to g_sql_buf[1], which holds the create 
-
 
562
                 * table script */
-
 
563
                sql_len += sql_len2;
-
 
564
                _expand_buf(1, sql_len);
-
 
565
                strcpy(g_sql_buf[1], g_sql_buf[2]);
-
 
566
 
-
 
567
                /* deal with possibly factor columns */
642
                /* deal with possibly factor columns */
568
                if (_is_factor2(iname, "factor", colname)) {
643
                if (_is_factor2(iname, "factor", colname)) {
569
                    /* found a factor column */
644
                    /* found a factor column */
-
 
645
 
570
                    if (dup_indices[i] == 0)  {
646
                    if (dup_indices[i] == 0)  {
571
                        _copy_factor_levels2("factor", iname, colname, iname2, colname);
647
                        _copy_factor_levels2("factor", iname, colname, iname2, colname);
572
                    } else {
648
                    } else {
573
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
649
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
574
                        _copy_factor_levels2("factor", iname, colname, iname2, 
650
                        _copy_factor_levels2("factor", iname, colname, iname2, 
Line 576... Line 652...
576
                    }
652
                    }
577
                }
653
                }
578
 
654
 
579
                /* and deal with ordered factors too... life is hard, then you die */
655
                /* and deal with ordered factors too... life is hard, then you die */
580
                if (_is_factor2(iname, "ordered", colname)) {
656
                if (_is_factor2(iname, "ordered", colname)) {
-
 
657
                    
581
                    if (dup_indices[i] == 0)  {
658
                    if (dup_indices[i] == 0)  {
582
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
659
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
583
                                colname);
660
                                colname);
584
                    } else {
661
                    } else {
585
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
662
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
Line 594... Line 671...
594
            sqlite3_finalize(stmt);
671
            sqlite3_finalize(stmt);
595
 
672
 
596
            /* no table index created, that's for v2 I hope*/
673
            /* no table index created, that's for v2 I hope*/
597
            sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
674
            sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
598
            res = _sqlite_exec(g_sql_buf[1]);
675
            res = _sqlite_exec(g_sql_buf[1]);
599
            if (_sqlite_error(res)) return R_NilValue; 
676
            if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
600
 
677
 
601
            /* insert data (with row names). buf[0] contains a select */
678
            /* insert data (with row names). buf[0] contains a select */
602
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
679
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
603
                    g_sql_buf[0]);
680
                    g_sql_buf[0]);
604
            res = _sqlite_exec(g_sql_buf[1]);
681
            res = _sqlite_exec(g_sql_buf[1]);
605
            if (_sqlite_error(res)) return R_NilValue;
682
            if (_sqlite_error(res)) return R_NilValue;
606
 
683
 
607
            /* add new sdf to workspace */
684
            /* add new sdf to workspace */
608
            sprintf(g_sql_buf[0], "%s.db", iname2);
685
            sprintf(g_sql_buf[0], "%s.db", iname2);
609
            res = _add_sdf1(iname, g_sql_buf[0]);
686
            res = _add_sdf1(g_sql_buf[0], iname2);
610
            if (_sqlite_error(res)) return R_NilValue;
687
            if (_sqlite_error(res)) return R_NilValue;
611
 
688
 
612
            /* create SEXP for the SDF */
689
            /* create SEXP for the SDF */
613
            ret = _create_sdf_sexp(iname2);
690
            ret = _create_sdf_sexp(iname2);
-
 
691
            return ret;
614
        } else { sqlite3_finalize(stmt); return R_NilValue; }
692
        } else { sqlite3_finalize(stmt); return R_NilValue; }
615
    } else if (row == R_NilValue || idxlen < 1) {
693
    } else if (row == R_NilValue || idxlen < 1) {
616
        sqlite3_finalize(stmt);
694
        sqlite3_finalize(stmt);
617
        return R_NilValue;
695
        return R_NilValue;
618
    } else if (IS_NUMERIC(row)) {
696
    } else if (IS_NUMERIC(row)) {
619
        sqlite3_finalize(stmt);
697
        sqlite3_finalize(stmt);
620
 
698
 
-
 
699
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
-
 
700
        row_cnt = _get_row_count2(g_sql_buf[1]);
-
 
701
 
621
        /* append " limit ?,1" to the formed select statement */
702
        /* append " limit ?,1" to the formed select statement */
622
        _expand_buf(0, buflen+10);
703
        _expand_buf(0, buflen+10);
623
        buflen += sprintf(g_sql_buf[0], " limit ?,1");
704
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
-
 
705
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
624
 
706
 
625
        /* indexing will be base-1, because [row name] is at 0 */
-
 
626
        index = ((int) REAL(row)[0]);
707
        index = ((int) REAL(row)[0]) - 1;
627
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
708
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
628
 
709
 
629
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
710
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
630
        if (_sqlite_error(res)) return R_NilValue;
711
        if (_sqlite_error(res)) return R_NilValue;
631
 
712
 
632
        sqlite3_bind_int(stmt, 1, index);
713
        sqlite3_bind_int(stmt, 1, index);
633
        res = sqlite3_step(stmt);
714
        res = sqlite3_step(stmt);
634
 
715
 
635
        /* create data frame */
716
        /* create data frame */
636
        ret = _setup_df_sexp2(stmt, col_index_len, idxlen, dup_indices);
717
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
637
 
-
 
638
        if (index > 0) {
718
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
639
 
719
 
640
        /* put data in it */
720
        /* put data in it */
-
 
721
        row_index_len = 0;
-
 
722
        if (index >= 0 && index < row_cnt) {
-
 
723
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
-
 
724
        }
-
 
725
 
-
 
726
        for (i = 1; i < idxlen; i++) {
-
 
727
            sqlite3_reset(stmt);
-
 
728
            index = ((int) REAL(row)[i]) - 1;
-
 
729
            if (index >= 0 && index < row_cnt) {
-
 
730
                sqlite3_bind_int(stmt, 1, index);
-
 
731
                res = sqlite3_step(stmt);
-
 
732
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
-
 
733
            }
-
 
734
        }
-
 
735
        sqlite3_finalize(stmt);
-
 
736
 
-
 
737
        /* shrink vectors */
-
 
738
        if (row_index_len < idxlen) {
-
 
739
            for (i = 0; i < col_index_len; i++) {
-
 
740
                SET_VECTOR_ELT(ret, i, 
-
 
741
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
-
 
742
            }
-
 
743
        }
-
 
744
 
-
 
745
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
641
    } else if (IS_INTEGER(row)) {
746
    } else if (IS_INTEGER(row)) {
-
 
747
        /* same stuff as with IS_INTEGER, except for setting of var index*/
-
 
748
        sqlite3_finalize(stmt);
-
 
749
 
-
 
750
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
-
 
751
        row_cnt = _get_row_count2(g_sql_buf[1]);
-
 
752
 
-
 
753
        /* append " limit ?,1" to the formed select statement */
-
 
754
        _expand_buf(0, buflen+10);
-
 
755
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
-
 
756
 
-
 
757
        index = INTEGER(row)[0] - 1;
-
 
758
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
-
 
759
 
-
 
760
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
-
 
761
        if (_sqlite_error(res)) return R_NilValue;
-
 
762
 
-
 
763
        sqlite3_bind_int(stmt, 1, index);
-
 
764
        res = sqlite3_step(stmt);
-
 
765
 
-
 
766
        /* create data frame */
-
 
767
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
-
 
768
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
-
 
769
 
-
 
770
        /* put data in it */
-
 
771
        row_index_len = 0;
-
 
772
        if (index >= 0 && index < row_cnt) {
-
 
773
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
-
 
774
        }
-
 
775
 
-
 
776
        for (i = 1; i < idxlen; i++) {
-
 
777
            sqlite3_reset(stmt);
-
 
778
            index = INTEGER(row)[i] - 1;
-
 
779
            if (index >= 0 && index < row_cnt) {
-
 
780
                sqlite3_bind_int(stmt, 1, index);
-
 
781
                res = sqlite3_step(stmt); 
-
 
782
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
-
 
783
            }
-
 
784
        }
-
 
785
        sqlite3_finalize(stmt);
-
 
786
 
-
 
787
        /* shrink vectors */
-
 
788
        if (row_index_len < idxlen) {
-
 
789
            for (i = 0; i < col_index_len; i++) {
-
 
790
                SET_VECTOR_ELT(ret, i, 
-
 
791
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
642
        /* */
792
            }
-
 
793
        }
-
 
794
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
643
    } else if (IS_LOGICAL(row)) {
795
    } else if (IS_LOGICAL(row)) {
644
        /* */
796
        /* */
-
 
797
        sqlite3_finalize(stmt);
-
 
798
        int est_row_cnt = 0;
-
 
799
 
-
 
800
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
-
 
801
        row_cnt = _get_row_count2(g_sql_buf[1]);
-
 
802
 
-
 
803
        /* append " limit ?,1" to the formed select statement */
-
 
804
        _expand_buf(0, buflen+10);
-
 
805
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
-
 
806
 
-
 
807
        /* find if there is any TRUE element in the vector */
-
 
808
        for (i = 0; i < idxlen & i < row_cnt; i++) {
-
 
809
            if (LOGICAL(row)[i]) break;
-
 
810
        }
-
 
811
 
-
 
812
        if (i < idxlen & i < row_cnt) {
-
 
813
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
-
 
814
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
-
 
815
 
-
 
816
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
-
 
817
            if (_sqlite_error(res)) return R_NilValue;
-
 
818
 
-
 
819
            sqlite3_bind_int(stmt, 1, i);
-
 
820
            sqlite3_step(stmt);
-
 
821
            ret = _setup_df_sexp1(stmt, iname, col_index_len, est_row_cnt, dup_indices);
-
 
822
            row_index_len = 0;
-
 
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 */
645
    }
846
    }
646
 
847
 
-
 
848
    if (ret != R_NilValue && col_index_len > 1) {
-
 
849
        SEXP class = mkString("data.frame");
-
 
850
        SET_CLASS(ret, class);
-
 
851
        _set_rownames2(ret);
-
 
852
    } else if (ret != R_NilValue && col_index_len == 1) {
-
 
853
        ret = VECTOR_ELT(ret, 0);
647
        
854
    }
648
 
855
 
649
    return ret;
856
    return ret;
650
}
857
}
651
 
858
 
-
 
859
 
652
SEXP sopen(SEXP name) {
860
SEXP sopen(SEXP name) {
653
    char *filename;
861
    char *filename;
654
    
862
    
655
    if (IS_CHARACTER(name)) {
863
    if (IS_CHARACTER(name)) {
656
        filename = CHAR(STRING_ELT(name,0));
864
        filename = CHAR(STRING_ELT(name,0));