xo::db::DB-postgresql instproc multirow (public)

 <instance of xo::db::DB-postgresql[i]> multirow [ -dbn dbn ] \
    [ -bind bind ] [ -local local ] [ -upvar_level upvar_level ] \
    [ -extend extend ] [ -prepare prepare ] var_name qn sql [ body ]

Defined in /var/www/openacs/packages/xotcl-core/tcl/05-db-procs.tcl

Append query results to a template multirow, creating it if needed.

Switches:
-dbn (optional)
Database name, or empty for the default.
-bind (optional)
Bind values for the query.
-local (optional, defaults to "false")
Keep the multirow local to the selected caller frame.
-upvar_level (optional, defaults to "1")
Caller level at which to store the multirow.
-extend (optional)
Additional column names to populate in the row body.
-prepare (optional)
Optional argument types for a prepared statement.
Parameters:
var_name (required)
Template multirow name.
qn (required)
Query name.
sql (required)
SQL statement.
body (optional)
Optional Tcl script to run for each row.

Testcases:
test_prepared_statements, test_multirow, test_db_out_of_pools
Source code:
set prepare [expr {[info exists prepare] ? [list -prepare $prepare] : ""}]
set rows [uplevel 1 [list ::xo::dc list_of_lists -with_headers true -dbn $dbn -bind $bind {*}$prepare $qn $sql]]
set headers [lindex $rows 0]

if { $local } {
  set level_up [expr {$upvar_level + 1}]
} else {
  set level_up \#[::template::adp_level]
}

set cols [concat $headers $extend]
if {[::template::multirow -local -ulevel $level_up exists $var_name]} {
  #
  # We enforce here, that appending to an existing multirow
  # can only happen when we are extracting the same columns.
  #
  set existing_cols [::template::multirow -local -ulevel $level_up columns $var_name]
  if {$cols ne $existing_cols} {
    error "Cannot append to a multirow with different columns"
  }
} else {
  ::template::multirow -local -ulevel $level_up create $var_name {*}$cols
}

foreach values [lrange $rows 1 end] {
  if {[string length $body] > 0} {
    #
    # We have a code to execute. Bring all of the multirow
    # variables in scope.
    #

    #
    # Vars from the query
    #
    foreach att $headers value $values {
      uplevel 1 [list set $att $value]
    }

    #
    # Extended variables, initialized to empty.
    #
    foreach att $extend {
      uplevel 1 [list set $att ""]
    }

    #
    # Run the code and trap any exception.
    #
    try {

      uplevel 1 $body

    } on error {errMsg} {

      error $errMsg $::errorInfo $::errorCode

    } on return {} {

      error "Cannot return from inside a ::xo::dc multirow loop"

    } on break {} {

      break

    } on continue {} {

      continue

    }

    #
    # Collect the values after the code has been executed.
    #
    set values [lmap att $cols {
      if {[uplevel 1 [list info exists $att]]} {
        uplevel 1 [list set $att]
      }
    }]
  } else {
    #
    # No code to execute. We can just bulk append the values
    # from the row.
    #
  }
  ::template::multirow -local -ulevel $level_up append $var_name {*}$values
}
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: