The R Project SVN R-packages

Rev

Rev 4798 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4798 Rev 7488
1
#include <string.h>
1
#include <string.h>
2
#include "sqlite_dataframe.h"
2
#include "sqlite_dataframe.h"
3
 
3
 
4
/****************************************************************************
4
/****************************************************************************
5
 * UTILITY FUNCTIONS
5
 * UTILITY FUNCTIONS
6
 ****************************************************************************/
6
 ****************************************************************************/
7
 
7
 
8
/* if user supplied a name (1st arg), return that name. otherwise, use the 
8
/* if user supplied a name (1st arg), return that name. otherwise, use the 
9
 * default "data". rname is R name, iname is internal name */
9
 * default "data". rname is R name, iname is internal name */
10
static int _check_sdf_name(SEXP name, const char **rname, char **iname, int *file_idx) {
10
static int _check_sdf_name(SEXP name, const char **rname, char **iname, int *file_idx) {
11
    int namelen = 0;
11
    int namelen = 0;
12
 
12
 
13
    /* check if arg name is supplied */
13
    /* check if arg name is supplied */
14
    if (name == R_NilValue) {
14
    if (name == R_NilValue) {
15
        *rname = "data";
15
        *rname = "data";
16
        namelen = 5;
16
        namelen = 5;
17
        *iname = (char*)R_alloc(13, sizeof(char)); /* .SQLiteDF/data10000.db\0 */
17
        *iname = (char*)R_alloc(13, sizeof(char)); /* .SQLiteDF/data10000.db\0 */
18
        *file_idx = 1;
18
        *file_idx = 1;
19
        sprintf(*iname, "%s%d.db", *rname, *file_idx);
19
        sprintf(*iname, "%s%d.db", *rname, *file_idx);
20
    } else if (IS_CHARACTER(name)) {
20
    } else if (IS_CHARACTER(name)) {
21
        *rname = CHAR_ELT(name,0);
21
        *rname = CHAR_ELT(name,0);
22
        if (!_is_r_sym(*rname)) { 
22
        if (!_is_r_sym(*rname)) { 
23
            error("supplied name \"%s\"is not a valid R symbol.", *rname); 
23
            error("supplied name \"%s\"is not a valid R symbol.", *rname); 
24
        }
24
        }
25
        namelen = strlen(*rname);
25
        namelen = strlen(*rname);
26
        *iname = (char*)R_alloc(namelen + 9, sizeof(char)); /* .SQLiteDF/<name>10000.db\0 */
26
        *iname = (char*)R_alloc(namelen + 9, sizeof(char)); /* .SQLiteDF/<name>10000.db\0 */
27
        sprintf(*iname, "%s.db", *rname);
27
        sprintf(*iname, "%s.db", *rname);
28
    } else {
28
    } else {
29
        Rprintf("Error: the supplied value for arg name is not a string.\n");
29
        Rprintf("Error: the supplied value for arg name is not a string.\n");
30
    }
30
    }
31
    return namelen;
31
    return namelen;
32
}
32
}
33
 
33
 
34
static int _find_free_filename2(const char *rname, char *dirname, char **iname, 
34
static int _find_free_filename2(const char *rname, char *dirname, char **iname, 
35
                                int *namelen, int *file_idx) {
35
                                int *namelen, int *file_idx) {
36
    sqlite3_stmt *stmt;
36
    sqlite3_stmt *stmt;
37
    char *tmp_iname;
37
    char *tmp_iname;
38
    sqlite3_prepare(g_workspace, "select 1 from workspace where internal_name=?", -1,
38
    sqlite3_prepare(g_workspace, "select 1 from workspace where internal_name=?", -1,
39
            &stmt, NULL);
39
            &stmt, NULL);
40
    do {
40
    do {
41
        if (dirname != NULL) {
41
        if (dirname != NULL) {
42
            sprintf(g_sql_buf[2], "%s/%s", dirname, *iname);
42
            sprintf(g_sql_buf[2], "%s/%s", dirname, *iname);
43
            tmp_iname = g_sql_buf[2];
43
            tmp_iname = g_sql_buf[2];
44
        } else {
44
        } else {
45
            tmp_iname = *iname;
45
            tmp_iname = *iname;
46
        }
46
        }
47
 
47
 
48
        if (!_file_exists(tmp_iname)) {
48
        if (!_file_exists(tmp_iname)) {
49
            sqlite3_reset(stmt);
49
            sqlite3_reset(stmt);
50
            sqlite3_bind_text(stmt, 1, tmp_iname, -1, SQLITE_STATIC);
50
            sqlite3_bind_text(stmt, 1, tmp_iname, -1, SQLITE_STATIC);
51
            if (sqlite3_step(stmt) != SQLITE_ROW) break;
51
            if (sqlite3_step(stmt) != SQLITE_ROW) break;
52
        }
52
        }
53
        *namelen = sprintf(*iname, "%s%d.db", rname, ++(*file_idx)) - 3;
53
        *namelen = sprintf(*iname, "%s%d.db", rname, ++(*file_idx)) - 3;
54
    } while (*file_idx < 10000);
54
    } while (*file_idx < 10000);
55
 
55
 
56
    sqlite3_finalize(stmt);
56
    sqlite3_finalize(stmt);
57
    return *file_idx;
57
    return *file_idx;
58
}
58
}
59
 
59
 
60
/* creates an sdf attribute table */
60
/* creates an sdf attribute table */
61
static int _create_sdf_attribute2(const char *iname) {
61
static int _create_sdf_attribute2(const char *iname) {
62
    sprintf(g_sql_buf[2], "create table [%s].sdf_attributes(attr text, "
62
    sprintf(g_sql_buf[2], "create table [%s].sdf_attributes(attr text, "
63
            "value text, primary key (attr))", iname);
63
            "value text, primary key (attr))", iname);
64
    int res;
64
    int res;
65
    res = _sqlite_exec(g_sql_buf[2]);
65
    res = _sqlite_exec(g_sql_buf[2]);
66
    if (res == SQLITE_OK) {
66
    if (res == SQLITE_OK) {
67
        sprintf(g_sql_buf[2], "insert into [%s].sdf_attributes values ('name',"
67
        sprintf(g_sql_buf[2], "insert into [%s].sdf_attributes values ('name',"
68
                "'%s');", iname, iname);
68
                "'%s');", iname, iname);
69
        res = _sqlite_exec(g_sql_buf[2]);
69
        res = _sqlite_exec(g_sql_buf[2]);
70
    }
70
    }
71
    return res;
71
    return res;
72
}
72
}
73
 
73
 
74
char *_create_sdf_skeleton1(SEXP name, int *onamelen, int protect) {
74
char *_create_sdf_skeleton1(SEXP name, int *onamelen, int protect) {
75
    const char *rname;
75
    const char *rname;
76
    char *iname;
76
    char *iname;
77
    int namelen, file_idx = 0, res;
77
    int namelen, file_idx = 0, res;
78
 
78
 
79
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
79
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
80
 
80
 
81
    if (!namelen) return NULL;
81
    if (!namelen) return NULL;
82
 
82
 
83
    _find_free_filename2(rname, ".SQLiteDF", &iname, &namelen, &file_idx);
83
    _find_free_filename2(rname, ".SQLiteDF", &iname, &namelen, &file_idx);
84
 
84
 
85
    if (file_idx >= 10000) { 
85
    if (file_idx >= 10000) { 
86
        Rprintf("Error: cannot find free SDF name.\n");
86
        Rprintf("Error: cannot find free SDF name.\n");
87
        return NULL;
87
        return NULL;
88
    }
88
    }
89
 
89
 
90
    /* add to workspace */
90
    /* add to workspace */
91
    sprintf(g_sql_buf[3], ".SQLiteDF/%s", iname);
91
    sprintf(g_sql_buf[3], ".SQLiteDF/%s", iname);
92
    iname[namelen] = 0; /* remove ".db" */
92
    iname[namelen] = 0; /* remove ".db" */
93
    res = _add_sdf1(g_sql_buf[3], iname);
93
    res = _add_sdf1(g_sql_buf[3], iname);
94
    if (_sqlite_error(res)) return NULL;
94
    if (_sqlite_error(res)) return NULL;
95
 
95
 
96
    /* detach SDF's if necessary to attach this one. if file does not
96
    /* detach SDF's if necessary to attach this one. if file does not
97
     * exist, then a sqlite db will be created after we make our 1st table */
97
     * exist, then a sqlite db will be created after we make our 1st table */
98
    if (!USE_SDF1(iname, FALSE, protect)) { /* _delete_sdf2(iname); */ return NULL; }
98
    if (!USE_SDF1(iname, FALSE, protect)) { /* _delete_sdf2(iname); */ return NULL; }
99
 
99
 
100
 
100
 
101
    /* create attributes table */
101
    /* create attributes table */
102
    res = _create_sdf_attribute2(iname);
102
    res = _create_sdf_attribute2(iname);
103
    if (_sqlite_error(res)) {
103
    if (_sqlite_error(res)) {
104
        sprintf(g_sql_buf[2], "detach '%s'", iname);
104
        sprintf(g_sql_buf[2], "detach '%s'", iname);
105
        _delete_sdf2(iname);
105
        _delete_sdf2(iname);
106
        return NULL; 
106
        return NULL; 
107
    }
107
    }
108
 
108
 
109
    if (onamelen) *onamelen = namelen;
109
    if (onamelen) *onamelen = namelen;
110
    return iname;
110
    return iname;
111
}
111
}
112
/* checks if a column has a corresponding factor|ordered table */
112
/* checks if a column has a corresponding factor|ordered table */
113
int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
113
int _is_factor2(const char *iname, const char *factor_type, const char *colname) {
114
    sqlite3_stmt *stmt;
114
    sqlite3_stmt *stmt;
115
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
115
    sprintf(g_sql_buf[2], "select * from [%s].[%s %s]", iname, factor_type,
116
            colname);
116
            colname);
117
    int res;
117
    int res;
118
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
118
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
119
    sqlite3_finalize(stmt);
119
    sqlite3_finalize(stmt);
120
    return res == SQLITE_OK;
120
    return res == SQLITE_OK;
121
}
121
}
122
 
122
 
123
/* create a factor|ordered table */
123
/* create a factor|ordered table */
124
int _create_factor_table2(const char *iname, const char *factor_type, 
124
int _create_factor_table2(const char *iname, const char *factor_type, 
125
        const char *colname) {
125
        const char *colname) {
126
    sqlite3_stmt *stmt;
126
    sqlite3_stmt *stmt;
127
    sprintf(g_sql_buf[2], "create table [%s].[%s %s] (level int, label text, "
127
    sprintf(g_sql_buf[2], "create table [%s].[%s %s] (level int, label text, "
128
            "primary key(level), unique(label));", iname, factor_type, colname);
128
            "primary key(level), unique(label));", iname, factor_type, colname);
129
    int res;
129
    int res;
130
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
130
    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
131
    if (res == SQLITE_OK) sqlite3_step(stmt);
131
    if (res == SQLITE_OK) sqlite3_step(stmt);
132
    sqlite3_finalize(stmt);
132
    sqlite3_finalize(stmt);
133
    return res; /* error on dup name? */
133
    return res; /* error on dup name? */
134
}
134
}
135
 
135
 
