The R Project SVN R-packages

Rev

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

Rev 3282 Rev 3284
Line 45... Line 45...
45
 
45
 
46
/* creates an sdf attribute table */
46
/* creates an sdf attribute table */
47
static int _create_sdf_attribute2(const char *iname) {
47
static int _create_sdf_attribute2(const char *iname) {
48
    sprintf(g_sql_buf[2], "create table [%s].sdf_attributes(attr text, "
48
    sprintf(g_sql_buf[2], "create table [%s].sdf_attributes(attr text, "
49
            "value text, primary key (attr))", iname);
49
            "value text, primary key (attr))", iname);
-
 
50
    int res;
50
    int res = _sqlite_exec(g_sql_buf[2]);
51
    res = _sqlite_exec(g_sql_buf[2]);
51
    if (res == SQLITE_OK) {
52
    if (res == SQLITE_OK) {
52
        sprintf(g_sql_buf[2], "insert into [%s].sdf_attributes values ('name',"
53
        sprintf(g_sql_buf[2], "insert into [%s].sdf_attributes values ('name',"
53
                "'%s');", iname, iname);
54
                "'%s');", iname, iname);
54
        res = _sqlite_exec(g_sql_buf[2]);
55
        res = _sqlite_exec(g_sql_buf[2]);
55
    }
56
    }
Line 59... Line 60...
59
/* checks if a column has a corresponding factor|ordered table */
60
/* checks if a column has a corresponding factor|ordered table */
60
static int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
61
static int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
61
    sqlite3_stmt *stmt;
62
    sqlite3_stmt *stmt;
62
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
63
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
63
            colname);
64
            colname);
-
 
65
    int res;
64
    int res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
66
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
65
    sqlite3_finalize(stmt);
67
    sqlite3_finalize(stmt);
66
    return res == SQLITE_OK;
68
    return res == SQLITE_OK;
67
}
69
}
68
 
70
 
69
/* create a factor|ordered table */
71
/* create a factor|ordered table */
70
static int _create_factor_table2(const char *iname, const char *factor_type, 
72
static int _create_factor_table2(const char *iname, const char *factor_type, 
71
        const char *colname) {
73
        const char *colname) {
72
    sqlite3_stmt *stmt;
74
    sqlite3_stmt *stmt;
73
    sprintf(g_sql_buf[2], "create table [%s].[%s %s] (level int, label text, "
75
    sprintf(g_sql_buf[2], "create table [%s].[%s %s] (level int, label text, "
74
            "primary key(level), unique(label));", iname, factor_type, colname);
76
            "primary key(level), unique(label));", iname, factor_type, colname);
-
 
77
    int res;
75
    int res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
78
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
76
    if (res == SQLITE_OK) sqlite3_step(stmt);
79
    if (res == SQLITE_OK) sqlite3_step(stmt);
77
    sqlite3_finalize(stmt);
80
    sqlite3_finalize(stmt);
78
    return res; /* error on dup name? */
81
    return res; /* error on dup name? */
79
}
82
}
80
 
83
 
Line 166... Line 169...
166
        type = TYPEOF(vec);
169
        type = TYPEOF(vec);
167
 
170
 
168
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
171
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
169
 
172
 
170
        if (type == CHARSXP) {
173
        if (type == CHARSXP) {
171
            SET_STRING_ELT(vec, row, mkChar(sqlite3_column_text(stmt, i)));
174
            SET_STRING_ELT(vec, row, mkChar((char *)sqlite3_column_text(stmt, i)));
172
        } else if (type == INTSXP) {
175
        } else if (type == INTSXP) {
173
            INTEGER(vec)[row] = sqlite3_column_int(stmt, i);
176
            INTEGER(vec)[row] = sqlite3_column_int(stmt, i);
174
        } else if (type == REALSXP) {
177
        } else if (type == REALSXP) {
175
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
178
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
176
        } else if (type == LGLSXP) {
179
        } else if (type == LGLSXP) {
Line 345... Line 348...
345
         * add the new sdf to the workspace
348
         * add the new sdf to the workspace
346
         */
349
         */
347
        iname[namelen] = '.';
350
        iname[namelen] = '.';
348
        strcpy(g_sql_buf[0], iname);
351
        strcpy(g_sql_buf[0], iname);
349
        iname[namelen] = 0;
352
        iname[namelen] = 0;
350
        res = _add_sdf1(iname, g_sql_buf[0]);
353
        res = _add_sdf1(g_sql_buf[0], iname);
351
        if (_sqlite_error(res)) return R_NilValue; /* why? */
354
        if (_sqlite_error(res)) return R_NilValue; /* why? */
352
 
355
 
353
        /*
356
        /*
354
         * create a new object representing the sdf
357
         * create a new object representing the sdf
355
         */
358
         */
Line 366... Line 369...
366
SEXP sdf_get_names(SEXP sdf) {
369
SEXP sdf_get_names(SEXP sdf) {
367
    char *iname = SDF_INAME(sdf);
370
    char *iname = SDF_INAME(sdf);
368
    int len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
371
    int len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
369
 
372
 
370
    sqlite3_stmt *stmt;
373
    sqlite3_stmt *stmt;
371
    int res;
374
    int res, i;
372
   
375
   
373
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
376
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
374
    if (_sqlite_error(res)) return R_NilValue;
377
    if (_sqlite_error(res)) return R_NilValue;
375
 
378
 
376
    SEXP ret;
379
    SEXP ret;
377
 
380
 
378
    len = sqlite3_column_count(stmt)-1;
381
    len = sqlite3_column_count(stmt)-1;
379
    PROTECT(ret = NEW_CHARACTER(len));
382
    PROTECT(ret = NEW_CHARACTER(len));
380
 
383
 
381
    for (int i = 0; i < len; i++) {
384
    for (i = 0; i < len; i++) {
382
        SET_STRING_ELT(ret, i, mkChar(sqlite3_column_name(stmt, i+1)));
385
        SET_STRING_ELT(ret, i, mkChar(sqlite3_column_name(stmt, i+1)));
383
    }
386
    }
384
 
387
 
385
    sqlite3_finalize(stmt);
388
    sqlite3_finalize(stmt);
386
    UNPROTECT(1);
389
    UNPROTECT(1);