The R Project SVN R-packages

Rev

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

Rev 3397 Rev 3419
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_skeleton1(SEXP name, int *onamelen) {
60
char *_create_sdf_skeleton1(SEXP name, int *onamelen, int protect) {
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_SDF1(iname, FALSE)) { /* _delete_sdf2(iname); */ return NULL; }
83
    if (!USE_SDF1(iname, FALSE, protect)) { /* _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 93... Line 93...
93
 
93
 
94
    if (onamelen) *onamelen = namelen;
94
    if (onamelen) *onamelen = namelen;
95
    return iname;
95
    return iname;
96
}
96
}
97
/* checks if a column has a corresponding factor|ordered table */
97
/* checks if a column has a corresponding factor|ordered table */
98
static int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
98
int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
99
    sqlite3_stmt *stmt;
99
    sqlite3_stmt *stmt;
100
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
100
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
101
            colname);
101
            colname);
102
    int res;
102
    int res;
103
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
103
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
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_skeleton1(name, &namelen);
247
    iname = _create_sdf_skeleton1(name, &namelen, FALSE);
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 313... Line 313...
313
                _sqlite_exec("commit");
313
                _sqlite_exec("commit");
314
            }
314
            }
315
        }
315
        }
316
        
316
        
317
        _expand_buf(0,sql_len+35);
317
        _expand_buf(0,sql_len+35);
318
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ", primary key([row name]));"); */
318
        sql_len += sprintf(g_sql_buf[0]+sql_len, ", primary key([row name]));");
319
        sql_len += sprintf(g_sql_buf[0]+sql_len, ");");
319
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ");"); */
320
        res = _sqlite_exec(g_sql_buf[0]);
320
        res = _sqlite_exec(g_sql_buf[0]);
321
        if (_sqlite_error(res)) return R_NilValue; /* why? */
321
        if (_sqlite_error(res)) return R_NilValue; /* why? */
322
 
322
 
323
        /*
323
        /*
324
         * add the data in df to the sdf
324
         * add the data in df to the sdf
Line 384... Line 384...
384
}
384
}
385
 
385
 
386
SEXP sdf_get_names(SEXP sdf) {
386
SEXP sdf_get_names(SEXP sdf) {
387
    char *iname;
387
    char *iname;
388
    iname = SDF_INAME(sdf);
388
    iname = SDF_INAME(sdf);
389
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
389
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
390
 
390
 
391
    int len;
391
    int len;
392
    len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
392
    len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
393
 
393
 
394
    sqlite3_stmt *stmt;
394
    sqlite3_stmt *stmt;
Line 412... Line 412...
412
}
412
}
413
 
413
 
414
SEXP sdf_get_length(SEXP sdf) {
414
SEXP sdf_get_length(SEXP sdf) {
415
    char *iname;
415
    char *iname;
416
    iname  = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
416
    iname  = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
417
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
417
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
418
 
418
 
419
    int len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
419
    int len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
420
 
420
 
421
    sqlite3_stmt *stmt;
421
    sqlite3_stmt *stmt;
422
    int res;
422
    int res;
Line 436... Line 436...
436
}
436
}
437
 
437
 
438
/* get row count */
438
/* get row count */
439
SEXP sdf_get_row_count(SEXP sdf) {
439
SEXP sdf_get_row_count(SEXP sdf) {
440
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
440
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
441
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
441
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
442
 
442
 
443
    int nrow;
443
    int nrow;
444
    SEXP ret;
444
    SEXP ret;
445
   
445
   
446
    nrow = _get_row_count2(iname, TRUE);
446
    nrow = _get_row_count2(iname, TRUE);
Line 477... Line 477...
477
}
477
}
478
 
478
 
479
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
479
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
480
    SEXP ret = R_NilValue;
480
    SEXP ret = R_NilValue;
481
    char *iname = SDF_INAME(sdf);
481
    char *iname = SDF_INAME(sdf);
482
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
482
    if (!USE_SDF1(iname, TRUE, TRUE)) return R_NilValue;
483
 
483
 
484
    sqlite3_stmt *stmt;
484
    sqlite3_stmt *stmt;
485
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
485
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
486
    int i, j,res;
486
    int i, j,res;
487
    int *col_indices, col_index_len, *dup_indices;
487
    int *col_indices, col_index_len, *dup_indices;
Line 627... Line 627...
627
            /* find a new name. data<n> ? */
627
            /* find a new name. data<n> ? */
628
            char *iname2;
628
            char *iname2;
629
            const char *colname;
629
            const char *colname;
630
            int namelen, sql_len, sql_len2;
630
            int namelen, sql_len, sql_len2;
631
 
631
 
632
            iname2 = _create_sdf_skeleton1(R_NilValue, &namelen);
632
            iname2 = _create_sdf_skeleton1(R_NilValue, &namelen, TRUE);
633
 
633
 
634
            /* create sdf_data table */
634
            /* create sdf_data table */
635
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
635
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
636
            sql_len2 = 0;
636
            sql_len2 = 0;
637
 
637
 
Line 692... Line 692...
692
            /* add new sdf to workspace */
692
            /* add new sdf to workspace */
693
            sprintf(g_sql_buf[0], "%s.db", iname2);
693
            sprintf(g_sql_buf[0], "%s.db", iname2);
694
            res = _add_sdf1(g_sql_buf[0], iname2);
694
            res = _add_sdf1(g_sql_buf[0], iname2);
695
            if (_sqlite_error(res)) return R_NilValue;
695
            if (_sqlite_error(res)) return R_NilValue;
696
 
696
 
-
 
697
            /* remove protection */
-
 
698
            UNUSE_SDF2(iname2);
-
 
699
 
697
            /* create SEXP for the SDF */
700
            /* create SEXP for the SDF */
698
            ret = _create_sdf_sexp(iname2);
701
            ret = _create_sdf_sexp(iname2);
699
            return ret;
702
            return ret;
700
        } else { sqlite3_finalize(stmt); return R_NilValue; }
703
        } else { sqlite3_finalize(stmt); return R_NilValue; }
701
    } else if (row == R_NilValue || idxlen < 1) {
704
    } else if (row == R_NilValue || idxlen < 1) {
Line 848... Line 851...
848
        }
851
        }
849
 
852
 
850
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
853
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
851
    }
854
    }
852
 
855
 
-
 
856
    UNUSE_SDF2(iname);
-
 
857
 
853
    if (ret != R_NilValue && col_index_len > 1) {
858
    if (ret != R_NilValue && col_index_len > 1) {
854
        SEXP class = mkString("data.frame");
859
        SEXP class = mkString("data.frame");
855
        SET_CLASS(ret, class);
860
        SET_CLASS(ret, class);
856
        _set_rownames2(ret);
861
        _set_rownames2(ret);
857
    } else if (ret != R_NilValue && col_index_len == 1) {
862
    } else if (ret != R_NilValue && col_index_len == 1) {