136
/* copy a factor|ordered table from a sdf(db) to another sdf(db) */
136
/* copy a factor|ordered table from a sdf(db) to another sdf(db) */
137
int _copy_factor_levels2(const char *factor_type, const char *iname_src,
137
int _copy_factor_levels2(const char *factor_type, const char *iname_src,
138
        const char *colname_src, const char *iname_dst, const char *colname_dst) {
138
        const char *colname_src, const char *iname_dst, const char *colname_dst) {
139
    sqlite3_stmt *stmt;
139
    sqlite3_stmt *stmt;
140
    int res;
140
    int res;
141
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
141
    res = _create_factor_table2(iname_dst, factor_type, colname_dst);
142
    if (res == SQLITE_OK) {
142
    if (res == SQLITE_OK) {
143
        sprintf(g_sql_buf[2], "insert into [%s].[%s %s] select * from [%s].[%s %s]",
143
        sprintf(g_sql_buf[2], "insert into [%s].[%s %s] select * from [%s].[%s %s]",
144
                iname_dst, factor_type, colname_dst, iname_src, factor_type,
144
                iname_dst, factor_type, colname_dst, iname_src, factor_type,
145
                colname_src);
145
                colname_src);
146
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
146
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
147
        if (res == SQLITE_OK) res = sqlite3_step(stmt);
147
        if (res == SQLITE_OK) res = sqlite3_step(stmt);
148
        sqlite3_finalize(stmt);
148
        sqlite3_finalize(stmt);
149
    }
149
    }
150
    return res; /* error on dup name? */
150
    return res; /* error on dup name? */
151
}
151
}
152
 
152
 
153
 
153
 
154
/* assumes stmt has col_cnt + 1 columns, col 0 is [row name]. returned SEXP is 
154
/* assumes stmt has col_cnt + 1 columns, col 0 is [row name]. returned SEXP is 
155
 * not UNPROTECT-ed, user will have to do that. will create the names &
155
 * not UNPROTECT-ed, user will have to do that. will create the names &
156
 * attach factor infos.
156
 * attach factor infos.
157
 * not that this will only create the data frame, not pull data from sqlite */
157
 * not that this will only create the data frame, not pull data from sqlite */
158
static SEXP _setup_df_sexp1(sqlite3_stmt *stmt, const char *iname, 
158
static SEXP _setup_df_sexp1(sqlite3_stmt *stmt, const char *iname, 
159
        int col_cnt, int row_cnt, int *dup_indices) {
159
        int col_cnt, int row_cnt, int *dup_indices) {
160
    SEXP ret, names, value = R_NilValue;
160
    SEXP ret, names, value = R_NilValue;
161
    int i;
161
    int i;
162
    const char *colname, *coltype;
162
    const char *colname, *coltype;
163
 
163
 
164
    PROTECT(ret = NEW_LIST(col_cnt));
164
    PROTECT(ret = NEW_LIST(col_cnt));
165
 
165
 
166
    /* set up names. */
166
    /* set up names. */
167
    PROTECT(names = NEW_CHARACTER(col_cnt));
167
    PROTECT(names = NEW_CHARACTER(col_cnt));
168
    for (i = 0; i < col_cnt; i++) {
168
    for (i = 0; i < col_cnt; i++) {
169
        colname = sqlite3_column_name(stmt, i+1);  /* +1 bec [row name is 0] */
169
        colname = sqlite3_column_name(stmt, i+1);  /* +1 bec [row name is 0] */
170
        coltype = sqlite3_column_decltype(stmt, i+1);
170
        coltype = sqlite3_column_decltype(stmt, i+1);
171
 
171
 
172
        if (dup_indices == NULL || dup_indices[i] == 0) {
172
        if (dup_indices == NULL || dup_indices[i] == 0) {
173
            strcpy(g_sql_buf[1], colname);
173
            strcpy(g_sql_buf[1], colname);
174
        } else {
174
        } else {
175
            sprintf(g_sql_buf[1], "%s.%d", colname, dup_indices[i]);
175
            sprintf(g_sql_buf[1], "%s.%d", colname, dup_indices[i]);
176
        }
176
        }
177
 
177
 
178
        SET_STRING_ELT(names, i, mkChar(g_sql_buf[1]));
178
        SET_STRING_ELT(names, i, mkChar(g_sql_buf[1]));
179
 
179
 
180
        if (strcmp(coltype, "text") == 0) {
180
        if (strcmp(coltype, "text") == 0) {
181
            PROTECT(value = NEW_CHARACTER(row_cnt));
181
            PROTECT(value = NEW_CHARACTER(row_cnt));
182
        } else if (strcmp(coltype, "double") == 0) {
182
        } else if (strcmp(coltype, "double") == 0) {
183
            PROTECT(value = NEW_NUMERIC(row_cnt));
183
            PROTECT(value = NEW_NUMERIC(row_cnt));
184
        } else if (strcmp(coltype, "bit") == 0) {
184
        } else if (strcmp(coltype, "bit") == 0) {
185
            PROTECT(value = NEW_LOGICAL(row_cnt));
185
            PROTECT(value = NEW_LOGICAL(row_cnt));
186
        } else if (strcmp(coltype, "integer") == 0 || 
186
        } else if (strcmp(coltype, "integer") == 0 || 
187
                   strcmp(coltype, "int") == 0) {
187
                   strcmp(coltype, "int") == 0) {
188
            PROTECT(value = NEW_INTEGER(row_cnt));
188
            PROTECT(value = NEW_INTEGER(row_cnt));
189
 
189
 
190
            /* attach level values, & set class of factor/ordered if
190
            /* attach level values, & set class of factor/ordered if
191
             * value is a factor/ordered */
191
             * value is a factor/ordered */
192
            _get_factor_levels1(iname, colname, value, TRUE);
192
            _get_factor_levels1(iname, colname, value, TRUE);
193
        } else {
193
        } else {
194
            UNPROTECT(2); /* unprotect ret, names */
194
            UNPROTECT(2); /* unprotect ret, names */
195
            error("not supported type %s for %s\n", coltype, colname);
195
            error("not supported type %s for %s\n", coltype, colname);
196
        }
196
        }
197
 
197
 
198
        SET_VECTOR_ELT(ret, i, value);
198
        SET_VECTOR_ELT(ret, i, value);
199
        UNPROTECT(1); /* unprotect value */
199
        UNPROTECT(1); /* unprotect value */
200
                    
200
                    
201
    }
201
    }
202
    SET_NAMES(ret, names);
202
    SET_NAMES(ret, names);
203
    UNPROTECT(1); /* unprotect names only */
203
    UNPROTECT(1); /* unprotect names only */
204
    return ret;
204
    return ret;
205
}
205
}
206
 
206
 
207
/* expected that 1st col of stmt is [row name], so we start with index 1 */
207
/* expected that 1st col of stmt is [row name], so we start with index 1 */
208
static int _add_row_to_df(SEXP df, sqlite3_stmt *stmt, int row, int ncols) {
208
static int _add_row_to_df(SEXP df, sqlite3_stmt *stmt, int row, int ncols) {
209
    SEXP vec;
209
    SEXP vec;
210
    int type = -1, is_null, i;
210
    int type = -1, is_null, i;
211
    for (i = 1; i <= ncols; i++) {
211
    for (i = 1; i <= ncols; i++) {
212
        vec = VECTOR_ELT(df, i-1);
212
        vec = VECTOR_ELT(df, i-1);
213
        type = TYPEOF(vec);
213
        type = TYPEOF(vec);
214
 
214
 
215
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
215
        is_null = (sqlite3_column_type(stmt, row) == SQLITE_NULL);
216
 
216
 
217
        if (type == STRSXP || type == CHARSXP) {
217
        if (type == STRSXP || type == CHARSXP) {
218
            SET_STRING_ELT(vec, row, mkChar((char *)sqlite3_column_text(stmt, i)));
218
            SET_STRING_ELT(vec, row, mkChar((char *)sqlite3_column_text(stmt, i)));
219
        } else if (type == INTSXP) {
219
        } else if (type == INTSXP) {
220
            INTEGER(vec)[row] = sqlite3_column_int(stmt, i);
220
            INTEGER(vec)[row] = sqlite3_column_int(stmt, i);
221
        } else if (type == REALSXP) {
221
        } else if (type == REALSXP) {
222
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
222
            REAL(vec)[row] = sqlite3_column_double(stmt, i);
223
        } else if (type == LGLSXP) {
223
        } else if (type == LGLSXP) {
224
            LOGICAL(vec)[row] = sqlite3_column_int(stmt, i);
224
            LOGICAL(vec)[row] = sqlite3_column_int(stmt, i);
225
        }
225
        }
226
    }
226
    }
227
    return type;
227
    return type;
228
}
228
}
229
 
229
 
230
/* set ordinary df row name as numbers */
230
/* set ordinary df row name as numbers */
231
static void _set_rownames2(SEXP df) {
231
static void _set_rownames2(SEXP df) {
232
    SEXP value = VECTOR_ELT(df, 0);
232
    SEXP value = VECTOR_ELT(df, 0);
233
    int len = LENGTH(value), i;
233
    int len = LENGTH(value), i;
234
    PROTECT(value = NEW_CHARACTER(len));
234
    PROTECT(value = NEW_CHARACTER(len));
235
    for (i = 0; i < len; i++) {
235
    for (i = 0; i < len; i++) {
236
        sprintf(g_sql_buf[2], "%d", i+1);
236
        sprintf(g_sql_buf[2], "%d", i+1);
237
        SET_STRING_ELT(value, i, mkChar(g_sql_buf[2]));
237
        SET_STRING_ELT(value, i, mkChar(g_sql_buf[2]));
238
    }
238
    }
239
 
239
 
240
    SET_ROWNAMES(df, value);
240
    SET_ROWNAMES(df, value);
241
    UNPROTECT(1);
241
    UNPROTECT(1);
242
}
242
}
243
 
243
 
244
 
244
 
245
/****************************************************************************
245
/****************************************************************************
246
 * SDF FUNCTIONS
246
 * SDF FUNCTIONS
247
 ****************************************************************************/
247
 ****************************************************************************/
248
 
248
 
