The R Project SVN R-packages

Rev

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

Rev 3457 Rev 3471
Line 32... Line 32...
32
        Rprintf("Error: the supplied value for arg name is not a string.\n");
32
        Rprintf("Error: the supplied value for arg name is not a string.\n");
33
    }
33
    }
34
    return namelen;
34
    return namelen;
35
}
35
}
36
 
36
 
37
static int _find_free_filename(char *rname, char **iname, int *namelen, int *file_idx) {
37
static int _find_free_filename2(char *rname, char **iname, int *namelen, int *file_idx) {
-
 
38
    sqlite3_stmt *stmt;
-
 
39
    sqlite3_prepare(g_workspace, "select 1 from workspace where internal_name=?", -1,
-
 
40
            &stmt, NULL);
38
    do {
41
    do {
39
        if (!_file_exists(*iname)) break;
42
        if (!_file_exists(*iname)) {
-
 
43
            sqlite3_reset(stmt);
-
 
44
            sqlite3_bind_text(stmt, 1, *iname, -1, SQLITE_STATIC);
-
 
45
            if (sqlite3_step(stmt) != SQLITE_ROW) break;
-
 
46
        }
40
        *namelen = sprintf(*iname, "%s%d.db", rname, ++(*file_idx)) - 3;
47
        *namelen = sprintf(*iname, "%s%d.db", rname, ++(*file_idx)) - 3;
41
    } while (*file_idx < 10000);
48
    } while (*file_idx < 10000);
42
 
49
 
-
 
50
    sqlite3_finalize(stmt);
43
    return *file_idx;
51
    return *file_idx;
44
}
52
}
45
 
53
 
46
/* creates an sdf attribute table */
54
/* creates an sdf attribute table */
47
static int _create_sdf_attribute2(const char *iname) {
55
static int _create_sdf_attribute2(const char *iname) {
Line 63... Line 71...
63
 
71
 
64
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
72
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
65
 
73
 
66
    if (!namelen) return NULL;
74
    if (!namelen) return NULL;
67
 
75
 
68
    _find_free_filename(rname, &iname, &namelen, &file_idx);
76
    _find_free_filename2(rname, &iname, &namelen, &file_idx);
69
 
77
 
70
    if (file_idx >= 10000) { 
78
    if (file_idx >= 10000) { 
71
        Rprintf("Error: cannot find free SDF name.\n");
79
        Rprintf("Error: cannot find free SDF name.\n");
72
        return NULL;
80
        return NULL;
73
    }
81
    }
Line 466... Line 474...
466
    /* register to workspace */
474
    /* register to workspace */
467
 
475
 
468
    return R_NilValue;
476
    return R_NilValue;
469
}
477
}
470
 
478
 
