The R Project SVN R-packages

Rev

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

Rev 3324 Rev 3358
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_skeleton1(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, FALSE)) { /* _delete_sdf2(iname); */ return NULL; }
83
    if (!USE_SDF1(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
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_skeleton1(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 */
258
        /* variables for adding rownames */
259
        SEXP rownames;
259
        SEXP rownames;
260
        int nrows;
260
        int nrows;
261
        char *row_name;
261
        char *row_name;
262
 
262
 
263
        /* TODO: put constraints on table after inserting everything? */
263
        /* TODO: put constraints on table after inserting everything? */
264
 
264
 
265
 
265
 
266
        /* 
266
        /* 
267
         * create the create table and insert sql scripts by looping through
267
         * create the create table and insert sql scripts by looping through
268
         * the columns of df
268
         * the columns of df
269
         */
269
         */
270
        types = (int *)R_alloc(ncols, sizeof(int));
270
        types = (int *)R_alloc(ncols, sizeof(int));
271
        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);
272
        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);
273
 
273
 
274
        for (i = 0; i < ncols; i++) {
274
        for (i = 0; i < ncols; i++) {
275
            col_name = CHAR(STRING_ELT(names, i));
275
            col_name = CHAR(STRING_ELT(names, i));
276
 
276
 
277
            /* add column definition to the create table sql */
277
            /* add column definition to the create table sql */
278
            _expand_buf(0, sql_len+strlen(col_name)+10);
278
            _expand_buf(0, sql_len+strlen(col_name)+10);
279
 
279
 
280
            variable = _getListElement(df, col_name);
280
            variable = _getListElement(df, col_name);
281
            class = CHAR(STRING_ELT(GET_CLASS(variable), 0));
281
            class = CHAR(STRING_ELT(GET_CLASS(variable), 0));
282
            type = TYPEOF(variable);
282
            type = TYPEOF(variable);
283
            types[i] = type;
283
            types[i] = type;
284
 
284
 
285
            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, 
286
                    _get_column_type(class, type));
286
                    _get_column_type(class, type));
287
 
287
 
288
            /* add handler to insert table sql */
288
            /* add handler to insert table sql */
289
            _expand_buf(1, sql_len+5);
289
            _expand_buf(1, sql_len+5);
290
            strcpy(g_sql_buf[1]+sql_len2, ",?");
290
            strcpy(g_sql_buf[1]+sql_len2, ",?");
291
            sql_len2 += 2; 
291
            sql_len2 += 2; 
292
 
292
 
293
            /* create separate table for factors decode */
293
            /* create separate table for factors decode */
294
            if (strcmp(class, "factor") == 0 || strcmp(class, "ordered") == 0){
294
            if (strcmp(class, "factor") == 0 || strcmp(class, "ordered") == 0){
295
                if (_create_factor_table2(iname, class, col_name)) 
295
                if (_create_factor_table2(iname, class, col_name)) 
296
                    return R_NilValue; /* dup tbl name? */
296
                    return R_NilValue; /* dup tbl name? */
297
 
297
 
298
                levels = GET_LEVELS(variable);
298
                levels = GET_LEVELS(variable);
299
                sprintf(g_sql_buf[2], "insert into [%s].[%s %s] values(?, ?);",
299
                sprintf(g_sql_buf[2], "insert into [%s].[%s %s] values(?, ?);",
300
                        iname, class, col_name);
300
                        iname, class, col_name);
301
                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);
302
                if (_sqlite_error(res)) return R_NilValue; /* dup tbl name? */
302
                if (_sqlite_error(res)) return R_NilValue; /* dup tbl name? */
303
 
303
 
304
                for (j = 0; j < GET_LENGTH(levels); j++) {
304
                for (j = 0; j < GET_LENGTH(levels); j++) {
305
                    sqlite3_reset(stmt);
305
                    sqlite3_reset(stmt);
306
                    factor = CHAR(STRING_ELT(levels, j));
306
                    factor = CHAR(STRING_ELT(levels, j));
307
                    sqlite3_bind_int(stmt, 1, j+1);
307
                    sqlite3_bind_int(stmt, 1, j+1);
308
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
308
                    sqlite3_bind_text(stmt, 2, factor, -1, SQLITE_STATIC);
309
                    sqlite3_step(stmt);
309
                    sqlite3_step(stmt);
310
                }
310
                }
311
                sqlite3_finalize(stmt);
311
                sqlite3_finalize(stmt);
312
            }
312
            }
313
        }
313
        }
314
        
314
        
315
        _expand_buf(0,sql_len+35);
315
        _expand_buf(0,sql_len+35);
316
        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]));");
317
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ");"); */
317
        /* sql_len += sprintf(g_sql_buf[0]+sql_len, ");"); */
318
        res = _sqlite_exec(g_sql_buf[0]);
318
        res = _sqlite_exec(g_sql_buf[0]);
319
        if (_sqlite_error(res)) return R_NilValue; /* why? */
319
        if (_sqlite_error(res)) return R_NilValue; /* why? */
320
 
320
 
321
        /*
321
        /*
322
         * add the data in df to the sdf
322
         * add the data in df to the sdf
323
         */
323
         */
324
        rownames = getAttrib(df, R_RowNamesSymbol);
324
        rownames = getAttrib(df, R_RowNamesSymbol);
325
        nrows = GET_LENGTH(rownames);
325
        nrows = GET_LENGTH(rownames);
326
 
326
 
327
        sprintf(g_sql_buf[1]+sql_len2, ")");
327
        sprintf(g_sql_buf[1]+sql_len2, ")");
328
        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);
329
 
329
 
330
        for (i = 0; i < nrows; i++) {
330
        for (i = 0; i < nrows; i++) {
331
            sqlite3_reset(stmt);
331
            sqlite3_reset(stmt);
332
 
332
 
333
            /* since this is coming from a real dataframe, we're assured that
333
            /* since this is coming from a real dataframe, we're assured that
334
             * the rownames are unique */
334
             * the rownames are unique */
335
            if (IS_CHARACTER(rownames)) {
335
            if (IS_CHARACTER(rownames)) {
336
                row_name = CHAR(STRING_ELT(rownames, i));
336
                row_name = CHAR(STRING_ELT(rownames, i));
337
                if (*row_name) /* if not empty string */
337
                if (*row_name) /* if not empty string */
338
                    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);
339
                else sqlite3_bind_int(stmt, 1, i);
340
            } else if (IS_INTEGER(rownames)) {
340
            } else if (IS_INTEGER(rownames)) {
341
                sqlite3_bind_int(stmt, 1, INTEGER(rownames)[i]);
341
                sqlite3_bind_int(stmt, 1, INTEGER(rownames)[i]);
342
            } else sqlite3_bind_int(stmt, 1, i);
342
            } else sqlite3_bind_int(stmt, 1, i);
343
 
343
 
344
            for (j = 0; j < ncols; j++) {
344
            for (j = 0; j < ncols; j++) {
345
                variable = VECTOR_ELT(df, j);
345
                variable = VECTOR_ELT(df, j);
346
                switch(types[j]) {
346
                switch(types[j]) {
347
                    case INTSXP : 
347
                    case INTSXP : 
348
                        sqlite3_bind_int(stmt, j+2, INTEGER(variable)[i]);
348
                        sqlite3_bind_int(stmt, j+2, INTEGER(variable)[i]);
349
                        break;
349
                        break;
350
                    case REALSXP:
350
                    case REALSXP:
351
                        sqlite3_bind_double(stmt, j+2, REAL(variable)[i]);
351
                        sqlite3_bind_double(stmt, j+2, REAL(variable)[i]);
352
                        break;
352
                        break;
353
                    case CHARSXP:
353
                    case CHARSXP:
354
                        col_name = CHAR(STRING_ELT(variable,i));
354
                        col_name = CHAR(STRING_ELT(variable,i));
355
                        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);
356
                }
356
                }
357
                /* TODO: handle NA's & NULL's */
357
                /* TODO: handle NA's & NULL's */
358
            }
358
            }
