The R Project SVN R-packages

Rev

Rev 3307 | Rev 3324 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3307 Rev 3308
1
#include <stdio.h>
1
#include <stdio.h>
2
#include <string.h>
2
#include <string.h>
3
#include "sqlite_dataframe.h"
3
#include "sqlite_dataframe.h"
4
 
4
 
5
/****************************************************************************
5
/****************************************************************************
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
static 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);
17
        if (!_is_r_sym(*rname)) { 
17
        if (!_is_r_sym(*rname)) { 
18
            Rprintf("Error: supplied name \"%s\"is not a valid R symbol.\n", 
18
            Rprintf("Error: supplied name \"%s\"is not a valid R symbol.\n", 
19
                    *rname); 
19
                    *rname); 
20
            return FALSE; 
20
            return FALSE; 
21
        }
21
        }
22
        namelen = strlen(*rname);
22
        namelen = strlen(*rname);
23
        *iname = (char*)R_alloc(namelen + 9, sizeof(char)); /* <name>10000.db\0 */
23
        *iname = (char*)R_alloc(namelen + 9, sizeof(char)); /* <name>10000.db\0 */
24
        sprintf(*iname, "%s.db", *rname);
24
        sprintf(*iname, "%s.db", *rname);
25
    } else if (name == R_NilValue) {
25
    } else if (name == R_NilValue) {
26
        *rname = "data";
26
        *rname = "data";
27
        namelen = 5;
27
        namelen = 5;
28
        *iname = (char*)R_alloc(13, sizeof(char)); /* data10000.db\0 */
28
        *iname = (char*)R_alloc(13, sizeof(char)); /* data10000.db\0 */
29
        *file_idx = 1;
29
        *file_idx = 1;
30
        sprintf(*iname, "%s%d.db", *rname, *file_idx);
30
        sprintf(*iname, "%s%d.db", *rname, *file_idx);
31
    } else {
31
    } else {
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
static 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
 
43
    return *file_idx;
43
    return *file_idx;
44
}
44
}
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;
51
    res = _sqlite_exec(g_sql_buf[2]);
51
    res = _sqlite_exec(g_sql_buf[2]);
52
    if (res == SQLITE_OK) {
52
    if (res == SQLITE_OK) {
53
        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',"
54
                "'%s');", iname, iname);
54
                "'%s');", iname, iname);
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) {
60
char *_create_sdf_skeleton2(SEXP name, int *onamelen) {
61
    char *iname, *rname;
61
    char *iname, *rname;
62
    int namelen, file_idx = 0, res;
62
    int namelen, file_idx = 0, res;
63
 
63
 
64
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
64
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
65
 
65
 
66
    if (!namelen) return NULL;
66
    if (!namelen) return NULL;
67
 
67
 
68
    _find_free_filename(rname, &iname, &namelen, &file_idx);
68
    _find_free_filename(rname, &iname, &namelen, &file_idx);
69
 
69
 
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;
90
}
96
}
91
/* checks if a column has a corresponding factor|ordered table */
97
/* checks if a column has a corresponding factor|ordered table */
92
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) {
93
    sqlite3_stmt *stmt;
99
    sqlite3_stmt *stmt;
94
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
100
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
95
            colname);
101
            colname);
96
    int res;
102
    int res;
97
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
103
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
98
    sqlite3_finalize(stmt);
104
    sqlite3_finalize(stmt);
99
    return res == SQLITE_OK;
105
    return res == SQLITE_OK;
100
}
106
}
101
 
107
 
102
/* create a factor|ordered table */
108
/* create a factor|ordered table */
103
static int _create_factor_table2(const char *iname, const char *factor_type, 
109
static int _create_factor_table2(const char *iname, const char *factor_type, 
104
        const char *colname) {
110
        const char *colname) {
105
    sqlite3_stmt *stmt;
111
    sqlite3_stmt *stmt;
106
    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, "
107
            "primary key(level), unique(label));", iname, factor_type, colname);
113
            "primary key(level), unique(label));", iname, factor_type, colname);
108
    int res;
114
    int res;
109
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
115
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
110
    if (res == SQLITE_OK) sqlite3_step(stmt);
116
    if (res == SQLITE_OK) sqlite3_step(stmt);
111
    sqlite3_finalize(stmt);
117
    sqlite3_finalize(stmt);
112
    return res; /* error on dup name? */
118
    return res; /* error on dup name? */
113
}
119
}
114
 
120
 
115
/* copy a factor|ordered table from a sdf(db) to another sdf(db) */
121
/* copy a factor|ordered table from a sdf(db) to another sdf(db) */
116
static int _copy_factor_levels2(const char *factor_type, const char *iname_src,
122
static int _copy_factor_levels2(const char *factor_type, const char *iname_src,
117
        const char *colname_src, const char *iname_dst, const char *colname_dst) {
123
        const char *colname_src, const char *iname_dst, const char *colname_dst) {
118
    sqlite3_stmt *stmt;
124
    sqlite3_stmt *stmt;
119
    int res;
125
    int res;
120
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
126
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
121
    if (res == SQLITE_OK) {
127
    if (res == SQLITE_OK) {
122
        sprintf(g_sql_buf[2], "insert into [%s].[%s %s] select * from [%s].[%s %s]",
128
        sprintf(g_sql_buf[2], "insert into [%s].[%s %s] select * from [%s].[%s %s]",
123
                iname_src, factor_type, colname_src, iname_dst, factor_type,
129
                iname_src, factor_type, colname_src, iname_dst, factor_type,
124
                colname_dst);
130
                colname_dst);
125
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
131
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
126
        if (res == SQLITE_OK) sqlite3_step(stmt);
132
        if (res == SQLITE_OK) sqlite3_step(stmt);
127
        sqlite3_finalize(stmt);
133
        sqlite3_finalize(stmt);
128
    }
134
    }
129
    return res; /* error on dup name? */
135
    return res; /* error on dup name? */
130
}
136
}
131
 
137
 
132
 
138
 
133
/* assumes stmt has col_cnt + 1 columns, col 0 is [row name]. returned SEXP is 
139
/* assumes stmt has col_cnt + 1 columns, col 0 is [row name]. returned SEXP is 
134
 * not UNPROTECT-ed, user will have to do that. will create the names &
140
 * not UNPROTECT-ed, user will have to do that. will create the names &
135
 * attach factor infos */
141
 * attach factor infos */
