The R Project SVN R-packages

Rev

Rev 3456 | Rev 3509 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3456 Rev 3471
Line 87... Line 87...
87
               (strcmp(sqlite3_column_name(stmt, 1), "value") == 0) &&
87
               (strcmp(sqlite3_column_name(stmt, 1), "value") == 0) &&
88
               (strcmp(sqlite3_column_decltype(stmt, 1), "text") == 0))) ? ret : NULL ;
88
               (strcmp(sqlite3_column_decltype(stmt, 1), "text") == 0))) ? ret : NULL ;
89
        
89
        
90
        if (ret == NULL) goto _is_sdf_cleanup;
90
        if (ret == NULL) goto _is_sdf_cleanup;
91
 
91
 
-
 
92
        /* get internal name. we are assuming here that the 1st row of the attributes
92
        /* get internal name */
93
         * table always contains the column name */
93
        res = sqlite3_step(stmt);
94
        res = sqlite3_step(stmt);
94
        ret = (res == SQLITE_ROW) ? ret : NULL;
95
        ret = (res == SQLITE_ROW) ? ret : NULL;
95
        if (ret == NULL) goto _is_sdf_cleanup;
96
        if (ret == NULL) goto _is_sdf_cleanup;
96
 
97
 
97
        /* copy to buf2, because when we finalize stmt, we won't be sure
98
        /* copy to buf2, because when we finalize stmt, we won't be sure
Line 188... Line 189...
188
     */
189
     */
189
    filename = R_alloc(18, sizeof(char)); /* workspace10000.db\0 */
190
    filename = R_alloc(18, sizeof(char)); /* workspace10000.db\0 */
190
    sprintf(filename, "%s.db", basename);
191
    sprintf(filename, "%s.db", basename);
191
    while(_file_exists(filename) && file_idx < 10000) {
192
    while(_file_exists(filename) && file_idx < 10000) {
192
        if ((g_workspace = _is_workspace(filename)) != NULL) break;
193
        if ((g_workspace = _is_workspace(filename)) != NULL) break;
193
        Rprintf("%s is not a workspace\n", filename);
194
        warning("%s is not a SQLiteDF workspace\n", filename);
194
        sprintf(filename, "%s%d.db", basename, ++file_idx);
195
        sprintf(filename, "%s%d.db", basename, ++file_idx);
195
    }
196
    }
196
 
197
 
197
    if ((g_workspace == NULL) && (file_idx < 10000)) {
198
    if ((g_workspace == NULL) && (file_idx < 10000)) {
198
        /* no workspace found but there are still "available" file name */
199
        /* no workspace found but there are still "available" file name */
199
        /* if (file_idx) warn("workspace will be stored at #{filename}") */
200
        /* if (file_idx) warn("workspace will be stored at #{filename}") */
200
        sqlite3_open(filename, &g_workspace);
201
        sqlite3_open(filename, &g_workspace);
201
        _sqlite_exec("create table workspace(rel_filename text, full_filename text,"
202
        _sqlite_exec("create table workspace(rel_filename text, full_filename text,"
202
               "internal_name text, loaded bit, uses int, used bit)");
203
               "internal_name text unique, loaded bit, uses int, used bit)");
203
        ret = ScalarLogical(TRUE);
204
        ret = ScalarLogical(TRUE);
204
    } else if (g_workspace != NULL) {
205
    } else if (g_workspace != NULL) {
205
        /* a valid workspace has been found, load each of the tables */
206
        /* a valid workspace has been found, load each of the tables */
206
        int res, nrows, ncols; 
207
        int res, nrows, ncols; 
207
        char **result_set, *fname, *iname;
208
        char **result_set, *fname, *iname;
Line 244... Line 245...
244
}
245
}
245
        
246
        
