Posted to tcl by AspiringTickler at Sun Oct 03 10:33:32 GMT 2021view raw

  1. proc withdb { var handler } {
  2. sqlite3 $var "db"
  3. $var timeout 10000
  4. try { uplevel $handler } finally { $var close }
  5. }
  6.  
  7. proc someImport {id} {
  8. withdb db {
  9. if [db eval { SELECT COUNT() FROM foo WHERE id = $id }] {
  10. puts [format "%s already exists" $id]
  11. return # the return here should return someImport (and does with return -level 2), but not without
  12. }
  13. db eval { INSERT OR IGNORE INTO foo(id) VALUES($id) }
  14. }
  15. }

Comments

Posted by AspiringTickler at Sun Oct 03 10:59:23 GMT 2021 [text] [code]

``` proc withdb { var handler } { sqlite3 $var "db" $var timeout 10000 tailcall try $handler finally [list $var close] } ``` Usage of tailcall and try was suggested by aspect1 and works exactly as I intended. This technique is also mentioned here: https://wiki.tcl-lang.org/page/try under the section "Use with [tailcall]"