249
SEXP sdf_create_sdf(SEXP df, SEXP name) {
249
SEXP sdf_create_sdf(SEXP df, SEXP name) {
250
    SEXP ret;
250
    SEXP ret;
251
    char *iname; 
251
    char *iname; 
252
    int namelen, res, i, j;
252
    int namelen, res, i, j;
253
 
253
 
254
    /* find free name, attach sdf, create sdf_attributes */
254
    /* find free name, attach sdf, create sdf_attributes */
255
    iname = _create_sdf_skeleton1(name, &namelen, FALSE);
255
    iname = _create_sdf_skeleton1(name, &namelen, FALSE);
256
    
256
    
257
    if (iname != NULL) {
257
    if (iname != NULL) {
258
        int sql_len, sql_len2;
258
        int sql_len, sql_len2;
259
        sqlite3_stmt *stmt;
259
        sqlite3_stmt *stmt;
260
 
260
 
261
        /* create sdf_data table */
261
        /* create sdf_data table */
262
        SEXP names = GET_NAMES(df), variable, levels, var_class;
262
        SEXP names = GET_NAMES(df), variable, levels, var_class;
263
        int ncols = GET_LENGTH(names), type, *types;
263
        int ncols = GET_LENGTH(names), type, *types;
264
        const char *col_name, *class, *factor;
264
        const char *col_name, *class, *factor;
265
 
265
 
266
        /* variables for adding rownames */
266
        /* variables for adding rownames */
267
        SEXP rownames;
267
        SEXP rownames;
268
        int nrows;
268
        int nrows;
269
        const char *row_name;
269
        const char *row_name;
270
 
270
 
271
        /* TODO: put constraints on table after inserting everything? */
271
        /* TODO: put constraints on table after inserting everything? */
272
 
272
 
273
 
273
 
274
        /* 
274
        /* 
275
         * create the create table and insert sql scripts by looping through
275
         * create the create table and insert sql scripts by looping through
276
         * the columns of df
276
         * the columns of df
277
         */
277
         */
278
        types = (int *)R_alloc(ncols, sizeof(int));
278
        types = (int *)R_alloc(ncols, sizeof(int));
279
        sql_len = sprintf(g_sql_buf[0], "create table [%s].sdf_data ([row name] text", iname);
279
        sql_len = sprintf(g_sql_buf[0], "create table [%s].sdf_data ([row name] text", iname);
280
        sql_len2 = sprintf(g_sql_buf[1], "insert into [%s].sdf_data values(?", iname);
280
        sql_len2 = sprintf(g_sql_buf[1], "insert into [%s].sdf_data values(?", iname);
281
 
281
 
282
        for (i = 0; i < ncols; i++) {
282
        for (i = 0; i < ncols; i++) {
283
            col_name = CHAR(STRING_ELT(names, i));
283
            col_name = CHAR(STRING_ELT(names, i));
284
 
284
 
285
            /* add column definition to the create table sql */
285
            /* add column definition to the create table sql */
286
            _expand_buf(0, sql_len+strlen(col_name)+10);
286
            _expand_buf(0, sql_len+strlen(col_name)+10);
287
 
287
 
288
            variable = _getListElement(df, col_name);
288
            variable = _getListElement(df, col_name);
289
            var_class = GET_CLASS(variable);
289
            var_class = GET_CLASS(variable);
290
            class = (var_class == R_NilValue) ? NULL : CHAR(STRING_ELT(var_class, 0));
290
            class = (var_class == R_NilValue) ? NULL : CHAR(STRING_ELT(var_class, 0));
291
            type = TYPEOF(variable);
291
            type = TYPEOF(variable);
292
            types[i] = type;
292
            types[i] = type;
293
 
293
 
294
            sql_len += sprintf(g_sql_buf[0]+sql_len, ", [%s] %s", col_name, 
294
            sql_len += sprintf(g_sql_buf[0]+sql_len, ", [%s] %s", col_name, 
295
                    _get_column_type(class, type));
295
                    _get_column_type(class, type));
296
 
296
 
297
            /* add handler to insert table sql */
297
            /* add handler to insert table sql */
298
            _expand_buf(1, sql_len+5);
298
            _expand_buf(1, sql_len+5);
299
            strcpy(g_sql_buf[1]+sql_len2, ",?");
299
            strcpy(g_sql_buf[1]+sql_len2, ",?");
300
            sql_len2 += 2; 
300
            sql_len2 += 2; 
301
 
301
 
302
            /* create separate table for factors decode */
302
            /* create separate table for factors decode */
303
            if (class != NULL && (strcmp(class, "factor") == 0 || strcmp(class, "ordered") == 0)){
303
            if (class != NULL && (strcmp(class, "factor") == 0 || strcmp(class, "ordered") == 0)){
304
                if (_create_factor_table2(iname, class, col_name)) 
304
                if (_create_factor_table2(iname, class, col_name)) 
305
                    return R_NilValue; /* dup tbl name? */
305
                    return R_NilValue; /* dup tbl name? */
306
 
306
 
307
                _sqlite_exec("begin");
307
                _sqlite_exec("begin");
308
                levels = GET_LEVELS(variable);
308
                levels = GET_LEVELS(variable);
309
                sprintf(g_sql_buf[2], "insert into [%s].[%s %s] values(?, ?);",
309
                sprintf(g_sql_buf[2], "insert into [%s].[%s %s] values(?, ?);",
310
                        iname, class, col_name);
310
                        iname, class, col_name);
311
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
311
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
312
                if (_sqlite_error(res)) return R_NilValue; /* dup tbl name? */
312
                if (_sqlite_error(res)) return R_NilValue; /* dup tbl name? */
313
 
313
 
314
                for (j = 0; j < GET_LENGTH(levels); j++) {
314
                for (j = 0; j < GET_LENGTH(levels); j++) {
315
                    sqlite3_reset(stmt);
315
                    sqlite3_reset(stmt);
316
                    factor = CHAR(STRING_ELT(levels, j));
316
                    factor = CHAR(STRING_ELT(levels, j));
317
                    sqlite3_bind_int(stmt, 1, j+1);
317
                    sqlite3_bind_int(stmt, 1, j+1);
318
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
318
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
319
                    sqlite3_step(stmt);
319
                    sqlite3_step(stmt);
320
                }
320
                }
321
                sqlite3_finalize(stmt);
321
                sqlite3_finalize(stmt);
322
                _sqlite_exec("commit");
322
                _sqlite_exec("commit");
323
            }
323
            }
324
        }
324
        }
325
        
325
        
326
        _expand_buf(0,sql_len+35);
326
        _expand_buf(0,sql_len+35);
327
        sql_len += sprintf(g_sql_buf[0]+sql_len, ", primary key([row name]));");
327
        sql_len += sprintf(g_sql_buf[0]+sql_len, ", primary key([row name]));");
328
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ");"); */
328
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ");"); */
329
        res = _sqlite_exec(g_sql_buf[0]);
329
        res = _sqlite_exec(g_sql_buf[0]);
330
        if (_sqlite_error(res)) return R_NilValue; /* why? */
330
        if (_sqlite_error(res)) return R_NilValue; /* why? */
331
 
331
 
332
        /*
332
        /*
333
         * add the data in df to the sdf
333
         * add the data in df to the sdf
334
         */
334
         */
335
        rownames = getAttrib(df, R_RowNamesSymbol);
335
        rownames = getAttrib(df, R_RowNamesSymbol);
336
        nrows = GET_LENGTH(rownames);
336
        nrows = GET_LENGTH(rownames);
337
 
337
 
338
        _sqlite_error(_sqlite_exec("begin"));
338
        _sqlite_error(_sqlite_exec("begin"));
339
        sprintf(g_sql_buf[1]+sql_len2, ")");
339
        sprintf(g_sql_buf[1]+sql_len2, ")");
340
        res = sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
340
        res = sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
341
 
341
 
342
        for (i = 0; i < nrows; i++) {
342
        for (i = 0; i < nrows; i++) {
343
            sqlite3_reset(stmt);
343
            sqlite3_reset(stmt);
344
 
344
 
345
            /* since this is coming from a real dataframe, we're assured that
345
            /* since this is coming from a real dataframe, we're assured that
346
             * the rownames are unique */
346
             * the rownames are unique */
347
            if (IS_CHARACTER(rownames)) {
347
            if (IS_CHARACTER(rownames)) {
348
                row_name = CHAR(STRING_ELT(rownames, i));
348
                row_name = CHAR(STRING_ELT(rownames, i));
349
                if (*row_name) /* if not empty string */
349
                if (*row_name) /* if not empty string */
350
                    sqlite3_bind_text(stmt, 1, row_name, strlen(row_name), SQLITE_STATIC);
350
                    sqlite3_bind_text(stmt, 1, row_name, strlen(row_name), SQLITE_STATIC);
351
                else sqlite3_bind_int(stmt, 1, i);
351
                else sqlite3_bind_int(stmt, 1, i);
352
            } else if (IS_INTEGER(rownames)) {
352
            } else if (IS_INTEGER(rownames)) {
353
                sqlite3_bind_int(stmt, 1, INTEGER(rownames)[i]);
353
                sqlite3_bind_int(stmt, 1, INTEGER(rownames)[i]);
354
            } else sqlite3_bind_int(stmt, 1, i);
354
            } else sqlite3_bind_int(stmt, 1, i);
355
 
355
 
356
            for (j = 0; j < ncols; j++) {
356
            for (j = 0; j < ncols; j++) {
357
                variable = VECTOR_ELT(df, j);
357
                variable = VECTOR_ELT(df, j);
358
                switch(types[j]) {
358
                switch(types[j]) {
359
                    case INTSXP : 
359
                    case INTSXP : 
360
                        sqlite3_bind_int(stmt, j+2, INTEGER(variable)[i]);
360
                        sqlite3_bind_int(stmt, j+2, INTEGER(variable)[i]);
361
                        break;
361
                        break;
362
                    case LGLSXP : 
362
                    case LGLSXP : 
363
                        /* they might make it a bit although currentLY 
363
                        /* they might make it a bit although currentLY 
364
                         * LOGICAL==INTEGER macro */
364
                         * LOGICAL==INTEGER macro */
365
                        sqlite3_bind_int(stmt, j+2, LOGICAL(variable)[i]);
365
                        sqlite3_bind_int(stmt, j+2, LOGICAL(variable)[i]);
366
                        break;
366
                        break;
367
                    case REALSXP:
367
                    case REALSXP:
368
                        sqlite3_bind_double(stmt, j+2, REAL(variable)[i]);
368
                        sqlite3_bind_double(stmt, j+2, REAL(variable)[i]);
369
                        break;
369
                        break;
370
                    case CHARSXP:
370
                    case CHARSXP:
371
                    case STRSXP:
371
                    case STRSXP:
372
                        sqlite3_bind_text(stmt, j+2, CHAR_ELT(variable, i), -1, SQLITE_STATIC);
372
                        sqlite3_bind_text(stmt, j+2, CHAR_ELT(variable, i), -1, SQLITE_STATIC);
373
                }
373
                }
374
                /* TODO: handle NA's & NULL's */
374
                /* TODO: handle NA's & NULL's */
375
            }
375
            }
376
 
376
 
377
            res = sqlite3_step(stmt);
377
            res = sqlite3_step(stmt);
378
            if (res != SQLITE_DONE) { 
378
            if (res != SQLITE_DONE) { 
379
                _sqlite_exec("ROLLBACK");
379
                _sqlite_exec("ROLLBACK");
380
                sqlite3_finalize(stmt);
380
                sqlite3_finalize(stmt);
381
                Rprintf("SQLITE ERROR: %s\n", sqlite3_errmsg(g_workspace));
381
                Rprintf("SQLITE ERROR: %s\n", sqlite3_errmsg(g_workspace));
382
                return R_NilValue; /* why? */
382
                return R_NilValue; /* why? */
383
            }
383
            }
384
        }
384
        }
385
                
385
                
386
        sqlite3_finalize(stmt);
386
        sqlite3_finalize(stmt);
387
        _sqlite_error(_sqlite_exec("COMMIT"));
387
        _sqlite_error(_sqlite_exec("COMMIT"));
388
 
388
 
389
        /* create a new object representing the sdf */
389
        /* create a new object representing the sdf */
390
        ret = _create_sdf_sexp(iname);
390
        ret = _create_sdf_sexp(iname);
391
 
391
 
392
    } else {
392
    } else {
393
        Rprintf("ERROR: unable to create a sqlite data frame.\n");
393
        Rprintf("ERROR: unable to create a sqlite data frame.\n");
394
        ret = R_NilValue;
394
        ret = R_NilValue;
395
    }
395
    }
396
        
396
        
397
    return ret;
397
    return ret;
398
}
398
}
399
 
399
 
400
SEXP sdf_get_names(SEXP sdf) {
400
SEXP sdf_get_names(SEXP sdf) {
401
    const char *iname;
401
    const char *iname;
402
    iname = SDF_INAME(sdf);
402
    iname = SDF_INAME(sdf);
403
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
403
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
404
 
404
 
405
    int len;
405
    int len;
406
    len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
406
    len = sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
407
 
407
 
408
    sqlite3_stmt *stmt;
408
    sqlite3_stmt *stmt;
409
    int res, i;
409
    int res, i;
410
   
410
   
411
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
411
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], len, &stmt, NULL);
412
    if (_sqlite_error(res)) return R_NilValue;
412
    if (_sqlite_error(res)) return R_NilValue;
413
 
413
 
414
    SEXP ret;
414
    SEXP ret;
415
 
415
 
416
    len = sqlite3_column_count(stmt)-1;
416
    len = sqlite3_column_count(stmt)-1;
417
    PROTECT(ret = NEW_CHARACTER(len));
417
    PROTECT(ret = NEW_CHARACTER(len));
418
 
418
 
419
    for (i = 0; i < len; i++) {
419
    for (i = 0; i < len; i++) {
420
        SET_STRING_ELT(ret, i, mkChar(sqlite3_column_name(stmt, i+1)));
420
        SET_STRING_ELT(ret, i, mkChar(sqlite3_column_name(stmt, i+1)));
421
    }
421
    }
422
 
422
 
423
    sqlite3_finalize(stmt);
423
    sqlite3_finalize(stmt);
424
    UNPROTECT(1);
424
    UNPROTECT(1);
425
    return ret;
425
    return ret;
426
}
426
}
427
 
427
 
