The R Project SVN R-packages

Rev

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

Rev 3284 Rev 3307
Line 6... Line 6...
6
 * UTILITY FUNCTIONS
6
 * UTILITY FUNCTIONS
7
 ****************************************************************************/
7
 ****************************************************************************/
8
 
8
 
9
/* if user supplied a name, return that name. otherwise, use the 
9
/* if user supplied a name, return that name. otherwise, use the 
10
 * default "data" */
10
 * default "data" */
11
int _check_sdf_name(SEXP name, char **rname, char **iname, int *file_idx) {
11
static int _check_sdf_name(SEXP name, char **rname, char **iname, int *file_idx) {
12
    int namelen = 0;
12
    int namelen = 0;
13
 
13
 
14
    /* check if arg name is supplied */
14
    /* check if arg name is supplied */
15
    if (IS_CHARACTER(name)) {
15
    if (IS_CHARACTER(name)) {
16
        *rname = CHAR_ELT(name,0);
16
        *rname = CHAR_ELT(name,0);
Line 32... Line 32...
32
        Rprintf("Error: the supplied value for arg name is not a string.\n");
32
        Rprintf("Error: the supplied value for arg name is not a string.\n");
33
    }
33
    }
34
    return namelen;
34
    return namelen;
35
}
35
}
36
 
36
 
37
int _find_free_filename(char *rname, char **iname, int *namelen, int *file_idx) {
37
static int _find_free_filename(char *rname, char **iname, int *namelen, int *file_idx) {
38
    do {
38
    do {
39
        if (!_file_exists(*iname)) break;
39
        if (!_file_exists(*iname)) break;
40
        *namelen = sprintf(*iname, "%s%d.db", rname, ++(*file_idx)) - 3;
40
        *namelen = sprintf(*iname, "%s%d.db", rname, ++(*file_idx)) - 3;
41
    } while (*file_idx < 10000);
41
    } while (*file_idx < 10000);
42
 
42
 
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_skeleton2(SEXP name, int *onamelen) {
-
 
61
    char *iname, *rname;
-
 
62
    int namelen, file_idx = 0, res;
-
 
63
 
-
 
64
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
-
 
65
 
-
 
66
    if (!namelen) return NULL;
-
 
67
 
-
 
68
    _find_free_filename(rname, &iname, &namelen, &file_idx);
-
 
69
 
-
 
70
    if (file_idx >= 10000) { 
-
 
71
        Rprintf("Error: cannot find free SDF name.\n");
-
 
72
        return NULL;
-
 
73
    }
-
 
74
 
-
 
75
    /* create new db by attaching a non-existent file */
-
 
76
    iname[namelen] = 0; /* remove ".db" */
-
 
77
    sprintf(g_sql_buf[2], "attach '%s.db' as [%s]", iname, iname);
-
 
78
    res = _sqlite_exec(g_sql_buf[2]);
-
 
79
    if (_sqlite_error(res)) return NULL; 
-
 
80
 
-
 
81
    /* create attributes table */
-
 
82
    res = _create_sdf_attribute2(iname);
-
 
83
    if (_sqlite_error(res)) {
-
 
84
        sprintf(g_sql_buf[2], "detach '%s'", iname);
-
 
85
        return NULL; 
-
 
86
    }
-
 
87
 
-
 
88
    *onamelen = namelen;
-
 
89
    return iname;
-
 
90
}
60
/* checks if a column has a corresponding factor|ordered table */
91
/* checks if a column has a corresponding factor|ordered table */
61
static int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
92
static int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
62
    sqlite3_stmt *stmt;
93
    sqlite3_stmt *stmt;
63
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
94
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
64
            colname);
95
            colname);
Line 201... Line 232...
201
 * SDF FUNCTIONS
232
 * SDF FUNCTIONS
202
 ****************************************************************************/
233
 ****************************************************************************/
203
 
234
 
