Index: dbfile.scm ================================================================== --- dbfile.scm +++ dbfile.scm @@ -204,12 +204,12 @@ ;; if db not open, open inmem, rundb and sync then return inmem ;; inuse gets set automatically for rundb's ;; (define (dbfile:get-dbdat dbstruct run-id) (let* ((subdb (dbfile:get-subdb dbstruct run-id))) - (if (or (not subdb) stack-empty? (dbr:subdb-dbstack subdb)) - #f + (if (stack-empty? (dbr:subdb-dbstack subdb)) + #f (stack-pop! (dbr:subdb-dbstack subdb))))) ;; return a previously opened db handle to the stack of available handles (define (dbfile:add-dbdat dbstruct run-id dbdat) (let* ((subdb (dbfile:get-subdb dbstruct run-id))) @@ -222,17 +222,15 @@ (areapath (dbr:dbstruct-areapath dbstruct)) (tmppath (dbr:dbstruct-tmppath dbstruct)) (mtdbpath (dbfile:run-id->path areapath run-id)) (tmpdbpath (dbfile:run-id->path tmppath run-id)) (mtdbdat (dbfile:open-sqlite3-db mtdbpath init-proc)) - (tmpdbdat (dbfile:open-sqlite3-db tmpdbpath init-proc)) ;; push this on the stack (newsubdb (make-dbr:subdb dbname: dbname mtdbfile: mtdbpath tmpdbfile: tmpdbpath mtdbdat: mtdbdat))) (dbfile:set-subdb dbstruct run-id newsubdb) - (dbfile:add-dbdat dbstruct run-id tmpdbdat) newsubdb)) ;; return the new subdb - but shouldn't really use it ;; returns dbdat with dbh and dbfilepath ;; 1. if needed setup the subdb for the given run-id ;; 2. if there is no existing db handle in the stack @@ -268,11 +266,11 @@ (init-proc db)) #;(dbfile:lock-create-open dbpath (lambda (db) (init-proc db)))) (write-access (file-write-access? dbpath))) - (dbfile:print-err "db:open-sqlite-db "dbpath) + (dbfile:print-err "db:open-sqlite-db "dbpath " db: " db) #;(if (and dbexists (not write-access)) (set! *db-write-access* #f)) ;; (cons db dbpath))) (make-dbr:dbdat dbfile: dbpath dbh: db read-only: (not write-access)))) Index: tests/simplerun/thebeginning.scm ================================================================== --- tests/simplerun/thebeginning.scm +++ tests/simplerun/thebeginning.scm @@ -2,30 +2,36 @@ (import dbfile) (trace-call-sites #t) (trace ;; dbfile:setup - ;; dbfile:open-sqlite3-db + dbfile:open-sqlite3-db ;; dbfile:init-subdb ;; dbfile:add-dbdat + ;; db:initialize-main-db ;; dbfile:set-subdb ;; db:with-db ;; dbfile:get-subdb ) (define tmpdir (common:get-db-tmp-area)) (test #f #t (dbr:dbstruct? (dbfile:setup #t *toppath* tmpdir))) (test #f #t (dbr:dbstruct? (db:setup #t))) (define dbstruct *dbstruct-dbs*) -(test #f #f (dbfile:get-subdb dbstruct #f)) ;; get main.db (never opened yet) -(test #f #f (dbfile:get-subdb dbstruct 1)) ;; get 1.db -(test #f #t (hash-table? (dbr:dbstruct-subdbs dbstruct))) -(test #f #t (dbr:dbdat? (dbfile:open-db *dbstruct-dbs* #f db:initialize-main-db))) -(test #f '("SYSTEM" "RELEASE") (db:get-keys *dbstruct-dbs*)) -(test #f #f (dbr:dbdat? (dbfile:get-dbdat dbstruct 1))) ;; not open yet -(test #f #f (sqlite3:database? (db:open-db dbstruct #f))) -(test #f #f (sqlite3:database? (db:open-db dbstruct 1))) - -;; (test #f #t (stack? (dbr:subdb-dbstack subdb -;; test #f #f (db:get-subdb dbstruct 1)) -;; -;; ; (test #f #f (stack? (dbr:subdb-dbstack subdb))) +(test #f #t (dbr:dbdat? (dbfile:open-db *dbstruct-dbs* #f db:initialize-main-db))) +(define mydbdat (dbfile:open-db *dbstruct-dbs* #f db:initialize-main-db)) +(dbfile:add-dbdat dbstruct #f mydbdat) +(test #f #t (dbr:subdb? (dbfile:get-subdb dbstruct #f))) +(test #f #t (dbr:dbdat? (dbfile:get-dbdat dbstruct #f))) +(test #f #f (dbr:dbdat? (dbfile:get-dbdat dbstruct #f))) ;; stack empty so should fail. + +(test #f #t (hash-table? (dbr:dbstruct-subdbs dbstruct))) +(test #f #t (stack? (dbr:subdb-dbstack (dbfile:get-subdb dbstruct #f)))) +(test #f '("SYSTEM" "RELEASE") (db:get-keys *dbstruct-dbs*)) + + +(test #f #t (dbr:dbdat? (dbfile:open-db dbstruct 1 db:initialize-main-db))) +(define rundbdat (dbfile:open-db dbstruct 1 db:initialize-main-db)) +(dbfile:add-dbdat dbstruct 1 rundbdat) +(test #f #t (dbr:subdb? (dbfile:get-subdb dbstruct 1))) +(test #f #t (dbr:dbdat? (dbfile:get-dbdat dbstruct 1))) +