The R Project SVN R-packages

Rev

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

Rev 3307 Rev 3308
Line 70... Line 70...
70
    if (file_idx >= 10000) { 
70
    if (file_idx >= 10000) { 
71
        Rprintf("Error: cannot find free SDF name.\n");
71
        Rprintf("Error: cannot find free SDF name.\n");
72
        return NULL;
72
        return NULL;
73
    }
73
    }
74
 
74
 
75
    /* create new db by attaching a non-existent file */
75
    /* add to workspace */
-
 
76
    strcpy(g_sql_buf[3], iname);
76
    iname[namelen] = 0; /* remove ".db" */
77
    iname[namelen] = 0; /* remove ".db" */
77
    sprintf(g_sql_buf[2], "attach '%s.db' as [%s]", iname, iname);
78
    res = _add_sdf1(g_sql_buf[3], iname);
78
    res = _sqlite_exec(g_sql_buf[2]);
79
    if (_sqlite_error(res)) return NULL;
-
 
80
 
-
 
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 */
79
    if (_sqlite_error(res)) return NULL; 
83
    if (!USE_SDF(iname)) { _delete_sdf2(iname); return NULL; }
-
 
84
 
80
 
85
 
81
    /* create attributes table */
86
    /* create attributes table */
82
    res = _create_sdf_attribute2(iname);
87
    res = _create_sdf_attribute2(iname);
83
    if (_sqlite_error(res)) {
88
    if (_sqlite_error(res)) {
84
        sprintf(g_sql_buf[2], "detach '%s'", iname);
89
        sprintf(g_sql_buf[2], "detach '%s'", iname);
-
 
90
        _delete_sdf2(iname);
85
        return NULL; 
91
        return NULL; 
86
    }
92
    }
87
 
93
 
88
    *onamelen = namelen;
94
    *onamelen = namelen;
89
    return iname;
95
    return iname;
Line 350... Line 356...
350
            }
356
            }
351
        }
357
        }
352
                
358
                
353
        sqlite3_finalize(stmt);
359
        sqlite3_finalize(stmt);
354
 
360
 
355
        /*
-
 
356
         * add the new sdf to the workspace
-
 
357
         */
-
 
358
        iname[namelen] = '.';
-
 
359
        strcpy(g_sql_buf[0], iname);
-
 
360
        iname[namelen] = 0;
-
 
361
        res = _add_sdf1(g_sql_buf[0], iname);
-
 
362
        if (_sqlite_error(res)) return R_NilValue; /* why? */
-
 
363
 
-
 
364
        /*
-
 
365
         * create a new object representing the sdf
361
        /* create a new object representing the sdf */
366
         */
-
 
367
        ret = _create_sdf_sexp(iname);
362
        ret = _create_sdf_sexp(iname);
368
 
363
 
369
    } else {
364
    } else {
370
        Rprintf("ERROR: cannot find a free internal name.");
365
        Rprintf("ERROR: unable to create a sqlite data frame.\n");
371
        ret = R_NilValue;
366
        ret = R_NilValue;
372
    }
367
    }
373
        
368
        
374
    return ret;
369
    return ret;
375
}
370
}
376
 
371
 
377
SEXP sdf_get_names(SEXP sdf) {
372
SEXP sdf_get_names(SEXP sdf) {
-
 
373
    char *iname;
378
    char *iname = SDF_INAME(sdf);
374
    iname = SDF_INAME(sdf);
-
 
375
    if (!USE_SDF(iname)) return R_NilValue;
-
 
376
 
-
 
377
    int len;
379
    int len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
378
    len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
380
 
379
 
381
    sqlite3_stmt *stmt;
380
    sqlite3_stmt *stmt;
382
    int res, i;
381
    int res, i;
383
   
382
   
384
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
383
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
Line 397... Line 396...
397
    UNPROTECT(1);
396
    UNPROTECT(1);
398
    return ret;
397
    return ret;
399
}
398
}
400
 
399
 
401
SEXP sdf_get_length(SEXP sdf) {
400
SEXP sdf_get_length(SEXP sdf) {
-
 
401
    char *iname;
402
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
402
    iname  = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
-
 
403
    if (!USE_SDF(iname)) return R_NilValue;
-
 
404
 
403
    int len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
405
    int len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
404
 
406
 
405
    sqlite3_stmt *stmt;
407
    sqlite3_stmt *stmt;
406
    int res;
408
    int res;
407
   
409
   
Line 420... Line 422...
420
}
422
}
421
 
423
 
422
/* get row count */
424
/* get row count */
423
SEXP sdf_get_row_count(SEXP sdf) {
425
SEXP sdf_get_row_count(SEXP sdf) {
424
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
426
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
-
 
427
    if (!USE_SDF(iname)) return R_NilValue;
-
 
428
 
425
    char **out;
429
    char **out;
426
    int res, ncol, nrow;
430
    int res, ncol, nrow;
427
    SEXP ret;
431
    SEXP ret;
428
   
432
   
429
    sprintf(g_sql_buf[0], "select count(*) from [%s].sdf_data;", iname);
433
    sprintf(g_sql_buf[0], "select count(*) from [%s].sdf_data;", iname);
Line 464... Line 468...
464
}
468
}
465
 
469
 
466
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
470
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
467
    SEXP ret = R_NilValue;
471
    SEXP ret = R_NilValue;
468
    char *iname = SDF_INAME(sdf);
472
    char *iname = SDF_INAME(sdf);
-
 
473
    if (!USE_SDF(iname)) return R_NilValue;
-
 
474
 
469
    sqlite3_stmt *stmt;
475
    sqlite3_stmt *stmt;
470
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
476
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
471
    int i, j,res;
477
    int i, j,res;
472
    int *col_indices, col_index_len, *dup_indices;
478
    int *col_indices, col_index_len, *dup_indices;
473
    int row_index_len = 0;
479
    int row_index_len = 0;