db_source_sql_file (public)

 db_source_sql_file [ -dbn dbn ] [ -callback callback ] file

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

Sources a SQL file into Oracle (SQL*Plus format file) or PostgreSQL (psql format file).

Switches:
-dbn (optional)
The database name to use. If empty_string, uses the default database.
-callback (optional, defaults to "apm_ns_write_callback")
Parameters:
file (required)

Partial Call Graph (max 5 caller/called nodes):
%3 ad_acs_require_basic_schemata ad_acs_require_basic_schemata (private) db_source_sql_file db_source_sql_file ad_acs_require_basic_schemata->db_source_sql_file apm_package_delete apm_package_delete (public) apm_package_delete->db_source_sql_file apm_package_install_data_model apm_package_install_data_model (private) apm_package_install_data_model->db_source_sql_file install::xml::action::source install::xml::action::source (private) install::xml::action::source->db_source_sql_file tsearch2_driver::install::preinstall_checks tsearch2_driver::install::preinstall_checks (private) tsearch2_driver::install::preinstall_checks->db_source_sql_file ad_file ad_file (public) db_source_sql_file->ad_file apm_callback_and_log apm_callback_and_log (public) db_source_sql_file->apm_callback_and_log db_driverkey db_driverkey (public) db_source_sql_file->db_driverkey db_get_database db_get_database (public) db_source_sql_file->db_get_database db_get_dbhost db_get_dbhost (public) db_source_sql_file->db_get_dbhost

Testcases:
No testcase defined.
Source code:
    set proc_name {db_source_sql_file}
    set driverkey [db_driverkey $dbn]

    switch -- $driverkey {

        oracle {
            set user_pass [db_get_sql_user -dbn $dbn]
            cd [ad_file dirname $file]
            set fp [open "|[ad_file join $::env(ORACLE_HOME) bin sqlplus] $user_pass @$file" "r+"]
            fconfigure $fp -buffering line
            puts $fp "exit"

            while { [gets $fp line] >= 0 } {
                # Don't bother writing out lines which are purely whitespace.
                if { ![string is space $line] } {
                    apm_callback_and_log $callback "[ns_quotehtml $line]\n"
                }
            }
            close $fp
        }

        postgresql {
            set file_name [ad_file tail $file]

            set pguser [db_get_username]
            if { $pguser ne "" } {
                set pguser "-U $pguser"
            }

            set pgport [db_get_port]
            if { $pgport ne "" } {
                set pgport "-p $pgport"
            }

            set pgpass [db_get_password]
            if { $pgpass ne "" } {
                set pgpass "<<$pgpass"
            }

            #
            # GN: windows requires $pghost "-h ..."
            #
            if { ([db_get_dbhost] eq "localhost" || [db_get_dbhost] eq "")
                 && $::tcl_platform(platform) ne "windows"
             } {
                set pghost ""
            } else {
                set pghost "-h [db_get_dbhost]"
            }

            set dir [ad_file dirname $file]
            set cmd "[file join [db_get_pgbin] psql]  $pghost $pgport $pguser  -f $file  [db_get_database$pgpass"

            try {
                if {[info commands proxy::exec] ne ""} {
                    ns_log notice [list ::proxy::exec -call $cmd -cd $dir]
                    ::proxy::exec -call $cmd -cd $dir
                } {
                    cd $dir
                    set fp [open "|$cmd" r]
                    set result ""
                    catch {set result [read $fp]}
                    close $fp
                    set result
                }
            } on error {result} {
            } on ok {result} {
            }
            set error_found 0
            foreach line [split $result \n] {
                #
                # Don't bother writing out lines which are purely
                # whitespace.
                #
                if { ![string is space $line] } {
                    apm_callback_and_log $callback "[ns_quotehtml $line]\n"
                }
                #
                # PSQL dumps errors and notice information on
                # stderr, and has no option to turn this off.  So
                # we have to chug through the "error" lines
                # looking for those that really signal an error.
                #
                if { [string first NOTICE $line] == -1 } {
                    append error_lines "$line\n"
                    set error_found [expr { $error_found
                                            || [string first ERROR $line] != -1
                                            || [string first FATAL $line] != -1 } ]
                }
            }
            ns_log notice "ERROR_FOUND=$error_found"
            if { $error_found } {
                return -code error -errorinfo $error_lines -errorcode $::errorCode $error_lines
            }
        }

        nsodbc {
            error "$proc_name is not supported for this database."
        }
        default {
            error "$proc_name is not supported for this database."
        }
    }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: