The R Project SVN R-packages

Rev

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

Rev 3324 Rev 3358
Line 213... Line 213...
213
            for (i = 1; i <= maxrows && i < nrows; i++) {
213
            for (i = 1; i <= maxrows && i < nrows; i++) {
214
                /* we will use rel_filename in opening the file, so that
214
                /* we will use rel_filename in opening the file, so that
215
                 * if the user is "sensible", files will be dir agnostic */
215
                 * if the user is "sensible", files will be dir agnostic */
216
                fname = result_set[i*ncols]; iname = result_set[i*ncols+1];
216
                fname = result_set[i*ncols]; iname = result_set[i*ncols+1];
217
 
217
 
218
                if (!USE_SDF(iname, TRUE)) maxrows++;
218
                if (!USE_SDF1(iname, TRUE)) maxrows++;
219
 
219
 
220
                /* update full_filename */
220
                /* update full_filename */
221
                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 "
222
                        "where iname='%s'", _get_full_pathname2(fname), iname);
222
                        "where iname='%s'", _get_full_pathname2(fname), iname);
223
                _sqlite_exec(g_sql_buf[0]);
223
                _sqlite_exec(g_sql_buf[0]);
Line 235... Line 235...
235
    /* register sqlite math functions */
235
    /* register sqlite math functions */
236
    __register_vector_math();
236
    __register_vector_math();
237
    return ret;
237
    return ret;
238
}
238
}
239
        
239
        
240
int USE_SDF(const char *iname, int exists) {
240
int USE_SDF1(const char *iname, int exists) {
241
    sqlite3_stmt *stmt;
241
    sqlite3_stmt *stmt;
242
    int loaded, res;
242
    int loaded, res;
243
 
243
 
244
    /* determine if iname is loaded */
244
    /* determine if iname is loaded */
245
    sprintf(g_sql_buf[0], "select loaded, rel_filename from workspace where internal_name='%s'", iname);
245
    sprintf(g_sql_buf[2], "select loaded, rel_filename from workspace where internal_name='%s'", iname);
246
    sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
246
    sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, NULL);
247
    res = sqlite3_step(stmt);
247
    res = sqlite3_step(stmt);
248
    if (exists && res != SQLITE_ROW) { 
248
    if (exists && res != SQLITE_ROW) { 
249
        sqlite3_finalize(stmt); 
249
        sqlite3_finalize(stmt); 
250
        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);
251
        return 0; 
251
        return 0; 
252
    }
252
    }
253
    loaded = sqlite3_column_int(stmt, 0);
253
    loaded = sqlite3_column_int(stmt, 0);
254
    strcpy(g_sql_buf[0], (char*)sqlite3_column_text(stmt, 1)); /* ref filename */
254
    strcpy(g_sql_buf[1], (char*)sqlite3_column_text(stmt, 1)); /* ref filename */
255
    sqlite3_finalize(stmt);
255
    sqlite3_finalize(stmt);
256
 
256
 
257
    if (!loaded) {
257
    if (!loaded) {
258
        /* test if we have to detach somebody */
-
 
259
        int nloaded;
258
        int nloaded;
260
        char *fname = g_sql_buf[0];
259
        char *fname = g_sql_buf[1];
261
 
260
 
-
 
261
        /* test first if we will be loading valid sdf */
262
        if (exists && !_file_exists(fname)) {
262
        if (exists && !_file_exists(fname)) {
263
            Rprintf("Error: SDF %s does not exist.\n", iname);
263
            Rprintf("Error: SDF %s does not exist.\n", iname);
264
            _delete_sdf2(iname);
264
            _delete_sdf2(iname);
265
            return 0;
265
            return 0;
266
        }
266
        }
Line 269... Line 269...
269
            Rprintf("Error: %s is not a valid SDF.\n", fname);
269
            Rprintf("Error: %s is not a valid SDF.\n", fname);
270
            _delete_sdf2(iname);
270
            _delete_sdf2(iname);
271
            return 0;
271
            return 0;
272
        }
272
        }
273
 
273
 
274
        /* juggle b/w buffers */
-
 
275
        strcpy(g_sql_buf[2], g_sql_buf[0]);
-
 
276
        fname = g_sql_buf[2];
-
 
277
 
-
 
278
        sqlite3_prepare(g_workspace, "select count(*) from workspace where loaded=1",
274
        sqlite3_prepare(g_workspace, "select count(*) from workspace where loaded=1",
279
                -1, &stmt, NULL);
275
                -1, &stmt, NULL);
280
        sqlite3_step(stmt);
276
        sqlite3_step(stmt);
281
        nloaded = sqlite3_column_int(stmt, 0);
277
        nloaded = sqlite3_column_int(stmt, 0);
282
        sqlite3_finalize(stmt);
278
        sqlite3_finalize(stmt);
283
 
279
 
-
 
280
        /* test if we have to detach somebody */