428
SEXP sdf_get_length(SEXP sdf) {
428
SEXP sdf_get_length(SEXP sdf) {
429
    const char *iname;
429
    const char *iname;
430
    int len;
430
    int len;
431
    sqlite3_stmt *stmt;
431
    sqlite3_stmt *stmt;
432
    int res;
432
    int res;
433
 
433
 
434
    iname  = SDF_INAME(sdf); 
434
    iname  = SDF_INAME(sdf); 
435
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
435
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data;", iname);
436
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
436
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
437
 
437
 
438
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
438
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
439
    if (_sqlite_error(res)) return R_NilValue;
439
    if (_sqlite_error(res)) return R_NilValue;
440
 
440
 
441
    len = sqlite3_column_count(stmt)-1;
441
    len = sqlite3_column_count(stmt)-1;
442
    sqlite3_finalize(stmt);
442
    sqlite3_finalize(stmt);
443
    return ScalarInteger(len);
443
    return ScalarInteger(len);
444
}
444
}
445
 
445
 
446
/* get row count */
446
/* get row count */
447
SEXP sdf_get_row_count(SEXP sdf) {
447
SEXP sdf_get_row_count(SEXP sdf) {
448
    const char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
448
    const char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
449
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
449
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
450
 
450
 
451
    int nrow;
451
    int nrow;
452
    SEXP ret;
452
    SEXP ret;
453
   
453
   
454
    nrow = _get_row_count2(iname, TRUE);
454
    nrow = _get_row_count2(iname, TRUE);
455
 
455
 
456
    if (nrow < 1) ret = R_NilValue;
456
    if (nrow < 1) ret = R_NilValue;
457
    else ret = ScalarInteger(nrow);
457
    else ret = ScalarInteger(nrow);
458
 
458
 
459
    return ret;
459
    return ret;
460
}
460
}
461
 
461
 
462
    
462
    
463
SEXP sdf_import_table(SEXP _filename, SEXP _name, SEXP _sep, SEXP _quote, 
463
SEXP sdf_import_table(SEXP _filename, SEXP _name, SEXP _sep, SEXP _quote, 
464
        SEXP _rownames, SEXP _colnames) {
464
        SEXP _rownames, SEXP _colnames) {
465
    const char *filename = CHAR_ELT(_filename, 0);
465
    const char *filename = CHAR_ELT(_filename, 0);
466
    FILE *f = fopen(filename, "r");
466
    FILE *f = fopen(filename, "r");
467
 
467
 
468
    if (f == NULL) {
468
    if (f == NULL) {
469
        Rprintf("Error: File %s does not exist.", filename);
469
        Rprintf("Error: File %s does not exist.", filename);
470
        return R_NilValue;
470
        return R_NilValue;
471
    }
471
    }
472
 
472
 
473
    /*char *sep = CHAR_ELT(_sep, 0), *quote = CHAR_ELT(_quote,0);
473
    /*char *sep = CHAR_ELT(_sep, 0), *quote = CHAR_ELT(_quote,0);
474
    char *name = CHAR_ELT(_name, 0);*/
474
    char *name = CHAR_ELT(_name, 0);*/
475
 
475
 
476
    /* create the table */
476
    /* create the table */
477
    /* insert the stuffs */
477
    /* insert the stuffs */
478
    /* register to workspace */
478
    /* register to workspace */
479
 
479
 
480
    return R_NilValue;
480
    return R_NilValue;
481
}
481
}
482
 
482
 
483
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col, SEXP new_sdf) {
483
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col, SEXP new_sdf) {
484
    SEXP ret = R_NilValue;
484
    SEXP ret = R_NilValue;
485
    const char *iname = SDF_INAME(sdf);
485
    const char *iname = SDF_INAME(sdf);
486
    sqlite3_stmt *stmt;
486
    sqlite3_stmt *stmt;
487
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
487
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
488
    int i, j,res;
488
    int i, j,res;
489
    int *col_indices = NULL, col_index_len = 0, *dup_indices = NULL;
489
    int *col_indices = NULL, col_index_len = 0, *dup_indices = NULL;
490
    int row_index_len = 0, force_new_df = LOGICAL(new_sdf)[0];
490
    int row_index_len = 0, force_new_df = LOGICAL(new_sdf)[0];
491
    const char *colname;
491
    const char *colname;
492
 
492
 
493
    /* dup_indices is used to handle when same column chosen more than once.
493
    /* dup_indices is used to handle when same column chosen more than once.
494
     * e.g. iris[,c(1,1)] have names Sepal.Length, Sepal.Length.1 *
494
     * e.g. iris[,c(1,1)] have names Sepal.Length, Sepal.Length.1 *
495
     * col_indices is used to store the column index of selected columns
495
     * col_indices is used to store the column index of selected columns
496
     * in the sdf_data table */
496
     * in the sdf_data table */
497
 
497
 
498
    if (!USE_SDF1(iname, TRUE, TRUE)) return R_NilValue;
498
    if (!USE_SDF1(iname, TRUE, TRUE)) return R_NilValue;
499
 
499
 
500
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
500
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
501
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
501
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
502
    if (_sqlite_error(res)) return R_NilValue;
502
    if (_sqlite_error(res)) return R_NilValue;
503
 
503
 
504
    buflen = sprintf(g_sql_buf[0], "select [row name]");
504
    buflen = sprintf(g_sql_buf[0], "select [row name]");
505
    idxlen = LENGTH(col);
505
    idxlen = LENGTH(col);
506
    col_cnt = sqlite3_column_count(stmt) - 1;
506
    col_cnt = sqlite3_column_count(stmt) - 1;
507
 
507
 
508
    /*
508
    /*
509
     * PROCESS COLUMN INDEX: set columns in the select statement
509
     * PROCESS COLUMN INDEX: set columns in the select statement
510
     */
510
     */
511
    if (col == R_NilValue) {
511
    if (col == R_NilValue) {
512
        for (i = 1; i < col_cnt+1; i++) {
512
        for (i = 1; i < col_cnt+1; i++) {
513
            colname = sqlite3_column_name(stmt, i);
513
            colname = sqlite3_column_name(stmt, i);
514
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
514
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
515
            _expand_buf(0, buflen + 100);
515
            _expand_buf(0, buflen + 100);
516
        }
516
        }
517
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
517
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
518
        col_index_len = col_cnt;
518
        col_index_len = col_cnt;
519
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
519
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
520
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
520
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
521
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
521
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
522
    } else if (col == R_NilValue || idxlen < 1) {
522
    } else if (col == R_NilValue || idxlen < 1) {
523
        sqlite3_finalize(stmt);
523
        sqlite3_finalize(stmt);
524
        return R_NilValue;
524
        return R_NilValue;
525
    } else if (IS_NUMERIC(col)) {
525
    } else if (IS_NUMERIC(col)) {
526
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
526
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
527
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
527
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
528
        col_index_len = 0;
528
        col_index_len = 0;
529
 
529
 
530
        for (i = 0; i < idxlen; i++) {
530
        for (i = 0; i < idxlen; i++) {
531
            /* no need to correct for 0 base because col 0 is [row name] */
531
            /* no need to correct for 0 base because col 0 is [row name] */
532
            index = ((int) REAL(col)[i]); 
532
            index = ((int) REAL(col)[i]); 
533
            if (index > col_cnt) {
533
            if (index > col_cnt) {
534
                sqlite3_finalize(stmt);
534
                sqlite3_finalize(stmt);
535
                error("undefined columns selected\n");
535
                error("undefined columns selected\n");
536
            } else if (index > 0) {
536
            } else if (index > 0) {
537
                colname = sqlite3_column_name(stmt,index);
537
                colname = sqlite3_column_name(stmt,index);
538
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
538
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
539
                _expand_buf(0, buflen + 100);
539
                _expand_buf(0, buflen + 100);
540
                dup_indices[col_index_len] = 0;
540
                dup_indices[col_index_len] = 0;
541
                for (j = 0; j < col_index_len; j++) {
541
                for (j = 0; j < col_index_len; j++) {
542
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
542
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
543
                }
543
                }
544
                col_indices[col_index_len++] = index;
544
                col_indices[col_index_len++] = index;
545
            } else if (index < 0) {
545
            } else if (index < 0) {
546
                sqlite3_finalize(stmt);
546
                sqlite3_finalize(stmt);
547
                error("negative indices not supported.\n");
547
                error("negative indices not supported.\n");
548
            }
548
            }
549
        }
549
        }
550
 
550
 
551
        if (col_index_len == 0) {
551
        if (col_index_len == 0) {
552
            sqlite3_finalize(stmt);
552
            sqlite3_finalize(stmt);
553
            Rprintf("Error: no indices detected??\n");
553
            Rprintf("Error: no indices detected??\n");
554
            return R_NilValue;
554
            return R_NilValue;
555
        } else { 
555
        } else { 
556
            _expand_buf(0, buflen+20+strlen(iname));
556
            _expand_buf(0, buflen+20+strlen(iname));
557
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
557
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
558
        }
558
        }
559
    } else if (IS_INTEGER(col)) {
559
    } else if (IS_INTEGER(col)) {
560
        /* identical logic with IS_NUMERIC, except that we don't have to cast
560
        /* identical logic with IS_NUMERIC, except that we don't have to cast
561
         * stuffs from SEXP col. the alternative is doing if idxlen times. */
561
         * stuffs from SEXP col. the alternative is doing if idxlen times. */
562
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
562
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
563
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
563
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
564
        col_index_len = 0;
564
        col_index_len = 0;
565
 
565
 
566
        for (i = 0; i < idxlen; i++) {
566
        for (i = 0; i < idxlen; i++) {
567
            /* no need to correct for 0 base because col 0 is [row name] */
567
            /* no need to correct for 0 base because col 0 is [row name] */
568
            index = INTEGER(col)[i];
568
            index = INTEGER(col)[i];
569
            if (index > col_cnt) {
569
            if (index > col_cnt) {
570
                sqlite3_finalize(stmt);
570
                sqlite3_finalize(stmt);
571
                error("undefined columns selected\n");
571
                error("undefined columns selected\n");
572
            } else if (index > 0) {
572
            } else if (index > 0) {
573
                colname = sqlite3_column_name(stmt,index);
573
                colname = sqlite3_column_name(stmt,index);
574
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
574
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", colname);
575
                _expand_buf(0, buflen + 100);
575
                _expand_buf(0, buflen + 100);
576
                dup_indices[col_index_len] = 0;
576
                dup_indices[col_index_len] = 0;
577
                for (j = 0; j < col_index_len; j++) {
577
                for (j = 0; j < col_index_len; j++) {
578
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
578
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
579
                }
579
                }
580
                col_indices[col_index_len++] = index;
580
                col_indices[col_index_len++] = index;
581
            } else if (index < 0) {
581
            } else if (index < 0) {
582
                sqlite3_finalize(stmt);
582
                sqlite3_finalize(stmt);
583
                error("negative indices not supported.\n");
583
                error("negative indices not supported.\n");
584
            }
584
            }
585
        }
585
        }
586
 
586
 
587
        if (col_index_len == 0) {
587
        if (col_index_len == 0) {
588
            sqlite3_finalize(stmt);
588
            sqlite3_finalize(stmt);
589
            error("no valid indices detected");
589
            error("no valid indices detected");
590
        } else { 
590
        } else { 
591
            /*Rprintf("debug: col_index_len: %d, idxlen: %d, buf 0: %s\n", col_index_len, idxlen, g_sql_buf[0]);*/
591
            /*Rprintf("debug: col_index_len: %d, idxlen: %d, buf 0: %s\n", col_index_len, idxlen, g_sql_buf[0]);*/
592
            _expand_buf(0, buflen+20+strlen(iname));
592
            _expand_buf(0, buflen+20+strlen(iname));
593
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
593
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
594
        }
594
        }
595
    } else if (IS_LOGICAL(col)) {
595
    } else if (IS_LOGICAL(col)) {
596
        /* recycling stuff, so max column output is the # of cols in the df */
596
        /* recycling stuff, so max column output is the # of cols in the df */
597
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
597
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
598
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
598
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
599
        col_index_len = 0;
599
        col_index_len = 0;
600
        for (i = 0; i < col_cnt; i++) {
600
        for (i = 0; i < col_cnt; i++) {
601
            if (LOGICAL(col)[i%idxlen]) {
601
            if (LOGICAL(col)[i%idxlen]) {
602
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
602
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
603
                        sqlite3_column_name(stmt,i+1));
603
                        sqlite3_column_name(stmt,i+1));
604
 
604
 
605
                dup_indices[col_index_len] = 0;
605
                dup_indices[col_index_len] = 0;
606
                col_indices[col_index_len++] = i;
606
                col_indices[col_index_len++] = i;
607
            }
607
            }
608
 
608
 
609
        }
609
        }
610
 
610
 
611
        if (col_index_len == 0) {
611
        if (col_index_len == 0) {
612
            sqlite3_finalize(stmt);
612
            sqlite3_finalize(stmt);
613
            warning("no column selected.\n");
613
            warning("no column selected.\n");
614
            return R_NilValue;
614
            return R_NilValue;
615
        } else { 
615
        } else { 
616
            _expand_buf(0, buflen+20+strlen(iname));
616
            _expand_buf(0, buflen+20+strlen(iname));
617
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
617
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
618
        }
618
        }
619
    } else {
619
    } else {
620
        sqlite3_finalize(stmt);
620
        sqlite3_finalize(stmt);
621
        error("don't know how to handle column index.\n");
621
        error("don't know how to handle column index.\n");
622
    }