359
 
359
 
360
            res = sqlite3_step(stmt);
360
            res = sqlite3_step(stmt);
361
            if (res != SQLITE_DONE) { 
361
            if (res != SQLITE_DONE) { 
362
                sqlite3_finalize(stmt);
362
                sqlite3_finalize(stmt);
363
                Rprintf("ERROR: %s\n", sqlite3_errmsg(g_workspace));
363
                Rprintf("ERROR: %s\n", sqlite3_errmsg(g_workspace));
364
                return R_NilValue; /* why? */
364
                return R_NilValue; /* why? */
365
            }
365
            }
366
        }
366
        }
367
                
367
                
368
        sqlite3_finalize(stmt);
368
        sqlite3_finalize(stmt);
369
 
369
 
370
        /* create a new object representing the sdf */
370
        /* create a new object representing the sdf */
371
        ret = _create_sdf_sexp(iname);
371
        ret = _create_sdf_sexp(iname);
372
 
372
 
373
    } else {
373
    } else {
374
        Rprintf("ERROR: unable to create a sqlite data frame.\n");
374
        Rprintf("ERROR: unable to create a sqlite data frame.\n");
375
        ret = R_NilValue;
375
        ret = R_NilValue;
376
    }
376
    }
377
        
377
        
378
    return ret;
378
    return ret;
379
}
379
}
380
 
380
 
381
SEXP sdf_get_names(SEXP sdf) {
381
SEXP sdf_get_names(SEXP sdf) {
382
    char *iname;
382
    char *iname;
383
    iname = SDF_INAME(sdf);
383
    iname = SDF_INAME(sdf);
384
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
384
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
385
 
385
 
386
    int len;
386
    int len;
387
    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);
388
 
388
 
389
    sqlite3_stmt *stmt;
389
    sqlite3_stmt *stmt;
390
    int res, i;
390
    int res, i;
391
   
391
   
392
    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);
393
    if (_sqlite_error(res)) return R_NilValue;
393
    if (_sqlite_error(res)) return R_NilValue;
394
 
394
 
395
    SEXP ret;
395
    SEXP ret;
396
 
396
 
397
    len = sqlite3_column_count(stmt)-1;
397
    len = sqlite3_column_count(stmt)-1;
398
    PROTECT(ret = NEW_CHARACTER(len));
398
    PROTECT(ret = NEW_CHARACTER(len));
399
 
399
 
400
    for (i = 0; i < len; i++) {
400
    for (i = 0; i < len; i++) {
401
        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)));
402
    }
402
    }
403
 
403
 
404
    sqlite3_finalize(stmt);
404
    sqlite3_finalize(stmt);
405
    UNPROTECT(1);
405
    UNPROTECT(1);
406
    return ret;
406
    return ret;
407
}
407
}
408
 
408
 
409
SEXP sdf_get_length(SEXP sdf) {
409
SEXP sdf_get_length(SEXP sdf) {
410
    char *iname;
410
    char *iname;
411
    iname  = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
411
    iname  = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
412
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
412
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
413
 
413
 
414
    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);
415
 
415
 
416
    sqlite3_stmt *stmt;
416
    sqlite3_stmt *stmt;
417
    int res;
417
    int res;
418
   
418
   
419
    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);
420
    if (_sqlite_error(res)) return R_NilValue;
420
    if (_sqlite_error(res)) return R_NilValue;
421
 
421
 
422
    SEXP ret;
422
    SEXP ret;
423
 
423
 
424
    len = sqlite3_column_count(stmt)-1;
424
    len = sqlite3_column_count(stmt)-1;
425
    PROTECT(ret = NEW_INTEGER(1));
425
    PROTECT(ret = NEW_INTEGER(1));
426
    INTEGER(ret)[0] = len;
426
    INTEGER(ret)[0] = len;
427
 
427
 
428
    sqlite3_finalize(stmt);
428
    sqlite3_finalize(stmt);
429
    UNPROTECT(1);
429
    UNPROTECT(1);
430
    return ret;
430
    return ret;
431
}
431
}
432
 
432
 
433
/* get row count */
433
/* get row count */
434
SEXP sdf_get_row_count(SEXP sdf) {
434
SEXP sdf_get_row_count(SEXP sdf) {
435
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
435
    char *iname = CHAR(STRING_ELT(_getListElement(sdf, "iname"),0));
436
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
436
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
437
 
437
 
438
    char **out;
438
    int nrow;
439
    int res, ncol, nrow;
-
 
440
    SEXP ret;
439
    SEXP ret;
441
   
440
   
442
    sprintf(g_sql_buf[0], "select count(*) from [%s].sdf_data;", iname);
-
 
443
    res = sqlite3_get_table(g_workspace, g_sql_buf[0], &out, &nrow, &ncol, NULL);
-
 
444
    if (_sqlite_error(res)) return R_NilValue;
441
    nrow = _get_row_count2(iname, TRUE);
445
 
-
 
446
 
442
 
447
    if (nrow != 1) ret = R_NilValue;
443
    if (nrow < 1) ret = R_NilValue;
448
    else {
444
    else {
449
        PROTECT(ret = NEW_INTEGER(1));
445
        PROTECT(ret = NEW_INTEGER(1));
450
        INTEGER(ret)[0] = atoi(out[1]);
446
        INTEGER(ret)[0] = nrow;
451
        UNPROTECT(1);
447
        UNPROTECT(1);
452
    }
448
    }
453
 
449
 
454
    sqlite3_free_table(out);
-
 
455
    return ret;
450
    return ret;
456
}
451
}
457
 
452
 
458
    
453
    
459
SEXP sdf_import_table(SEXP _filename, SEXP _name, SEXP _sep, SEXP _quote, 
454
SEXP sdf_import_table(SEXP _filename, SEXP _name, SEXP _sep, SEXP _quote, 
460
        SEXP _rownames, SEXP _colnames) {
455
        SEXP _rownames, SEXP _colnames) {
461
    char *filename = CHAR_ELT(_filename, 0);
456
    char *filename = CHAR_ELT(_filename, 0);
462
    FILE *f = fopen(filename, "r");
457
    FILE *f = fopen(filename, "r");
463
 
458
 
464
    if (f == NULL) {
459
    if (f == NULL) {
465
        Rprintf("Error: File %s does not exist.", filename);
460
        Rprintf("Error: File %s does not exist.", filename);
466
        return R_NilValue;
461
        return R_NilValue;
467
    }
462
    }
468
 
463
 
469
    /*char *sep = CHAR_ELT(_sep, 0), *quote = CHAR_ELT(_quote,0);
464
    /*char *sep = CHAR_ELT(_sep, 0), *quote = CHAR_ELT(_quote,0);
470
    char *name = CHAR_ELT(_name, 0);*/
465
    char *name = CHAR_ELT(_name, 0);*/
471
 
466
 
472
    /* create the table */
467
    /* create the table */
473
    /* insert the stuffs */
468
    /* insert the stuffs */
474
    /* register to workspace */
469
    /* register to workspace */
475
 
470
 
476
    return R_NilValue;
471
    return R_NilValue;
477
}
472
}
478
 
