Index: openacs-4/packages/acs-tcl/tcl/00-database-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/Attic/00-database-procs.tcl,v diff -u -r1.126.2.1 -r1.126.2.2 --- openacs-4/packages/acs-tcl/tcl/00-database-procs.tcl 14 Feb 2019 16:15:00 -0000 1.126.2.1 +++ openacs-4/packages/acs-tcl/tcl/00-database-procs.tcl 18 Feb 2019 12:40:09 -0000 1.126.2.2 @@ -1596,7 +1596,7 @@ code_block once for each row with variables set to column values (or a set or array populated if -column_array or column_set is specified). If the query returns no rows, executes - if_no_rows_block (if provided).

+ if_no_rows_block (if provided). In place of 'if_no_rows' also the 'else' keyword can be used.

Example: @@ -1618,10 +1618,8 @@ set code_block [lindex $args 0] } elseif { $arglength == 3 } { # Should have code block + if_no_rows + code block. - if { [lindex $args 1] ne "if_no_rows" - && [lindex $args 1] ne "else" - } { - return -code error "Expected if_no_rows as second-to-last argument" + if { [lindex $args 1] ni {"if_no_rows" "else"}} { + return -code error "Expected if_no_rows or else as second-to-last argument" } lassign $args code_block . if_no_rows_code_block } else { @@ -1682,7 +1680,7 @@ } } # If the if_no_rows_code is defined, go ahead and run it. - if { $counter > 0 && [info exists if_no_rows_code_block] } { + if { $counter == 0 && [info exists if_no_rows_code_block] } { uplevel 1 $if_no_rows_code_block } } Index: openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl,v diff -u -r1.71 -r1.71.2.1 --- openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl 10 Dec 2018 14:47:25 -0000 1.71 +++ openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl 18 Feb 2019 12:40:09 -0000 1.71.2.1 @@ -1088,6 +1088,41 @@ } +aa_register_case \ + -cats {db smoke production_safe} \ + -procs {db_foreach} \ + db__db_foreach { + Checks that db_foreach works as expected + } { + set results [list] + db_foreach query {SELECT a FROM (VALUES (1), (2), (3), (4), (5), (6), (7)) AS X(a)} { + lappend results $a + } + aa_equals "db_foreach collects correct values from query" \ + [list 1 2 3 4 5 6 7] \ + $results + + set results "" + db_foreach query {select 1 from dual where 1 = 2} { + set results "found" + } else { + set results "not found" + } + aa_equals "db_foreach executes the 'no row' code block using the 'else' syntax" \ + "not found" \ + $results + + set results "" + db_foreach query {select 1 from dual where 1 = 2} { + set results "found" + } if_no_rows { + set results "not found" + } + aa_equals "db_foreach executes the 'no row' code block using the 'if_no_rows' syntax" \ + "not found" \ + $results + } + aa_register_case -cats {api db} db__caching { test db_* API caching } {