The R Project SVN R-packages

Rev

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

Rev 3281 Rev 3282
Line 60... Line 60...
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
    sqlite3_finalize(stmt);
65
    return res == SQLITE_OK;
66
    return res == SQLITE_OK;
66
}
67
}
67
 
68
 
68
/* create a factor|ordered table */
69
/* create a factor|ordered table */
69
static int _create_factor_table2(const char *iname, const char *factor_type, 
70
static int _create_factor_table2(const char *iname, const char *factor_type, 
70
        const char *colname) {
71
        const char *colname) {
71
    sqlite3_stmt *stmt;
72
    sqlite3_stmt *stmt;
72
    sprintf(g_sql_buf[2], "create table [%s].[%s %s] (level int, label text, "
73
    sprintf(g_sql_buf[2], "create table [%s].[%s %s] (level int, label text, "
73
            "primary key(level), unique(label));", iname, factor_type, colname);
74
            "primary key(level), unique(label));", iname, factor_type, colname);
74
    int res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
75
    int res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
-
 
76
    if (res == SQLITE_OK) sqlite3_step(stmt);
75
    sqlite3_finalize(stmt);
77
    sqlite3_finalize(stmt);
76
    return res; /* error on dup name? */
78
    return res; /* error on dup name? */
77
}
79
}
78
 
80
 
79
/* copy a factor|ordered table from a sdf(db) to another sdf(db) */
81
/* copy a factor|ordered table from a sdf(db) to another sdf(db) */
Line 85... Line 87...
85
    if (res == SQLITE_OK) {
87
    if (res == SQLITE_OK) {
86
        sprintf(g_sql_buf[2], "insert into [%s].[%s %s] select * from [%s].[%s %s]",
88
        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,
89
                iname_src, factor_type, colname_src, iname_dst, factor_type,
88
                colname_dst);
90
                colname_dst);
89
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
91
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
-
 
92
        if (res == SQLITE_OK) sqlite3_step(stmt);
90
        sqlite3_finalize(stmt);
93
        sqlite3_finalize(stmt);
91
    }
94
    }
92
    return res; /* error on dup name? */
95
    return res; /* error on dup name? */
93
}
96
}
94
 
97
 
Line 155... Line 158...
155
}
158
}
156
 
159
 
157
/* expected that 1st col of stmt is [row name], so we start with index 1 */
160
/* 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) {
161
static int _add_row_to_df(SEXP df, sqlite3_stmt *stmt, int row, int ncols) {
159
    SEXP vec;
162
    SEXP vec;
160
    int type, is_null, i;
163
    int type = -1, is_null, i;
161
    for (i = 1; i <= ncols; i++) {
164
    for (i = 1; i <= ncols; i++) {
162
        vec = VECTOR_ELT(df, i-1);
165
        vec = VECTOR_ELT(df, i-1);
163
        type = TYPEOF(vec);
166
        type = TYPEOF(vec);
164
 
167
 
165
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
168
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
Line 172... Line 175...
172
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
175
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
173
        } else if (type == LGLSXP) {
176
        } else if (type == LGLSXP) {
174
            LOGICAL(vec)[row] = sqlite3_column_int(stmt, i);
177
            LOGICAL(vec)[row] = sqlite3_column_int(stmt, i);
175
        }
178
        }
176
    }
179
    }
-
 
180
    return type;
177
}
181
}
178
 
182
 
179
static void _set_rownames2(SEXP df) {
183
static void _set_rownames2(SEXP df) {
180
    SEXP value = VECTOR_ELT(df, 0);
184
    SEXP value = VECTOR_ELT(df, 0);
181
    int len = LENGTH(value), i;
185
    int len = LENGTH(value), i;
Line 338... Line 342...
338
        sqlite3_finalize(stmt);
342
        sqlite3_finalize(stmt);
339
 
343
 
340
        /*
344
        /*
341
         * add the new sdf to the workspace
345
         * add the new sdf to the workspace
342
         */
346
         */
343
        strcpy(g_sql_buf[0], iname);
-
 
344
        iname[namelen] = '.';
347
        iname[namelen] = '.';
-
 
348
        strcpy(g_sql_buf[0], iname);
-
 
349
        iname[namelen] = 0;
345
        res = _add_sdf1(iname, g_sql_buf[0]);
350
        res = _add_sdf1(iname, g_sql_buf[0]);
346
        if (_sqlite_error(res)) return R_NilValue; /* why? */
351
        if (_sqlite_error(res)) return R_NilValue; /* why? */
347
 
352
 
348
        /*
353
        /*
349
         * create a new object representing the sdf
354
         * create a new object representing the sdf
Line 435... Line 440...
435
    if (f == NULL) {
440
    if (f == NULL) {
436
        Rprintf("Error: File %s does not exist.", filename);
441
        Rprintf("Error: File %s does not exist.", filename);
437
        return R_NilValue;
442
        return R_NilValue;
438
    }
443
    }
439
 
444
 
440
    char *sep = CHAR_ELT(_sep, 0), *quote = CHAR_ELT(_quote,0);
445
    /*char *sep = CHAR_ELT(_sep, 0), *quote = CHAR_ELT(_quote,0);
441
    char *name = CHAR_ELT(_name, 0);
446
    char *name = CHAR_ELT(_name, 0);*/
