The R Project SVN R-packages

Rev

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

Rev 3308 Rev 3324
Line 109... Line 109...
109
 
109
 
110
    return ret;
110
    return ret;
111
}
111
}
112
 
112
 
113
/* remove an sdf from the workspace */
113
/* remove an sdf from the workspace */
114
void _delete_sdf2(char *iname) {
114
void _delete_sdf2(const char *iname) {
115
    sprintf(g_sql_buf[2], "delete from workspace where internal_name='%s';", iname);
115
    sprintf(g_sql_buf[2], "delete from workspace where internal_name='%s';", iname);
116
    _sqlite_exec(g_sql_buf[2]);
116
    _sqlite_exec(g_sql_buf[2]);
117
}
117
}
118
 
118
 
119
/* add a sdf to the workspace */
119
/* add a sdf to the workspace */
Line 197... Line 197...
197
        /* a valid workspace has been found, load each of the tables */
197
        /* a valid workspace has been found, load each of the tables */
198
        int res, nrows, ncols; 
198
        int res, nrows, ncols; 
199
        char **result_set, *fname, *iname;
199
        char **result_set, *fname, *iname;
200
        
200
        
201
        /* since only 30 can be loaded, sort sdf's by # of uses the last time */
201
        /* since only 30 can be loaded, sort sdf's by # of uses the last time */
202
        res = sqlite3_get_table(g_workspace, "select * from workspace order by uses desc", 
202
        res = sqlite3_get_table(g_workspace, "select rel_filename, internal_name"
-
 
203
                " from workspace order by uses desc", 
203
                &result_set, &nrows, &ncols, NULL);
204
                &result_set, &nrows, &ncols, NULL);
204
 
205
 
205
        /* reset fields uses and loaded to 0 and false */
206
        /* reset fields uses and loaded to 0 and false */
206
        res = _sqlite_exec("update workspace set uses=0, loaded=0");
207
        res = _sqlite_exec("update workspace set uses=0, loaded=0");
207
        _sqlite_error(res);
208
        _sqlite_error(res);
208
        
209
        
