The R Project SVN R-packages

Rev

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

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