Posted to tcl by AspiringTickler at Sun Oct 03 10:33:32 GMT 2021view raw
- proc withdb { var handler } {
- sqlite3 $var "db"
- $var timeout 10000
- try { uplevel $handler } finally { $var close }
- }
- proc someImport {id} {
- withdb db {
- if [db eval { SELECT COUNT() FROM foo WHERE id = $id }] {
- puts [format "%s already exists" $id]
- return # the return here should return someImport (and does with return -level 2), but not without
- }
- db eval { INSERT OR IGNORE INTO foo(id) VALUES($id) }
- }
- }
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]"