246
int USE_SDF1(const char *iname, int exists, int protect) {
247
int USE_SDF1(const char *iname, int exists, int protect) {
247
    sqlite3_stmt *stmt;
248
    sqlite3_stmt *stmt;
248
    int loaded, res;
249
    int loaded, res;
-
 
250
    char *iname_final = (char *)iname;
249
 
251
 
250
    /* determine if iname is loaded */
252
    /* determine if iname is loaded */
251
    sprintf(g_sql_buf[2], "select loaded, rel_filename from workspace where internal_name='%s'", iname);
253
    sprintf(g_sql_buf[2], "select loaded, rel_filename from workspace where internal_name='%s'", iname);
252
    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
254
    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
253
    res = sqlite3_step(stmt);
255
    res = sqlite3_step(stmt);
254
    if (exists && res != SQLITE_ROW) { 
256
    if (exists && res != SQLITE_ROW) { 
-
 
257
        error("No SDF with name '%s' found in the workspace.\n", iname);
255
        sqlite3_finalize(stmt); 
258
        sqlite3_finalize(stmt); 
256
        Rprintf("Error: No SDF with name '%s' found in the workspace.\n", iname);
-
 
257
        return 0; 
259
        return 0; 
258
    }
260
    }
259
    loaded = (res == SQLITE_ROW) ? sqlite3_column_int(stmt, 0) : 0;
261
    loaded = (res == SQLITE_ROW) ? sqlite3_column_int(stmt, 0) : 0;
260
    strcpy(g_sql_buf[1], (char*)sqlite3_column_text(stmt, 1)); /* ref filename */
262
    strcpy(g_sql_buf[1], (char*)sqlite3_column_text(stmt, 1)); /* ref filename */
261
    sqlite3_finalize(stmt);
263
    sqlite3_finalize(stmt);
Line 263... Line 265...
263
    if (!loaded) {
265
    if (!loaded) {
264
        char *fname = g_sql_buf[1];
266
        char *fname = g_sql_buf[1];
265
 
267
 
266
        /* test first if we will be loading valid sdf */
268
        /* test first if we will be loading valid sdf */
267
        if (exists && !_file_exists(fname)) {
269
        if (exists && !_file_exists(fname)) {
268
            Rprintf("Error: SDF %s does not exist.\n", iname);
-
 
269
            _delete_sdf2(iname);
270
            _delete_sdf2(iname);
-
 
271
            warning("SDF %s does not exist.\n", iname);
270
            return 0;
272
            return 0;
271
        }
273
        }
272
 
274
 
273
        if (exists && _is_sdf2(fname) == NULL) {
275
        if (exists && _is_sdf2(fname) == NULL) {
274
            Rprintf("Error: %s is not a valid SDF.\n", fname);
-
 
275
            _delete_sdf2(iname);
276
            _delete_sdf2(iname);
-
 
277
            warning("%s is not a valid SDF.\n", fname);
276
            return 0;
278
            return 0;
277
        }
279
        }
278
 
280
 
-
 
281
        /* g_sql_buf[2] contains the internal name as stored in the sdf
-
 
282
         * attribute. sync workspace record to that if needed */
-
 
283
        if (exists && strcmp(iname, g_sql_buf[2]) != 0) {
-
 
284
            if (!_is_r_sym(g_sql_buf[2])) {
-
 
285
                warning("name stored in SDF is not valid. Ignoring that name...");
-
 
286
                goto __out_of_syncname;
-
 
287
            }
-
 
288
            iname_final = R_alloc(strlen(g_sql_buf[2]) + 1, sizeof(g_sql_buf[2]));
-
 
289
            strcpy(iname_final, g_sql_buf[2]);
-
 
290
 
-
 
291
            sprintf(g_sql_buf[2], "update workspace set internal_name='%s' where "
-
 
292
                        "internal_name='%s'", iname_final, iname);
-
 
293
            _sqlite_error(_sqlite_exec(g_sql_buf[2]));
-
 
294
 
-
 
295
        }
-
 
296
__out_of_syncname:
-
 
297
 
279
        /* unload sdf's if we run to the MAX_ATTACHED limit */
298
        /* unload sdf's if we run to the MAX_ATTACHED limit */
280
        _prepare_attach2();
299
        _prepare_attach2();
281
 
300
 
282
        /* set loaded to true */
301
        /* set loaded to true */
283
        sprintf(g_sql_buf[2], "update workspace set loaded=1 where internal_name='%s'", iname);
302
        sprintf(g_sql_buf[2], "update workspace set loaded=1 where internal_name='%s'", iname_final);
284
        res = _sqlite_exec(g_sql_buf[2]);
303
        res = _sqlite_exec(g_sql_buf[2]);
285
        _sqlite_error(res);
304
        _sqlite_error(res);
286
 
305
 
287
        /* load, using relative path */
306
        /* load, using relative path */
288
        sprintf(g_sql_buf[2], "attach '%s' as [%s]", fname, iname);
307
        sprintf(g_sql_buf[2], "attach '%s' as [%s]", fname, iname_final);
289
        res = _sqlite_exec(g_sql_buf[2]);
308
        res = _sqlite_exec(g_sql_buf[2]);
290
        if (_sqlite_error(res)) return 0;
309
        if (_sqlite_error(res)) return 0;
291
    }
310
    }
292
 
311
 
293
    /* upgrade its uses count, and protect if necessary */
312
    /* upgrade its uses count, and protect if necessary */
294
    if (protect) protect = 1; 
313
    if (protect) protect = 1; 
295
    sprintf(g_sql_buf[2], "update workspace set uses=uses+1, used=%d" 
314
    sprintf(g_sql_buf[2], "update workspace set uses=uses+1, used=%d" 
296
            " where internal_name='%s'", protect, iname);
315
            " where internal_name='%s'", protect, iname_final);
297
    _sqlite_exec(g_sql_buf[2]);
316
    _sqlite_exec(g_sql_buf[2]);
298
    return 1;
317
    return 1;
299
}
318
}
300
 
319
 
301
int UNUSE_SDF2(const char *iname) {
320
int UNUSE_SDF2(const char *iname) {