622
    }
623
 
623
 
624
    /* 
624
    /* 
625
     * PROCESS ROW INDEX: setup limit or where clause
625
     * PROCESS ROW INDEX: setup limit or where clause
626
     */
626
     */
627
    idxlen = LENGTH(row);
627
    idxlen = LENGTH(row);
628
    if (row == R_NilValue) {
628
    if (row == R_NilValue) {
629
        if (col_index_len == 1 && !force_new_df) {
629
        if (col_index_len == 1 && !force_new_df) {
630
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
630
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
631
            sqlite3_finalize(stmt);
631
            sqlite3_finalize(stmt);
632
            return ret;
632
            return ret;
633
        } else if (col_index_len > 1 || (force_new_df && col_index_len == 1)) {
633
        } else if (col_index_len > 1 || (force_new_df && col_index_len == 1)) {
634
            /* create a new SDF, logic similar to sdf_create_sdf */
634
            /* create a new SDF, logic similar to sdf_create_sdf */
635
 
635
 
636
            /* find a new name. data<n> ? */
636
            /* find a new name. data<n> ? */
637
            char *iname2;
637
            char *iname2;
638
            int namelen, sql_len, sql_len2;
638
            int namelen, sql_len, sql_len2;
639
 
639
 
640
            iname2 = _create_sdf_skeleton1(R_NilValue, &namelen, TRUE);
640
            iname2 = _create_sdf_skeleton1(R_NilValue, &namelen, TRUE);
641
 
641
 
642
            /* create sdf_data table */
642
            /* create sdf_data table */
643
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
643
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
644
            sql_len2 = 0;
644
            sql_len2 = 0;
645
 
645
 
646
            for (i = 0; i < col_index_len; i++) {
646
            for (i = 0; i < col_index_len; i++) {
647
                colname = sqlite3_column_name(stmt, col_indices[i]);
647
                colname = sqlite3_column_name(stmt, col_indices[i]);
648
                if (dup_indices[i] == 0) {
648
                if (dup_indices[i] == 0) {
649
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s] %s", colname,
649
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s] %s", colname,
650
                            sqlite3_column_decltype(stmt, col_indices[i]));
650
                            sqlite3_column_decltype(stmt, col_indices[i]));
651
                } else {
651
                } else {
652
                    /* un-duplicate col names by appending num, just like R */
652
                    /* un-duplicate col names by appending num, just like R */
653
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
653
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
654
                            dup_indices[i], 
654
                            dup_indices[i], 
655
                            sqlite3_column_decltype(stmt, col_indices[i]));
655
                            sqlite3_column_decltype(stmt, col_indices[i]));
656
                }
656
                }
657
                _expand_buf(1, sql_len + 100);
657
                _expand_buf(1, sql_len + 100);
658
 
658
 
659
                /* deal with possibly factor columns */
659
                /* deal with possibly factor columns */
660
                if (_is_factor2(iname, "factor", colname)) {
660
                if (_is_factor2(iname, "factor", colname)) {
661
                    /* found a factor column */
661
                    /* found a factor column */
662
 
662
 
663
                    if (dup_indices[i] == 0)  {
663
                    if (dup_indices[i] == 0)  {
664
                        _copy_factor_levels2("factor", iname, colname, iname2, colname);
664
                        _copy_factor_levels2("factor", iname, colname, iname2, colname);
665
                    } else {
665
                    } else {
666
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
666
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
667
                        _copy_factor_levels2("factor", iname, colname, iname2, 
667
                        _copy_factor_levels2("factor", iname, colname, iname2, 
668
                                g_sql_buf[3]);
668
                                g_sql_buf[3]);
669
                    }
669
                    }
670
                }
670
                }
671
 
671
 
672
                /* and deal with ordered factors too... life is hard, then you die */
672
                /* and deal with ordered factors too... life is hard, then you die */
673
                if (_is_factor2(iname, "ordered", colname)) {
673
                if (_is_factor2(iname, "ordered", colname)) {
674
                    
674
                    
675
                    if (dup_indices[i] == 0)  {
675
                    if (dup_indices[i] == 0)  {
676
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
676
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
677
                                colname);
677
                                colname);
678
                    } else {
678
                    } else {
679
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
679
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
680
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
680
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
681
                                g_sql_buf[3]);
681
                                g_sql_buf[3]);
682
                    }
682
                    }
683
                }
683
                }
684
                                
684
                                
685
            }
685
            }
686
 
686
 
687
            /* don't need it anymore */
687
            /* don't need it anymore */
688
            sqlite3_finalize(stmt);
688
            sqlite3_finalize(stmt);
689
 
689
 
690
            /* no table index created, that's for v2 I hope*/
690
            /* no table index created, that's for v2 I hope*/
691
            sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
691
            sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
692
            res = _sqlite_exec(g_sql_buf[1]);
692
            res = _sqlite_exec(g_sql_buf[1]);
693
            if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
693
            if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
694
 
694
 
695
            /* insert data (with row names). buf[0] contains a select */
695
            /* insert data (with row names). buf[0] contains a select */
696
            _expand_buf(1, g_sql_buf_sz[0] + 32);
696
            _expand_buf(1, g_sql_buf_sz[0] + 32);
697
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
697
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
698
                    g_sql_buf[0]);
698
                    g_sql_buf[0]);
699
            res = _sqlite_exec(g_sql_buf[1]);
699
            res = _sqlite_exec(g_sql_buf[1]);
700
            if (_sqlite_error(res)) return R_NilValue;
700
            if (_sqlite_error(res)) return R_NilValue;
701
 
701
 
702
            /* remove protection */
702
            /* remove protection */
703
            UNUSE_SDF2(iname2);
703
            UNUSE_SDF2(iname2);
704
 
704
 
705
            /* create SEXP for the SDF */
705
            /* create SEXP for the SDF */
706
            ret = _create_sdf_sexp(iname2);
706
            ret = _create_sdf_sexp(iname2);
707
            return ret;
707
            return ret;
708
        } else { sqlite3_finalize(stmt); return R_NilValue; }
708
        } else { sqlite3_finalize(stmt); return R_NilValue; }
