The R Project SVN R-packages

Rev

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

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