473
 
479
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
474
SEXP sdf_get_index(SEXP sdf, SEXP row, SEXP col) {
480
    SEXP ret = R_NilValue;
475
    SEXP ret = R_NilValue;
481
    char *iname = SDF_INAME(sdf);
476
    char *iname = SDF_INAME(sdf);
482
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
477
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
483
 
478
 
484
    sqlite3_stmt *stmt;
479
    sqlite3_stmt *stmt;
485
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
480
    int buflen = 0, idxlen, col_cnt, row_cnt, index;
486
    int i, j,res;
481
    int i, j,res;
487
    int *col_indices, col_index_len, *dup_indices;
482
    int *col_indices, col_index_len, *dup_indices;
488
    int row_index_len = 0;
483
    int row_index_len = 0;
489
 
484
 
490
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
485
    sprintf(g_sql_buf[0], "select * from [%s].sdf_data ", iname);
491
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
486
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
492
    if (_sqlite_error(res)) return R_NilValue;
487
    if (_sqlite_error(res)) return R_NilValue;
493
 
488
 
494
    buflen = sprintf(g_sql_buf[0], "select [row name]");
489
    buflen = sprintf(g_sql_buf[0], "select [row name]");
495
    idxlen = LENGTH(col);
490
    idxlen = LENGTH(col);
496
    col_cnt = sqlite3_column_count(stmt) - 1;
491
    col_cnt = sqlite3_column_count(stmt) - 1;
497
 
492
 
498
    /*
493
    /*
499
     * PROCESS COLUMN INDEX
494
     * PROCESS COLUMN INDEX
500
     */
495
     */
501
    if (col == R_NilValue) {
496
    if (col == R_NilValue) {
502
        for (i = 1; i < col_cnt+1; i++) {
497
        for (i = 1; i < col_cnt+1; i++) {
503
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
498
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
504
                    sqlite3_column_name(stmt, i)); 
499
                    sqlite3_column_name(stmt, i)); 
505
        }
500
        }
506
        _expand_buf(0, buflen+20+strlen(iname));
501
        _expand_buf(0, buflen+20+strlen(iname));
507
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
502
        buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
508
        col_index_len = col_cnt;
503
        col_index_len = col_cnt;
509
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
504
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
510
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
505
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
511
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
506
        for (i = 0; i < col_cnt; i++) { col_indices[i] = i; dup_indices[i] = 0; }
512
    } else if (col == R_NilValue || idxlen < 1) {
507
    } else if (col == R_NilValue || idxlen < 1) {
513
        sqlite3_finalize(stmt);
508
        sqlite3_finalize(stmt);
514
        return R_NilValue;
509
        return R_NilValue;
515
    } else if (IS_NUMERIC(col)) {
510
    } else if (IS_NUMERIC(col)) {
516
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
511
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
517
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
512
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
518
        col_index_len = 0;
513
        col_index_len = 0;
519
 
514
 
520
        for (i = 0; i < idxlen; i++) {
515
        for (i = 0; i < idxlen; i++) {
521
            /* no need to correct for 0 base because col 0 is [row name] */
516
            /* no need to correct for 0 base because col 0 is [row name] */
522
            index = ((int) REAL(col)[i]); 
517
            index = ((int) REAL(col)[i]); 
523
            if (index > col_cnt) {
518
            if (index > col_cnt) {
524
                sqlite3_finalize(stmt);
519
                sqlite3_finalize(stmt);
525
                Rprintf("Error: undefined columns selected\n");
520
                Rprintf("Error: undefined columns selected\n");
526
                return R_NilValue;
521
                return R_NilValue;
527
            } else if (index > 0) {
522
            } else if (index > 0) {
528
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
523
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
529
                        sqlite3_column_name(stmt,index));
524
                        sqlite3_column_name(stmt,index));
530
                dup_indices[col_index_len] = 0;
525
                dup_indices[col_index_len] = 0;
531
                for (j = 0; j < col_index_len; j++) {
526
                for (j = 0; j < col_index_len; j++) {
532
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
527
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
533
                }
528
                }
534
                col_indices[col_index_len++] = index;
529
                col_indices[col_index_len++] = index;
535
            } else if (index < 0) {
530
            } else if (index < 0) {
536
                sqlite3_finalize(stmt);
531
                sqlite3_finalize(stmt);
537
                Rprintf("Error: negative indices not supported.\n");
532
                Rprintf("Error: negative indices not supported.\n");
538
                return R_NilValue;
533
                return R_NilValue;
539
            }
534
            }
540
        }
535
        }
541
 
536
 
542
        if (col_index_len == 0) {
537
        if (col_index_len == 0) {
543
            sqlite3_finalize(stmt);
538
            sqlite3_finalize(stmt);
544
            Rprintf("Error: no indices detected??\n");
539
            Rprintf("Error: no indices detected??\n");
545
            return R_NilValue;
540
            return R_NilValue;
546
        } else { 
541
        } else { 
547
            _expand_buf(0, buflen+20+strlen(iname));
542
            _expand_buf(0, buflen+20+strlen(iname));
548
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
543
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
549
        }
544
        }
550
    } else if (IS_INTEGER(col)) {
545
    } else if (IS_INTEGER(col)) {
551
        /* identical logic with IS_NUMERIC, except that we don't have to cast
546
        /* identical logic with IS_NUMERIC, except that we don't have to cast
552
         * stuffs from SEXP col. the alternative is doing if idxlen times. */
547
         * stuffs from SEXP col. the alternative is doing if idxlen times. */
553
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
548
        col_indices = (int *)R_alloc(idxlen, sizeof(int));
554
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
549
        dup_indices = (int *)R_alloc(idxlen, sizeof(int));
555
        col_index_len = 0;
550
        col_index_len = 0;
556
 
551
 
557
        for (i = 0; i < idxlen; i++) {
552
        for (i = 0; i < idxlen; i++) {
558
            /* no need to correct for 0 base because col 0 is [row name] */
553
            /* no need to correct for 0 base because col 0 is [row name] */
559
            index = INTEGER(col)[i];
554
            index = INTEGER(col)[i];
560
            if (index > col_cnt) {
555
            if (index > col_cnt) {
561
                sqlite3_finalize(stmt);
556
                sqlite3_finalize(stmt);
562
                Rprintf("Error: undefined columns selected\n");
557
                Rprintf("Error: undefined columns selected\n");
563
                return R_NilValue;
558
                return R_NilValue;
564
            } else if (index > 0) {
559
            } else if (index > 0) {
565
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
560
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
566
                        sqlite3_column_name(stmt,index));
561
                        sqlite3_column_name(stmt,index));
567
                dup_indices[col_index_len] = 0;
562
                dup_indices[col_index_len] = 0;
568
                for (j = 0; j < col_index_len; j++) {
563
                for (j = 0; j < col_index_len; j++) {
569
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
564
                    if (col_indices[j] == index) dup_indices[col_index_len]++;
570
                }
565
                }
571
                col_indices[col_index_len++] = index;
566
                col_indices[col_index_len++] = index;
572
            } else if (index < 0) {
567
            } else if (index < 0) {
573
                sqlite3_finalize(stmt);
568
                sqlite3_finalize(stmt);
574
                Rprintf("Error: negative indices not supported.\n");
569
                Rprintf("Error: negative indices not supported.\n");
575
                return R_NilValue;
570
                return R_NilValue;
576
            }
571
            }
577
        }
