The R Project SVN R-packages

Rev

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

Rev 3324 Rev 3358
Line 55... Line 55...
55
        res = _sqlite_exec(g_sql_buf[2]);
55
        res = _sqlite_exec(g_sql_buf[2]);
56
    }
56
    }
57
    return res;
57
    return res;
58
}
58
}
59
 
59
 
60
char *_create_sdf_skeleton2(SEXP name, int *onamelen) {
60
char *_create_sdf_skeleton1(SEXP name, int *onamelen) {
61
    char *iname, *rname;
61
    char *iname, *rname;
62
    int namelen, file_idx = 0, res;
62
    int namelen, file_idx = 0, res;
63
 
63
 
64
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
64
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
65
 
65
 
Line 78... Line 78...
78
    res = _add_sdf1(g_sql_buf[3], iname);
78
    res = _add_sdf1(g_sql_buf[3], iname);
79
    if (_sqlite_error(res)) return NULL;
79
    if (_sqlite_error(res)) return NULL;
80
 
80
 
81
    /* detach SDF's if necessary to attach this one. if file does not
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 */
82
     * exist, then a sqlite db will be created after we make our 1st table */
83
    if (!USE_SDF(iname, FALSE)) { /* _delete_sdf2(iname); */ return NULL; }
83
    if (!USE_SDF1(iname, FALSE)) { /* _delete_sdf2(iname); */ return NULL; }
84
 
84
 
85
 
85
 
86
    /* create attributes table */
86
    /* create attributes table */
87
    res = _create_sdf_attribute2(iname);
87
    res = _create_sdf_attribute2(iname);
88
    if (_sqlite_error(res)) {
88
    if (_sqlite_error(res)) {
Line 117... Line 117...
117
    sqlite3_finalize(stmt);
117
    sqlite3_finalize(stmt);
118
    return res; /* error on dup name? */
118
    return res; /* error on dup name? */
119
}
119
}
120
 
120
 
121
/* copy a factor|ordered table from a sdf(db) to another sdf(db) */
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,
122
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) {
123
        const char *colname_src, const char *iname_dst, const char *colname_dst) {
124
    sqlite3_stmt *stmt;
124
    sqlite3_stmt *stmt;
125
    int res;
125
    int res;
126
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
126
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
127
    if (res == SQLITE_OK) {
127
    if (res == SQLITE_OK) {
Line 242... Line 242...
242
    SEXP ret;
242
    SEXP ret;
243
    char *iname; 
243
    char *iname; 
244
    int namelen, res, i, j;
244
    int namelen, res, i, j;
245
 
245
 
246
    /* find free name, attach sdf, create sdf_attributes */
246
    /* find free name, attach sdf, create sdf_attributes */
247
    iname = _create_sdf_skeleton2(name, &namelen);
247
    iname = _create_sdf_skeleton1(name, &namelen);
248
    
248
    
249
    if (iname != NULL) {
249
    if (iname != NULL) {
250
        int sql_len, sql_len2;
250
        int sql_len, sql_len2;
251
        sqlite3_stmt *stmt;
251
        sqlite3_stmt *stmt;
252
 
252
 
Line 379... Line 379...
379
}
379
}
380
 
380
 
381
SEXP sdf_get_names(SEXP sdf) {
381
SEXP sdf_get_names(SEXP sdf) {
382
    char *iname;
382
    char *iname;
383
    iname = SDF_INAME(sdf);
383
    iname = SDF_INAME(sdf);
384
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
384
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
385
 
385
 
386
    int len;
386
    int len;
387
    len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
387
    len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
388
 
388
 
389
    sqlite3_stmt *stmt;
389
    sqlite3_stmt *stmt;
Line 407... Line 407...
407
}
407
}
408
 
408
 
409
SEXP sdf_get_length(SEXP sdf) {
409
SEXP sdf_get_length(SEXP sdf) {
410
    char *iname;
410
    char *iname;
411
    iname  = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
411
    iname  = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
412
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
412
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
413
 
413
 
414
    int len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
414
    int len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
415
 
415
 
416
    sqlite3_stmt *stmt;
416
    sqlite3_stmt *stmt;
417
    int res;
417
    int res;
Line 431... Line 431...
431
}
431
}
432
 
432
 
433
/* get row count */
433
/* get row count */
434
SEXP sdf_get_row_count(SEXP sdf) {
434
SEXP sdf_get_row_count(SEXP sdf) {
435
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
435
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
436
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
436
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
437
 
437
 
438
    char **out;
438
    int nrow;
439
    int res, ncol, nrow;
-
 
440
    SEXP ret;
439
    SEXP ret;
441
   
440
   
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;
441
    nrow = _get_row_count2(iname, TRUE);
445
 
-
 
446
 
442
 
447
    if (nrow != 1) ret = R_NilValue;
443
    if (nrow < 1) ret = R_NilValue;
448
    else {
444
    else {
449
        PROTECT(ret = NEW_INTEGER(1));
445
        PROTECT(ret = NEW_INTEGER(1));
450
        INTEGER(ret)[0] = atoi(out[1]);
446
        INTEGER(ret)[0] = nrow;
451
        UNPROTECT(1);
447
        UNPROTECT(1);
452
    }
448
    }
453
 
449
 
454
    sqlite3_free_table(out);
-
 
455
    return ret;
450
    return ret;
456
}
451
}
457
 
452
 
458
    
453
    
459
SEXP sdf_import_table(SEXP _filename, SEXP _name, SEXP _sep, SEXP _quote, 
454
SEXP sdf_import_table(SEXP _filename, SEXP _name, SEXP _sep, SEXP _quote, 
Line 477... Line 472...
477
}
472
}
478
 
473
 
479
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
474
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
480
    SEXP ret = R_NilValue;
475
    SEXP ret = R_NilValue;
481
    char *iname = SDF_INAME(sdf);
476
    char *iname = SDF_INAME(sdf);
482
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
477
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
483
 
478
 
484
    sqlite3_stmt *stmt;
479
    sqlite3_stmt *stmt;
485
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
480
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
486
    int i, j,res;
481
    int i, j,res;
487
    int *col_indices, col_index_len, *dup_indices;
482
    int *col_indices, col_index_len, *dup_indices;
Line 627... Line 622...
627
            /* find a new name. data<n> ? */
622
            /* find a new name. data<n> ? */
628
            char *iname2;
623
            char *iname2;
629
            const char *colname;
624
            const char *colname;
630
            int namelen, sql_len, sql_len2;
625
            int namelen, sql_len, sql_len2;
631
 
626
 
632
            iname2 = _create_sdf_skeleton2(R_NilValue, &namelen);
627
            iname2 = _create_sdf_skeleton1(R_NilValue, &namelen);
633
 
628
 
634
            /* create sdf_data table */
629
            /* create sdf_data table */
635
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
630
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
636
            sql_len2 = 0;
631
            sql_len2 = 0;
637
 
632
 
Line 703... Line 698...
703
        return R_NilValue;
698
        return R_NilValue;
704
    } else if (IS_NUMERIC(row)) {
699
    } else if (IS_NUMERIC(row)) {
705
        sqlite3_finalize(stmt);
700
        sqlite3_finalize(stmt);
706
 
701
 
707
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
702
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
708
        row_cnt = _get_row_count2(g_sql_buf[1]);
703
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
709
 
704
 
710
        /* append " limit ?,1" to the formed select statement */
705
        /* append " limit ?,1" to the formed select statement */
711
        _expand_buf(0, buflen+10);
706
        _expand_buf(0, buflen+10);
712
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
707
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
713
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
708
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
Line 753... Line 748...
753
    } else if (IS_INTEGER(row)) {
748
    } else if (IS_INTEGER(row)) {
754
        /* same stuff as with IS_INTEGER, except for setting of var index*/
749
        /* same stuff as with IS_INTEGER, except for setting of var index*/
755
        sqlite3_finalize(stmt);
750
        sqlite3_finalize(stmt);
756
 
751
 
757
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
752
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
758
        row_cnt = _get_row_count2(g_sql_buf[1]);
753
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
759
 
754
 
760
        /* append " limit ?,1" to the formed select statement */
755
        /* append " limit ?,1" to the formed select statement */
761
        _expand_buf(0, buflen+10);
756
        _expand_buf(0, buflen+10);
762
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
757
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
763
 
758
 
Line 802... Line 797...
802
        /* */
797
        /* */
803
        sqlite3_finalize(stmt);
798
        sqlite3_finalize(stmt);
804
        int est_row_cnt = 0;
799
        int est_row_cnt = 0;
805
 
800
 
806
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
801
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
807
        row_cnt = _get_row_count2(g_sql_buf[1]);
802
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
808
 
803
 
809
        /* append " limit ?,1" to the formed select statement */
804
        /* append " limit ?,1" to the formed select statement */
810
        _expand_buf(0, buflen+10);
805
        _expand_buf(0, buflen+10);
811
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
806
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
812
 
807
 
Line 860... Line 855...
860
 
855
 
861
    return ret;
856
    return ret;
862
}
857
}
863
 
858
 
864
SEXP sdf_rbind(SEXP sdf, SEXP data) {
859
SEXP sdf_rbind(SEXP sdf, SEXP data) {
865
    char *class = CHAR_ELT(data, 0);
860
    char *class = CHAR_ELT(GET_CLASS(data), 0);
866
    char *iname = SDF_INAME(sdf), *colname;
861
    char *iname = SDF_INAME(sdf), *colname, *rowname;
867
    SEXP ret = R_NilValue, col, names, levels;
862
    SEXP ret = R_NilValue, col, names, levels, rownames;
868
    int len, buflen, buflen2, i, j, res, type, nrows;
863
    int ncols, buflen, buflen2, i, j, res, nrows, *types;
869
    sqlite3_stmt *stmt;
864
    sqlite3_stmt *stmt;
-
 
865
    const char *type;
870
 
866
 
871
    if (strcmp(class,"data.frame") == 0) {
867
    if (strcmp(class,"data.frame") == 0) {
872
        /* check table names, types. variables may not be in same order, as
868
        /* check table names, types. variables may not be in same order, as
873
         * long as names and types are identical. */
869
         * long as names and types are identical. */
874
        names = GET_NAMES(data);
870
        names = GET_NAMES(data);
875
        len = GET_LENGTH(names);
871
        ncols = GET_LENGTH(names);
876
 
872
 
877
        buflen = sprintf(g_sql_buf[0], "select 1");
873
        buflen = sprintf(g_sql_buf[0], "select 1");
878
        for (i = 0; i < len; i++) {
874
        for (i = 0; i < ncols; i++) {
879
            _expand_buf(0, buflen + 100);
875
            _expand_buf(0, buflen + 100);
880
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
876
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
881
        }
877
        }
882
        _expand_buf(0, buflen + 100);
878
        _expand_buf(0, buflen + 100);
883
        sprintf(g_sql_buf[0]+buflen, " from %s", iname);
879
        sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
884
 
880
 
885
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
881
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
886
        if (res != SQLITE_OK) { /* column name mismatch */
882
        if (res != SQLITE_OK) { /* column name mismatch */
887
            Rprintf("Error: column names should exactly match the names of the SDF.\n");
883
            Rprintf("Error: column names should exactly match the names of the SDF.\n");
888
            return ret;
884
            return ret;
889
        }
885
        }
890
 
886
 
891
        /* now check the type */
887
        /* now check the type */
892
        sqlite3_step(stmt);
888
        types = (int *)R_alloc(ncols, sizeof(int));
893
        for (i = 0; i< len; i++) {
889
        for (i = 0; i< ncols; i++) {
894
            do { 
-
 
895
                type = sqlite3_column_type(stmt, i+1); 
890
            type = sqlite3_column_decltype(stmt, i+1); /* +1 bec 1st col is "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
 
891
 
905
            col = VECTOR_ELT(data, i);
892
            col = VECTOR_ELT(data, i);
906
            class = CHAR_ELT(GET_CLASS(col),0);
893
            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
 
894
 
-
 
895
            if (strcmp(type, "double") == 0 && IS_NUMERIC(col)) types[i] = SQLITE_FLOAT;
-
 
896
            else if (strcmp(type, "text") == 0 && IS_CHARACTER(col)) types[i] = SQLITE_TEXT;
-
 
897
            else if (strcmp(type, "int") == 0) { 
-
 
898
                types[i] = SQLITE_INTEGER;
915
                /* test if the sdf column is indeed a factor */
899
                if (isUnordered(col)) {  /* test if factor */
916
                if (strcmp(class,"factor") == 0) {
900
                    colname = CHAR_ELT(names, i);
917
                    sqlite3_stmt *stmt2;
901
                    sqlite3_stmt *stmt2;
918
                    if (!_is_factor2(iname, "factor", colname)) break;
902
                    if (!_is_factor2(iname, "factor", colname)) break;
919
                    sprintf(g_sql_buf[1], "[%s].[factor %s]", iname, colname);
903
                    sprintf(g_sql_buf[1], "[%s].[factor %s]", iname, colname);
920
                    nrows = _get_row_count2(g_sql_buf[1]);
904
                    nrows = _get_row_count2(g_sql_buf[1], 0);
921
 
905
 
-
 
906
                    /* levels in sdf must be >= levels in df */
922
                    levels = GET_LEVELS(col);
907
                    levels = GET_LEVELS(col);
923
                    if (nrows < GET_LENGTH(levels)) {
908
                    if (nrows < GET_LENGTH(levels)) {
924
                        Rprintf("Error: The data frame variable %s has more levels than"
909
                        Rprintf("Error: The data frame variable %s has more levels than"
925
                                " its SDF counterpart.\n", colname);
910
                                " its SDF counterpart.\n", colname);
926
                        break;
911
                        break;
927
                    }
912
                    }
928
 
913
 
929
                    sprintf(g_sql_buf[2], "select label from %s ordered by level", g_sql_buf[1]);
914
                    sprintf(g_sql_buf[2], "select label from %s order by level", g_sql_buf[1]);
930
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
915
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
-
 
916
                    _sqlite_error(res);
931
                    
917
                    
932
                    for (j = 0; j < nrows; j++)  {
918
                    for (j = 0; j < GET_LENGTH(levels); j++)  {
933
                        sqlite3_step(stmt2);
919
                        sqlite3_step(stmt2);
934
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) == 0) {
920
                        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"
921
                            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),
922
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
937
                                    CHAR_ELT(levels, j));
923
                                    CHAR_ELT(levels, j));
-
 
924
                            sqlite3_finalize(stmt2);
938
                            break;
925
                            break;
939
                        }
926
                        }
940
                    }
927
                    }
941
 
-
 
942
                    if (j < nrows); break;
928
                    sqlite3_finalize(stmt2);
943
 
-
 
944
                }  else if (strcmp(class,"ordered") == 0) { /* same with ordered */
929
                } else if (isOrdered(col)) { /* same with ordered */
-
 
930
                    colname = CHAR_ELT(names, i);
945
                    sqlite3_stmt *stmt2;
931
                    sqlite3_stmt *stmt2;
946
                    if (!_is_factor2(iname, "ordered", colname)) break;
932
                    if (!_is_factor2(iname, "ordered", colname)) break;
947
                    sprintf(g_sql_buf[1], "[%s].[ordered %s]", iname, colname);
933
                    sprintf(g_sql_buf[1], "[%s].[ordered %s]", iname, colname);
948
                    nrows = _get_row_count2(g_sql_buf[1]);
934
                    nrows = _get_row_count2(g_sql_buf[1], 0);
949
 
935
 
-
 
936
                    /* levels in sdf must be >= levels in df */
950
                    levels = GET_LEVELS(col);
937
                    levels = GET_LEVELS(col);
951
                    if (nrows < GET_LENGTH(levels)) {
938
                    if (nrows < GET_LENGTH(levels)) {
952
                        Rprintf("Error: The data frame variable %s has more levels than"
939
                        Rprintf("Error: The data frame variable %s has more levels than"
953
                                " its SDF counterpart.\n", colname);
940
                                " its SDF counterpart.\n", colname);
954
                        break;
941
                        break;
955
                    }
942
                    }
956
 
943
 
957
                    sprintf(g_sql_buf[2], "select label from %s ordered by level", g_sql_buf[1]);
944
                    sprintf(g_sql_buf[2], "select label from %s order by level", g_sql_buf[1]);
958
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
945
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
959
                    
946
                    
960
                    for (j = 0; j < nrows; j++)  {
947
                    for (j = 0; j < GET_LENGTH(levels); j++)  {
961
                        sqlite3_step(stmt2);
948
                        sqlite3_step(stmt2);
962
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) == 0) {
949
                        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"
950
                            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),
951
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
965
                                    CHAR_ELT(levels, j));
952
                                    CHAR_ELT(levels, j));
-
 
953
                            sqlite3_finalize(stmt2);
966
                            break;
954
                            break;
967
                        }
955
                        }
968
                    }