136
static SEXP _setup_df_sexp1(sqlite3_stmt *stmt, const char *iname, 
142
static SEXP _setup_df_sexp1(sqlite3_stmt *stmt, const char *iname, 
137
        int col_cnt, int row_cnt, int *dup_indices) {
143
        int col_cnt, int row_cnt, int *dup_indices) {
138
    SEXP ret, names, value, class = R_NilValue;
144
    SEXP ret, names, value, class = R_NilValue;
139
    int i, type;
145
    int i, type;
140
    const char *colname, *coltype;
146
    const char *colname, *coltype;
141
 
147
 
142
    PROTECT(ret = NEW_LIST(col_cnt));
148
    PROTECT(ret = NEW_LIST(col_cnt));
143
 
149
 
144
    /* set up names. */
150
    /* set up names. */
145
    PROTECT(names = NEW_CHARACTER(col_cnt));
151
    PROTECT(names = NEW_CHARACTER(col_cnt));
146
    for (i = 0; i < col_cnt; i++) {
152
    for (i = 0; i < col_cnt; i++) {
147
        colname = sqlite3_column_name(stmt, i+1);  /* +1 bec [row name is 0] */
153
        colname = sqlite3_column_name(stmt, i+1);  /* +1 bec [row name is 0] */
148
        coltype = sqlite3_column_decltype(stmt, i+1);
154
        coltype = sqlite3_column_decltype(stmt, i+1);
149
 
155
 
150
        if (dup_indices[i] == 0) {
156
        if (dup_indices[i] == 0) {
151
            strcpy(g_sql_buf[1], colname);
157
            strcpy(g_sql_buf[1], colname);
152
        } else {
158
        } else {
153
            sprintf(g_sql_buf[1], "%s.%d", colname, dup_indices[i]);
159
            sprintf(g_sql_buf[1], "%s.%d", colname, dup_indices[i]);
154
        }
160
        }
155
 
161
 
156
        SET_STRING_ELT(names, i, mkChar(g_sql_buf[1]));
162
        SET_STRING_ELT(names, i, mkChar(g_sql_buf[1]));
157
 
163
 
158
        if (strcmp(coltype, "text") == 0) {
164
        if (strcmp(coltype, "text") == 0) {
159
            PROTECT(value = NEW_CHARACTER(row_cnt));
165
            PROTECT(value = NEW_CHARACTER(row_cnt));
160
        } else if (strcmp(coltype, "double") == 0) {
166
        } else if (strcmp(coltype, "double") == 0) {
161
            PROTECT(value = NEW_NUMERIC(row_cnt));
167
            PROTECT(value = NEW_NUMERIC(row_cnt));
162
        } else if (strcmp(coltype, "bit") == 0) {
168
        } else if (strcmp(coltype, "bit") == 0) {
163
            PROTECT(value = NEW_LOGICAL(row_cnt));
169
            PROTECT(value = NEW_LOGICAL(row_cnt));
164
        } else if (strcmp(coltype, "integer") == 0 || 
170
        } else if (strcmp(coltype, "integer") == 0 || 
165
                   strcmp(coltype, "int") == 0) {
171
                   strcmp(coltype, "int") == 0) {
166
            PROTECT(value = NEW_INTEGER(row_cnt));
172
            PROTECT(value = NEW_INTEGER(row_cnt));
167
            type = _get_factor_levels1(iname, colname, value);
173
            type = _get_factor_levels1(iname, colname, value);
168
            if (type == VAR_FACTOR) {
174
            if (type == VAR_FACTOR) {
169
                PROTECT(class = mkString("factor"));
175
                PROTECT(class = mkString("factor"));
170
                SET_CLASS(value, class);
176
                SET_CLASS(value, class);
171
                UNPROTECT(1);
177
                UNPROTECT(1);
172
            } else if (type == VAR_ORDERED) {
178
            } else if (type == VAR_ORDERED) {
173
                PROTECT(class = NEW_CHARACTER(2));
179
                PROTECT(class = NEW_CHARACTER(2));
174
                SET_STRING_ELT(class, 0, mkChar("ordered"));
180
                SET_STRING_ELT(class, 0, mkChar("ordered"));
175
                SET_STRING_ELT(class, 1, mkChar("factor"));
181
                SET_STRING_ELT(class, 1, mkChar("factor"));
176
                SET_CLASS(value, class);
182
                SET_CLASS(value, class);
177
                UNPROTECT(1);
183
                UNPROTECT(1);
178
            }
184
            }
179
        } else {
185
        } else {
180
            Rprintf("Error: not supported type %s for %s\n", coltype, colname);
186
            Rprintf("Error: not supported type %s for %s\n", coltype, colname);
181
            UNPROTECT(2); /* unprotect ret, names */
187
            UNPROTECT(2); /* unprotect ret, names */
182
            return R_NilValue;
188
            return R_NilValue;
183
        }
189
        }
184
 
190
 
185
        SET_VECTOR_ELT(ret, i, value);
191
        SET_VECTOR_ELT(ret, i, value);
186
        UNPROTECT(1); /* unprotect value */
192
        UNPROTECT(1); /* unprotect value */
187
                    
193
                    
188
    }
194
    }
189
    SET_NAMES(ret, names);
195
    SET_NAMES(ret, names);
190
    UNPROTECT(1); /* unprotect names only */
196
    UNPROTECT(1); /* unprotect names only */
191
    return ret;
197
    return ret;
192
}
198
}
193
 
199
 
194
/* expected that 1st col of stmt is [row name], so we start with index 1 */
200
/* expected that 1st col of stmt is [row name], so we start with index 1 */
195
static int _add_row_to_df(SEXP df, sqlite3_stmt *stmt, int row, int ncols) {
201
static int _add_row_to_df(SEXP df, sqlite3_stmt *stmt, int row, int ncols) {
196
    SEXP vec;
202
    SEXP vec;
197
    int type = -1, is_null, i;
203
    int type = -1, is_null, i;
198
    for (i = 1; i <= ncols; i++) {
204
    for (i = 1; i <= ncols; i++) {
199
        vec = VECTOR_ELT(df, i-1);
205
        vec = VECTOR_ELT(df, i-1);
200
        type = TYPEOF(vec);
206
        type = TYPEOF(vec);
201
 
207
 
202
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
208
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
203
 
209
 
204
        if (type == CHARSXP) {
210
        if (type == CHARSXP) {
205
            SET_STRING_ELT(vec, row, mkChar((char *)sqlite3_column_text(stmt, i)));
211
            SET_STRING_ELT(vec, row, mkChar((char *)sqlite3_column_text(stmt, i)));
206
        } else if (type == INTSXP) {
212
        } else if (type == INTSXP) {
207
            INTEGER(vec)[row] = sqlite3_column_int(stmt, i);
213
            INTEGER(vec)[row] = sqlite3_column_int(stmt, i);
208
        } else if (type == REALSXP) {
214
        } else if (type == REALSXP) {
209
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
215
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
210
        } else if (type == LGLSXP) {
216
        } else if (type == LGLSXP) {
211
            LOGICAL(vec)[row] = sqlite3_column_int(stmt, i);
217
            LOGICAL(vec)[row] = sqlite3_column_int(stmt, i);
212
        }
218
        }
213
    }
219
    }
214
    return type;
220
    return type;
215
}
221
}
216
 
222
 
