The R Project SVN R-packages

Rev

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

Rev 3446 Rev 3457
Line 205... Line 205...
205
        vec = VECTOR_ELT(df, i-1);
205
        vec = VECTOR_ELT(df, i-1);
206
        type = TYPEOF(vec);
206
        type = TYPEOF(vec);
207
 
207
 
208
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
208
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
209
 
209
 
210
        if (type == CHARSXP) {
210
        if (type == STRSXP || type == CHARSXP) {
211
            SET_STRING_ELT(vec, row, mkChar((char *)sqlite3_column_text(stmt, i)));
211
            SET_STRING_ELT(vec, row, mkChar((char *)sqlite3_column_text(stmt, i)));
212
        } else if (type == INTSXP) {
212
        } else if (type == INTSXP) {
213
            INTEGER(vec)[row] = sqlite3_column_int(stmt, i);
213
            INTEGER(vec)[row] = sqlite3_column_int(stmt, i);
214
        } else if (type == REALSXP) {
214
        } else if (type == REALSXP) {
215
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
215
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
Line 218... Line 218...
218
        }
218
        }
219
    }
219
    }
220
    return type;
220
    return type;
221
}
221
}
222
 
222
 
-
 
223
/* set ordinary df row name as numbers */
223
static void _set_rownames2(SEXP df) {
224
static void _set_rownames2(SEXP df) {
224
    SEXP value = VECTOR_ELT(df, 0);
225
    SEXP value = VECTOR_ELT(df, 0);
225
    int len = LENGTH(value), i;
226
    int len = LENGTH(value), i;
226
    PROTECT(value = NEW_CHARACTER(len));
227
    PROTECT(value = NEW_CHARACTER(len));
227
    for (i = 0; i < len; i++) {
228
    for (i = 0; i < len; i++) {
Line 353... Line 354...
353
                        break;
354
                        break;
354
                    case REALSXP:
355
                    case REALSXP:
355
                        sqlite3_bind_double(stmt, j+2, REAL(variable)[i]);
356
                        sqlite3_bind_double(stmt, j+2, REAL(variable)[i]);
356
                        break;
357
                        break;
357
                    case CHARSXP:
358
                    case CHARSXP:
358
                        col_name = CHAR(STRING_ELT(variable,i));
359
                    case STRSXP:
359
                        sqlite3_bind_text(stmt, j+2, col_name, strlen(col_name), SQLITE_STATIC);
360
                        sqlite3_bind_text(stmt, j+2, CHAR_ELT(variable, i), -1, SQLITE_STATIC);
360
                }
361
                }
361
                /* TODO: handle NA's & NULL's */
362
                /* TODO: handle NA's & NULL's */
362
            }
363
            }
363
 
364
 
364
            res = sqlite3_step(stmt);
365
            res = sqlite3_step(stmt);
Line 485... Line 486...
485
    buflen = sprintf(g_sql_buf[0], "select [row name]");
486
    buflen = sprintf(g_sql_buf[0], "select [row name]");
486
    idxlen = LENGTH(col);
487
    idxlen = LENGTH(col);
487
    col_cnt = sqlite3_column_count(stmt) - 1;
488
    col_cnt = sqlite3_column_count(stmt) - 1;
488
 
489
 
489
    /*
490
    /*
490
     * PROCESS COLUMN INDEX
491
     * PROCESS COLUMN INDEX: set columns in the select statement
491
     */
492
     */
492
    if (col == R_NilValue) {
493
    if (col == R_NilValue) {
493
        for (i = 1; i < col_cnt+1; i++) {
494
        for (i = 1; i < col_cnt+1; i++) {
494
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
495
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
495
                    sqlite3_column_name(stmt, i)); 
496
                    sqlite3_column_name(stmt, i)); 
Line 602... Line 603...
602
        Rprintf("Error: don't know how to handle column index.\n");
603
        Rprintf("Error: don't know how to handle column index.\n");
603
        return R_NilValue;
604
        return R_NilValue;
604
    }
605
    }
605
 
606
 
606
    /* 
607
    /* 
607
     * PROCESS ROW INDEX
608
     * PROCESS ROW INDEX: setup limit or where clause
608
     */
609
     */
609
    idxlen = LENGTH(row);