442
 
447
 
443
    /* create the table */
448
    /* create the table */
444
    /* insert the stuffs */
449
    /* insert the stuffs */
445
    /* register to workspace */
450
    /* register to workspace */
446
 
451
 
447
    return R_NilValue;
452
    return R_NilValue;
448
}
453
}
449
 
454
 
450
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
455
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
451
    SEXP ret, *df;
456
    SEXP ret = R_NilValue;
452
    char *iname = SDF_INAME(sdf);
457
    char *iname = SDF_INAME(sdf);
453
    sqlite3_stmt *stmt, *stmt2;
458
    sqlite3_stmt *stmt;
454
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
459
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
455
    int i, j,res;
460
    int i, j,res;
456
    int *col_indices, col_index_len, *dup_indices;
461
    int *col_indices, col_index_len, *dup_indices;
457
    int row_index_len;
462
    int row_index_len = 0;
458
 
463
 
459
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
464
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
460
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
465
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
461
    if (_sqlite_error(res)) return R_NilValue;
466
    if (_sqlite_error(res)) return R_NilValue;
462
 
467
 
Line 598... Line 603...
598
            /* sqlite3_finalize(stmt); */
603
            /* sqlite3_finalize(stmt); */
599
 
604
 
600
            /* find a new name. data<n> ? */
605
            /* find a new name. data<n> ? */
601
            char *iname2, *rname2;
606
            char *iname2, *rname2;
602
            const char *colname;
607
            const char *colname;
603
            int namelen, file_idx = 0, sql_len, sql_len2, dup_idx;
608
            int namelen, file_idx = 0, sql_len, sql_len2;
604
            namelen = _check_sdf_name(R_NilValue, &rname2, &iname2, &file_idx);
609
            namelen = _check_sdf_name(R_NilValue, &rname2, &iname2, &file_idx);
605
 
610
 
606
            if (!namelen) return R_NilValue; 
611
            if (!namelen) return R_NilValue; 
607
 
612
 
608
            _find_free_filename(rname2, &iname2, &namelen, &file_idx);
613
            _find_free_filename(rname2, &iname2, &namelen, &file_idx);
Line 716... Line 721...
716
        /* create data frame */
721
        /* create data frame */
717
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
722
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
718
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
723
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
719
 
724
 
720
        /* put data in it */
725
        /* put data in it */
721
        row_index_len = 0;
-
 
722
        if (index >= 0 && index < row_cnt) {
726
        if (index >= 0 && index < row_cnt) {
723
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
727
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
724
        }
728
        }
725
 
729
 
726
        for (i = 1; i < idxlen; i++) {
730
        for (i = 1; i < idxlen; i++) {
Line 766... Line 770...
766
        /* create data frame */
770
        /* create data frame */
767
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
771
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
768
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
772
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
769
 
773
 
770
        /* put data in it */
774
        /* put data in it */
771
        row_index_len = 0;
-
 
772
        if (index >= 0 && index < row_cnt) {
775
        if (index >= 0 && index < row_cnt) {
773
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
776
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
774
        }
777
        }
775
 
778
 
776
        for (i = 1; i < idxlen; i++) {
779
        for (i = 1; i < idxlen; i++) {
Line 803... Line 806...
803
        /* append " limit ?,1" to the formed select statement */
806
        /* append " limit ?,1" to the formed select statement */
804
        _expand_buf(0, buflen+10);
807
        _expand_buf(0, buflen+10);
805
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
808
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
806
 
809
 
807
        /* find if there is any TRUE element in the vector */
810
        /* find if there is any TRUE element in the vector */
808
        for (i = 0; i < idxlen & i < row_cnt; i++) {
811
        for (i = 0; i < idxlen && i < row_cnt; i++) {
809
            if (LOGICAL(row)[i]) break;
812
            if (LOGICAL(row)[i]) break;
810
        }
813
        }
811
 
814
 
812
        if (i < idxlen & i < row_cnt) {
815
        if (i < idxlen && i < row_cnt) {
813
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
816
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
814
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
817
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
815
 
818
 
816
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
819
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
817
            if (_sqlite_error(res)) return R_NilValue;
820
            if (_sqlite_error(res)) return R_NilValue;
818
 
821
 
819
            sqlite3_bind_int(stmt, 1, i);
822
            sqlite3_bind_int(stmt, 1, i);
820
            sqlite3_step(stmt);
823
            sqlite3_step(stmt);
821
            ret = _setup_df_sexp1(stmt, iname, col_index_len, est_row_cnt, dup_indices);
824
            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);
825
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
824
            
826
            
825
            for (i++ ; i < row_cnt; i++) {
827
            for (i++ ; i < row_cnt; i++) {
826
                if (LOGICAL(row)[i%idxlen]) {
828
                if (LOGICAL(row)[i%idxlen]) {
827
                    sqlite3_reset(stmt);
829
                    sqlite3_reset(stmt);