217
static void _set_rownames2(SEXP df) {
223
static void _set_rownames2(SEXP df) {
218
    SEXP value = VECTOR_ELT(df, 0);
224
    SEXP value = VECTOR_ELT(df, 0);
219
    int len = LENGTH(value), i;
225
    int len = LENGTH(value), i;
220
    PROTECT(value = NEW_CHARACTER(len));
226
    PROTECT(value = NEW_CHARACTER(len));
221
    for (i = 0; i < len; i++) {
227
    for (i = 0; i < len; i++) {
222
        sprintf(g_sql_buf[2], "%d", i+1);
228
        sprintf(g_sql_buf[2], "%d", i+1);
223
        SET_STRING_ELT(value, i, mkChar(g_sql_buf[2]));
229
        SET_STRING_ELT(value, i, mkChar(g_sql_buf[2]));
224
    }
230
    }
225
 
231
 
226
    SET_ROWNAMES(df, value);
232
    SET_ROWNAMES(df, value);
227
    UNPROTECT(1);
233
    UNPROTECT(1);
228
}
234
}
229
 
235
 
230
 
236
 
231
/****************************************************************************
237
/****************************************************************************
232
 * SDF FUNCTIONS
238
 * SDF FUNCTIONS
233
 ****************************************************************************/
239
 ****************************************************************************/
234
 
240
 