209
        if (res == SQLITE_OK && nrows >= 1 && ncols == WORKSPACE_COLUMNS) {
210
        if (res == SQLITE_OK && nrows >= 1 && ncols == 2) {
210
            int maxrows;  /* actual # of sdfs to be attached */
211
            int maxrows;  /* actual # of sdfs to be attached */
211
            maxrows = (nrows > 30) ? 30 : nrows;
212
            maxrows = (nrows > 30) ? 30 : nrows;
212
            for (i = 1; i <= maxrows && i < nrows; i++) {
213
            for (i = 1; i <= maxrows && i < nrows; i++) {
213
                /* we will use rel_filename in opening the file, so that
214
                /* we will use rel_filename in opening the file, so that
214
                 * if the user is "sensible", files will be dir agnostic */
215
                 * if the user is "sensible", files will be dir agnostic */
215
                fname = result_set[i*ncols]; iname = result_set[i*ncols+2];
216
                fname = result_set[i*ncols]; iname = result_set[i*ncols+1];
216
                
-
 
217
                if (!_file_exists(fname)) {
-
 
218
                    Rprintf("Warning: SDF %s does not exist.\n", iname);
-
 
219
                    _delete_sdf2(iname); maxrows++;
-
 
220
                    continue;
-
 
221
                }
-
 
222
 
-
 
223
                if (_is_sdf2(fname) == NULL) {
-
 
224
                    Rprintf("Warning: %s is not a valid SDF.\n", fname);
-
 
225
                    _delete_sdf2(iname); maxrows++;
-
 
226
                    continue;
-
 
227
                }
-
 
228
 
217
 
229
                /* attach db */
-
 
230
                sprintf(g_sql_buf[0], "attach '%s' as %s", fname, iname);
-
 
231
                _sqlite_exec(g_sql_buf[0]);
218
                if (!USE_SDF(iname, TRUE)) maxrows++;
232
 
219
 
233
                /* update full_filename */
220
                /* update full_filename */
234
                sprintf(g_sql_buf[0], "update workspace set full_filename='%s', loaded=1 "
221
                sprintf(g_sql_buf[0], "update workspace set full_filename='%s', loaded=1 "
235
                        "where iname='%s'", _get_full_pathname2(fname), iname);
222
                        "where iname='%s'", _get_full_pathname2(fname), iname);
236
                _sqlite_exec(g_sql_buf[0]);
223
                _sqlite_exec(g_sql_buf[0]);
Line 248... Line 235...
248
    /* register sqlite math functions */
235
    /* register sqlite math functions */
249
    __register_vector_math();
236
    __register_vector_math();
250
    return ret;
237
    return ret;
251
}
238
}
252
        
239
        
253
int USE_SDF(const char *iname) {
240
int USE_SDF(const char *iname, int exists) {
254
    sqlite3_stmt *stmt;
241
    sqlite3_stmt *stmt;
255
    int loaded, res;
242
    int loaded, res;
256
 
243
 
257
    /* determine if iname is loaded */
244
    /* determine if iname is loaded */
258
    sprintf(g_sql_buf[0], "select loaded, rel_filename from workspace where internal_name='%s'", iname);
245
    sprintf(g_sql_buf[0], "select loaded, rel_filename from workspace where internal_name='%s'", iname);
259
    sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
246
    sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
260
    res = sqlite3_step(stmt);
247
    res = sqlite3_step(stmt);
261
    if (res != SQLITE_ROW) { 
248
    if (exists && res != SQLITE_ROW) { 
262
        sqlite3_finalize(stmt); 
249
        sqlite3_finalize(stmt); 
263
        Rprintf("Error: No SDF with name '%s' found in the workspace.\n", iname);
250
        Rprintf("Error: No SDF with name '%s' found in the workspace.\n", iname);
264
        return 0; 
251
        return 0; 
265
    }
252
    }
266
    loaded = sqlite3_column_int(stmt, 0);
253
    loaded = sqlite3_column_int(stmt, 0);
267
    strcpy(g_sql_buf[2], (char*)sqlite3_column_text(stmt, 1));
254
    strcpy(g_sql_buf[0], (char*)sqlite3_column_text(stmt, 1)); /* ref filename */
268
    sqlite3_finalize(stmt);
255
    sqlite3_finalize(stmt);
269
 
256
 
270
    if (!loaded) {
257
    if (!loaded) {
271
        /* test if we have to detach somebody */
258
        /* test if we have to detach somebody */
272
        int nloaded;
259
        int nloaded;
-
 
260
        char *fname = g_sql_buf[0];
-
 
261
 
-
 
262
        if (exists && !_file_exists(fname)) {
-
 
263
            Rprintf("Error: SDF %s does not exist.\n", iname);
-
 
264
            _delete_sdf2(iname);
-
 
265
            return 0;
-
 
266
        }
-
 
267
 
-
 
268
        if (exists && _is_sdf2(fname) == NULL) {
-
 
269
            Rprintf("Error: %s is not a valid SDF.\n", fname);
-
 
270
            _delete_sdf2(iname);
-
 
271
            return 0;
-
 
272
        }
-
 
273
 
-
 
274
        /* juggle b/w buffers */
-
 
275
        strcpy(g_sql_buf[2], g_sql_buf[0]);
-
 
276
        fname = g_sql_buf[2];
-
 
277
 
273
        sqlite3_prepare(g_workspace, "select count(*) from workspace where loaded=1",
278
        sqlite3_prepare(g_workspace, "select count(*) from workspace where loaded=1",
274
                -1, &stmt, NULL);
279
                -1, &stmt, NULL);
275
        sqlite3_step(stmt);
280
        sqlite3_step(stmt);
276
        nloaded = sqlite3_column_int(stmt, 0);
281
        nloaded = sqlite3_column_int(stmt, 0);
277
        sqlite3_finalize(stmt);
282
        sqlite3_finalize(stmt);
Line 291... Line 296...
291
            _sqlite_exec(g_sql_buf[1]);
296
            _sqlite_exec(g_sql_buf[1]);
292
        }
297
        }
293
 
298
 
294
        /* set loaded to true */
299
        /* set loaded to true */
295
        sprintf(g_sql_buf[0], "update workspace set loaded=1 where internal_name='%s'", iname);
300
        sprintf(g_sql_buf[0], "update workspace set loaded=1 where internal_name='%s'", iname);
296
        _sqlite_exec(g_sql_buf[0]);
301
        res = _sqlite_exec(g_sql_buf[0]);
-
 
302
        _sqlite_error(res);
297
 
303
 
298
        /* load, using relative path */
304
        /* load, using relative path */
299
        sprintf(g_sql_buf[0], "attach '%s' as [%s]", g_sql_buf[2], iname);
305
        sprintf(g_sql_buf[0], "attach '%s' as [%s]", fname, iname);
300
        res = _sqlite_exec(g_sql_buf[0]);
306
        res = _sqlite_exec(g_sql_buf[0]);
301
        if (_sqlite_error(res)) return 0;
307
        if (_sqlite_error(res)) return 0;
302
    }
308
    }
303
 
309
 
304
    /* upgrade its uses count */
310
    /* upgrade its uses count */
Line 354... Line 360...
354
    }
360
    }
