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):
- 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