709
    } else if (row == R_NilValue || idxlen < 1) {
709
    } else if (row == R_NilValue || idxlen < 1) {
710
        sqlite3_finalize(stmt);
710
        sqlite3_finalize(stmt);
711
        return R_NilValue;
711
        return R_NilValue;
712
    } else if (IS_NUMERIC(row)) {
712
    } else if (IS_NUMERIC(row)) {
713
        sqlite3_finalize(stmt);
713
        sqlite3_finalize(stmt);
714
 
714
 
715
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
715
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
716
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
716
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
717
 
717
 
718
        /* append " limit ?,1" to the formed select statement */
718
        /* append " limit ?,1" to the formed select statement */
719
        _expand_buf(0, buflen+10);
719
        _expand_buf(0, buflen+10);
720
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
720
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
721
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
721
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
722
 
722
 
723
        index = ((int) REAL(row)[0]) - 1;
723
        index = ((int) REAL(row)[0]) - 1;
724
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
724
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
725
 
725
 
726
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
726
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
727
        if (_sqlite_error(res)) return R_NilValue;
727
        if (_sqlite_error(res)) return R_NilValue;
728
 
728
 
729
        sqlite3_bind_int(stmt, 1, index);
729
        sqlite3_bind_int(stmt, 1, index);
730
        res = sqlite3_step(stmt);
730
        res = sqlite3_step(stmt);
731
 
731
 
732
        /* create data frame */
732
        /* create data frame */
733
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
733
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
734
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
734
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
735
 
735
 
736
        /* put data in it */
736
        /* put data in it */
737
        if (index >= 0 && index < row_cnt) {
737
        if (index >= 0 && index < row_cnt) {
738
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
738
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
739
        }
739
        }
740
 
740
 
741
        for (i = 1; i < idxlen; i++) {
741
        for (i = 1; i < idxlen; i++) {
742
            sqlite3_reset(stmt);
742
            sqlite3_reset(stmt);
743
            index = ((int) REAL(row)[i]) - 1;
743
            index = ((int) REAL(row)[i]) - 1;
744
            if (index >= 0 && index < row_cnt) {
744
            if (index >= 0 && index < row_cnt) {
745
                sqlite3_bind_int(stmt, 1, index);
745
                sqlite3_bind_int(stmt, 1, index);
746
                res = sqlite3_step(stmt);
746
                res = sqlite3_step(stmt);
747
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
747
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
748
            }
748
            }
749
        }
749
        }
750
        sqlite3_finalize(stmt);
750
        sqlite3_finalize(stmt);
751
 
751
 
752
        /* shrink vectors */
752
        /* shrink vectors */
753
        if (row_index_len < idxlen) {
753
        if (row_index_len < idxlen) {
754
            for (i = 0; i < col_index_len; i++) {
754
            for (i = 0; i < col_index_len; i++) {
755
                SET_VECTOR_ELT(ret, i, 
755
                SET_VECTOR_ELT(ret, i, 
756
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
756
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
757
            }
757
            }
758
        }
758
        }
759
 
759
 
760
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
760
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
761
    } else if (IS_INTEGER(row)) {
761
    } else if (IS_INTEGER(row)) {
762
        /* same stuff as with IS_INTEGER, except for setting of var index*/
762
        /* same stuff as with IS_INTEGER, except for setting of var index*/
763
        sqlite3_finalize(stmt);
763
        sqlite3_finalize(stmt);
764
 
764
 
765
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
765
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
766
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
766
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
767
 
767
 
768
        /* append " limit ?,1" to the formed select statement */
768
        /* append " limit ?,1" to the formed select statement */
769
        _expand_buf(0, buflen+10);
769
        _expand_buf(0, buflen+10);
770
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
770
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
771
 
771
 
772
        index = INTEGER(row)[0] - 1;
772
        index = INTEGER(row)[0] - 1;
773
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
773
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
774
 
774
 
775
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
775
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
776
        if (_sqlite_error(res)) return R_NilValue;
776
        if (_sqlite_error(res)) return R_NilValue;
777
 
777
 
778
        sqlite3_bind_int(stmt, 1, index);
778
        sqlite3_bind_int(stmt, 1, index);
779
        res = sqlite3_step(stmt);
779
        res = sqlite3_step(stmt);
780
 
780
 
781
        /* create data frame */
781
        /* create data frame */
782
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
782
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
783
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
783
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
784
 
784
 
785
        /* put data in it */
785
        /* put data in it */
786
        if (index >= 0 && index < row_cnt) {
786
        if (index >= 0 && index < row_cnt) {
787
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
787
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
788
        }
788
        }
789
 
789
 
790
        for (i = 1; i < idxlen; i++) {
790
        for (i = 1; i < idxlen; i++) {
791
            sqlite3_reset(stmt);
791
            sqlite3_reset(stmt);
792
            index = INTEGER(row)[i] - 1;
792
            index = INTEGER(row)[i] - 1;
793
            if (index >= 0 && index < row_cnt) {
793
            if (index >= 0 && index < row_cnt) {
794
                sqlite3_bind_int(stmt, 1, index);
794
                sqlite3_bind_int(stmt, 1, index);
795
                res = sqlite3_step(stmt); 
795
                res = sqlite3_step(stmt); 
796
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
796
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
797
            }
797
            }
798
        }
798
        }
799
        sqlite3_finalize(stmt);
799
        sqlite3_finalize(stmt);
800
 
800
 
801
        /* shrink vectors */
801
        /* shrink vectors */
802
        if (row_index_len < idxlen) {
802
        if (row_index_len < idxlen) {
803
            for (i = 0; i < col_index_len; i++) {
803
            for (i = 0; i < col_index_len; i++) {
804
                SET_VECTOR_ELT(ret, i, 
804
                SET_VECTOR_ELT(ret, i, 
805
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
805
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
806
            }
806
            }
807
        }
807
        }
808
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
808
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
809
    } else if (IS_LOGICAL(row)) {
809
    } else if (IS_LOGICAL(row)) {
810
        /* */
810
        /* */
811
        sqlite3_finalize(stmt);
811
        sqlite3_finalize(stmt);
812
        int est_row_cnt = 0;
812
        int est_row_cnt = 0;
813
 
813
 
814
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
814
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
815
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
815
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
816
 
816
 
817
        /* append " limit ?,1" to the formed select statement */
817
        /* append " limit ?,1" to the formed select statement */
818
        _expand_buf(0, buflen+10);
818
        _expand_buf(0, buflen+10);
819
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
819
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
820
 
820
 
821
        /* find if there is any TRUE element in the vector */
821
        /* find if there is any TRUE element in the vector */
822
        for (i = 0; i < idxlen && i < row_cnt; i++) {
822
        for (i = 0; i < idxlen && i < row_cnt; i++) {
823
            if (LOGICAL(row)[i]) break;
823
            if (LOGICAL(row)[i]) break;
824
        }
824
        }
825
 
825
 
826
        if (i < idxlen && i < row_cnt) {
826
        if (i < idxlen && i < row_cnt) {
827
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
827
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
828
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
828
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
829
 
829
 
830
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
830
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
831
            if (_sqlite_error(res)) return R_NilValue;
831
            if (_sqlite_error(res)) return R_NilValue;
832
 
832
 
833
            sqlite3_bind_int(stmt, 1, i);
833
            sqlite3_bind_int(stmt, 1, i);
834
            sqlite3_step(stmt);
834
            sqlite3_step(stmt);
835
            ret = _setup_df_sexp1(stmt, iname, col_index_len, est_row_cnt, dup_indices);
835
            ret = _setup_df_sexp1(stmt, iname, col_index_len, est_row_cnt, dup_indices);
836
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
836
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
837
            
837
            
838
            for (i++ ; i < row_cnt; i++) {
838
            for (i++ ; i < row_cnt; i++) {
839
                if (LOGICAL(row)[i%idxlen]) {
839
                if (LOGICAL(row)[i%idxlen]) {
840
                    sqlite3_reset(stmt);
840
                    sqlite3_reset(stmt);
841
                    sqlite3_bind_int(stmt, 1, i);
841
                    sqlite3_bind_int(stmt, 1, i);
842
                    sqlite3_step(stmt);
842
                    sqlite3_step(stmt);
843
                    _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
843
                    _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
844
                }
844
                }
845
            }
845
            }
846
        }
846
        }
847
 
847
 
848
        sqlite3_finalize(stmt);
848
        sqlite3_finalize(stmt);
849
 
849
 
850
        /* shrink vectors */
850
        /* shrink vectors */
851
        if (est_row_cnt > 0 && row_index_len < est_row_cnt) {
851
        if (est_row_cnt > 0 && row_index_len < est_row_cnt) {
852
            for (i = 0; i < col_index_len; i++) {
852
            for (i = 0; i < col_index_len; i++) {
853
                SET_VECTOR_ELT(ret, i, 
853
                SET_VECTOR_ELT(ret, i, 
854
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
854
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
855
            }
855
            }
856
        }
856
        }
857
 
857
 
858
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
858
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
859
    }
859
    }
860
 
860
 
861
    UNUSE_SDF2(iname);
861
    UNUSE_SDF2(iname);
862
 
862
 
863
    if (ret != R_NilValue && col_index_len > 1) {
863
    if (ret != R_NilValue && col_index_len > 1) {
864
        SET_CLASS(ret, mkString("data.frame"));
864
        SET_CLASS(ret, mkString("data.frame"));
865
        _set_rownames2(ret);
865
        _set_rownames2(ret);
866
    } else if (ret != R_NilValue && col_index_len == 1) {
866
    } else if (ret != R_NilValue && col_index_len == 1) {
867
        ret = VECTOR_ELT(ret, 0);
867
        ret = VECTOR_ELT(ret, 0);
868
    }
868
    }
869
 
869
 
870
    return ret;
870
    return ret;
871
}
871
}
872
 
872
 
873
SEXP sdf_rbind(SEXP sdf, SEXP data) {
873
SEXP sdf_rbind(SEXP sdf, SEXP data) {
874
    const char *iname = SDF_INAME(sdf), *class, *colname;
874
    const char *iname = SDF_INAME(sdf), *class, *colname;
875
    SEXP ret = R_NilValue, col, names, levels, rownames;
875
    SEXP ret = R_NilValue, col, names, levels, rownames;
876
    int ncols, buflen, buflen2, i, j, res, nrows, *types, rownames_type;
876
    int ncols, buflen, buflen2, i, j, res, nrows, *types, rownames_type;
877
    sqlite3_stmt *stmt;
877
    sqlite3_stmt *stmt;
878
    const char *type, *rn_str; 
878
    const char *type, *rn_str; 
879
 
879
 
880
    class = CHAR_ELT(GET_CLASS(data), 0);
880
    class = CHAR_ELT(GET_CLASS(data), 0);
881
    if (strcmp(class,"data.frame") == 0) {
881
    if (strcmp(class,"data.frame") == 0) {
882
        /* check table names, types. variables may not be in same order, as
882
        /* check table names, types. variables may not be in same order, as
883
         * long as names and types are identical. */
883
         * long as names and types are identical. */
884
        names = GET_NAMES(data);
884
        names = GET_NAMES(data);
885
        ncols = GET_LENGTH(names);
885
        ncols = GET_LENGTH(names);
886
 
886
 
887
        buflen = sprintf(g_sql_buf[0], "select 1");
887
        buflen = sprintf(g_sql_buf[0], "select 1");
888
        for (i = 0; i < ncols; i++) {
888
        for (i = 0; i < ncols; i++) {
889
            _expand_buf(0, buflen + 100);
889
            _expand_buf(0, buflen + 100);
890
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
890
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
891
        }
891
        }
892
        _expand_buf(0, buflen + 100);
892
        _expand_buf(0, buflen + 100);
893
        sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
893
        sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
894
 
894
 
895
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
895
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
896
        if (res != SQLITE_OK) { /* column name mismatch */
896
        if (res != SQLITE_OK) { /* column name mismatch */
897
            _sqlite_error(res);
897
            _sqlite_error(res);
898
            Rprintf("Error: column names should exactly match the names of the SDF.\n");
898
            Rprintf("Error: column names should exactly match the names of the SDF.\n");
899
            return ret;
899
            return ret;
900
        }
900
        }
901
 
901
 
902
        /* now check the type */
902
        /* now check the type */
903
        types = (int *)R_alloc(ncols, sizeof(int));
903
        types = (int *)R_alloc(ncols, sizeof(int));
904
        for (i = 0; i< ncols; i++) {
904
        for (i = 0; i< ncols; i++) {
905
            type = sqlite3_column_decltype(stmt, i+1); /* +1 bec 1st col is "1" */
905
            type = sqlite3_column_decltype(stmt, i+1); /* +1 bec 1st col is "1" */
906
 
906
 
907
            col = VECTOR_ELT(data, i);
907
            col = VECTOR_ELT(data, i);
908
            /* class = CHAR_ELT(GET_CLASS(col),0); */
908
            /* class = CHAR_ELT(GET_CLASS(col),0); */
909
 
909
 
910
            if (strcmp(type, "double") == 0 && IS_NUMERIC(col)) types[i] = SQLITE_FLOAT;
910
            if (strcmp(type, "double") == 0 && IS_NUMERIC(col)) types[i] = SQLITE_FLOAT;
911
            else if (strcmp(type, "text") == 0 && IS_CHARACTER(col)) types[i] = SQLITE_TEXT;
911
            else if (strcmp(type, "text") == 0 && IS_CHARACTER(col)) types[i] = SQLITE_TEXT;
912
            else if (strcmp(type, "int") == 0) { 
912
            else if (strcmp(type, "int") == 0) { 
913
                types[i] = SQLITE_INTEGER;
913
                types[i] = SQLITE_INTEGER;
914
                if (isUnordered(col)) {  /* test if factor */
914
                if (isUnordered(col)) {  /* test if factor */
915
                    colname = CHAR_ELT(names, i);
915
                    colname = CHAR_ELT(names, i);
916
                    sqlite3_stmt *stmt2;
916
                    sqlite3_stmt *stmt2;
917
                    if (!_is_factor2(iname, "factor", colname)) break;
917
                    if (!_is_factor2(iname, "factor", colname)) break;
918
                    sprintf(g_sql_buf[1], "[%s].[factor %s]", iname, colname);
918
                    sprintf(g_sql_buf[1], "[%s].[factor %s]", iname, colname);
919
                    nrows = _get_row_count2(g_sql_buf[1], 0);
919
                    nrows = _get_row_count2(g_sql_buf[1], 0);
920
 
920
 
921
                    /* levels in sdf must be >= levels in df */
921
                    /* levels in sdf must be >= levels in df */
922
                    levels = GET_LEVELS(col);
922
                    levels = GET_LEVELS(col);
923
                    if (nrows < GET_LENGTH(levels)) {
923
                    if (nrows < GET_LENGTH(levels)) {
924
                        Rprintf("Error: The data frame variable %s has more levels than"
924
                        Rprintf("Error: The data frame variable %s has more levels than"
925
                                " its SDF counterpart.\n", colname);
925
                                " its SDF counterpart.\n", colname);
926
                        break;
926
                        break;
927
                    }
927
                    }
928
 
928
 
929
                    sprintf(g_sql_buf[2], "select label from %s order by level", g_sql_buf[1]);
929
                    sprintf(g_sql_buf[2], "select label from %s order by level", g_sql_buf[1]);
930
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
930
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
931
                    _sqlite_error(res);
931
                    _sqlite_error(res);
932
                    
932
                    
933
                    for (j = 0; j < GET_LENGTH(levels); j++)  {
933
                    for (j = 0; j < GET_LENGTH(levels); j++)  {
934
                        sqlite3_step(stmt2);
934
                        sqlite3_step(stmt2);
935
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) != 0) {
935
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) != 0) {
936
                            Rprintf("Error: Level label mismatch in variable %s, [%s] in data frame"
936
                            Rprintf("Error: Level label mismatch in variable %s, [%s] in data frame"
937
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
937
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
938
                                    CHAR_ELT(levels, j));
938
                                    CHAR_ELT(levels, j));
939
                            sqlite3_finalize(stmt2);
939
                            sqlite3_finalize(stmt2);
940
                            break;
940
                            break;
941
                        }
941
                        }
942
                    }
942
                    }
943
                    sqlite3_finalize(stmt2);
943
                    sqlite3_finalize(stmt2);
944
                } else if (isOrdered(col)) { /* same with ordered */
944
                } else if (isOrdered(col)) { /* same with ordered */
945
                    colname = CHAR_ELT(names, i);
945
                    colname = CHAR_ELT(names, i);
946
                    sqlite3_stmt *stmt2;
946
                    sqlite3_stmt *stmt2;
947
                    if (!_is_factor2(iname, "ordered", colname)) break;
947
                    if (!_is_factor2(iname, "ordered", colname)) break;
948
                    sprintf(g_sql_buf[1], "[%s].[ordered %s]", iname, colname);
948
                    sprintf(g_sql_buf[1], "[%s].[ordered %s]", iname, colname);
949
                    nrows = _get_row_count2(g_sql_buf[1], 0);
949
                    nrows = _get_row_count2(g_sql_buf[1], 0);
950
 
950
 
951
                    /* levels in sdf must be >= levels in df */
951
                    /* levels in sdf must be >= levels in df */
952
                    levels = GET_LEVELS(col);
952
                    levels = GET_LEVELS(col);
953
                    if (nrows < GET_LENGTH(levels)) {
953
                    if (nrows < GET_LENGTH(levels)) {
954
                        Rprintf("Error: The data frame variable %s has more levels than"
954
                        Rprintf("Error: The data frame variable %s has more levels than"
955
                                " its SDF counterpart.\n", colname);
955
                                " its SDF counterpart.\n", colname);
956
                        break;
956
                        break;
957
                    }
957
                    }
958
 
958
 
959
                    sprintf(g_sql_buf[2], "select label from %s order by level", g_sql_buf[1]);
959
                    sprintf(g_sql_buf[2], "select label from %s order by level", g_sql_buf[1]);
960
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
960
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
961
                    
961
                    
962
                    for (j = 0; j < GET_LENGTH(levels); j++)  {
962
                    for (j = 0; j < GET_LENGTH(levels); j++)  {
963
                        sqlite3_step(stmt2);
963
                        sqlite3_step(stmt2);
964
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) != 0) {
964
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) != 0) {
965
                            Rprintf("Error: Level label mismatch in variable %s, [%s] in data frame"
965
                            Rprintf("Error: Level label mismatch in variable %s, [%s] in data frame"
966
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
966
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
967
                                    CHAR_ELT(levels, j));
967
                                    CHAR_ELT(levels, j));
968
                            sqlite3_finalize(stmt2);
968
                            sqlite3_finalize(stmt2);
969
                            break;
969
                            break;
970
                        }
970
                        }
971
                    }
971
                    }
972
                    sqlite3_finalize(stmt2);
972
                    sqlite3_finalize(stmt2);
973
                }  /* else if class == "ordered" */
973
                }  /* else if class == "ordered" */
974
            } /* if integer */
974
            } /* if integer */
975
        } /* for loop for column type checking */