572
        }
578
 
573
 
579
        if (col_index_len == 0) {
574
        if (col_index_len == 0) {
580
            sqlite3_finalize(stmt);
575
            sqlite3_finalize(stmt);
581
            Rprintf("Error: no indices detected??\n");
576
            Rprintf("Error: no indices detected??\n");
582
            return R_NilValue;
577
            return R_NilValue;
583
        } else { 
578
        } else { 
584
            _expand_buf(0, buflen+20+strlen(iname));
579
            _expand_buf(0, buflen+20+strlen(iname));
585
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
580
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
586
        }
581
        }
587
    } else if (IS_LOGICAL(col)) {
582
    } else if (IS_LOGICAL(col)) {
588
        /* recycling stuff, so max column output is the # of cols in the df */
583
        /* recycling stuff, so max column output is the # of cols in the df */
589
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
584
        col_indices = (int *)R_alloc(col_cnt, sizeof(int));
590
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
585
        dup_indices = (int *)R_alloc(col_cnt, sizeof(int));
591
        col_index_len = 0;
586
        col_index_len = 0;
592
        for (i = 0; i < col_cnt; i++) {
587
        for (i = 0; i < col_cnt; i++) {
593
            if (LOGICAL(col)[i%idxlen]) {
588
            if (LOGICAL(col)[i%idxlen]) {
594
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
589
                buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", 
595
                        sqlite3_column_name(stmt,i+1));
590
                        sqlite3_column_name(stmt,i+1));
596
                dup_indices[col_index_len] = 0;
591
                dup_indices[col_index_len] = 0;
597
                col_indices[col_index_len++] = i;
592
                col_indices[col_index_len++] = i;
598
            }
593
            }
599
        }
594
        }
600
 
595
 
601
        if (col_index_len == 0) {
596
        if (col_index_len == 0) {
602
            sqlite3_finalize(stmt);
597
            sqlite3_finalize(stmt);
603
            Rprintf("Warning: no column selected.\n");
598
            Rprintf("Warning: no column selected.\n");
604
            return R_NilValue;
599
            return R_NilValue;
605
        } else { 
600
        } else { 
606
            _expand_buf(0, buflen+20+strlen(iname));
601
            _expand_buf(0, buflen+20+strlen(iname));
607
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
602
            buflen += sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
608
        }
603
        }
609
    } else {
604
    } else {
610
        sqlite3_finalize(stmt);
605
        sqlite3_finalize(stmt);
611
        Rprintf("Error: don't know how to handle column index.\n");
606
        Rprintf("Error: don't know how to handle column index.\n");
612
        return R_NilValue;
607
        return R_NilValue;
613
    }
608
    }
614
 
609
 
615
    /* 
610
    /* 
616
     * PROCESS ROW INDEX
611
     * PROCESS ROW INDEX
617
     */
612
     */
618
    idxlen = LENGTH(row);
613
    idxlen = LENGTH(row);
619
    if (row == R_NilValue) {
614
    if (row == R_NilValue) {
620
        if (col_index_len == 1) {
615
        if (col_index_len == 1) {
621
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
616
            ret = sdf_get_variable(sdf, mkString(sqlite3_column_name(stmt,col_indices[0])));
622
            sqlite3_finalize(stmt);
617
            sqlite3_finalize(stmt);
623
            return ret;
618
            return ret;
624
        } if (col_index_len > 1) {
619
        } if (col_index_len > 1) {
625
            /* create a new SDF, logic similar to sdf_create_sdf */
620
            /* create a new SDF, logic similar to sdf_create_sdf */
626
 
621
 
627
            /* find a new name. data<n> ? */
622
            /* find a new name. data<n> ? */
628
            char *iname2;
623
            char *iname2;
629
            const char *colname;
624
            const char *colname;
630
            int namelen, sql_len, sql_len2;
625
            int namelen, sql_len, sql_len2;
631
 
626
 
632
            iname2 = _create_sdf_skeleton2(R_NilValue, &namelen);
627
            iname2 = _create_sdf_skeleton1(R_NilValue, &namelen);
633
 
628
 
634
            /* create sdf_data table */
629
            /* create sdf_data table */
635
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
630
            sql_len = sprintf(g_sql_buf[1], "create table [%s].sdf_data ([row name] text", iname2);
636
            sql_len2 = 0;
631
            sql_len2 = 0;
637
 
632
 
638
            for (i = 0; i < col_index_len; i++) {
633
            for (i = 0; i < col_index_len; i++) {
639
                colname = sqlite3_column_name(stmt, col_indices[i]);
634
                colname = sqlite3_column_name(stmt, col_indices[i]);
640
                if (dup_indices[i] == 0) {
635
                if (dup_indices[i] == 0) {
641
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s] %s", colname,
636
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s] %s", colname,
642
                            sqlite3_column_decltype(stmt, col_indices[i]));
637
                            sqlite3_column_decltype(stmt, col_indices[i]));
643
                } else {
638
                } else {
644
                    /* un-duplicate col names by appending num, just like R */
639
                    /* un-duplicate col names by appending num, just like R */
645
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
640
                    sql_len += sprintf(g_sql_buf[1]+sql_len, ", [%s.%d] %s", colname,
646
                            dup_indices[i], 
641
                            dup_indices[i], 
647
                            sqlite3_column_decltype(stmt, col_indices[i]));
642
                            sqlite3_column_decltype(stmt, col_indices[i]));
648
                }
643
                }
649
 
644
 
650
                /* deal with possibly factor columns */
645
                /* deal with possibly factor columns */
651
                if (_is_factor2(iname, "factor", colname)) {
646
                if (_is_factor2(iname, "factor", colname)) {
652
                    /* found a factor column */
647
                    /* found a factor column */
653
 
648
 
654
                    if (dup_indices[i] == 0)  {
649
                    if (dup_indices[i] == 0)  {
655
                        _copy_factor_levels2("factor", iname, colname, iname2, colname);
650
                        _copy_factor_levels2("factor", iname, colname, iname2, colname);
656
                    } else {
651
                    } else {
657
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
652
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
658
                        _copy_factor_levels2("factor", iname, colname, iname2, 
653
                        _copy_factor_levels2("factor", iname, colname, iname2, 
659
                                g_sql_buf[3]);
654
                                g_sql_buf[3]);
660
                    }
655
                    }
661
                }
656
                }
662
 
657
 
663
                /* and deal with ordered factors too... life is hard, then you die */
658
                /* and deal with ordered factors too... life is hard, then you die */
664
                if (_is_factor2(iname, "ordered", colname)) {
659
                if (_is_factor2(iname, "ordered", colname)) {
665
                    
660
                    
666
                    if (dup_indices[i] == 0)  {
661
                    if (dup_indices[i] == 0)  {
667
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
662
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
668
                                colname);
663
                                colname);
669
                    } else {
664
                    } else {
670
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
665
                        sprintf(g_sql_buf[3], "%s.%d", colname, dup_indices[i]);
671
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
666
                        _copy_factor_levels2("ordered", iname, colname, iname2, 
672
                                g_sql_buf[3]);
667
                                g_sql_buf[3]);
673
                    }
668
                    }
674
                }
669
                }
675
                                
670
                                
676
            }
671
            }
677
 
672
 
678
            /* don't need it anymore */
673
            /* don't need it anymore */
679
            sqlite3_finalize(stmt);
674
            sqlite3_finalize(stmt);
680
 
675
 
681
            /* no table index created, that's for v2 I hope*/
676
            /* no table index created, that's for v2 I hope*/
682
            sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
677
            sql_len += sprintf(g_sql_buf[1]+sql_len, ");");
683
            res = _sqlite_exec(g_sql_buf[1]);
678
            res = _sqlite_exec(g_sql_buf[1]);
684
            if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
679
            if (_sqlite_error(res)) { Rprintf("here? %s\n", g_sql_buf[1]); return R_NilValue; }
685
 
680
 
686
            /* insert data (with row names). buf[0] contains a select */
681
            /* insert data (with row names). buf[0] contains a select */
687
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
682
            sprintf(g_sql_buf[1], "insert into [%s].sdf_data %s", iname2,
688
                    g_sql_buf[0]);
683
                    g_sql_buf[0]);
689
            res = _sqlite_exec(g_sql_buf[1]);
684
            res = _sqlite_exec(g_sql_buf[1]);
690
            if (_sqlite_error(res)) return R_NilValue;
685
            if (_sqlite_error(res)) return R_NilValue;
691
 
686
 
692
            /* add new sdf to workspace */
687
            /* add new sdf to workspace */
693
            sprintf(g_sql_buf[0], "%s.db", iname2);
688
            sprintf(g_sql_buf[0], "%s.db", iname2);
694
            res = _add_sdf1(g_sql_buf[0], iname2);
689
            res = _add_sdf1(g_sql_buf[0], iname2);
695
            if (_sqlite_error(res)) return R_NilValue;
690
            if (_sqlite_error(res)) return R_NilValue;
696
 
691
 
697
            /* create SEXP for the SDF */
692
            /* create SEXP for the SDF */
698
            ret = _create_sdf_sexp(iname2);
693
            ret = _create_sdf_sexp(iname2);
699
            return ret;
694
            return ret;
700
        } else { sqlite3_finalize(stmt); return R_NilValue; }
695
        } else { sqlite3_finalize(stmt); return R_NilValue; }