956
                    }
969
 
-
 
970
                    if (j < nrows); break;
957
                    sqlite3_finalize(stmt2);
971
 
-
 
972
                }  /* else if class == "ordered" */
958
                }  /* else if class == "ordered" */
973
            } /* if integer */
959
            } /* if integer */
974
        } /* for loop for column type checking */
960
        } /* for loop for column type checking */
975
 
961
 
-
 
962
        sqlite3_finalize(stmt);
976
        if (i < len) {
963
        if (i < ncols) {
977
            Rprintf("Error: type mismatch at variable %s\n", CHAR_ELT(names, i));
964
            Rprintf("Error: type mismatch at variable %s\n", CHAR_ELT(names, i));
978
            return ret;
965
            return ret;
979
        }
966
        }
980
 
967
 
981
        /* create the insert statement */
968
        /* create the insert statement */
982
        buflen = sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name]", iname);
969
        buflen = sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name]", iname);
983
        buflen2 = sprintf(g_sql_buf[1], "values(?");
970
        buflen2 = sprintf(g_sql_buf[1], "values(?");
984
        for (i = 0; i < len; i++) {
971
        for (i = 0; i < ncols; i++) {
985
            _expand_buf(0, buflen+100);
972
            _expand_buf(0, buflen+100);
986
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
973
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
987
            _expand_buf(1, buflen2+5);
974
            _expand_buf(1, buflen2+5);
988
            buflen2 += sprintf(g_sql_buf[1]+buflen2, ",?");
975
            buflen2 += sprintf(g_sql_buf[1]+buflen2, ",?");
989
        }
976
        }
