db_exec_lob_postgresql (private)

 db_exec_lob_postgresql [ -ulevel ulevel ] [ -subst subst ] type db \
    statement_name pre_sql [ file ]

Defined in packages/acs-tcl/tcl/01-database-procs.tcl

A helper procedure to execute a SQL statement, potentially binding depending on the value of the $bind variable in the calling environment (if set). Low level replacement for db_exec which emulates blob handling.

Switches:
-ulevel
(defaults to "2") (optional)
-subst
(defaults to "all") (optional)
Parameters:
type
db
statement_name
pre_sql
file (optional)

Partial Call Graph (max 5 caller/called nodes):
%3 ad_file ad_file (public) db_bind_var_substitution db_bind_var_substitution (public) db_qd_replace_sql db_qd_replace_sql (public) ds_collect_db_call ds_collect_db_call (public) db_exec_lob_postgresql db_exec_lob_postgresql db_exec_lob_postgresql->ad_file db_exec_lob_postgresql->db_bind_var_substitution db_exec_lob_postgresql->db_qd_replace_sql db_exec_lob_postgresql->ds_collect_db_call

Testcases:
No testcase defined.
Source code:
    set start_time [expr {[clock clicks -microseconds]/1000.0}]

    # Query Dispatcher (OpenACS - ben)
    set sql [db_qd_replace_sql  -ulevel [expr {$ulevel + 1}]  -subst $subst  $statement_name  $pre_sql]

    # create a function definition statement for the inline code
    # binding is emulated in tcl. (OpenACS - Dan)

    set errno [catch {
        upvar bind bind
        if { [info exists bind] && [llength $bind] != 0 } {
            if { [llength $bind] == 1 } {
                set lob_sql [db_bind_var_substitution $sql [ns_set array $bind]]
            } else {
                set lob_sql [db_bind_var_substitution $sql $bind]
            }
        } else {
            set lob_sql [uplevel $ulevel [list db_bind_var_substitution $sql]]
        }

        # get the content - assume it is in column 0, or optionally it can
        # be returned as "content" with the storage type indicated by the
        # "storage_type" column.

        set selection [ns_db 1row $db $lob_sql]
        set content [ns_set value $selection 0]

        foreach var {storage_type content} {
            set i [ns_set find $selection $var]
            if {$i != -1} {
                set $var [ns_set value $selection $i]
            }
        }

        # this is an ugly hack, but it allows content to be written
        # to a file/connection if it is stored as a lob or if it is
        # stored in the content-repository as a file. (DanW - Openacs)

        switch -- $type {

            blob_get {

                if {[info exists storage_type]} {
                    switch -- $storage_type {
                        file {
                            if {[ad_file exists $content]} {
                                set ifp [open $content r]

                                # DRB: this could be made faster by setting the buffersize
                                # to the size of the file, but for very large files allocating
                                # that much more memory on top of that needed by Tcl for storage
                                # of the data might not be wise.

                                fconfigure $ifp -translation binary

                                set data [read $ifp]
                                close $ifp
                                return $data
                            } else {
                                error "file: $content doesn't exist"
                            }
                        }

                        lob {
                            if {[regexp {^[0-9]+$} $content match]} {
                                return [ns_pg blob_get $db $content]
                            } else {
                                error "invalid lob_id: should be an integer"
                            }
                        }

                        default {
                            error "invalid storage type"
                        }
                    }
                } elseif {[ad_file exists $content]} {
                    set ifp [open $content r]
                    fconfigure $ifp -translation binary
                    set data [read $ifp]
                    close $ifp
                    return $data
                } elseif {[regexp {^[0-9]+$} $content match]} {
                    return [ns_pg blob_get $db $content]
                } else {
                    error "invalid query"
                }
            }

            blob_select_file {

                if {[info exists storage_type]} {
                    switch -- $storage_type {
                        file {
                            if {[ad_file exists $content]} {
                                file copy -- $content $file
                            } else {
                                error "file: $content doesn't exist"
                            }
                        }

                        lob {
                            if {[regexp {^[0-9]+$} $content match]} {
                                ns_pg blob_select_file $db $content $file
                            } else {
                                error "invalid lob_id: should be an integer"
                            }
                        }

                        default {
                            error "invalid storage type"
                        }
                    }
                } elseif {[ad_file exists $content]} {
                    file copy -- $content $file
                } elseif {[regexp {^[0-9]+$} $content match]} {
                    ns_pg blob_select_file $db $content $file
                } else {
                    error "invalid query"
                }
            }

            write_blob {

                if {[info exists storage_type]} {
                    switch -- $storage_type {
                        file {
                            if {[ad_file exists $content]} {
                                set ofp [open $content r]
                                fconfigure $ofp -encoding binary
                                ns_writefp $ofp
                                close $ofp
                            } else {
                                error "file: $content doesn't exist"
                            }
                        }

                        text {
                            ns_write $content
                        }

                        lob {
                            if {[regexp {^[0-9]+$} $content match]} {
                                ns_pg blob_write $db $content
                            } else {
                                error "invalid lob_id: should be an integer"
                            }
                        }

                        default {
                            error "invalid storage type"
                        }
                    }
                } elseif {[ad_file exists $content]} {
                    set ofp [open $content r]
                    fconfigure $ofp -encoding binary
                    ns_writefp $ofp
                    close $ofp
                } elseif {[regexp {^[0-9]+$} $content match]} {
                    ns_pg blob_write $db $content
                } else {
                    ns_write $content
                }
            }
        }

        return

    } error]

    set errinfo $::errorInfo
    set errcode $::errorCode

    ds_collect_db_call $db 0or1row $statement_name $sql $start_time $errno $error

    if { $errno == 2 } {
        return $error
    }

    return -code $errno -errorinfo $errinfo -errorcode $errcode $error
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: