Forum OpenACS Development: Re: db_multirow and db_foreach -unclobber

Collapse
Posted by Lars Pind on
For what it's worth, here's the diff:
Index: 00-database-procs.tcl
===================================================================
RCS file: /cvsroot/openacs-4/packages/acs-tcl/tcl/00-database-procs.tcl,v
retrieving revision 1.19.2.5
diff -u -r1.19.2.5 00-database-procs.tcl
--- 00-database-procs.tcl	23 May 2003 13:11:29 -0000	1.19.2.5
+++ 00-database-procs.tcl	3 Jun 2003 08:05:35 -0000
@@ -442,6 +442,7 @@
 ad_proc -public db_multirow {
     -local:boolean
     -append:boolean
+    -unclobber:boolean
     {-extend {}}
     var_name
     statement_name
@@ -584,6 +585,24 @@
 Columns in this query: [join [lsort -ascii $local_columns] ", "]" "" "ACS_MULTIROW_APPEND_COLUMNS_MISMATCH"
                     }
                 }
+
+                # Save values of columns which we might clobber
+                if { $unclobber_p && ![empty_string_p $code_block] } {
+                    foreach col $columns {
+                        upvar 1 $col column_value __saved_$col column_save
+
+                        if { [info exists column_value] } {
+                            if { [array exists column_value] } {
+                                array set column_save [array get column_value]
+                            } else {
+                                set column_save $column_value
+                            }
+
+                            # Clear the variable
+                            unset column_value
+                        }
+                    }
+                }
             }
 
 	    if { [empty_string_p $code_block] } {
@@ -650,6 +669,30 @@
             incr local_counter
 	    set array_val(rownum) $counter
 	}
+    }
+
+    # Restore values of columns which we've saved
+    if { $unclobber_p && ![empty_string_p $code_block] && $local_counter > 0 } {
+        foreach col $columns {
+            upvar 1 $col column_value __saved_$col column_save
+
+            # Unset it first, so the road's paved to restoring
+            if { [info exists column_value] } {
+                unset column_value
+            }
+
+            # Restore it
+            if { [info exists column_save] } {
+                if { [array exists column_save] } {
+                    array set column_value [array get column_save]
+                } else {
+                    set column_value $column_save
+                }
+                
+                # And then remove the saved col
+                unset column_save
+            }
+        }
     }
 
     # If the if_no_rows_code is defined, go ahead and run it.