990
 
977
 
991
        _expand_buf(2, buflen + buflen2 + 10);
978
        _expand_buf(2, buflen + buflen2 + 10);
992
        sprintf(g_sql_buf[2], "%s) %s", g_sql_buf[0], g_sql_buf[1]);
979
        sprintf(g_sql_buf[2], "%s) %s)", g_sql_buf[0], g_sql_buf[1]);
-
 
980
        Rprintf("%s\n", g_sql_buf[2]);
993
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
981
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
994
        if (_sqlite_error(res)) return ret;
982
        if (_sqlite_error(res)) return ret;
995
 
983
 
996
        /* finally, add rows */
984
        /* finally, add rows */
997
        nrows = LENGTH(GET_ROWNAMES(data));
985
        nrows = LENGTH(GET_ROWNAMES(data));
-
 
986
        rownames = GET_ROWNAMES(data);
998
 
987
 
999
        for (i = 0; i < nrows; i++) {
988
        for (i = 0; i < nrows; i++) {
-
 
989
            sqlite3_bind_text(stmt, 1, CHAR_ELT(rownames, i), -1, SQLITE_STATIC);
-
 
990
            for (j = 0; j < ncols; j++) {
-
 
991
                col = VECTOR_ELT(data, j);
-
 
992
                switch(types[j]) {
-
 
993
                    case SQLITE_FLOAT:
-
 
994
                        sqlite3_bind_double(stmt, j+2, REAL(col)[i]); break;
-
 
995
                    case SQLITE_TEXT:
-
 
996
                        sqlite3_bind_text(stmt, j+2, CHAR_ELT(col, i), -1, SQLITE_STATIC); break;
-
 
997
                    case SQLITE_INTEGER:
-
 
998
                    default:  /* runtime error if we're wrong */
-
 
999
                        sqlite3_bind_int(stmt, j+2, INTEGER(col)[i]); break;
1000
            
1000
                }
1001
        }
1001
            }
1002
 
1002
 
-
 
1003
            res = sqlite3_step(stmt);
-
 
1004
            if (res != SQLITE_DONE) { /* row name pk conflict */
-
 
1005
                rowname = CHAR_ELT(rownames, i);
-
 
1006
                for (j = 1; res != SQLITE_DONE; j++) { /* _normally_, j shouldn't overflow */
-
 
1007
                    sqlite3_reset(stmt);
-
 
1008
                    sprintf(g_sql_buf[2], "%s-%d", rowname, j);
-
 
1009
                    sqlite3_bind_text(stmt, 1, g_sql_buf[2], -1, SQLITE_STATIC);
-
 
1010
                    res = sqlite3_step(stmt);
-
 
1011
                }
-
 
1012
            }
-
 
1013
            sqlite3_reset(stmt);
-
 
1014
        }
1003
 
1015
 
-
 
1016
        sqlite3_finalize(stmt);
-
 
1017
        ret = sdf;
1004
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1018
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1005
    }
1019
    }
1006
 
1020
 
1007
    return ret;
1021
    return ret;
1008
}
1022
}
1009
 
1023
 
1010
 
1024
 
-
 
1025
SEXP sdf_get_iname(SEXP sdf) {
-
 
1026
    return _getListElement(sdf, "iname");
-
 
1027
}
-
 
1028
 
1011
SEXP sopen(SEXP name) {
1029
SEXP sopen(SEXP name) {
1012
    char *filename;
1030
    char *filename;
1013
    
1031
    
1014
    if (IS_CHARACTER(name)) {
1032
    if (IS_CHARACTER(name)) {
1015
        filename = CHAR(STRING_ELT(name,0));
1033
        filename = CHAR(STRING_ELT(name,0));