975
        } /* for loop for column type checking */
976
 
976
 
977
        sqlite3_finalize(stmt);
977
        sqlite3_finalize(stmt);
978
        if (i < ncols) {
978
        if (i < ncols) {
979
            Rprintf("Error: type mismatch at variable %s\n", CHAR_ELT(names, i));
979
            Rprintf("Error: type mismatch at variable %s\n", CHAR_ELT(names, i));
980
            return ret;
980
            return ret;
981
        }
981
        }
982
 
982
 
983
        /* create the insert statement */
983
        /* create the insert statement */
984
        buflen = sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name]", iname);
984
        buflen = sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name]", iname);
985
        buflen2 = sprintf(g_sql_buf[1], "values(?");
985
        buflen2 = sprintf(g_sql_buf[1], "values(?");
986
        for (i = 0; i < ncols; i++) {
986
        for (i = 0; i < ncols; i++) {
987
            _expand_buf(0, buflen+100);
987
            _expand_buf(0, buflen+100);
988
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
988
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
989
            _expand_buf(1, buflen2+5);
989
            _expand_buf(1, buflen2+5);
990
            buflen2 += sprintf(g_sql_buf[1]+buflen2, ",?");
990
            buflen2 += sprintf(g_sql_buf[1]+buflen2, ",?");
991
        }
991
        }
992
 
992
 
993
        _expand_buf(2, buflen + buflen2 + 10);
993
        _expand_buf(2, buflen + buflen2 + 10);
994
        sprintf(g_sql_buf[2], "%s) %s)", g_sql_buf[0], g_sql_buf[1]);
994
        sprintf(g_sql_buf[2], "%s) %s)", g_sql_buf[0], g_sql_buf[1]);
995
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
995
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
996
        if (_sqlite_error(res)) return ret;
996
        if (_sqlite_error(res)) return ret;
997
 
997
 
998
        /* finally, add rows */
998
        /* finally, add rows */
999
        rownames = getAttrib(data, R_RowNamesSymbol); /* GET_ROWNAMES(data); */
999
        rownames = getAttrib(data, R_RowNamesSymbol); /* GET_ROWNAMES(data); */
1000
        nrows = LENGTH(rownames);
1000
        nrows = LENGTH(rownames);
1001
        rownames_type = TYPEOF(rownames);
1001
        rownames_type = TYPEOF(rownames);
1002
 
1002
 
1003
        _sqlite_begin;
1003
        _sqlite_begin;
1004
        for (i = 0; i < nrows; i++) {
1004
        for (i = 0; i < nrows; i++) {
1005
            sqlite3_reset(stmt);
1005
            sqlite3_reset(stmt);
1006
            if (rownames_type == STRSXP) {
1006
            if (rownames_type == STRSXP) {
1007
                rn_str = CHAR_ELT(rownames, i);
1007
                rn_str = CHAR_ELT(rownames, i);
1008
            } else if (rownames_type == INTSXP) {
1008
            } else if (rownames_type == INTSXP) {
1009
                sprintf(g_sql_buf[1], "%d", INTEGER(rownames)[i]);
1009
                sprintf(g_sql_buf[1], "%d", INTEGER(rownames)[i]);
1010
                rn_str = g_sql_buf[1];
1010
                rn_str = g_sql_buf[1];
1011
            } else if (rownames_type == REALSXP) {
1011
            } else if (rownames_type == REALSXP) {
1012
                sprintf(g_sql_buf[1], "%f", REAL(rownames)[i]);
1012
                sprintf(g_sql_buf[1], "%f", REAL(rownames)[i]);
1013
                rn_str = g_sql_buf[1];
1013
                rn_str = g_sql_buf[1];
1014
            } else {
1014
            } else {
1015
                sprintf(g_sql_buf[1], "%d", i);
1015
                sprintf(g_sql_buf[1], "%d", i);
1016
                rn_str = g_sql_buf[1];
1016
                rn_str = g_sql_buf[1];
1017
            }
1017
            }
1018
            sqlite3_bind_text(stmt, 1, rn_str, -1, SQLITE_STATIC);
1018
            sqlite3_bind_text(stmt, 1, rn_str, -1, SQLITE_STATIC);
1019
 
1019
 
1020
            for (j = 0; j < ncols; j++) {
1020
            for (j = 0; j < ncols; j++) {
1021
                col = VECTOR_ELT(data, j);
1021
                col = VECTOR_ELT(data, j);
1022
                switch(types[j]) {
1022
                switch(types[j]) {
1023
                    case SQLITE_FLOAT:
1023
                    case SQLITE_FLOAT:
1024
                        sqlite3_bind_double(stmt, j+2, REAL(col)[i]); break;
1024
                        sqlite3_bind_double(stmt, j+2, REAL(col)[i]); break;
1025
                    case SQLITE_TEXT:
1025
                    case SQLITE_TEXT:
1026
                        sqlite3_bind_text(stmt, j+2, CHAR_ELT(col, i), -1, SQLITE_STATIC); break;
1026
                        sqlite3_bind_text(stmt, j+2, CHAR_ELT(col, i), -1, SQLITE_STATIC); break;
1027
                    case SQLITE_INTEGER:
1027
                    case SQLITE_INTEGER:
1028
                    default:  /* runtime error if we're wrong */
1028
                    default:  /* runtime error if we're wrong */
1029
                        sqlite3_bind_int(stmt, j+2, INTEGER(col)[i]); break;
1029
                        sqlite3_bind_int(stmt, j+2, INTEGER(col)[i]); break;
1030
                }
1030
                }
1031
            }
1031
            }
1032
 
1032
 
1033
            res = sqlite3_step(stmt);
1033
            res = sqlite3_step(stmt);
1034
            if (res != SQLITE_DONE) { /* row name pk conflict */
1034
            if (res != SQLITE_DONE) { /* row name pk conflict */
1035
                for (j = 1; res != SQLITE_DONE; j++) { /* _normally_, j shouldn't overflow */
1035
                for (j = 1; res != SQLITE_DONE; j++) { /* _normally_, j shouldn't overflow */
1036
                    sqlite3_reset(stmt);
1036
                    sqlite3_reset(stmt);
1037
                    sprintf(g_sql_buf[2], "%s-%d", rn_str, j);
1037
                    sprintf(g_sql_buf[2], "%s-%d", rn_str, j);
1038
                    sqlite3_bind_text(stmt, 1, g_sql_buf[2], -1, SQLITE_STATIC);
1038
                    sqlite3_bind_text(stmt, 1, g_sql_buf[2], -1, SQLITE_STATIC);
1039
                    res = sqlite3_step(stmt);
1039
                    res = sqlite3_step(stmt);
1040
                }
1040
                }
1041
            } 
1041
            } 
1042
        }
1042
        }
1043
 
1043
 
1044
        sqlite3_finalize(stmt);
1044
        sqlite3_finalize(stmt);
1045
        _sqlite_commit;
1045
        _sqlite_commit;
1046
        ret = sdf;
1046
        ret = sdf;
1047
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1047
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1048
    }
1048
    }
1049
 
1049
 
1050
    return ret;
1050
    return ret;
1051
}
1051
}
1052
 
1052
 
1053
 
1053
 
1054
SEXP sdf_get_iname(SEXP sdf) {
1054
SEXP sdf_get_iname(SEXP sdf) {
1055
    const char *iname;
1055
    const char *iname;
1056
    char *sql;
1056
    char *sql;
1057
    sqlite3_stmt *stmt;
1057
    sqlite3_stmt *stmt;
1058
    SEXP ret, names;
1058
    SEXP ret, names;
1059
 
1059
 
1060
    iname = SDF_INAME(sdf);
1060
    iname = SDF_INAME(sdf);
1061
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
1061
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
1062
 
1062
 
1063
    sql = "select internal_name, rel_filename from workspace where internal_name=?";
1063
    sql = "select internal_name, rel_filename from workspace where internal_name=?";
1064
    sqlite3_prepare(g_workspace, sql, -1, &stmt, NULL);
1064
    sqlite3_prepare(g_workspace, sql, -1, &stmt, NULL);
1065
    sqlite3_bind_text(stmt, 1, iname, -1, SQLITE_STATIC);
1065
    sqlite3_bind_text(stmt, 1, iname, -1, SQLITE_STATIC);
1066
    sqlite3_step(stmt);
1066
    sqlite3_step(stmt);
1067
 
1067
 
1068
    PROTECT(ret = NEW_CHARACTER(2));
1068
    PROTECT(ret = NEW_CHARACTER(2));
1069
    SET_STRING_ELT(ret, 0, mkChar((char *)sqlite3_column_text(stmt, 0)));
1069
    SET_STRING_ELT(ret, 0, mkChar((char *)sqlite3_column_text(stmt, 0)));
1070
    SET_STRING_ELT(ret, 1, mkChar((char *)sqlite3_column_text(stmt, 1)));
1070
    SET_STRING_ELT(ret, 1, mkChar((char *)sqlite3_column_text(stmt, 1)));
1071
 
1071
 
1072
    PROTECT(names = NEW_CHARACTER(2));
1072
    PROTECT(names = NEW_CHARACTER(2));
1073
    SET_STRING_ELT(names, 0, mkChar("Internal Name"));
1073
    SET_STRING_ELT(names, 0, mkChar("Internal Name"));
1074
    SET_STRING_ELT(names, 1, mkChar("File"));
1074
    SET_STRING_ELT(names, 1, mkChar("File"));
1075
 
1075
 
1076
    SET_NAMES(ret, names);
1076
    SET_NAMES(ret, names);
1077
 
1077
 
1078
    sqlite3_finalize(stmt);
1078
    sqlite3_finalize(stmt);
1079
    UNPROTECT(2);
1079
    UNPROTECT(2);
1080
 
1080
 
1081
    return ret;
1081
    return ret;
1082
}
1082
}
1083
 
