nsmcp::DatabaseSession method execute (public)

 <instance of nsmcp::DatabaseSession[i]> execute \
    [ -arrayName arrayName ] [ -script script ] sql

Defined in /usr/local/ns/tcl/nsmcp/database.tcl

Execute trusted SQL statements with bindings from the caller's scope. Materialize ns_set rows directly as alternating key/value lists, or run the row callback through the supplied caller array; always flush result sets.

Switches:
-arrayName (optional)
Optional caller-array name populated for each fetched row.
-script (optional)
Tcl script executed in the documented caller or callback context.
Parameters:
sql (required)
Trusted SQL text; current values are resolved in the caller's scope.
Returns:
The collected row lists, or an empty list when rows are consumed through a callback.

Testcases:
No testcase defined.
Source code:
set rows {}
foreach statement [:statements $sql] {
    # Core resolves current caller values and rejects missing names.
    set kind [uplevel 1 [list ns_db exec ${:handle} -bindvars -- $statement]]
    set command [string toupper [lindex [split [string trim $statement]] 0]]
    if {$command eq "BEGIN"} {
        set :inTransaction true
    }
    if {$command in {COMMIT ROLLBACK END}} {
        set :inTransaction false
    }
    if {$kind ne "NS_ROWS"} {
        continue
    }
    set row [ns_db bindrow ${:handle}]
    try {
        # Column names do not change between rows. Callback metadata
        # retains their original order, including duplicate names.
        if {$arrayName ne ""} {
            set keys [ns_set keys $row]
        }
        while {[ns_db getrow ${:handle} $row]} {
            set record [ns_set array $row]
            if {$arrayName eq ""} {
                lappend rows $record
            } else {
                upvar 1 $arrayName target
                array unset target
                array set target $record
                set target(*) $keys
                set code [catch {
                    uplevel 1 $script
                } value options]
                if {$code==3} {
                    break
                }
                if {$code==4} {
                    continue
                }
                if {$code} {
                    return -options $options $value
                }
            }
        }
    } finally {
        ns_db flush ${:handle}
        ns_set free $row
    }
}
return $rows
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: