The R Project SVN R-packages

Rev

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

Rev 3358 Rev 3397
Line 89... Line 89...
89
        sprintf(g_sql_buf[2], "detach '%s'", iname);
89
        sprintf(g_sql_buf[2], "detach '%s'", iname);
90
        _delete_sdf2(iname);
90
        _delete_sdf2(iname);
91
        return NULL; 
91
        return NULL; 
92
    }
92
    }
93
 
93
 
94
    *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
static int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
99
    sqlite3_stmt *stmt;
99
    sqlite3_stmt *stmt;
Line 104... Line 104...
104
    sqlite3_finalize(stmt);
104
    sqlite3_finalize(stmt);
105
    return res == SQLITE_OK;
105
    return res == SQLITE_OK;
106
}
106
}
107
 
107
 
108
/* create a factor|ordered table */
108
/* create a factor|ordered table */
109
static int _create_factor_table2(const char *iname, const char *factor_type, 
109
int _create_factor_table2(const char *iname, const char *factor_type, 
110
        const char *colname) {
110
        const char *colname) {
111
    sqlite3_stmt *stmt;
111
    sqlite3_stmt *stmt;
112
    sprintf(g_sql_buf[2], "create table [%s].[%s %s] (level int, label text, "
112
    sprintf(g_sql_buf[2], "create table [%s].[%s %s] (level int, label text, "
113
            "primary key(level), unique(label));", iname, factor_type, colname);
113
            "primary key(level), unique(label));", iname, factor_type, colname);
114
    int res;
114
    int res;
Line 293... Line 293...
293
            /* create separate table for factors decode */
293
            /* create separate table for factors decode */
294
            if (strcmp(class, "factor") == 0 || strcmp(class, "ordered") == 0){
294
            if (strcmp(class, "factor") == 0 || strcmp(class, "ordered") == 0){
295
                if (_create_factor_table2(iname, class, col_name)) 
295
                if (_create_factor_table2(iname, class, col_name)) 
296
                    return R_NilValue; /* dup tbl name? */
296
                    return R_NilValue; /* dup tbl name? */
297
 
297
 
-
 
298
                _sqlite_exec("begin");
298
                levels = GET_LEVELS(variable);
299
                levels = GET_LEVELS(variable);
299
                sprintf(g_sql_buf[2], "insert into [%s].[%s %s] values(?, ?);",
300
                sprintf(g_sql_buf[2], "insert into [%s].[%s %s] values(?, ?);",
300
                        iname, class, col_name);
301
                        iname, class, col_name);
301
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
302
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
302
                if (_sqlite_error(res)) return R_NilValue; /* dup tbl name? */
303
                if (_sqlite_error(res)) return R_NilValue; /* dup tbl name? */
Line 307... Line 308...
307
                    sqlite3_bind_int(stmt, 1, j+1);
308
                    sqlite3_bind_int(stmt, 1, j+1);
308
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
309
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
309
                    sqlite3_step(stmt);
310
                    sqlite3_step(stmt);
310
                }
311
                }
311
                sqlite3_finalize(stmt);
312
                sqlite3_finalize(stmt);
-
 
313
                _sqlite_exec("commit");
312
            }
314
            }
313
        }
315
        }
314
        
316
        
315
        _expand_buf(0,sql_len+35);
317
        _expand_buf(0,sql_len+35);
316
        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]));"); */
317
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ");"); */
319
        sql_len += sprintf(g_sql_buf[0]+sql_len, ");");
318
        res = _sqlite_exec(g_sql_buf[0]);
320
        res = _sqlite_exec(g_sql_buf[0]);
319
        if (_sqlite_error(res)) return R_NilValue; /* why? */
321
        if (_sqlite_error(res)) return R_NilValue; /* why? */
320
 
322
 
321
        /*
323
        /*
322
         * add the data in df to the sdf
324
         * add the data in df to the sdf
323
         */
325
         */
324
        rownames = getAttrib(df, R_RowNamesSymbol);
326
        rownames = getAttrib(df, R_RowNamesSymbol);
325
        nrows = GET_LENGTH(rownames);
327
        nrows = GET_LENGTH(rownames);
326
 
328
 
-
 