204
SEXP sdf_create_sdf(SEXP df, SEXP name) {
235
SEXP sdf_create_sdf(SEXP df, SEXP name) {
205
    SEXP ret;
236
    SEXP ret;
206
    char *rname, *iname; 
237
    char *iname; 
207
    int file_idx = 0, namelen, res, i, j;
238
    int namelen, res, i, j;
208
 
-
 
209
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
-
 
210
    if (!namelen) return R_NilValue;
-
 
211
 
-
 
212
 
239
 
213
    /* at this point, iname will contain #{iname}.db */
240
    /* find free name, attach sdf, create sdf_attributes */
214
    _find_free_filename(rname, &iname, &namelen, &file_idx);
241
    iname = _create_sdf_skeleton2(name, &namelen);
215
    
242
    
216
    /* found a free number */
-
 
217
    if (file_idx < 10000) {
243
    if (iname != NULL) {
218
        /* create a sdf db file by running "attach" statement to non-existent
-
 
219
         * db file
-
 
220
         */
-
 
221
        int sql_len, sql_len2;
244
        int sql_len, sql_len2;
222
        sqlite3_stmt *stmt;
245
        sqlite3_stmt *stmt;
223
 
246
 
224
        iname[namelen] = 0;  /* remove ".db" */
-
 
225
        sql_len = sprintf(g_sql_buf[0], "attach '%s.db' as %s", iname, iname);
-
 
226
 
-
 
227
        res = _sqlite_exec(g_sql_buf[0]);
-
 
228
        if (_sqlite_error(res)) return R_NilValue; /* duplicate dbname */
-
 
229
            
-
 
230
 
-
 
231
        /* 
-
 
232
         * create tables for the sdf db
-
 
233
         */
-
 
234
 
-
 
235
        /* create sdf_attributes table */
-
 
236
        res = _create_sdf_attribute2(iname);
-
 
237
        if (_sqlite_error(res)) return R_NilValue;
-
 
238
 
-
 
239
        /* create sdf_data table */
247
        /* create sdf_data table */
240
        SEXP names = GET_NAMES(df), variable, levels;
248
        SEXP names = GET_NAMES(df), variable, levels;
241
        int ncols = GET_LENGTH(names), type, *types;
249
        int ncols = GET_LENGTH(names), type, *types;
242
        char *col_name, *class, *factor;
250
        char *col_name, *class, *factor;
243
 
251
 
Line 284... Line 292...
284
 
292
 
285
                for (j = 0; j < GET_LENGTH(levels); j++) {
293
                for (j = 0; j < GET_LENGTH(levels); j++) {
286
                    sqlite3_reset(stmt);
294
                    sqlite3_reset(stmt);
287
                    factor = CHAR(STRING_ELT(levels, j));
295
                    factor = CHAR(STRING_ELT(levels, j));
288
                    sqlite3_bind_int(stmt, 1, j+1);
296
                    sqlite3_bind_int(stmt, 1, j+1);
289
                    sqlite3_bind_text(stmt, 2, factor, strlen(factor), SQLITE_STATIC);
297
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
290
                    sqlite3_step(stmt);
298
                    sqlite3_step(stmt);
291
                }
299
                }
292
                sqlite3_finalize(stmt);
300
                sqlite3_finalize(stmt);
293
            }
301
            }
294
        }
302
        }
Line 357... Line 365...
357
         * create a new object representing the sdf
365
         * create a new object representing the sdf
358
         */
366
         */
359
        ret = _create_sdf_sexp(iname);
367
        ret = _create_sdf_sexp(iname);
360
 
368
 
361
    } else {
369
    } else {
362
        Rprintf("ERROR: cannot find a free internal name for %s", rname);
370
        Rprintf("ERROR: cannot find a free internal name.");
363
        ret = R_NilValue;
371
        ret = R_NilValue;
364
    }
372
    }
365
        
373
        
366
    return ret;
374
    return ret;
367
}
375
}
Line 599... Line 607...
599
            sqlite3_finalize(stmt);
607
            sqlite3_finalize(stmt);
600
            return ret;
608
            return ret;
601
        } if (col_index_len > 1) {
609
        } if (col_index_len > 1) {
602
            /* create a new SDF, logic similar to sdf_create_sdf */
610
            /* create a new SDF, logic similar to sdf_create_sdf */
603
 
611
 
604
            /* we have to close the current statement so we can do
-
 
605
             * edits in the db */
-
 
606
            /* sqlite3_finalize(stmt); */
-
 
607
 
-
 
608
            /* find a new name. data<n> ? */
612
            /* find a new name. data<n> ? */
609
            char *iname2, *rname2;
613
            char *iname2;
610
            const char *colname;
614
            const char *colname;
611
            int namelen, file_idx = 0, sql_len, sql_len2;
615
            int namelen, sql_len, sql_len2;
612
            namelen = _check_sdf_name(R_NilValue, &rname2, &iname2, &file_idx);
-
 
613
 
-
 
614
            if (!namelen) return R_NilValue; 
-
 
615
 
-
 
616
            _find_free_filename(rname2, &iname2, &namelen, &file_idx);
-
 
617
 
-
 
618
            if (file_idx >= 10000) { 
-
 
619
                Rprintf("Error: cannot find free SDF name.\n");
-
 
620
                return R_NilValue;
-
 
621
            }
-
 
622
 
-
 
623
 
616
 
624
            /* create new db by attaching a non-existent file */
-
 
625
            iname2[namelen] = 0; /* remove ".db" */
-
 
626
            sprintf(g_sql_buf[1], "attach '%s.db' as %s", iname2, iname2);
-
 
627
            res = _sqlite_exec(g_sql_buf[1]);
-
 
628
            if (_sqlite_error(res)) return R_NilValue; 
-
 
629
 
-
 
630
            /* create attributes table */
-
 
631
            res = _create_sdf_attribute2(iname2);
617
            iname2 = _create_sdf_skeleton2(R_NilValue, &namelen);
632
            if (_sqlite_error(res)) return R_NilValue; 
-
 
633
 
618
 
634
            /* create sdf_data table */
619
            /* create sdf_data table */
635
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
620
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
636
            sql_len2 = 0;
621
            sql_len2 = 0;
637
 
622