235
SEXP sdf_create_sdf(SEXP df, SEXP name) {
241
SEXP sdf_create_sdf(SEXP df, SEXP name) {
236
    SEXP ret;
242
    SEXP ret;
237
    char *iname; 
243
    char *iname; 
238
    int namelen, res, i, j;
244
    int namelen, res, i, j;
239
 
245
 
240
    /* find free name, attach sdf, create sdf_attributes */
246
    /* find free name, attach sdf, create sdf_attributes */
241
    iname = _create_sdf_skeleton2(name, &namelen);
247
    iname = _create_sdf_skeleton2(name, &namelen);
242
    
248
    
243
    if (iname != NULL) {
249
    if (iname != NULL) {
244
        int sql_len, sql_len2;
250
        int sql_len, sql_len2;
245
        sqlite3_stmt *stmt;
251
        sqlite3_stmt *stmt;
246
 
252
 
247
        /* create sdf_data table */
253
        /* create sdf_data table */
248
        SEXP names = GET_NAMES(df), variable, levels;
254
        SEXP names = GET_NAMES(df), variable, levels;
249
        int ncols = GET_LENGTH(names), type, *types;
255
        int ncols = GET_LENGTH(names), type, *types;
250
        char *col_name, *class, *factor;
256
        char *col_name, *class, *factor;
251
 
257
 
252
        /* TODO: put constraints on table after inserting everything? */
258
        /* TODO: put constraints on table after inserting everything? */
253
 
259
 
254
 
260
 
255
        /* 
261
        /* 
256
         * create the create table and insert sql scripts by looping through
262
         * create the create table and insert sql scripts by looping through
257
         * the columns of df
263
         * the columns of df
258
         */
264
         */
259
        types = (int *)R_alloc(ncols, sizeof(int));
265
        types = (int *)R_alloc(ncols, sizeof(int));
260
        sql_len = sprintf(g_sql_buf[0], "create table [%s].sdf_data ([row name] text", iname);
266
        sql_len = sprintf(g_sql_buf[0], "create table [%s].sdf_data ([row name] text", iname);
261
        sql_len2 = sprintf(g_sql_buf[1], "insert into [%s].sdf_data values(?", iname);
267
        sql_len2 = sprintf(g_sql_buf[1], "insert into [%s].sdf_data values(?", iname);
262
 
268
 
263
        for (i = 0; i < ncols; i++) {
269
        for (i = 0; i < ncols; i++) {
264
            col_name = CHAR(STRING_ELT(names, i));
270
            col_name = CHAR(STRING_ELT(names, i));
265
 
271
 
266
            /* add column definition to the create table sql */
272
            /* add column definition to the create table sql */
267
            _expand_buf(0, sql_len+strlen(col_name)+10);
273
            _expand_buf(0, sql_len+strlen(col_name)+10);
268
 
274
 
269
            variable = _getListElement(df, col_name);
275
            variable = _getListElement(df, col_name);
270
            class = CHAR(STRING_ELT(GET_CLASS(variable), 0));
276
            class = CHAR(STRING_ELT(GET_CLASS(variable), 0));
271
            type = TYPEOF(variable);
277
            type = TYPEOF(variable);
272
            types[i] = type;
278
            types[i] = type;
273
 
279
 
274
            sql_len += sprintf(g_sql_buf[0]+sql_len, ", [%s] %s", col_name, 
280
            sql_len += sprintf(g_sql_buf[0]+sql_len, ", [%s] %s", col_name, 
275
                    _get_column_type(class, type));
281
                    _get_column_type(class, type));
276
 
282
 
277
            /* add handler to insert table sql */
283
            /* add handler to insert table sql */
278
            _expand_buf(1, sql_len+5);
284
            _expand_buf(1, sql_len+5);
279
            strcpy(g_sql_buf[1]+sql_len2, ",?");
285
            strcpy(g_sql_buf[1]+sql_len2, ",?");
280
            sql_len2 += 2; 
286
            sql_len2 += 2; 
281
 
287
 
282
            /* create separate table for factors decode */
288
            /* create separate table for factors decode */
283
            if (strcmp(class, "factor") == 0 || strcmp(class, "ordered") == 0){
289
            if (strcmp(class, "factor") == 0 || strcmp(class, "ordered") == 0){
284
                if (_create_factor_table2(iname, class, col_name)) 
290
                if (_create_factor_table2(iname, class, col_name)) 
285
                    return R_NilValue; /* dup tbl name? */
291
                    return R_NilValue; /* dup tbl name? */
286
 
292
 
287
                levels = GET_LEVELS(variable);
293
                levels = GET_LEVELS(variable);
288
                sprintf(g_sql_buf[2], "insert into [%s].[%s %s] values(?, ?);",
294
                sprintf(g_sql_buf[2], "insert into [%s].[%s %s] values(?, ?);",
289
                        iname, class, col_name);
295
                        iname, class, col_name);
290
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
296
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
291
                if (_sqlite_error(res)) return R_NilValue; /* dup tbl name? */
297
                if (_sqlite_error(res)) return R_NilValue; /* dup tbl name? */
292
 
298
 
293
                for (j = 0; j < GET_LENGTH(levels); j++) {
299
                for (j = 0; j < GET_LENGTH(levels); j++) {
294
                    sqlite3_reset(stmt);
300
                    sqlite3_reset(stmt);
295
                    factor = CHAR(STRING_ELT(levels, j));
301
                    factor = CHAR(STRING_ELT(levels, j));
296
                    sqlite3_bind_int(stmt, 1, j+1);
302
                    sqlite3_bind_int(stmt, 1, j+1);
297
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
303
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
298
                    sqlite3_step(stmt);
304
                    sqlite3_step(stmt);
299
                }
305
                }
300
                sqlite3_finalize(stmt);
306
                sqlite3_finalize(stmt);
301
            }
307
            }
302
        }
308
        }
303
        
309
        
304
        _expand_buf(0,sql_len+35);
310
        _expand_buf(0,sql_len+35);
305
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ", primary key([row name]));");*/
311
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ", primary key([row name]));");*/
306
        sql_len += sprintf(g_sql_buf[0]+sql_len, ");");
312
        sql_len += sprintf(g_sql_buf[0]+sql_len, ");");
307
        res = _sqlite_exec(g_sql_buf[0]);
313
        res = _sqlite_exec(g_sql_buf[0]);
308
        if (_sqlite_error(res)) return R_NilValue; /* why? */
314
        if (_sqlite_error(res)) return R_NilValue; /* why? */
309
 
315
 
310
        /*
316
        /*
311
         * add the data in df to the sdf
317
         * add the data in df to the sdf
312
         */
318
         */
313
        SEXP rownames = getAttrib(df, R_RowNamesSymbol);
319
        SEXP rownames = getAttrib(df, R_RowNamesSymbol);
314
        int nrows = GET_LENGTH(rownames);
320
        int nrows = GET_LENGTH(rownames);
315
        char *row_name;
321
        char *row_name;
316
 
322
 
317
        sprintf(g_sql_buf[1]+sql_len2, ")");
323
        sprintf(g_sql_buf[1]+sql_len2, ")");
318
        res = sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
324
        res = sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
319
 
325
 
320
        for (i = 0; i < nrows; i++) {
326
        for (i = 0; i < nrows; i++) {
321
            row_name = CHAR(STRING_ELT(rownames, i));
327
            row_name = CHAR(STRING_ELT(rownames, i));
322
            sqlite3_reset(stmt);
328
            sqlite3_reset(stmt);
323
 
329
 
324
            if (*row_name)
330
            if (*row_name)
325
                sqlite3_bind_text(stmt, 1, row_name, strlen(row_name), SQLITE_STATIC);
331
                sqlite3_bind_text(stmt, 1, row_name, strlen(row_name), SQLITE_STATIC);
326
            else
332
            else
327
                sqlite3_bind_int(stmt, 1, i);
333
                sqlite3_bind_int(stmt, 1, i);
328
 
334
 
329
            for (j = 0; j < ncols; j++) {
335
            for (j = 0; j < ncols; j++) {
330
                variable = VECTOR_ELT(df, j);
336
                variable = VECTOR_ELT(df, j);
331
                switch(types[j]) {
337
                switch(types[j]) {
332
                    case INTSXP : 
338
                    case INTSXP : 
333
                        sqlite3_bind_int(stmt, j+2, INTEGER(variable)[i]);
339
                        sqlite3_bind_int(stmt, j+2, INTEGER(variable)[i]);
334
                        break;
340
                        break;
335
                    case REALSXP:
341
                    case REALSXP:
336
                        sqlite3_bind_double(stmt, j+2, REAL(variable)[i]);
342
                        sqlite3_bind_double(stmt, j+2, REAL(variable)[i]);
337
                        break;
343
                        break;
338
                    case CHARSXP:
344
                    case CHARSXP:
339
                        col_name = CHAR(STRING_ELT(variable,i));
345
                        col_name = CHAR(STRING_ELT(variable,i));
340
                        sqlite3_bind_text(stmt, j+2, col_name, strlen(col_name), SQLITE_STATIC);
346
                        sqlite3_bind_text(stmt, j+2, col_name, strlen(col_name), SQLITE_STATIC);
341
                }
347
                }
342
                /* TODO: handle NA's & NULL's */
348
                /* TODO: handle NA's & NULL's */
343
            }
349
            }
344
 
350
 
345
            res = sqlite3_step(stmt);
351
            res = sqlite3_step(stmt);
346
            if (res != SQLITE_DONE) { 
352
            if (res != SQLITE_DONE) { 
347
                sqlite3_finalize(stmt);
353
                sqlite3_finalize(stmt);
348
                Rprintf("ERROR: %s\n", sqlite3_errmsg(g_workspace));
354
                Rprintf("ERROR: %s\n", sqlite3_errmsg(g_workspace));
349
                return R_NilValue; /* why? */
355
                return R_NilValue; /* why? */
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);
385
    if (_sqlite_error(res)) return R_NilValue;
384
    if (_sqlite_error(res)) return R_NilValue;
386
 
385
 
387
    SEXP ret;
386
    SEXP ret;
388
 
387
 
389
    len = sqlite3_column_count(stmt)-1;
388
    len = sqlite3_column_count(stmt)-1;
390
    PROTECT(ret = NEW_CHARACTER(len));
389
    PROTECT(ret = NEW_CHARACTER(len));
391
 
390
 
392
    for (i = 0; i < len; i++) {
391
    for (i = 0; i < len; i++) {
393
        SET_STRING_ELT(ret, i, mkChar(sqlite3_column_name(stmt, i+1)));
392
        SET_STRING_ELT(ret, i, mkChar(sqlite3_column_name(stmt, i+1)));
394
    }
393
    }
395
 
394
 
396
    sqlite3_finalize(stmt);
395
    sqlite3_finalize(stmt);
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
   
408
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
410
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
409
    if (_sqlite_error(res)) return R_NilValue;
411
    if (_sqlite_error(res)) return R_NilValue;
410
 
412
 
411
    SEXP ret;
413
    SEXP ret;
412
 
414
 
413
    len = sqlite3_column_count(stmt)-1;
415
    len = sqlite3_column_count(stmt)-1;
414
    PROTECT(ret = NEW_INTEGER(1));
416
    PROTECT(ret = NEW_INTEGER(1));
415
    INTEGER(ret)[0] = len;
417
    INTEGER(ret)[0] = len;
416
 
418
 
417
    sqlite3_finalize(stmt);
419
    sqlite3_finalize(stmt);
418
    UNPROTECT(1);
420
    UNPROTECT(1);
419
    return ret;
421
    return ret;
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);
430
    res = sqlite3_get_table(g_workspace, g_sql_buf[0], &out, &nrow, &ncol, NULL);
434
    res = sqlite3_get_table(g_workspace, g_sql_buf[0], &out, &nrow, &ncol, NULL);
431
    if (_sqlite_error(res)) return R_NilValue;
435
    if (_sqlite_error(res)) return R_NilValue;
432
 
436
 
433
 
437
 
434
    if (nrow != 1) ret = R_NilValue;
438
    if (nrow != 1) ret = R_NilValue;
435
    else {
439
    else {
436
        PROTECT(ret = NEW_INTEGER(1));
440
        PROTECT(ret = NEW_INTEGER(1));
437
        INTEGER(ret)[0] = atoi(out[1]);
441
        INTEGER(ret)[0] = atoi(out[1]);
438
        UNPROTECT(1);
442
        UNPROTECT(1);
439
    }
443
    }
440
 
444
 
441
    sqlite3_free_table(out);
445
    sqlite3_free_table(out);
442
    return ret;
446
    return ret;
443
}
447
}
444
 
448
 
445
    
449
    
446
SEXP sdf_import_table(SEXP _filename, SEXP _name, SEXP _sep, SEXP _quote, 
450
SEXP sdf_import_table(SEXP _filename, SEXP _name, SEXP _sep, SEXP _quote, 
447
        SEXP _rownames, SEXP _colnames) {
451
        SEXP _rownames, SEXP _colnames) {
448
    char *filename = CHAR_ELT(_filename, 0);
452
    char *filename = CHAR_ELT(_filename, 0);
449
    FILE *f = fopen(filename, "r");
453
    FILE *f = fopen(filename, "r");
450
 
454
 
451
    if (f == NULL) {
455
    if (f == NULL) {
452
        Rprintf("Error: File %s does not exist.", filename);
456
        Rprintf("Error: File %s does not exist.", filename);
453
        return R_NilValue;
457
        return R_NilValue;
454
    }
458
    }
455
 
459
 
456
    /*char *sep = CHAR_ELT(_sep, 0), *quote = CHAR_ELT(_quote,0);
460
    /*char *sep = CHAR_ELT(_sep, 0), *quote = CHAR_ELT(_quote,0);
457
    char *name = CHAR_ELT(_name, 0);*/
461
    char *name = CHAR_ELT(_name, 0);*/
458
 
462
 
459
    /* create the table */
463
    /* create the table */
460
    /* insert the stuffs */
464
    /* insert the stuffs */
461
    /* register to workspace */
465
    /* register to workspace */
462
 
466
 
463
    return R_NilValue;
467
    return R_NilValue;
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;
474
 
480
 
475
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
481
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
476
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
482
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
477
    if (_sqlite_error(res)) return R_NilValue;
483
    if (_sqlite_error(res)) return R_NilValue;
478
 
484
 
479
    buflen = sprintf(g_sql_buf[0], "select [row name]");
485
    buflen = sprintf(g_sql_buf[0], "select [row name]");
480
    idxlen = LENGTH(col);
486
    idxlen = LENGTH(col);
481
    col_cnt = sqlite3_column_count(stmt) - 1;
487
    col_cnt = sqlite3_column_count(stmt) - 1;
482
 
488
 
483
    /*
489
    /*
484
     * PROCESS COLUMN INDEX
490
     * PROCESS COLUMN INDEX
485
     */
491
     */
486
    if (col == R_NilValue) {
492
    if (col == R_NilValue) {
487
        for (i = 1; i < col_cnt+1; i++) {
493
        for (i = 1; i < col_cnt+1; i++) {
488
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
494
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
489
                    sqlite3_column_name(stmt, i)); 
495
                    sqlite3_column_name(stmt, i)); 
490
        }
496
        }
491
        _expand_buf(0, buflen+20+strlen(iname));
497
        _expand_buf(0, buflen+20+strlen(iname));
492
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
498
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
493
        col_index_len = col_cnt;
499
        col_index_len = col_cnt;
494
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
500
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
495
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
501
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
496
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
502
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
497
    } else if (col == R_NilValue || idxlen < 1) {
503
    } else if (col == R_NilValue || idxlen < 1) {
498
        sqlite3_finalize(stmt);
504
        sqlite3_finalize(stmt);
499
        return R_NilValue;
505
        return R_NilValue;
500
    } else if (IS_NUMERIC(col)) {
506
    } else if (IS_NUMERIC(col)) {
501
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
507
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
502
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
508
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
503
        col_index_len = 0;
509
        col_index_len = 0;
504
 
510
 
505
        for (i = 0; i < idxlen; i++) {
511
        for (i = 0; i < idxlen; i++) {
506
            /* no need to correct for 0 base because col 0 is [row name] */
512
            /* no need to correct for 0 base because col 0 is [row name] */
507
            index = ((int) REAL(col)[i]); 
513
            index = ((int) REAL(col)[i]); 
508
            if (index > col_cnt) {
514
            if (index > col_cnt) {
509
                sqlite3_finalize(stmt);
515
                sqlite3_finalize(stmt);
510
                Rprintf("Error: undefined columns selected\n");
516
                Rprintf("Error: undefined columns selected\n");
511
                return R_NilValue;
517
                return R_NilValue;
512
            } else if (index > 0) {
518
            } else if (index > 0) {
513
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
519
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
514
                        sqlite3_column_name(stmt,index));
520
                        sqlite3_column_name(stmt,index));
515
                dup_indices[col_index_len] = 0;
521
                dup_indices[col_index_len] = 0;
516
                for (j = 0; j < col_index_len; j++) {
522
                for (j = 0; j < col_index_len; j++) {
517
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
523
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
518
                }
524
                }
519
                col_indices[col_index_len++] = index;
525
                col_indices[col_index_len++] = index;
520
            } else if (index < 0) {
526
            } else if (index < 0) {
521
                sqlite3_finalize(stmt);
527
                sqlite3_finalize(stmt);
522
                Rprintf("Error: negative indices not supported.\n");
528
                Rprintf("Error: negative indices not supported.\n");
523
                return R_NilValue;
529
                return R_NilValue;
524
            }
530
            }
525
        }
531
        }
526
 
532
 
527
        if (col_index_len == 0) {
533
        if (col_index_len == 0) {
528
            sqlite3_finalize(stmt);
534
            sqlite3_finalize(stmt);
529
            Rprintf("Error: no indices detected??\n");
535
            Rprintf("Error: no indices detected??\n");
530
            return R_NilValue;
536
            return R_NilValue;
531
        } else { 
537
        } else { 
532
            _expand_buf(0, buflen+20+strlen(iname));
538
            _expand_buf(0, buflen+20+strlen(iname));
533
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
539
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
534
        }
540
        }
535
    } else if (IS_INTEGER(col)) {
541
    } else if (IS_INTEGER(col)) {
536
        /* identical logic with IS_NUMERIC, except that we don't have to cast
542
        /* identical logic with IS_NUMERIC, except that we don't have to cast
537
         * stuffs from SEXP col. the alternative is doing if idxlen times. */
543
         * stuffs from SEXP col. the alternative is doing if idxlen times. */
538
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
544
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
539
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
545
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
540
        col_index_len = 0;
546
        col_index_len = 0;
541
 
547
 
542
        for (i = 0; i < idxlen; i++) {
548
        for (i = 0; i < idxlen; i++) {
543
            /* no need to correct for 0 base because col 0 is [row name] */
549
            /* no need to correct for 0 base because col 0 is [row name] */
544
            index = INTEGER(col)[i];
550
            index = INTEGER(col)[i];
545
            if (index > col_cnt) {
551
            if (index > col_cnt) {
546
                sqlite3_finalize(stmt);
552
                sqlite3_finalize(stmt);
547
                Rprintf("Error: undefined columns selected\n");
553
                Rprintf("Error: undefined columns selected\n");
548
                return R_NilValue;
554
                return R_NilValue;
549
            } else if (index > 0) {
555
            } else if (index > 0) {
550
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
556
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
551
                        sqlite3_column_name(stmt,index));
557
                        sqlite3_column_name(stmt,index));
552
                dup_indices[col_index_len] = 0;
558
                dup_indices[col_index_len] = 0;
553
                for (j = 0; j < col_index_len; j++) {
559
                for (j = 0; j < col_index_len; j++) {
554
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
560
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
555
                }
561
                }
556
                col_indices[col_index_len++] = index;
562
                col_indices[col_index_len++] = index;
557
            } else if (index < 0) {
563
            } else if (index < 0) {
558
                sqlite3_finalize(stmt);
564
                sqlite3_finalize(stmt);
559
                Rprintf("Error: negative indices not supported.\n");
565
                Rprintf("Error: negative indices not supported.\n");
560
                return R_NilValue;
566
                return R_NilValue;
561
            }
567
            }
562
        }
568
        }
