The R Project SVN R-packages

Rev

Rev 3684 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3397 mrmanese 1
#include <stdio.h>
2
#include <string.h>
3
#include "sqlite_dataframe.h"
4
 
5
 
6
SEXP sdf_import_sqlite_table(SEXP _dbfilename, SEXP _tblname, SEXP _sdfiname) {
7
    sqlite3 *db;
8
    sqlite3_stmt *stmt;
9
    int res, nrows, ncols, buflen0, buflen1, buflen3, i;
10
    int tblname_len, colname_len, iname_len;
4798 mrmanese 11
    const char *dbfilename, *tblname;
12
    char *iname, *type;
3397 mrmanese 13
    const char *colname;
14
    SEXP ret = NULL;
15
 
16
    dbfilename = CHAR_ELT(_dbfilename, 0);
17
    tblname = CHAR_ELT(_tblname, 0);
18
    /* determine if we were passed a real sqlite db */
19
    db = _is_sqlitedb(dbfilename);
20
 
21
    if (!db) {
22
        Rprintf("Error: %s is not a SQLite database.\n", dbfilename);
23
        return R_NilValue;
24
    }
25
 
26
    /* check if the table exists in the db */
27
    sprintf(g_sql_buf[0], "select count(*) from [%s]", tblname);
28
    res = sqlite3_prepare(db, g_sql_buf[0], -1, &stmt, 0);
29
    if (_sqlite_error(res)) { 
30
        sqlite3_finalize(stmt); sqlite3_close(db);
31
        return R_NilValue; 
32
    }
33
    sqlite3_step(stmt);
34
    nrows = sqlite3_column_int(stmt, 0);
35
    sqlite3_finalize(stmt); sqlite3_close(db);
36
    if (nrows == 0) {
37
        Rprintf("Error: Table contains no rows. Aborting import operation.\n");
38
        return R_NilValue;
39
    }
40
 
41
    /* we have to attach it so we can transfer easily */
42
 
43
    /* unload sdf's if we run to the MAX_ATTACHED limit */
44
    _prepare_attach2();
45
 
46
    /* attach the table to be imported */
47
    sprintf(g_sql_buf[0], "attach '%s' as [@@import@@]", dbfilename);
48
    res = _sqlite_exec(g_sql_buf[0]);
49
    if (_sqlite_error(res)) return R_NilValue;
50
 
51
 
52
    /* create the sdf where the data will be imported to */
3419 mrmanese 53
    iname = _create_sdf_skeleton1(_sdfiname, NULL, FALSE);
3397 mrmanese 54
    if (iname == NULL) goto sdf_import_sqlite_table__out;
55
 
56
    iname_len = strlen(iname);  /* needed for buf resizing below */
57
    tblname_len = strlen(tblname);
58
 
59
    /* get column types and names */
60
    sprintf(g_sql_buf[0], "select * from [@@import@@].[%s]", tblname);
61
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
62
    _sqlite_error(res);
63
    sqlite3_step(stmt);
64
    ncols = sqlite3_column_count(stmt);
65
 
66
    /* create the sql scripts to execute: the create table and the insert-select */
67
    buflen0 = sprintf(g_sql_buf[0], "create table [%s].sdf_data([row name] text", iname);
68
    buflen1 = sprintf(g_sql_buf[1], "select cumsum(1)");
69
    buflen3 = sprintf(g_sql_buf[3], "from [@@import@@].[%s] ", tblname);
70
    for (i = 0; i < ncols; i++) {
71
        type = _str_tolower(g_sql_buf[2], sqlite3_column_decltype(stmt, i));
72
        colname = sqlite3_column_name(stmt, i);
73
        colname_len = strlen(colname);
74
        if (strstr(type, "int")) {
75
            _expand_buf(0, buflen0 + strlen(colname) + 10);
76
            buflen0 += sprintf(g_sql_buf[0]+buflen0, ",[%s] int", colname);
77
 
78
            _expand_buf(1, buflen1 + colname_len + 10);
79
            buflen1 += sprintf(g_sql_buf[1]+buflen1, ",[%s]", colname);
3473 mrmanese 80
        } else if (strstr(type, "char") || strcmp(type, "text") == 0) {
3397 mrmanese 81
            _expand_buf(0, buflen0 + strlen(colname) + 10);
82
            buflen0 += sprintf(g_sql_buf[0]+buflen0, ",[%s] int", colname);
83
 
84
            _expand_buf(1, buflen1 + colname_len + iname_len + 20);
85
            buflen1 += sprintf(g_sql_buf[1]+buflen1, ",[%s].[factor %s].level",
86
                   iname, colname);
87
 
88
            /* create factor table */
89
            _create_factor_table2(iname, "factor", colname);
90
 
91
            /* insert factor levels and labels */
3473 mrmanese 92
            sprintf(g_sql_buf[2], "insert into [%s].[factor %s] (label)"
93
                   " select distinct [%s] from [@@import@@].[%s]", 
3397 mrmanese 94
                   iname, colname, colname, tblname);
95
            res = _sqlite_exec(g_sql_buf[2]);
96
            _sqlite_error(res);
97
 
3473 mrmanese 98
            _init_sqlite_function_accumulator();
99
            sprintf(g_sql_buf[2], "update [%s].[factor %s] set level=cumsum(1)",
100
                   iname, colname);
101
            res = _sqlite_exec(g_sql_buf[2]);
102
            _sqlite_error(res);
103
 
3397 mrmanese 104
            /* add join clause to from clause */
105
            _expand_buf(3, buflen3 + colname_len * 3 + iname_len * 2 + strlen(tblname) + 32);
106
            buflen3 += sprintf(g_sql_buf[3] + buflen3, " join [%s].[factor %s] "
107
                    "on [@@import@@].[%s].[%s] = [%s].[factor %s].label",
108
                    iname, colname, tblname, colname, iname, colname);
3473 mrmanese 109
        } else if (strcmp(type, "blob") == 0 || !*type) {
3397 mrmanese 110
            /* not supported */
3473 mrmanese 111
            warning("skipping column %s because it is a blob.\n", colname);
3397 mrmanese 112
        } else { /* numeric / float / real / ... */
113
            _expand_buf(0, buflen0 + strlen(colname) + 10);
114
            buflen0 += sprintf(g_sql_buf[0]+buflen0, ",[%s] double", colname);
115
 
116
            _expand_buf(1, buflen1 + colname_len + 10);
117
            buflen1 += sprintf(g_sql_buf[1]+buflen1, ",[%s]", colname);
118
        }
119
    }
120
 
121
    sqlite3_finalize(stmt);
122
 
123
    /* execute create script */
124
    _expand_buf(0, buflen0 + 10);
125
    sprintf(g_sql_buf[0] + buflen0, ")");
126
    res = _sqlite_exec(g_sql_buf[0]);
127
    if (_sqlite_error(res)) {
128
        sdf_detach_sdf(mkString(iname));
129
        goto sdf_import_sqlite_table__out;
130
    }
131
 
3473 mrmanese 132
    /* execute insert-select script */
133
    _init_sqlite_function_accumulator();
3397 mrmanese 134
    _expand_buf(0, buflen1 + buflen3 + iname_len + 32 );
135
    sprintf(g_sql_buf[0], "insert into [%s].sdf_data %s %s", iname, g_sql_buf[1], g_sql_buf[3]);
136
    res = _sqlite_exec(g_sql_buf[0]);
137
    if (_sqlite_error(res)) {
138
        sdf_detach_sdf(mkString(iname));
139
        goto sdf_import_sqlite_table__out;
140
    }
141
 
142
    /* create sexp sdf */
143
    ret = _create_sdf_sexp(iname);
144
 
145
sdf_import_sqlite_table__out:
146
    _sqlite_exec("detach [@@import@@]");
147
    return ret;
148
}
3684 mrmanese 149
 
150
SEXP sdf_tempdir() {
151
    SEXP tempdir, ans;
152
 
153
    PROTECT(tempdir = allocList(1));
154
    SET_TYPEOF(tempdir, LANGSXP);
155
    SETCAR(tempdir,install("tempdir"));
156
    /* SET_VECTOR_ELT(tempdir, 0, install("tempdir")); */
157
    ans = eval(tempdir, R_GlobalEnv);
158
 
159
    UNPROTECT(1);
160
    return ans;
161
}