471
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
479
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col, SEXP new_sdf) {
472
    SEXP ret = R_NilValue;
480
    SEXP ret = R_NilValue;
473
    char *iname = SDF_INAME(sdf);
481
    char *iname = SDF_INAME(sdf);
474
    if (!USE_SDF1(iname, TRUE, TRUE)) return R_NilValue;
-
 
475
 
-
 
476
    sqlite3_stmt *stmt;
482
    sqlite3_stmt *stmt;
477
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
483
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
478
    int i, j,res;
484
    int i, j,res;
479
    int *col_indices, col_index_len, *dup_indices;
485
    int *col_indices, col_index_len, *dup_indices;
480
    int row_index_len = 0;
486
    int row_index_len = 0, force_new_df = LOGICAL(new_sdf)[0];
-
 
487
 
-
 
488
    if (!USE_SDF1(iname, TRUE, TRUE)) return R_NilValue;
481
 
489
 
482
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
490
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
483
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
491
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
484
    if (_sqlite_error(res)) return R_NilValue;
492
    if (_sqlite_error(res)) return R_NilValue;
485
 
493
 
Line 607... Line 615...
607
    /* 
615
    /* 
608
     * PROCESS ROW INDEX: setup limit or where clause
616
     * PROCESS ROW INDEX: setup limit or where clause
609
     */
617
     */
610
    idxlen = LENGTH(row);
618
    idxlen = LENGTH(row);
611
    if (row == R_NilValue) {
619
    if (row == R_NilValue) {
612
        if (col_index_len == 1) {
620
        if (col_index_len == 1 && !force_new_df) {
613
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
621
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
614
            sqlite3_finalize(stmt);
622
            sqlite3_finalize(stmt);
615
            return ret;
623
            return ret;
616
        } if (col_index_len > 1) {
624
        } if (col_index_len > 1 || (force_new_df && col_index_len == 1)) {
617
            /* create a new SDF, logic similar to sdf_create_sdf */
625
            /* create a new SDF, logic similar to sdf_create_sdf */
618
 
626
 
619
            /* find a new name. data<n> ? */
627
            /* find a new name. data<n> ? */
620
            char *iname2;
628
            char *iname2;
621
            const char *colname;
629
            const char *colname;
Line 679... Line 687...
679
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
687
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
680
                    g_sql_buf[0]);
688
                    g_sql_buf[0]);
681
            res = _sqlite_exec(g_sql_buf[1]);
689
            res = _sqlite_exec(g_sql_buf[1]);
682
            if (_sqlite_error(res)) return R_NilValue;
690
            if (_sqlite_error(res)) return R_NilValue;
683
 
691
 
684
            /* add new sdf to workspace */
-
 
685
            sprintf(g_sql_buf[0], "%s.db", iname2);
-
 
686
            res = _add_sdf1(g_sql_buf[0], iname2);
-
 
687
            if (_sqlite_error(res)) return R_NilValue;
-
 
688
 
-
 
689
            /* remove protection */
692
            /* remove protection */
690
            UNUSE_SDF2(iname2);
693
            UNUSE_SDF2(iname2);
691
 
694
 
692
            /* create SEXP for the SDF */
695
            /* create SEXP for the SDF */
693
            ret = _create_sdf_sexp(iname2);
696
            ret = _create_sdf_sexp(iname2);
Line 857... Line 860...
857
    return ret;
860
    return ret;
858
}
861
}
859
 
862
 
860
SEXP sdf_rbind(SEXP sdf, SEXP data) {
863
SEXP sdf_rbind(SEXP sdf, SEXP data) {
861
    char *class;
864
    char *class;
862
    char *iname = SDF_INAME(sdf), *colname, *rowname;
865
    char *iname = SDF_INAME(sdf), *colname;
863
    SEXP ret = R_NilValue, col, names, levels, rownames;
866
    SEXP ret = R_NilValue, col, names, levels, rownames;
864
    int ncols, buflen, buflen2, i, j, res, nrows, *types, rownames_type;
867
    int ncols, buflen, buflen2, i, j, res, nrows, *types, rownames_type;
865
    sqlite3_stmt *stmt;
868
    sqlite3_stmt *stmt;
866
    const char *type;
869
    const char *type;
867
    char *rn_str; int rn_int; double rn_dbl;
870
    char *rn_str; 
868
 
871
 
869
    class = CHAR_ELT(GET_CLASS(data), 0);
872
    class = CHAR_ELT(GET_CLASS(data), 0);
870
    if (strcmp(class,"data.frame") == 0) {
873
    if (strcmp(class,"data.frame") == 0) {
871
        /* check table names, types. variables may not be in same order, as
874
        /* check table names, types. variables may not be in same order, as
872
         * long as names and types are identical. */
875
         * long as names and types are identical. */
Line 1039... Line 1042...
1039
    return ret;
1042
    return ret;
1040
}
1043
}
1041
 
1044
 
1042
 
1045
 
1043
SEXP sdf_get_iname(SEXP sdf) {
1046
SEXP sdf_get_iname(SEXP sdf) {
-
 
1047
    char *iname, *sql;
-
 
1048
    sqlite3_stmt *stmt;
-
 
1049
    SEXP ret, names;
-
 
1050
 
-
 
1051
    iname = SDF_INAME(sdf);
-
 
1052
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
-
 
1053
 
-
 
1054
    sql = "select internal_name, rel_filename from workspace where internal_name=?";
-
 
1055
    sqlite3_prepare(g_workspace, sql, -1, &stmt, NULL);
-
 
1056
    sqlite3_bind_text(stmt, 1, iname, -1, SQLITE_STATIC);
-
 
1057
    sqlite3_step(stmt);
-
 
1058
 
-
 
1059
    PROTECT(ret = NEW_CHARACTER(2));
-
 
1060
    SET_STRING_ELT(ret, 0, mkChar((char *)sqlite3_column_text(stmt, 0)));
-
 
1061
    SET_STRING_ELT(ret, 1, mkChar((char *)sqlite3_column_text(stmt, 1)));
-
 
1062
 
-
 
1063
    PROTECT(names = NEW_CHARACTER(2));
-
 
1064
    SET_STRING_ELT(names, 0, mkChar("Internal Name"));
-
 
1065
    SET_STRING_ELT(names, 1, mkChar("File"));
-
 
1066
 
1044
    return _getListElement(sdf, "iname");
1067
    SET_NAMES(ret, names);
-
 
1068
 
-
 
1069
    sqlite3_finalize(stmt);
-
 
1070
    UNPROTECT(2);
-
 
1071
 
-
 
1072
    return ret;
1045
}
1073
}