563
 
569
 
564
        if (col_index_len == 0) {
570
        if (col_index_len == 0) {
565
            sqlite3_finalize(stmt);
571
            sqlite3_finalize(stmt);
566
            Rprintf("Error: no indices detected??\n");
572
            Rprintf("Error: no indices detected??\n");
567
            return R_NilValue;
573
            return R_NilValue;
568
        } else { 
574
        } else { 
569
            _expand_buf(0, buflen+20+strlen(iname));
575
            _expand_buf(0, buflen+20+strlen(iname));
570
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
576
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
571
        }
577
        }
572
    } else if (IS_LOGICAL(col)) {
578
    } else if (IS_LOGICAL(col)) {
573
        /* recycling stuff, so max column output is the # of cols in the df */
579
        /* recycling stuff, so max column output is the # of cols in the df */
574
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
580
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
575
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
581
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
576
        col_index_len = 0;
582
        col_index_len = 0;
577
        for (i = 0; i < col_cnt; i++) {
583
        for (i = 0; i < col_cnt; i++) {
578
            if (LOGICAL(col)[i%idxlen]) {
584
            if (LOGICAL(col)[i%idxlen]) {
579
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
585
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
580
                        sqlite3_column_name(stmt,i+1));
586
                        sqlite3_column_name(stmt,i+1));
581
                dup_indices[col_index_len] = 0;
587
                dup_indices[col_index_len] = 0;
582
                col_indices[col_index_len++] = i;
588
                col_indices[col_index_len++] = i;
583
            }
589
            }
584
        }