284
        if (nloaded == MAX_ATTACHED) {
281
        if (nloaded == MAX_ATTACHED) {
285
            /* have to evict */
282
            /* have to evict */
286
            sqlite3_prepare(g_workspace, "select internal_name from workspace "
283
            sqlite3_prepare(g_workspace, "select internal_name from workspace "
287
                    "where loaded=1 order by uses", -1, &stmt, NULL);
284
                    "where loaded=1 order by uses", -1, &stmt, NULL);
288
            sqlite3_step(stmt);
285
            sqlite3_step(stmt);
289
            char *iname;
286
            char *iname2;
290
            iname = (char *)sqlite3_column_text(stmt, 0);
287
            iname2 = (char *)sqlite3_column_text(stmt, 0);
291
            sprintf(g_sql_buf[0], "detach [%s]", iname);
288
            sprintf(g_sql_buf[2], "detach [%s]", iname2);
-
 
289
            _sqlite_exec(g_sql_buf[2]);
292
            sprintf(g_sql_buf[1], "update workspace set loaded=0 where internal_name='%s'", iname);
290
            sprintf(g_sql_buf[2], "update workspace set loaded=0 where internal_name='%s'", iname);
-
 
291
            _sqlite_exec(g_sql_buf[2]);
293
            sqlite3_finalize(stmt);
292
            sqlite3_finalize(stmt);
294
 
-
 
295
            _sqlite_exec(g_sql_buf[0]);
-
 
296
            _sqlite_exec(g_sql_buf[1]);
-
 
297
        }
293
        }
298
 
294
 
299
        /* set loaded to true */
295
        /* set loaded to true */
300
        sprintf(g_sql_buf[0], "update workspace set loaded=1 where internal_name='%s'", iname);
296
        sprintf(g_sql_buf[2], "update workspace set loaded=1 where internal_name='%s'", iname);
301
        res = _sqlite_exec(g_sql_buf[0]);
297
        res = _sqlite_exec(g_sql_buf[2]);
302
        _sqlite_error(res);
298
        _sqlite_error(res);
303
 
299
 
304
        /* load, using relative path */
300
        /* load, using relative path */
305
        sprintf(g_sql_buf[0], "attach '%s' as [%s]", fname, iname);
301
        sprintf(g_sql_buf[2], "attach '%s' as [%s]", fname, iname);
306
        res = _sqlite_exec(g_sql_buf[0]);
302
        res = _sqlite_exec(g_sql_buf[2]);
307
        if (_sqlite_error(res)) return 0;
303
        if (_sqlite_error(res)) return 0;
308
    }
304
    }
309
 
305
 
310
    /* upgrade its uses count */
306
    /* upgrade its uses count */
311
    sprintf(g_sql_buf[0], "update workspace set uses=uses+1 where internal_name='%s'", iname);
307
    sprintf(g_sql_buf[2], "update workspace set uses=uses+1 where internal_name='%s'", iname);
312
    _sqlite_exec(g_sql_buf[0]);
308
    _sqlite_exec(g_sql_buf[2]);
313
    return 1;
309
    return 1;
314
}
310
}
315
 
311
 
316
        
312
        
317
    
313
    
Line 360... Line 356...
360
    }
356
    }
361
 
357
 
362
    char *iname = CHAR(STRING_ELT(name, 0));
358
    char *iname = CHAR(STRING_ELT(name, 0));
363
    SEXP ret;
359
    SEXP ret;
364
 
360
 
365
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
361
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
366
 
362
 
367
    ret = _create_sdf_sexp(iname);
363
    ret = _create_sdf_sexp(iname);
368
    return ret;
364
    return ret;
369
}
365
}
370
 
366
 
Line 442... Line 438...
442
    
438
    
443
    /* add it to workspace */
439
    /* add it to workspace */
444
    res = _add_sdf1(fname, iname);
440
    res = _add_sdf1(fname, iname);
445
    if (_sqlite_error(res)) return R_NilValue;
441
    if (_sqlite_error(res)) return R_NilValue;
446
 
442
 
447
    /* attach using USE_SDF, detach other SDF if necessary */
443
    /* attach using USE_SDF1, detach other SDF if necessary */
448
    USE_SDF(iname, TRUE);
444
    USE_SDF1(iname, TRUE);
449
 
445
 
450
    /* if internal name found in newly-attached-SDF is the same as the
446
    /* if internal name found in newly-attached-SDF is the same as the
451
     * name wanted by the user, do nothing. otherwise, update sdf_attribute
447
     * name wanted by the user, do nothing. otherwise, update sdf_attribute
452
     * on attached-SDF. this is like attachSdf then renameSdf */
448
     * on attached-SDF. this is like attachSdf then renameSdf */
453
    if (iname != iname_orig && strcmp(iname, iname_orig) != 0) {
449
    if (iname != iname_orig && strcmp(iname, iname_orig) != 0) {
Line 498... Line 494...
498
    int res, ret_tmp;
494
    int res, ret_tmp;
499
 
495
 
500
    iname = SDF_INAME(sdf);
496
    iname = SDF_INAME(sdf);
501
    newname = CHAR_ELT(name, 0);
497
    newname = CHAR_ELT(name, 0);
502
    
498
    
503
    if (!USE_SDF(iname, TRUE)) return R_NilValue;
499
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
504
 
500
 
505
    /* check if valid r name */
501
    /* check if valid r name */
506
    if (!_is_r_sym(newname)) {
502
    if (!_is_r_sym(newname)) {
507
        Rprintf("Error: %s is not a valid R symbol.", iname);
503
        Rprintf("Error: %s is not a valid R symbol.", iname);
508
        return R_NilValue;
504
        return R_NilValue;