610
    idxlen = LENGTH(row);
610
    if (row == R_NilValue) {
611
    if (row == R_NilValue) {
611
        if (col_index_len == 1) {
612
        if (col_index_len == 1) {
612
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
613
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
Line 845... Line 846...
845
    }
846
    }
846
 
847
 
847
    UNUSE_SDF2(iname);
848
    UNUSE_SDF2(iname);
848
 
849
 
849
    if (ret != R_NilValue && col_index_len > 1) {
850
    if (ret != R_NilValue && col_index_len > 1) {
850
        SEXP class = mkString("data.frame");
851
        SET_CLASS(ret, mkString("data.frame"));
851
        SET_CLASS(ret, class);
-
 
852
        _set_rownames2(ret);
852
        _set_rownames2(ret);
853
    } else if (ret != R_NilValue && col_index_len == 1) {
853
    } else if (ret != R_NilValue && col_index_len == 1) {
854
        ret = VECTOR_ELT(ret, 0);
854
        ret = VECTOR_ELT(ret, 0);
855
    }
855
    }
856
 
856
 
857
    return ret;
857
    return ret;
858
}
858
}
859
 
859
 
860
SEXP sdf_rbind(SEXP sdf, SEXP data) {
860
SEXP sdf_rbind(SEXP sdf, SEXP data) {
861
    char *class = CHAR_ELT(GET_CLASS(data), 0);
861
    char *class;
862
    char *iname = SDF_INAME(sdf), *colname, *rowname;
862
    char *iname = SDF_INAME(sdf), *colname, *rowname;
863
    SEXP ret = R_NilValue, col, names, levels, rownames;
863
    SEXP ret = R_NilValue, col, names, levels, rownames;
864
    int ncols, buflen, buflen2, i, j, res, nrows, *types;
864
    int ncols, buflen, buflen2, i, j, res, nrows, *types, rownames_type;
865
    sqlite3_stmt *stmt;
865
    sqlite3_stmt *stmt;
866
    const char *type;
866
    const char *type;
-
 
867
    char *rn_str; int rn_int; double rn_dbl;
867
 
868
 
-
 
869
    class = CHAR_ELT(GET_CLASS(data), 0);
868
    if (strcmp(class,"data.frame") == 0) {
870
    if (strcmp(class,"data.frame") == 0) {
869
        /* check table names, types. variables may not be in same order, as
871
        /* check table names, types. variables may not be in same order, as
870
         * long as names and types are identical. */
872
         * long as names and types are identical. */
871
        names = GET_NAMES(data);
873
        names = GET_NAMES(data);
872
        ncols = GET_LENGTH(names);
874
        ncols = GET_LENGTH(names);
Line 879... Line 881...
879
        _expand_buf(0, buflen + 100);
881
        _expand_buf(0, buflen + 100);
880
        sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
882
        sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
881
 
883
 
882
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
884
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
883
        if (res != SQLITE_OK) { /* column name mismatch */
885
        if (res != SQLITE_OK) { /* column name mismatch */
-
 
886
            _sqlite_error(res);
884
            Rprintf("Error: column names should exactly match the names of the SDF.\n");
887
            Rprintf("Error: column names should exactly match the names of the SDF.\n");
885
            return ret;
888
            return ret;
886
        }
889
        }
887
 
890
 
888
        /* now check the type */
891
        /* now check the type */
889
        types = (int *)R_alloc(ncols, sizeof(int));
892
        types = (int *)R_alloc(ncols, sizeof(int));
890
        for (i = 0; i< ncols; i++) {
893
        for (i = 0; i< ncols; i++) {
891
            type = sqlite3_column_decltype(stmt, i+1); /* +1 bec 1st col is "1" */
894
            type = sqlite3_column_decltype(stmt, i+1); /* +1 bec 1st col is "1" */
892
 
895
 
893
            col = VECTOR_ELT(data, i);
896
            col = VECTOR_ELT(data, i);
894
            class = CHAR_ELT(GET_CLASS(col),0);
897
            /* class = CHAR_ELT(GET_CLASS(col),0); */
895
 
898
 
896
            if (strcmp(type, "double") == 0 && IS_NUMERIC(col)) types[i] = SQLITE_FLOAT;
899
            if (strcmp(type, "double") == 0 && IS_NUMERIC(col)) types[i] = SQLITE_FLOAT;
897
            else if (strcmp(type, "text") == 0 && IS_CHARACTER(col)) types[i] = SQLITE_TEXT;
900
            else if (strcmp(type, "text") == 0 && IS_CHARACTER(col)) types[i] = SQLITE_TEXT;
898
            else if (strcmp(type, "int") == 0) { 
901
            else if (strcmp(type, "int") == 0) { 
899
                types[i] = SQLITE_INTEGER;
902
                types[i] = SQLITE_INTEGER;
Line 980... Line 983...
980
        sprintf(g_sql_buf[2], "%s) %s)", g_sql_buf[0], g_sql_buf[1]);
983
        sprintf(g_sql_buf[2], "%s) %s)", g_sql_buf[0], g_sql_buf[1]);
981
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
984
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
982
        if (_sqlite_error(res)) return ret;
985
        if (_sqlite_error(res)) return ret;
983
 
986
 
984
        /* finally, add rows */
987
        /* finally, add rows */
-
 
988
        rownames = getAttrib(data, R_RowNamesSymbol); /* GET_ROWNAMES(data); */
985
        nrows = LENGTH(GET_ROWNAMES(data));
989
        nrows = LENGTH(rownames);
986
        rownames = GET_ROWNAMES(data);
990
        rownames_type = TYPEOF(rownames);
987
 
991
 
988
        _sqlite_exec("begin");
992
        _sqlite_begin;
989
        for (i = 0; i < nrows; i++) {
993
        for (i = 0; i < nrows; i++) {
-
 
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
            }
990
            sqlite3_bind_text(stmt, 1, CHAR_ELT(rownames, i), -1, SQLITE_STATIC);
1007
            sqlite3_bind_text(stmt, 1, rn_str, -1, SQLITE_STATIC);
-
 
1008
 
991
            for (j = 0; j < ncols; j++) {
1009
            for (j = 0; j < ncols; j++) {
992
                col = VECTOR_ELT(data, j);
1010
                col = VECTOR_ELT(data, j);
993
                switch(types[j]) {
1011
                switch(types[j]) {
994
                    case SQLITE_FLOAT:
1012
                    case SQLITE_FLOAT:
995
                        sqlite3_bind_double(stmt, j+2, REAL(col)[i]); break;
1013
                        sqlite3_bind_double(stmt, j+2, REAL(col)[i]); break;
Line 1001... Line 1019...
1001
                }
1019
                }
1002
            }
1020
            }
1003
 
1021
 
1004
            res = sqlite3_step(stmt);
1022
            res = sqlite3_step(stmt);
1005
            if (res != SQLITE_DONE) { /* row name pk conflict */
1023
            if (res != SQLITE_DONE) { /* row name pk conflict */
1006
                rowname = CHAR_ELT(rownames, i);
-
 
1007
                for (j = 1; res != SQLITE_DONE; j++) { /* _normally_, j shouldn't overflow */
1024
                for (j = 1; res != SQLITE_DONE; j++) { /* _normally_, j shouldn't overflow */
1008
                    sqlite3_reset(stmt);
1025
                    sqlite3_reset(stmt);
1009
                    sprintf(g_sql_buf[2], "%s-%d", rowname, j);
1026
                    sprintf(g_sql_buf[2], "%s-%d", rn_str, j);
1010
                    sqlite3_bind_text(stmt, 1, g_sql_buf[2], -1, SQLITE_STATIC);
1027
                    sqlite3_bind_text(stmt, 1, g_sql_buf[2], -1, SQLITE_STATIC);
1011
                    res = sqlite3_step(stmt);
1028
                    res = sqlite3_step(stmt);
1012
                }
1029
                }
1013
            }
1030
            } 
1014
            sqlite3_reset(stmt);
-
 
1015
        }
1031
        }
1016
 
1032
 
1017
        sqlite3_finalize(stmt);
1033
        sqlite3_finalize(stmt);
1018
        _sqlite_exec("begin");
1034
        _sqlite_commit;
1019
        ret = sdf;
1035
        ret = sdf;
1020
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1036
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1021
    }
1037
    }
1022
 
1038
 
1023
    return ret;
1039
    return ret;