/ Check-in [93a94798]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the sqlite3_db_filename() interface.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 93a947989b57959aacc37007a143fdf3921793ab
User & Date: drh 2011-11-17 00:56:20
Context
2011-11-17
11:43
Restore the test for sqlite3OsFullPathname() failure that was mistakenly removed when [ceee03c79a] was backed out by [69ec53fc1c]. check-in: 4d3cf9e1 user: drh tags: trunk
00:56
Add the sqlite3_db_filename() interface. check-in: 93a94798 user: drh tags: trunk
2011-11-16
23:29
Back out the [ceee03c79a] change. check-in: 69ec53fc user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/main.c.

  2963   2963   const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
  2964   2964     zFilename += sqlite3Strlen30(zFilename) + 1;
  2965   2965     while( zFilename[0] ){
  2966   2966       int x = strcmp(zFilename, zParam);
  2967   2967       zFilename += sqlite3Strlen30(zFilename) + 1;
  2968   2968       if( x==0 ) return zFilename;
  2969   2969       zFilename += sqlite3Strlen30(zFilename) + 1;
         2970  +  }
         2971  +  return 0;
         2972  +}
         2973  +
         2974  +/*
         2975  +** Return the filename of the database associated with a database
         2976  +** connection.
         2977  +*/
         2978  +const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
         2979  +  int i;
         2980  +  for(i=0; i<db->nDb; i++){
         2981  +    if( db->aDb[i].pBt && sqlite3StrICmp(zDbName, db->aDb[i].zName)==0 ){
         2982  +      return sqlite3BtreeGetFilename(db->aDb[i].pBt);
         2983  +    }
  2970   2984     }
  2971   2985     return 0;
  2972   2986   }

Changes to src/sqlite.h.in.

  4369   4369   ** returned by sqlite3_db_handle is the same [database connection]
  4370   4370   ** that was the first argument
  4371   4371   ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
  4372   4372   ** create the statement in the first place.
  4373   4373   */
  4374   4374   sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
  4375   4375   
         4376  +/*
         4377  +** CAPI3REF: Return The Filename For A Database Connection
         4378  +**
         4379  +** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
         4380  +** associated with database N of connection D.  ^The main database file
         4381  +** has the name "main".  If there is no attached database N on the database
         4382  +** connection D, or if database N is a temporary or in-memory database, then
         4383  +** a NULL pointer is returned.
         4384  +*/
         4385  +const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
         4386  +
  4376   4387   /*
  4377   4388   ** CAPI3REF: Find the next prepared statement
  4378   4389   **
  4379   4390   ** ^This interface returns a pointer to the next [prepared statement] after
  4380   4391   ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
  4381   4392   ** then this interface returns a pointer to the first prepared statement
  4382   4393   ** associated with the database connection pDb.  ^If no prepared statement

Changes to src/test1.c.

  4613   4613       return TCL_ERROR;
  4614   4614     }
  4615   4615     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  4616   4616     rc = sqlite3_db_release_memory(db);
  4617   4617     Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  4618   4618     return TCL_OK;
  4619   4619   }
         4620  +
         4621  +/*
         4622  +** Usage:  sqlite3_db_filename DB DBNAME
         4623  +**
         4624  +** Return the name of a file associated with a database.
         4625  +*/
         4626  +static int test_db_filename(
         4627  +  void * clientData,
         4628  +  Tcl_Interp *interp,
         4629  +  int objc,
         4630  +  Tcl_Obj *CONST objv[]
         4631  +){
         4632  +  sqlite3 *db;
         4633  +  const char *zDbName;
         4634  +  if( objc!=3 ){
         4635  +    Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
         4636  +    return TCL_ERROR;
         4637  +  }
         4638  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
         4639  +  zDbName = Tcl_GetString(objv[2]);
         4640  +  Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0);
         4641  +  return TCL_OK;
         4642  +}
  4620   4643   
  4621   4644   /*
  4622   4645   ** Usage:  sqlite3_soft_heap_limit ?N?
  4623   4646   **
  4624   4647   ** Query or set the soft heap limit for the current thread.  The
  4625   4648   ** limit is only changed if the N is present.  The previous limit
  4626   4649   ** is returned.
................................................................................
  5937   5960        { "sqlite3_sql",                   test_sql           ,0 },
  5938   5961        { "sqlite3_next_stmt",             test_next_stmt     ,0 },
  5939   5962        { "sqlite3_stmt_readonly",         test_stmt_readonly ,0 },
  5940   5963        { "uses_stmt_journal",             uses_stmt_journal ,0 },
  5941   5964   
  5942   5965        { "sqlite3_release_memory",        test_release_memory,     0},
  5943   5966        { "sqlite3_db_release_memory",     test_db_release_memory,  0},
         5967  +     { "sqlite3_db_filename",           test_db_filename,        0},
  5944   5968        { "sqlite3_soft_heap_limit",       test_soft_heap_limit,    0},
  5945   5969        { "sqlite3_thread_cleanup",        test_thread_cleanup,     0},
  5946   5970        { "sqlite3_pager_refcounts",       test_pager_refcounts,    0},
  5947   5971   
  5948   5972        { "sqlite3_load_extension",        test_load_extension,     0},
  5949   5973        { "sqlite3_enable_load_extension", test_enable_load,        0},
  5950   5974        { "sqlite3_extended_result_codes", test_extended_result_codes, 0},

Changes to test/attach.test.

    47     47   } {1 x 2 y}
    48     48   do_test attach-1.3 {
    49     49     execsql {
    50     50       ATTACH DATABASE 'test2.db' AS two;
    51     51       SELECT * FROM two.t2;
    52     52     }
    53     53   } {1 x 2 y}
           54  +
           55  +# Tests for the sqlite3_db_filename interface
           56  +#
           57  +do_test attach-1.3.1 {
           58  +  file tail [sqlite3_db_filename db main]
           59  +} {test.db}
           60  +do_test attach-1.3.2 {
           61  +  file tail [sqlite3_db_filename db MAIN]
           62  +} {test.db}
           63  +do_test attach-1.3.3 {
           64  +  file tail [sqlite3_db_filename db temp]
           65  +} {}
           66  +do_test attach-1.3.4 {
           67  +  file tail [sqlite3_db_filename db two]
           68  +} {test2.db}
           69  +do_test attach-1.3.5 {
           70  +  file tail [sqlite3_db_filename db three]
           71  +} {}
           72  +
    54     73   do_test attach-1.4 {
    55     74     execsql {
    56     75       SELECT * FROM t2;
    57     76     }
    58     77   } {1 x 2 y}
    59     78   do_test attach-1.5 {
    60     79     execsql {