590
        }
585
 
591
 
586
        if (col_index_len == 0) {
592
        if (col_index_len == 0) {
587
            sqlite3_finalize(stmt);
593
            sqlite3_finalize(stmt);
588
            Rprintf("Warning: no column selected.\n");
594
            Rprintf("Warning: no column selected.\n");
589
            return R_NilValue;
595
            return R_NilValue;
590
        } else { 
596
        } else { 
591
            _expand_buf(0, buflen+20+strlen(iname));
597
            _expand_buf(0, buflen+20+strlen(iname));
592
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
598
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
593
        }
599
        }
594
    } else {
600
    } else {
595
        sqlite3_finalize(stmt);
601
        sqlite3_finalize(stmt);
596
        Rprintf("Error: don't know how to handle column index.\n");
602
        Rprintf("Error: don't know how to handle column index.\n");
597
        return R_NilValue;
603
        return R_NilValue;
598
    }
604
    }
599
 
605
 
600
    /* 
606
    /* 
601
     * PROCESS ROW INDEX
607
     * PROCESS ROW INDEX
602
     */
608
     */
603
    idxlen = LENGTH(row);
609
    idxlen = LENGTH(row);
604
    if (row == R_NilValue) {
610
    if (row == R_NilValue) {
605
        if (col_index_len == 1) {
611
        if (col_index_len == 1) {
606
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
612
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
607
            sqlite3_finalize(stmt);
613
            sqlite3_finalize(stmt);
608
            return ret;
614
            return ret;
609
        } if (col_index_len > 1) {
615
        } if (col_index_len > 1) {
610
            /* create a new SDF, logic similar to sdf_create_sdf */
616
            /* create a new SDF, logic similar to sdf_create_sdf */
611
 
617
 
612
            /* find a new name. data<n> ? */
618
            /* find a new name. data<n> ? */
613
            char *iname2;
619
            char *iname2;
614
            const char *colname;
620
            const char *colname;
615
            int namelen, sql_len, sql_len2;
621
            int namelen, sql_len, sql_len2;
616
 
622
 
617
            iname2 = _create_sdf_skeleton2(R_NilValue, &namelen);
623
            iname2 = _create_sdf_skeleton2(R_NilValue, &namelen);
618
 
624
 
619
            /* create sdf_data table */
625
            /* create sdf_data table */
620
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
626
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
621
            sql_len2 = 0;
627
            sql_len2 = 0;
622
 
628
 
623
            for (i = 0; i < col_index_len; i++) {
629
            for (i = 0; i < col_index_len; i++) {
624
                colname = sqlite3_column_name(stmt, col_indices[i]);
630
                colname = sqlite3_column_name(stmt, col_indices[i]);
625
                if (dup_indices[i] == 0) {
631
                if (dup_indices[i] == 0) {
626
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s] %s", colname,
632
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s] %s", colname,
627
                            sqlite3_column_decltype(stmt, col_indices[i]));
633
                            sqlite3_column_decltype(stmt, col_indices[i]));
628
                } else {
634
                } else {
629
                    /* un-duplicate col names by appending num, just like R */
635
                    /* un-duplicate col names by appending num, just like R */
630
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
636
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
631
                            dup_indices[i], 
637
                            dup_indices[i], 
632
                            sqlite3_column_decltype(stmt, col_indices[i]));
638
                            sqlite3_column_decltype(stmt, col_indices[i]));
633
                }
639
                }
634
 
640
 
635
                /* deal with possibly factor columns */
641
                /* deal with possibly factor columns */
636
                if (_is_factor2(iname, "factor", colname)) {
642
                if (_is_factor2(iname, "factor", colname)) {
637
                    /* found a factor column */
643
                    /* found a factor column */
638
 
644
 
639
                    if (dup_indices[i] == 0)  {
645
                    if (dup_indices[i] == 0)  {
640
                        _copy_factor_levels2("factor", iname, colname, iname2, colname);
646
                        _copy_factor_levels2("factor", iname, colname, iname2, colname);
641
                    } else {
647
                    } else {
642
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
648
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
643
                        _copy_factor_levels2("factor", iname, colname, iname2, 
649
                        _copy_factor_levels2("factor", iname, colname, iname2, 
644
                                g_sql_buf[3]);
650
                                g_sql_buf[3]);
645
                    }
651
                    }
646
                }
652
                }
647
 
653
 
648
                /* and deal with ordered factors too... life is hard, then you die */
654
                /* and deal with ordered factors too... life is hard, then you die */