329
        _sqlite_error(_sqlite_exec("begin"));
327
        sprintf(g_sql_buf[1]+sql_len2, ")");
330
        sprintf(g_sql_buf[1]+sql_len2, ")");
328
        res = sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
331
        res = sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
329
 
332
 
330
        for (i = 0; i < nrows; i++) {
333
        for (i = 0; i < nrows; i++) {
331
            sqlite3_reset(stmt);
334
            sqlite3_reset(stmt);
Line 357... Line 360...
357
                /* TODO: handle NA's & NULL's */
360
                /* TODO: handle NA's & NULL's */
358
            }
361
            }
359
 
362
 
360
            res = sqlite3_step(stmt);
363
            res = sqlite3_step(stmt);
361
            if (res != SQLITE_DONE) { 
364
            if (res != SQLITE_DONE) { 
-
 
365
                _sqlite_exec("ROLLBACK");
362
                sqlite3_finalize(stmt);
366
                sqlite3_finalize(stmt);
363
                Rprintf("ERROR: %s\n", sqlite3_errmsg(g_workspace));
367
                Rprintf("SQLITE ERROR: %s\n", sqlite3_errmsg(g_workspace));
364
                return R_NilValue; /* why? */
368
                return R_NilValue; /* why? */
365
            }
369
            }
366
        }
370
        }
367
                
371
                
368
        sqlite3_finalize(stmt);
372
        sqlite3_finalize(stmt);
-
 
373
        _sqlite_error(_sqlite_exec("COMMIT"));
369
 
374
 
370
        /* create a new object representing the sdf */
375
        /* create a new object representing the sdf */
371
        ret = _create_sdf_sexp(iname);
376
        ret = _create_sdf_sexp(iname);
372
 
377
 
373
    } else {
378
    } else {
Line 975... Line 980...
975
            buflen2 += sprintf(g_sql_buf[1]+buflen2, ",?");
980
            buflen2 += sprintf(g_sql_buf[1]+buflen2, ",?");
976
        }
981
        }
977
 
982
 
978
        _expand_buf(2, buflen + buflen2 + 10);
983
        _expand_buf(2, buflen + buflen2 + 10);
979
        sprintf(g_sql_buf[2], "%s) %s)", g_sql_buf[0], g_sql_buf[1]);
984
        sprintf(g_sql_buf[2], "%s) %s)", g_sql_buf[0], g_sql_buf[1]);
980
        Rprintf("%s\n", g_sql_buf[2]);
-
 
981
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
985
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
982
        if (_sqlite_error(res)) return ret;
986
        if (_sqlite_error(res)) return ret;
983
 
987
 
984
        /* finally, add rows */
988
        /* finally, add rows */
985
        nrows = LENGTH(GET_ROWNAMES(data));
989
        nrows = LENGTH(GET_ROWNAMES(data));
986
        rownames = GET_ROWNAMES(data);
990
        rownames = GET_ROWNAMES(data);
987
 
991
 
-
 
992
        _sqlite_exec("begin");
988
        for (i = 0; i < nrows; i++) {
993
        for (i = 0; i < nrows; i++) {
989
            sqlite3_bind_text(stmt, 1, CHAR_ELT(rownames, i), -1, SQLITE_STATIC);
994
            sqlite3_bind_text(stmt, 1, CHAR_ELT(rownames, i), -1, SQLITE_STATIC);
990
            for (j = 0; j < ncols; j++) {
995
            for (j = 0; j < ncols; j++) {
991
                col = VECTOR_ELT(data, j);
996
                col = VECTOR_ELT(data, j);
992
                switch(types[j]) {
997
                switch(types[j]) {
Line 1012... Line 1017...
1012
            }
1017
            }
1013
            sqlite3_reset(stmt);
1018
            sqlite3_reset(stmt);
1014
        }
1019
        }
1015
 
1020
 
1016
        sqlite3_finalize(stmt);
1021
        sqlite3_finalize(stmt);
-
 
1022
        _sqlite_exec("begin");
1017
        ret = sdf;
1023
        ret = sdf;
1018
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1024
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1019
    }
1025
    }
1020
 
1026
 
1021
    return ret;
1027
    return ret;