The R Project SVN R-packages

Rev

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

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