649
                if (_is_factor2(iname, "ordered", colname)) {
655
                if (_is_factor2(iname, "ordered", colname)) {
650
                    
656
                    
651
                    if (dup_indices[i] == 0)  {
657
                    if (dup_indices[i] == 0)  {
652
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
658
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
653
                                colname);
659
                                colname);
654
                    } else {
660
                    } else {
655
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
661
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
656
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
662
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
657
                                g_sql_buf[3]);
663
                                g_sql_buf[3]);
658
                    }
664
                    }
659
                }
665
                }
660
                                
666
                                
661
            }
667
            }
662
 
668
 
663
            /* don't need it anymore */
669
            /* don't need it anymore */
664
            sqlite3_finalize(stmt);
670
            sqlite3_finalize(stmt);
665
 
671
 
666
            /* no table index created, that's for v2 I hope*/
672
            /* no table index created, that's for v2 I hope*/
667
            sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
673
            sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
668
            res = _sqlite_exec(g_sql_buf[1]);
674
            res = _sqlite_exec(g_sql_buf[1]);
669
            if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
675
            if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
670
 
676
 
671
            /* insert data (with row names). buf[0] contains a select */
677
            /* insert data (with row names). buf[0] contains a select */
672
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
678
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
673
                    g_sql_buf[0]);
679
                    g_sql_buf[0]);
674
            res = _sqlite_exec(g_sql_buf[1]);
680
            res = _sqlite_exec(g_sql_buf[1]);
675
            if (_sqlite_error(res)) return R_NilValue;
681
            if (_sqlite_error(res)) return R_NilValue;
676
 
682
 
677
            /* add new sdf to workspace */
683
            /* add new sdf to workspace */
678
            sprintf(g_sql_buf[0], "%s.db", iname2);
684
            sprintf(g_sql_buf[0], "%s.db", iname2);
679
            res = _add_sdf1(g_sql_buf[0], iname2);
685
            res = _add_sdf1(g_sql_buf[0], iname2);
680
            if (_sqlite_error(res)) return R_NilValue;
686
            if (_sqlite_error(res)) return R_NilValue;
681
 
687
 
682
            /* create SEXP for the SDF */
688
            /* create SEXP for the SDF */
683
            ret = _create_sdf_sexp(iname2);
689
            ret = _create_sdf_sexp(iname2);
684
            return ret;
690
            return ret;
685
        } else { sqlite3_finalize(stmt); return R_NilValue; }
691
        } else { sqlite3_finalize(stmt); return R_NilValue; }
