puts stderr "EXECUTING >$stmt<"
}
}
+
+proc db--exec-check {shvar stmt expected_status body} {
+ # pg_execute does not set errorCode and it throws away the
+ # statement handle so we can't get the error out. So
+ # use pg_exec, as wrapped up here.
+
+ # db--exec-check executes stmt and checks that the status is
+ # `expected_status'. If OK, executes body with $shvar set to the
+ # stmt handle. Otherwise throws with errorCode
+ # {OSSTEST-PSQL <pg-status> <pg-sqlstate>}
+
+ global errorInfo errorCode
+ upvar 1 $shvar sh
+
+ set sh [pg_exec dbh $stmt]
+
+ set rc [catch {
+ set status [pg_result $sh -status]
+ if {[string compare $status $expected_status]} {
+ set emsg [pg_result $sh -error]
+ set sqlstate [pg_result $sh -error sqlstate]
+ if {![string length $emsg]} {
+ set emsg "osstest expected status $expected_status got $status"
+ }
+ set context [pg_result $sh -error context]
+ error $emsg \
+ "$emsg\n while executing SQL\n$stmt\n in SQL context\n$context" \
+ [list OSSTEST-PSQL $status $sqlstate]
+ }
+ uplevel 1 $body
+ } emsg]
+
+ set ei $errorInfo
+ set ec $errorCode
+ catch { pg_result $sh -clear }
+
+ return -code $rc -errorinfo $ei -errorcode $ec $emsg
+}
+
proc db-execute {stmt} {
db-execute-debug $stmt
- uplevel 1 [list pg_execute dbh $stmt]
+ db--exec-check sh $stmt PGRES_COMMAND_OK {
+ return [pg_result $sh -cmdTuples]
+ }
}
-proc db-execute-array {arrayvar stmt args} {
+proc db-execute-array {arrayvar stmt {body {}}} {
db-execute-debug $stmt
- uplevel 1 [list pg_execute -array $arrayvar dbh $stmt] $args
+ db--exec-check sh $stmt PGRES_TUPLES_OK {
+ set nrows [pg_result $sh -numTuples]
+ for {set row 0} {$row < $nrows} {incr row} {
+ uplevel 1 [list pg_result $sh -tupleArray $row $arrayvar]
+ uplevel 1 $body
+ }
+ return $nrows
+ }
}
proc lock-tables {tables} {