1083
 
1084
SEXP sdf_select(SEXP sdf, SEXP select, SEXP where, SEXP limit, SEXP debug) {
1084
SEXP sdf_select(SEXP sdf, SEXP select, SEXP where, SEXP limit, SEXP debug) {
1085
    const char *iname, *tmp;
1085
    const char *iname, *tmp;
1086
    sqlite3_stmt *stmt;
1086
    sqlite3_stmt *stmt;
1087
    int buflen0 = 0, buflen1 = 0, len, nrows, res;
1087
    int buflen0 = 0, buflen1 = 0, len, nrows, res;
1088
    SEXP ret = NULL;
1088
    SEXP ret = NULL;
1089
    int dbg = LOGICAL(debug)[0];
1089
    int dbg = LOGICAL(debug)[0];
1090
 
1090
 
1091
    iname = SDF_INAME(sdf);
1091
    iname = SDF_INAME(sdf);
1092
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
1092
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
1093
 
1093
 
1094
    /* create the sql to be executed */
1094
    /* create the sql to be executed */
1095
 
1095
 
1096
    /* process select stmt */
1096
    /* process select stmt */
1097
    if (select == R_NilValue) {
1097
    if (select == R_NilValue) {
1098
        buflen0 = sprintf(g_sql_buf[0], "select * from [%s].sdf_data", iname);
1098
        buflen0 = sprintf(g_sql_buf[0], "select * from [%s].sdf_data", iname);
1099
    } else if (!IS_CHARACTER(select)) {
1099
    } else if (!IS_CHARACTER(select)) {
1100
        error("select argument is not a string");
1100
        error("select argument is not a string");
1101
    } else {
1101
    } else {
1102
        tmp = CHAR_ELT(select, 0);
1102
        tmp = CHAR_ELT(select, 0);
1103
        _expand_buf(0, strlen(tmp)+100);
1103
        _expand_buf(0, strlen(tmp)+100);
1104
        buflen0 = sprintf(g_sql_buf[0], "select [row name], %s from [%s].sdf_data", 
1104
        buflen0 = sprintf(g_sql_buf[0], "select [row name], %s from [%s].sdf_data", 
1105
                tmp, iname);
1105
                tmp, iname);
1106
    }
1106
    }
1107
 
1107
 
1108
    /* create separate count() statement, to determine number of rows */
1108
    /* create separate count() statement, to determine number of rows */
1109
    buflen1 = sprintf(g_sql_buf[1], "select count([row name]) from [%s].sdf_data", iname);
1109
    buflen1 = sprintf(g_sql_buf[1], "select count([row name]) from [%s].sdf_data", iname);
1110
 
1110
 
1111
    /* process where clause */
1111
    /* process where clause */
1112
    if (where == R_NilValue) {
1112
    if (where == R_NilValue) {
1113
        /* do nothing */
1113
        /* do nothing */
1114
    } else if (!IS_CHARACTER(where)) {
1114
    } else if (!IS_CHARACTER(where)) {
1115
        error("where argument is not a string");
1115
        error("where argument is not a string");
1116
    } else {
1116
    } else {
1117
        tmp = CHAR_ELT(where, 0);
1117
        tmp = CHAR_ELT(where, 0);
1118
        len = strlen(tmp);
1118
        len = strlen(tmp);
1119
        _expand_buf(0, buflen0+len+10);
1119
        _expand_buf(0, buflen0+len+10);
1120
        buflen0 += sprintf(g_sql_buf[0]+buflen0, " where %s", tmp);
1120
        buflen0 += sprintf(g_sql_buf[0]+buflen0, " where %s", tmp);
1121
        _expand_buf(1, buflen1+len+10);
1121
        _expand_buf(1, buflen1+len+10);
1122
        buflen1 += sprintf(g_sql_buf[1]+buflen1, " where %s", tmp);
1122
        buflen1 += sprintf(g_sql_buf[1]+buflen1, " where %s", tmp);
1123
    }
1123
    }
1124
 
1124
 
1125
    /* process limit clause */
1125
    /* process limit clause */
1126
    if (limit == R_NilValue) {
1126
    if (limit == R_NilValue) {
1127
        /* do nothing */
1127
        /* do nothing */
1128
    } else if (!IS_CHARACTER(limit)) {
1128
    } else if (!IS_CHARACTER(limit)) {
1129
        error("limit argument is not a string");
1129
        error("limit argument is not a string");
1130
    } else {
1130
    } else {
1131
        tmp = CHAR_ELT(limit, 0);
1131
        tmp = CHAR_ELT(limit, 0);
1132
        len = strlen(tmp);
1132
        len = strlen(tmp);
1133
        _expand_buf(0, buflen0+len+10);
1133
        _expand_buf(0, buflen0+len+10);
1134
        buflen0 += sprintf(g_sql_buf[0]+buflen0, " limit %s", tmp);
1134
        buflen0 += sprintf(g_sql_buf[0]+buflen0, " limit %s", tmp);
1135
        _expand_buf(1, buflen1+len+10);
1135
        _expand_buf(1, buflen1+len+10);
1136
        
1136
        
1137
        /* limit xxx happens after everything has been selected, and so
1137
        /* limit xxx happens after everything has been selected, and so
1138
         * limit actually limits the count(*) result! not the rows to
1138
         * limit actually limits the count(*) result! not the rows to
1139
         * be counted */
1139
         * be counted */
1140
    }
1140
    }
1141
 
1141
 
1142
    /* test if the "select count(*) " is a valid sql statement */
1142
    /* test if the "select count(*) " is a valid sql statement */
1143
    res = sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
1143
    res = sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
1144
    if (_sqlite_error(res)) error("Error in SQL select statement.");
1144
    if (_sqlite_error(res)) error("Error in SQL select statement.");
1145
 
1145
 
1146
    /* get the number of rows from the "select count(*) " query */
1146
    /* get the number of rows from the "select count(*) " query */
1147
    sqlite3_step(stmt);
1147
    sqlite3_step(stmt);
1148
    nrows = sqlite3_column_int(stmt, 0);
1148
    nrows = sqlite3_column_int(stmt, 0);
1149
    sqlite3_finalize(stmt);
1149
    sqlite3_finalize(stmt);
1150
 
1150
 
1151
    if (nrows == 0) return R_NilValue;
1151
    if (nrows == 0) return R_NilValue;
1152
 
1152
 
1153
    /* test again if actual select stmt is a valid sql statement */
1153
    /* test again if actual select stmt is a valid sql statement */
1154
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1154
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1155
    if (_sqlite_error(res)) error("Error in SQL select statement.");
1155
    if (_sqlite_error(res)) error("Error in SQL select statement.");
1156
    len = sqlite3_column_count(stmt);
1156
    len = sqlite3_column_count(stmt);
1157
 
1157
 
1158
    if (dbg)
1158
    if (dbg)
1159
        Rprintf("sql: %s\nncols: %d, nrows: %d\n", g_sql_buf[0], len, nrows);
1159
        Rprintf("sql: %s\nncols: %d, nrows: %d\n", g_sql_buf[0], len, nrows);
1160
 
1160
 
1161
    if (len < 2) { sqlite3_finalize(stmt); return R_NilValue; }
1161
    if (len < 2) { sqlite3_finalize(stmt); return R_NilValue; }
1162
    else if (len == 2) { /* return a vector */
1162
    else if (len == 2) { /* return a vector */
1163
        int coltype, idx = 0;
1163
        int coltype, idx = 0;
1164
        const char *colname;
1164
        const char *colname;
1165
 
1165
 
1166
        /* initialize R vector */
1166
        /* initialize R vector */
1167
        res = sqlite3_step(stmt);
1167
        res = sqlite3_step(stmt);
1168
        if (res == SQLITE_ROW)
1168
        if (res == SQLITE_ROW)
1169
            idx = _get_vector_index_typed_result(stmt, &ret, 1, nrows, &coltype);
1169
            idx = _get_vector_index_typed_result(stmt, &ret, 1, nrows, &coltype);
1170
 
1170
 
1171
        while ((res = sqlite3_step(stmt)) == SQLITE_ROW) {
1171
        while ((res = sqlite3_step(stmt)) == SQLITE_ROW) {
1172
            idx += _get_vector_index_typed_result(stmt, &ret, 1, idx, &coltype);
1172
            idx += _get_vector_index_typed_result(stmt, &ret, 1, idx, &coltype);
1173
        }
1173
        }
1174
 
1174
 
1175
        if (ret != R_NilValue) {
1175
        if (ret != R_NilValue) {
1176
            if (idx < nrows) ret = _shrink_vector(ret, idx);
1176
            if (idx < nrows) ret = _shrink_vector(ret, idx);
1177
            if (coltype == SQLITE_INTEGER) {
1177
            if (coltype == SQLITE_INTEGER) {
1178
                colname = sqlite3_column_name(stmt, 1);
1178
                colname = sqlite3_column_name(stmt, 1);
1179
                _get_factor_levels1(iname, colname, ret, TRUE);
1179
                _get_factor_levels1(iname, colname, ret, TRUE);
1180
            }
1180
            }
1181
            /*UNPROTECT(1); _get_vector_index_typed_result UNPROTECTs already*/
1181
            /*UNPROTECT(1); _get_vector_index_typed_result UNPROTECTs already*/
1182
        }
1182
        }
1183
    } else { /* return a data frame */
1183
    } else { /* return a data frame */
1184
        --len;  /* _setup_df_sexp1 does not count the preceding [row name] */
1184
        --len;  /* _setup_df_sexp1 does not count the preceding [row name] */
1185
        ret = _setup_df_sexp1(stmt, iname, len, nrows, NULL);
1185
        ret = _setup_df_sexp1(stmt, iname, len, nrows, NULL);
1186
        if (ret != R_NilValue) {
1186
        if (ret != R_NilValue) {
1187
            int idx;
1187
            int idx;
1188
 
1188
 
1189
            for (idx = 0; sqlite3_step(stmt) == SQLITE_ROW && idx < nrows; idx++) {
1189
            for (idx = 0; sqlite3_step(stmt) == SQLITE_ROW && idx < nrows; idx++) {
1190
                _add_row_to_df(ret, stmt, idx, len);
1190
                _add_row_to_df(ret, stmt, idx, len);
1191
            }
1191
            }
1192
 
1192
 
1193
            if (idx < nrows) {
1193
            if (idx < nrows) {
1194
                for (int i = 0; i < len; i++) {
1194
                for (int i = 0; i < len; i++) {
1195
                    SET_VECTOR_ELT(ret, i, 
1195
                    SET_VECTOR_ELT(ret, i, 
1196
                            _shrink_vector(VECTOR_ELT(ret, i), idx));
1196
                            _shrink_vector(VECTOR_ELT(ret, i), idx));
1197
                }
1197
                }
1198
            }
1198
            }
1199
 
1199
 
1200
            SET_CLASS(ret, mkString("data.frame"));
1200
            SET_CLASS(ret, mkString("data.frame"));
1201
            _set_rownames2(ret);
1201
            _set_rownames2(ret);
1202
            
1202
            
1203
            UNPROTECT(1);
1203
            UNPROTECT(1);
1204
        }
1204
        }
1205
    }
1205
    }
1206
 
1206
 
1207
    sqlite3_finalize(stmt);
1207
    sqlite3_finalize(stmt);
1208
    return ret;
1208
    return ret;
1209
}
1209
}