686
    } else if (row == R_NilValue || idxlen < 1) {
692
    } else if (row == R_NilValue || idxlen < 1) {
687
        sqlite3_finalize(stmt);
693
        sqlite3_finalize(stmt);
688
        return R_NilValue;
694
        return R_NilValue;
689
    } else if (IS_NUMERIC(row)) {
695
    } else if (IS_NUMERIC(row)) {
690
        sqlite3_finalize(stmt);
696
        sqlite3_finalize(stmt);
691
 
697
 
692
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
698
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
693
        row_cnt = _get_row_count2(g_sql_buf[1]);
699
        row_cnt = _get_row_count2(g_sql_buf[1]);
694
 
700
 
695
        /* append " limit ?,1" to the formed select statement */
701
        /* append " limit ?,1" to the formed select statement */
696
        _expand_buf(0, buflen+10);
702
        _expand_buf(0, buflen+10);
697
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
703
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
698
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
704
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
699
 
705
 
700
        index = ((int) REAL(row)[0]) - 1;
706
        index = ((int) REAL(row)[0]) - 1;
701
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
707
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
702
 
708
 
703
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
709
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
704
        if (_sqlite_error(res)) return R_NilValue;
710
        if (_sqlite_error(res)) return R_NilValue;
705
 
711
 
706
        sqlite3_bind_int(stmt, 1, index);
712
        sqlite3_bind_int(stmt, 1, index);
707
        res = sqlite3_step(stmt);
713
        res = sqlite3_step(stmt);
708
 
714
 
709
        /* create data frame */
715
        /* create data frame */
710
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
716
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
711
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
717
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
712
 
718
 
713
        /* put data in it */
719
        /* put data in it */
714
        if (index >= 0 && index < row_cnt) {
720
        if (index >= 0 && index < row_cnt) {
715
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
721
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
716
        }
722
        }
717
 
723
 
718
        for (i = 1; i < idxlen; i++) {
724
        for (i = 1; i < idxlen; i++) {
719
            sqlite3_reset(stmt);
725
            sqlite3_reset(stmt);
720
            index = ((int) REAL(row)[i]) - 1;
726
            index = ((int) REAL(row)[i]) - 1;
721
            if (index >= 0 && index < row_cnt) {
727
            if (index >= 0 && index < row_cnt) {
722
                sqlite3_bind_int(stmt, 1, index);
728
                sqlite3_bind_int(stmt, 1, index);
723
                res = sqlite3_step(stmt);
729
                res = sqlite3_step(stmt);
724
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
730
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
725
            }
731
            }
726
        }
732
        }
727
        sqlite3_finalize(stmt);
733
        sqlite3_finalize(stmt);
728
 
734
 
729
        /* shrink vectors */
735
        /* shrink vectors */
730
        if (row_index_len < idxlen) {
736
        if (row_index_len < idxlen) {
731
            for (i = 0; i < col_index_len; i++) {
737
            for (i = 0; i < col_index_len; i++) {
732
                SET_VECTOR_ELT(ret, i, 
738
                SET_VECTOR_ELT(ret, i, 
733
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
739
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
734
            }
740
            }
735
        }
741
        }
736
 
742
 
737
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
743
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
738
    } else if (IS_INTEGER(row)) {
744
    } else if (IS_INTEGER(row)) {
739
        /* same stuff as with IS_INTEGER, except for setting of var index*/
745
        /* same stuff as with IS_INTEGER, except for setting of var index*/
740
        sqlite3_finalize(stmt);
746
        sqlite3_finalize(stmt);
741
 
747
 
742
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
748
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
743
        row_cnt = _get_row_count2(g_sql_buf[1]);
749
        row_cnt = _get_row_count2(g_sql_buf[1]);
744
 
750
 
745
        /* append " limit ?,1" to the formed select statement */
751
        /* append " limit ?,1" to the formed select statement */
746
        _expand_buf(0, buflen+10);
752
        _expand_buf(0, buflen+10);
747
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
753
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
748
 
754
 
749
        index = INTEGER(row)[0] - 1;
755
        index = INTEGER(row)[0] - 1;
750
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
756
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
751
 
757
 
752
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
758
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
753
        if (_sqlite_error(res)) return R_NilValue;
759
        if (_sqlite_error(res)) return R_NilValue;
754
 
760
 
755
        sqlite3_bind_int(stmt, 1, index);
761
        sqlite3_bind_int(stmt, 1, index);
756
        res = sqlite3_step(stmt);
762
        res = sqlite3_step(stmt);
757
 
763
 
758
        /* create data frame */
764
        /* create data frame */
759
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
765
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
760
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
766
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
761
 
767
 
762
        /* put data in it */
768
        /* put data in it */
763
        if (index >= 0 && index < row_cnt) {
769
        if (index >= 0 && index < row_cnt) {
764
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
770
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
765
        }
771
        }
766
 
772
 
767
        for (i = 1; i < idxlen; i++) {
773
        for (i = 1; i < idxlen; i++) {
768
            sqlite3_reset(stmt);
774
            sqlite3_reset(stmt);
769
            index = INTEGER(row)[i] - 1;
775
            index = INTEGER(row)[i] - 1;
770
            if (index >= 0 && index < row_cnt) {
776
            if (index >= 0 && index < row_cnt) {
771
                sqlite3_bind_int(stmt, 1, index);
777
                sqlite3_bind_int(stmt, 1, index);
772
                res = sqlite3_step(stmt); 
778
                res = sqlite3_step(stmt); 
773
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
779
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
774
            }
780
            }
775
        }
781
        }
776
        sqlite3_finalize(stmt);
782
        sqlite3_finalize(stmt);
777
 
783
 
778
        /* shrink vectors */
784
        /* shrink vectors */
779
        if (row_index_len < idxlen) {
785
        if (row_index_len < idxlen) {
780
            for (i = 0; i < col_index_len; i++) {
786
            for (i = 0; i < col_index_len; i++) {
781
                SET_VECTOR_ELT(ret, i, 
787
                SET_VECTOR_ELT(ret, i, 
782
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
788
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
783
            }
789
            }
784
        }
790
        }
785
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
791
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
786
    } else if (IS_LOGICAL(row)) {
792
    } else if (IS_LOGICAL(row)) {
787
        /* */
793
        /* */
788
        sqlite3_finalize(stmt);
794
        sqlite3_finalize(stmt);
789
        int est_row_cnt = 0;
795
        int est_row_cnt = 0;
790
 
796
 
791
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
797
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
792
        row_cnt = _get_row_count2(g_sql_buf[1]);
798
        row_cnt = _get_row_count2(g_sql_buf[1]);
793
 
799
 
794
        /* append " limit ?,1" to the formed select statement */
800
        /* append " limit ?,1" to the formed select statement */
795
        _expand_buf(0, buflen+10);
801
        _expand_buf(0, buflen+10);
796
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
802
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
797
 
803
 
798
        /* find if there is any TRUE element in the vector */
804
        /* find if there is any TRUE element in the vector */
799
        for (i = 0; i < idxlen && i < row_cnt; i++) {
805
        for (i = 0; i < idxlen && i < row_cnt; i++) {
800
            if (LOGICAL(row)[i]) break;
806
            if (LOGICAL(row)[i]) break;
801
        }
807
        }
802
 
808
 
803
        if (i < idxlen && i < row_cnt) {
809
        if (i < idxlen && i < row_cnt) {
804
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
810
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
805
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
811
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
806
 
812
 
807
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
813
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
808
            if (_sqlite_error(res)) return R_NilValue;
814
            if (_sqlite_error(res)) return R_NilValue;
809
 
815
 
810
            sqlite3_bind_int(stmt, 1, i);
816
            sqlite3_bind_int(stmt, 1, i);
811
            sqlite3_step(stmt);
817
            sqlite3_step(stmt);
812
            ret = _setup_df_sexp1(stmt, iname, col_index_len, est_row_cnt, dup_indices);
818
            ret = _setup_df_sexp1(stmt, iname, col_index_len, est_row_cnt, dup_indices);
813
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
819
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
814
            
820
            
815
            for (i++ ; i < row_cnt; i++) {
821
            for (i++ ; i < row_cnt; i++) {
816
                if (LOGICAL(row)[i%idxlen]) {
822
                if (LOGICAL(row)[i%idxlen]) {
817
                    sqlite3_reset(stmt);
823
                    sqlite3_reset(stmt);
818
                    sqlite3_bind_int(stmt, 1, i);
824
                    sqlite3_bind_int(stmt, 1, i);
819
                    sqlite3_step(stmt);
825
                    sqlite3_step(stmt);
820
                    _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
826
                    _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
821
                }
827
                }
822
            }
828
            }
823
        }
829
        }
824
 
830
 
825
        sqlite3_finalize(stmt);
831
        sqlite3_finalize(stmt);
826
 
832
 
827
        /* shrink vectors */
833
        /* shrink vectors */
828
        if (est_row_cnt > 0 && row_index_len < est_row_cnt) {
834
        if (est_row_cnt > 0 && row_index_len < est_row_cnt) {
829
            for (i = 0; i < col_index_len; i++) {
835
            for (i = 0; i < col_index_len; i++) {
830
                SET_VECTOR_ELT(ret, i, 
836
                SET_VECTOR_ELT(ret, i, 
831
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
837
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
832
            }
838
            }
833
        }
839
        }
834
 
840
 
835
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
841
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
836
    }
842
    }
837
 
843
 
838
    if (ret != R_NilValue && col_index_len > 1) {
844
    if (ret != R_NilValue && col_index_len > 1) {
839
        SEXP class = mkString("data.frame");
845
        SEXP class = mkString("data.frame");
840
        SET_CLASS(ret, class);
846
        SET_CLASS(ret, class);
841
        _set_rownames2(ret);
847
        _set_rownames2(ret);
842
    } else if (ret != R_NilValue && col_index_len == 1) {
848
    } else if (ret != R_NilValue && col_index_len == 1) {
843
        ret = VECTOR_ELT(ret, 0);
849
        ret = VECTOR_ELT(ret, 0);
844
    }
850
    }
845
 
851
 
846
    return ret;
852
    return ret;
847
}
853
}
848
 
854
 
849
 
855
 
850
SEXP sopen(SEXP name) {
856
SEXP sopen(SEXP name) {
851
    char *filename;
857
    char *filename;
852
    
858
    
853
    if (IS_CHARACTER(name)) {
859
    if (IS_CHARACTER(name)) {
854
        filename = CHAR(STRING_ELT(name,0));
860
        filename = CHAR(STRING_ELT(name,0));
855
        Rprintf("%s\n", filename);
861
        Rprintf("%s\n", filename);
856
    }
862
    }
857
    /* sqlite3 *db;
863
    /* sqlite3 *db;
858
    int res = sqlite3_open(filename, &db); */
864
    int res = sqlite3_open(filename, &db); */
859
    SEXP ret;
865
    SEXP ret;
860
    PROTECT(ret = NEW_LOGICAL(1));
866
    PROTECT(ret = NEW_LOGICAL(1));
861
    LOGICAL(ret)[0] = IS_CHARACTER(name);
867
    LOGICAL(ret)[0] = IS_CHARACTER(name);
862
    /* sqlite3_close(db); */ 
868
    /* sqlite3_close(db); */ 
863
    UNPROTECT(1);
869
    UNPROTECT(1);
864
    return ret;
870
    return ret;
865
}
871
}