701
    } else if (row == R_NilValue || idxlen < 1) {
696
    } else if (row == R_NilValue || idxlen < 1) {
702
        sqlite3_finalize(stmt);
697
        sqlite3_finalize(stmt);
703
        return R_NilValue;
698
        return R_NilValue;
704
    } else if (IS_NUMERIC(row)) {
699
    } else if (IS_NUMERIC(row)) {
705
        sqlite3_finalize(stmt);
700
        sqlite3_finalize(stmt);
706
 
701
 
707
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
702
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
708
        row_cnt = _get_row_count2(g_sql_buf[1]);
703
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
709
 
704
 
710
        /* append " limit ?,1" to the formed select statement */
705
        /* append " limit ?,1" to the formed select statement */
711
        _expand_buf(0, buflen+10);
706
        _expand_buf(0, buflen+10);
712
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
707
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
713
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
708
        /*Rprintf("sql [%d]: %s\n", buflen, g_sql_buf[0]); */
714
 
709
 
715
        index = ((int) REAL(row)[0]) - 1;
710
        index = ((int) REAL(row)[0]) - 1;
716
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
711
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
717
 
712
 
718
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
713
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
719
        if (_sqlite_error(res)) return R_NilValue;
714
        if (_sqlite_error(res)) return R_NilValue;
720
 
715
 
721
        sqlite3_bind_int(stmt, 1, index);
716
        sqlite3_bind_int(stmt, 1, index);
722
        res = sqlite3_step(stmt);
717
        res = sqlite3_step(stmt);
723
 
718
 
724
        /* create data frame */
719
        /* create data frame */
725
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
720
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
726
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
721
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
727
 
722
 
728
        /* put data in it */
723
        /* put data in it */
729
        if (index >= 0 && index < row_cnt) {
724
        if (index >= 0 && index < row_cnt) {
730
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
725
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
731
        }
726
        }
732
 
727
 
733
        for (i = 1; i < idxlen; i++) {
728
        for (i = 1; i < idxlen; i++) {
734
            sqlite3_reset(stmt);
729
            sqlite3_reset(stmt);
735
            index = ((int) REAL(row)[i]) - 1;
730
            index = ((int) REAL(row)[i]) - 1;
736
            if (index >= 0 && index < row_cnt) {
731
            if (index >= 0 && index < row_cnt) {
737
                sqlite3_bind_int(stmt, 1, index);
732
                sqlite3_bind_int(stmt, 1, index);
738
                res = sqlite3_step(stmt);
733
                res = sqlite3_step(stmt);
739
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
734
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
740
            }
735
            }
741
        }
736
        }
742
        sqlite3_finalize(stmt);
737
        sqlite3_finalize(stmt);
743
 
738
 
744
        /* shrink vectors */
739
        /* shrink vectors */
745
        if (row_index_len < idxlen) {
740
        if (row_index_len < idxlen) {
746
            for (i = 0; i < col_index_len; i++) {
741
            for (i = 0; i < col_index_len; i++) {
747
                SET_VECTOR_ELT(ret, i, 
742
                SET_VECTOR_ELT(ret, i, 
748
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
743
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
749
            }
744
            }
750
        }
745
        }
751
 
746
 
752
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
747
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
753
    } else if (IS_INTEGER(row)) {
748
    } else if (IS_INTEGER(row)) {
754
        /* same stuff as with IS_INTEGER, except for setting of var index*/
749
        /* same stuff as with IS_INTEGER, except for setting of var index*/
755
        sqlite3_finalize(stmt);
750
        sqlite3_finalize(stmt);
756
 
751
 
757
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
752
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
758
        row_cnt = _get_row_count2(g_sql_buf[1]);
753
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
759
 
754
 
760
        /* append " limit ?,1" to the formed select statement */
755
        /* append " limit ?,1" to the formed select statement */
761
        _expand_buf(0, buflen+10);
756
        _expand_buf(0, buflen+10);
762
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
757
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
763
 
758
 
764
        index = INTEGER(row)[0] - 1;
759
        index = INTEGER(row)[0] - 1;
765
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
760
        if (idxlen < 0 && idxlen == 1) return R_NilValue;
766
 
761
 
767
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
762
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
768
        if (_sqlite_error(res)) return R_NilValue;
763
        if (_sqlite_error(res)) return R_NilValue;
769
 
764
 
770
        sqlite3_bind_int(stmt, 1, index);
765
        sqlite3_bind_int(stmt, 1, index);
771
        res = sqlite3_step(stmt);
766
        res = sqlite3_step(stmt);
772
 
767
 
773
        /* create data frame */
768
        /* create data frame */
774
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
769
        ret = _setup_df_sexp1(stmt, iname, col_index_len, idxlen, dup_indices);
775
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
770
        if (ret == R_NilValue) { sqlite3_finalize(stmt); return R_NilValue; }
776
 
771
 
777
        /* put data in it */
772
        /* put data in it */
778
        if (index >= 0 && index < row_cnt) {
773
        if (index >= 0 && index < row_cnt) {
779
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
774
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
780
        }
775
        }
781
 
776
 
782
        for (i = 1; i < idxlen; i++) {
777
        for (i = 1; i < idxlen; i++) {
783
            sqlite3_reset(stmt);
778
            sqlite3_reset(stmt);
784
            index = INTEGER(row)[i] - 1;
779
            index = INTEGER(row)[i] - 1;
785
            if (index >= 0 && index < row_cnt) {
780
            if (index >= 0 && index < row_cnt) {
786
                sqlite3_bind_int(stmt, 1, index);
781
                sqlite3_bind_int(stmt, 1, index);
787
                res = sqlite3_step(stmt); 
782
                res = sqlite3_step(stmt); 
788
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
783
                _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
789
            }
784
            }
790
        }
785
        }
791
        sqlite3_finalize(stmt);
786
        sqlite3_finalize(stmt);
792
 
787
 
793
        /* shrink vectors */
788
        /* shrink vectors */
794
        if (row_index_len < idxlen) {
789
        if (row_index_len < idxlen) {
795
            for (i = 0; i < col_index_len; i++) {
790
            for (i = 0; i < col_index_len; i++) {
796
                SET_VECTOR_ELT(ret, i, 
791
                SET_VECTOR_ELT(ret, i, 
797
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
792
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
798
            }
793
            }
799
        }
794
        }
800
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
795
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
801
    } else if (IS_LOGICAL(row)) {
796
    } else if (IS_LOGICAL(row)) {
802
        /* */
797
        /* */
803
        sqlite3_finalize(stmt);
798
        sqlite3_finalize(stmt);
804
        int est_row_cnt = 0;
799
        int est_row_cnt = 0;
805
 
800
 
806
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
801
        sprintf(g_sql_buf[1], "[%s].sdf_data", iname);
807
        row_cnt = _get_row_count2(g_sql_buf[1]);
802
        row_cnt = _get_row_count2(g_sql_buf[1], 0);
808
 
803
 
809
        /* append " limit ?,1" to the formed select statement */
804
        /* append " limit ?,1" to the formed select statement */
810
        _expand_buf(0, buflen+10);
805
        _expand_buf(0, buflen+10);
811
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
806
        buflen += sprintf(g_sql_buf[0]+buflen, " limit ?,1");
812
 
807
 
813
        /* find if there is any TRUE element in the vector */
808
        /* find if there is any TRUE element in the vector */
814
        for (i = 0; i < idxlen && i < row_cnt; i++) {
809
        for (i = 0; i < idxlen && i < row_cnt; i++) {
815
            if (LOGICAL(row)[i]) break;
810
            if (LOGICAL(row)[i]) break;
816
        }
811
        }
817
 
812
 
818
        if (i < idxlen && i < row_cnt) {
813
        if (i < idxlen && i < row_cnt) {
819
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
814
            est_row_cnt = (idxlen-i) * (row_cnt/idxlen);
820
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
815
            if (row_cnt%idxlen > i) est_row_cnt += (row_cnt%idxlen - i);
821
 
816
 
822
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
817
            res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
823
            if (_sqlite_error(res)) return R_NilValue;
818
            if (_sqlite_error(res)) return R_NilValue;
824
 
819
 
825
            sqlite3_bind_int(stmt, 1, i);
820
            sqlite3_bind_int(stmt, 1, i);
826
            sqlite3_step(stmt);
821
            sqlite3_step(stmt);
827
            ret = _setup_df_sexp1(stmt, iname, col_index_len, est_row_cnt, dup_indices);
822
            ret = _setup_df_sexp1(stmt, iname, col_index_len, est_row_cnt, dup_indices);
828
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
823
            _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
829
            
824
            
830
            for (i++ ; i < row_cnt; i++) {
825
            for (i++ ; i < row_cnt; i++) {
831
                if (LOGICAL(row)[i%idxlen]) {
826
                if (LOGICAL(row)[i%idxlen]) {
832
                    sqlite3_reset(stmt);
827
                    sqlite3_reset(stmt);
833
                    sqlite3_bind_int(stmt, 1, i);
828
                    sqlite3_bind_int(stmt, 1, i);
834
                    sqlite3_step(stmt);
829
                    sqlite3_step(stmt);
835
                    _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
830
                    _add_row_to_df(ret, stmt, row_index_len++, col_index_len);
836
                }
831
                }
837
            }
832
            }
838
        }
833
        }
839
 
834
 
840
        sqlite3_finalize(stmt);
835
        sqlite3_finalize(stmt);
841
 
836
 
842
        /* shrink vectors */
837
        /* shrink vectors */
843
        if (est_row_cnt > 0 && row_index_len < est_row_cnt) {
838
        if (est_row_cnt > 0 && row_index_len < est_row_cnt) {
844
            for (i = 0; i < col_index_len; i++) {
839
            for (i = 0; i < col_index_len; i++) {
845
                SET_VECTOR_ELT(ret, i, 
840
                SET_VECTOR_ELT(ret, i, 
846
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
841
                        _shrink_vector(VECTOR_ELT(ret, i), row_index_len));
847
            }
842
            }
848
        }
843
        }
849
 
844
 
850
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
845
        UNPROTECT(1); /* for the ret from _setup_df_sexp1 */
851
    }
846
    }
852
 
847
 
853
    if (ret != R_NilValue && col_index_len > 1) {
848
    if (ret != R_NilValue && col_index_len > 1) {
854
        SEXP class = mkString("data.frame");
849
        SEXP class = mkString("data.frame");
855
        SET_CLASS(ret, class);
850
        SET_CLASS(ret, class);
856
        _set_rownames2(ret);
851
        _set_rownames2(ret);
857
    } else if (ret != R_NilValue && col_index_len == 1) {
852
    } else if (ret != R_NilValue && col_index_len == 1) {
858
        ret = VECTOR_ELT(ret, 0);
853
        ret = VECTOR_ELT(ret, 0);
859
    }
854
    }
860
 
855
 
861
    return ret;
856
    return ret;
862
}
857
}
863
 
858
 
864
SEXP sdf_rbind(SEXP sdf, SEXP data) {
859
SEXP sdf_rbind(SEXP sdf, SEXP data) {
865
    char *class = CHAR_ELT(data, 0);
860
    char *class = CHAR_ELT(GET_CLASS(data), 0);
866
    char *iname = SDF_INAME(sdf), *colname;
861
    char *iname = SDF_INAME(sdf), *colname, *rowname;
867
    SEXP ret = R_NilValue, col, names, levels;
862
    SEXP ret = R_NilValue, col, names, levels, rownames;
868
    int len, buflen, buflen2, i, j, res, type, nrows;
863
    int ncols, buflen, buflen2, i, j, res, nrows, *types;
869
    sqlite3_stmt *stmt;
864
    sqlite3_stmt *stmt;
-
 
865
    const char *type;
870
 
866
 
871
    if (strcmp(class,"data.frame") == 0) {
867
    if (strcmp(class,"data.frame") == 0) {
872
        /* check table names, types. variables may not be in same order, as
868
        /* check table names, types. variables may not be in same order, as
873
         * long as names and types are identical. */
869
         * long as names and types are identical. */
874
        names = GET_NAMES(data);
870
        names = GET_NAMES(data);
875
        len = GET_LENGTH(names);
871
        ncols = GET_LENGTH(names);
876
 
872
 
877
        buflen = sprintf(g_sql_buf[0], "select 1");
873
        buflen = sprintf(g_sql_buf[0], "select 1");
878
        for (i = 0; i < len; i++) {
874
        for (i = 0; i < ncols; i++) {
879
            _expand_buf(0, buflen + 100);
875
            _expand_buf(0, buflen + 100);
880
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
876
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
881
        }
877
        }
882
        _expand_buf(0, buflen + 100);
878
        _expand_buf(0, buflen + 100);
883
        sprintf(g_sql_buf[0]+buflen, " from %s", iname);
879
        sprintf(g_sql_buf[0]+buflen, " from [%s].sdf_data", iname);
884
 
880
 
885
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
881
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
886
        if (res != SQLITE_OK) { /* column name mismatch */
882
        if (res != SQLITE_OK) { /* column name mismatch */
887
            Rprintf("Error: column names should exactly match the names of the SDF.\n");
883
            Rprintf("Error: column names should exactly match the names of the SDF.\n");
888
            return ret;
884
            return ret;
889
        }
885
        }
890
 
886
 
891
        /* now check the type */
887
        /* now check the type */
892
        sqlite3_step(stmt);
888
        types = (int *)R_alloc(ncols, sizeof(int));
893
        for (i = 0; i< len; i++) {
889
        for (i = 0; i< ncols; i++) {
894
            do { 
-
 
895
                type = sqlite3_column_type(stmt, i+1); 
890
            type = sqlite3_column_decltype(stmt, i+1); /* +1 bec 1st col is "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
 
891
 
905
            col = VECTOR_ELT(data, i);
892
            col = VECTOR_ELT(data, i);
906
            class = CHAR_ELT(GET_CLASS(col),0);
893
            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
 
894
 
-
 
895
            if (strcmp(type, "double") == 0 && IS_NUMERIC(col)) types[i] = SQLITE_FLOAT;
-
 
896
            else if (strcmp(type, "text") == 0 && IS_CHARACTER(col)) types[i] = SQLITE_TEXT;
-
 
897
            else if (strcmp(type, "int") == 0) { 
-
 
898
                types[i] = SQLITE_INTEGER;
915
                /* test if the sdf column is indeed a factor */
899
                if (isUnordered(col)) {  /* test if factor */
916
                if (strcmp(class,"factor") == 0) {
900
                    colname = CHAR_ELT(names, i);
917
                    sqlite3_stmt *stmt2;
901
                    sqlite3_stmt *stmt2;
918
                    if (!_is_factor2(iname, "factor", colname)) break;
902
                    if (!_is_factor2(iname, "factor", colname)) break;
919
                    sprintf(g_sql_buf[1], "[%s].[factor %s]", iname, colname);
903
                    sprintf(g_sql_buf[1], "[%s].[factor %s]", iname, colname);
920
                    nrows = _get_row_count2(g_sql_buf[1]);
904
                    nrows = _get_row_count2(g_sql_buf[1], 0);
921
 
905
 
-
 
906
                    /* levels in sdf must be >= levels in df */
922
                    levels = GET_LEVELS(col);
907
                    levels = GET_LEVELS(col);
923
                    if (nrows < GET_LENGTH(levels)) {
908
                    if (nrows < GET_LENGTH(levels)) {
924
                        Rprintf("Error: The data frame variable %s has more levels than"
909
                        Rprintf("Error: The data frame variable %s has more levels than"
925
                                " its SDF counterpart.\n", colname);
910
                                " its SDF counterpart.\n", colname);
926
                        break;
911
                        break;
927
                    }
912
                    }
928
 
913
 
929
                    sprintf(g_sql_buf[2], "select label from %s ordered by level", g_sql_buf[1]);
914
                    sprintf(g_sql_buf[2], "select label from %s order by level", g_sql_buf[1]);
930
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
915
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
-
 
916
                    _sqlite_error(res);
931
                    
917
                    
932
                    for (j = 0; j < nrows; j++)  {
918
                    for (j = 0; j < GET_LENGTH(levels); j++)  {
933
                        sqlite3_step(stmt2);
919
                        sqlite3_step(stmt2);
934
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) == 0) {
920
                        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"
921
                            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),
922
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
937
                                    CHAR_ELT(levels, j));
923
                                    CHAR_ELT(levels, j));
-
 
924
                            sqlite3_finalize(stmt2);
938
                            break;
925
                            break;
939
                        }
926
                        }
940
                    }
927
                    }
941
 
-
 
942
                    if (j < nrows); break;
928
                    sqlite3_finalize(stmt2);
943
 
-
 
944
                }  else if (strcmp(class,"ordered") == 0) { /* same with ordered */
929
                } else if (isOrdered(col)) { /* same with ordered */
-
 
930
                    colname = CHAR_ELT(names, i);
945
                    sqlite3_stmt *stmt2;
931
                    sqlite3_stmt *stmt2;
946
                    if (!_is_factor2(iname, "ordered", colname)) break;
932
                    if (!_is_factor2(iname, "ordered", colname)) break;
947
                    sprintf(g_sql_buf[1], "[%s].[ordered %s]", iname, colname);
933
                    sprintf(g_sql_buf[1], "[%s].[ordered %s]", iname, colname);
948
                    nrows = _get_row_count2(g_sql_buf[1]);
934
                    nrows = _get_row_count2(g_sql_buf[1], 0);
949
 
935
 
-
 
936
                    /* levels in sdf must be >= levels in df */
950
                    levels = GET_LEVELS(col);
937
                    levels = GET_LEVELS(col);
951
                    if (nrows < GET_LENGTH(levels)) {
938
                    if (nrows < GET_LENGTH(levels)) {
952
                        Rprintf("Error: The data frame variable %s has more levels than"
939
                        Rprintf("Error: The data frame variable %s has more levels than"
953
                                " its SDF counterpart.\n", colname);
940
                                " its SDF counterpart.\n", colname);
954
                        break;
941
                        break;
955
                    }
942
                    }
956
 
943
 
957
                    sprintf(g_sql_buf[2], "select label from %s ordered by level", g_sql_buf[1]);
944
                    sprintf(g_sql_buf[2], "select label from %s order by level", g_sql_buf[1]);
958
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
945
                    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, NULL);
959
                    
946
                    
960
                    for (j = 0; j < nrows; j++)  {
947
                    for (j = 0; j < GET_LENGTH(levels); j++)  {
961
                        sqlite3_step(stmt2);
948
                        sqlite3_step(stmt2);
962
                        if (strcmp((char *)sqlite3_column_text(stmt2, 0), CHAR_ELT(levels, j)) == 0) {
949
                        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"
950
                            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),
951
                                    " vs [%s] in SDF.\n", colname, (char *)sqlite3_column_text(stmt2, 0),
965
                                    CHAR_ELT(levels, j));
952
                                    CHAR_ELT(levels, j));
-
 
953
                            sqlite3_finalize(stmt2);
966
                            break;
954
                            break;
967
                        }
955
                        }
968
                    }
956
                    }