355
 
361
 
356
    char *iname = CHAR(STRING_ELT(name, 0));
362
    char *iname = CHAR(STRING_ELT(name, 0));
357
    SEXP ret;
363
    SEXP ret;
358
 
364
 
359
    if (!USE_SDF(iname)) return R_NilValue;
365
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
360
 
366
 
361
    ret = _create_sdf_sexp(iname);
367
    ret = _create_sdf_sexp(iname);
362
    return ret;
368
    return ret;
363
}
369
}
364
 
370
 
Line 437... Line 443...
437
    /* add it to workspace */
443
    /* add it to workspace */
438
    res = _add_sdf1(fname, iname);
444
    res = _add_sdf1(fname, iname);
439
    if (_sqlite_error(res)) return R_NilValue;
445
    if (_sqlite_error(res)) return R_NilValue;
440
 
446
 
441
    /* attach using USE_SDF, detach other SDF if necessary */
447
    /* attach using USE_SDF, detach other SDF if necessary */
442
    USE_SDF(iname);
448
    USE_SDF(iname, TRUE);
443
 
449
 
444
    /* if internal name found in newly-attached-SDF is the same as the
450
    /* if internal name found in newly-attached-SDF is the same as the
445
     * name wanted by the user, do nothing. otherwise, update sdf_attribute
451
     * name wanted by the user, do nothing. otherwise, update sdf_attribute
446
     * on attached-SDF. this is like attachSdf then renameSdf */
452
     * on attached-SDF. this is like attachSdf then renameSdf */
447
    if (iname != iname_orig && strcmp(iname, iname_orig) != 0) {
453
    if (iname != iname_orig && strcmp(iname, iname_orig) != 0) {
Line 492... Line 498...
492
    int res, ret_tmp;
498
    int res, ret_tmp;
493
 
499
 
494
    iname = SDF_INAME(sdf);
500
    iname = SDF_INAME(sdf);
495
    newname = CHAR_ELT(name, 0);
501
    newname = CHAR_ELT(name, 0);
496
    
502
    
497
    if (!USE_SDF(iname)) return R_NilValue;
503
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
498
 
504
 
499
    /* check if valid r name */
505
    /* check if valid r name */
500
    if (!_is_r_sym(newname)) {
506
    if (!_is_r_sym(newname)) {
501
        Rprintf("Error: %s is not a valid R symbol.", iname);
507
        Rprintf("Error: %s is not a valid R symbol.", iname);
502
        return R_NilValue;
508
        return R_NilValue;
Line 506... Line 512...
506
    if (_sdf_exists2(newname)) { /* name is already taken */
512
    if (_sdf_exists2(newname)) { /* name is already taken */
507
        Rprintf("Error: the name \"%s\" is already taken.\n", newname);
513
        Rprintf("Error: the name \"%s\" is already taken.\n", newname);
508
        return R_NilValue;
514
        return R_NilValue;
509
    }
515
    }
510
 
516
 
511
    /* get path of the sdf file, because we're goint to detach it */
517
    /* get path of the sdf file, because we're going to detach it */
512
    path = _get_sdf_detail2(iname, SDF_DETAIL_FULLFILENAME);
518
    path = _get_sdf_detail2(iname, SDF_DETAIL_FULLFILENAME);
513
    if (path == NULL) {
519
    if (path == NULL) {
514
        Rprintf("Error: no sdf named \"%s\" exists.\n", iname);
520
        Rprintf("Error: no sdf named \"%s\" exists.\n", iname);
515
        return R_NilValue;
521
        return R_NilValue;
516
    }
522
    }