969
 
-
 
970
                    if (j < nrows); break;
957
                    sqlite3_finalize(stmt2);
971
 
-
 
972
                }  /* else if class == "ordered" */
958
                }  /* else if class == "ordered" */
973
            } /* if integer */
959
            } /* if integer */
974
        } /* for loop for column type checking */
960
        } /* for loop for column type checking */
975
 
961
 
-
 
962
        sqlite3_finalize(stmt);
976
        if (i < len) {
963
        if (i < ncols) {
977
            Rprintf("Error: type mismatch at variable %s\n", CHAR_ELT(names, i));
964
            Rprintf("Error: type mismatch at variable %s\n", CHAR_ELT(names, i));
978
            return ret;
965
            return ret;
979
        }
966
        }
980
 
967
 
981
        /* create the insert statement */
968
        /* create the insert statement */
982
        buflen = sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name]", iname);
969
        buflen = sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name]", iname);
983
        buflen2 = sprintf(g_sql_buf[1], "values(?");
970
        buflen2 = sprintf(g_sql_buf[1], "values(?");
984
        for (i = 0; i < len; i++) {
971
        for (i = 0; i < ncols; i++) {
985
            _expand_buf(0, buflen+100);
972
            _expand_buf(0, buflen+100);
986
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
973
            buflen += sprintf(g_sql_buf[0]+buflen, ",[%s]", CHAR_ELT(names, i));
987
            _expand_buf(1, buflen2+5);
974
            _expand_buf(1, buflen2+5);
988
            buflen2 += sprintf(g_sql_buf[1]+buflen2, ",?");
975
            buflen2 += sprintf(g_sql_buf[1]+buflen2, ",?");
989
        }
976
        }
990
 
977
 
991
        _expand_buf(2, buflen + buflen2 + 10);
978
        _expand_buf(2, buflen + buflen2 + 10);
992
        sprintf(g_sql_buf[2], "%s) %s", g_sql_buf[0], g_sql_buf[1]);
979
        sprintf(g_sql_buf[2], "%s) %s)", g_sql_buf[0], g_sql_buf[1]);
-
 
980
        Rprintf("%s\n", g_sql_buf[2]);
993
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
981
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
994
        if (_sqlite_error(res)) return ret;
982
        if (_sqlite_error(res)) return ret;
995
 
983
 
996
        /* finally, add rows */
984
        /* finally, add rows */
997
        nrows = LENGTH(GET_ROWNAMES(data));
985
        nrows = LENGTH(GET_ROWNAMES(data));
-
 
986
        rownames = GET_ROWNAMES(data);
998
 
987
 
999
        for (i = 0; i < nrows; i++) {
988
        for (i = 0; i < nrows; i++) {
-
 
989
            sqlite3_bind_text(stmt, 1, CHAR_ELT(rownames, i), -1, SQLITE_STATIC);
-
 
990
            for (j = 0; j < ncols; j++) {
-
 
991
                col = VECTOR_ELT(data, j);
-
 
992
                switch(types[j]) {
-
 
993
                    case SQLITE_FLOAT:
-
 
994
                        sqlite3_bind_double(stmt, j+2, REAL(col)[i]); break;
-
 
995
                    case SQLITE_TEXT:
-
 
996
                        sqlite3_bind_text(stmt, j+2, CHAR_ELT(col, i), -1, SQLITE_STATIC); break;
-
 
997
                    case SQLITE_INTEGER:
-
 
998
                    default:  /* runtime error if we're wrong */
-
 
999
                        sqlite3_bind_int(stmt, j+2, INTEGER(col)[i]); break;
1000
            
1000
                }
1001
        }
1001
            }
1002
 
1002
 
-
 
1003
            res = sqlite3_step(stmt);
-
 
1004
            if (res != SQLITE_DONE) { /* row name pk conflict */
-
 
1005
                rowname = CHAR_ELT(rownames, i);
-
 
1006
                for (j = 1; res != SQLITE_DONE; j++) { /* _normally_, j shouldn't overflow */
-
 
1007
                    sqlite3_reset(stmt);
-
 
1008
                    sprintf(g_sql_buf[2], "%s-%d", rowname, j);
-
 
1009
                    sqlite3_bind_text(stmt, 1, g_sql_buf[2], -1, SQLITE_STATIC);
-
 
1010
                    res = sqlite3_step(stmt);
-
 
1011
                }
-
 
1012
            }
-
 
1013
            sqlite3_reset(stmt);
-
 
1014
        }
1003
 
1015
 
-
 
1016
        sqlite3_finalize(stmt);
-
 
1017
        ret = sdf;
1004
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1018
    } else if (strcmp(class,"sqlite.data.frame") == 0) {
1005
    }
1019
    }
1006
 
1020
 
1007
    return ret;
1021
    return ret;
1008
}
1022
}
1009
 
1023
 
1010
 
1024
 
-
 
1025
SEXP sdf_get_iname(SEXP sdf) {
-
 
1026
    return _getListElement(sdf, "iname");
-
 
1027
}
-
 
1028
 
1011
SEXP sopen(SEXP name) {
1029
SEXP sopen(SEXP name) {
1012
    char *filename;
1030
    char *filename;
1013
    
1031
    
1014
    if (IS_CHARACTER(name)) {
1032
    if (IS_CHARACTER(name)) {
1015
        filename = CHAR(STRING_ELT(name,0));
1033
        filename = CHAR(STRING_ELT(name,0));
1016
        Rprintf("%s\n", filename);
1034
        Rprintf("%s\n", filename);
1017
    }
1035
    }
1018
    /* sqlite3 *db;
1036
    /* sqlite3 *db;
1019
    int res = sqlite3_open(filename, &db); */
1037
    int res = sqlite3_open(filename, &db); */
1020
    SEXP ret;
1038
    SEXP ret;
1021
    PROTECT(ret = NEW_LOGICAL(1));
1039
    PROTECT(ret = NEW_LOGICAL(1));
1022
    LOGICAL(ret)[0] = IS_CHARACTER(name);
1040
    LOGICAL(ret)[0] = IS_CHARACTER(name);
1023
    /* sqlite3_close(db); */ 
1041
    /* sqlite3_close(db); */ 
1024
    UNPROTECT(1);
1042
    UNPROTECT(1);
1025
    return ret;
1043
    return ret;
1026
}
1044
}