From: Janusz Dobrowolski Date: Wed, 13 Oct 2010 12:39:50 +0000 (+0000) Subject: Added inline company switching with set_global_connection() X-Git-Tag: v2.4.2~19^2~567 X-Git-Url: https://delta.frontaccounting.com/gitweb/?p=fa-stable.git;a=commitdiff_plain;h=7e994a8e658a7660dad5a4cd0c24fb8d174cc937 Added inline company switching with set_global_connection() --- diff --git a/admin/inst_upgrade.php b/admin/inst_upgrade.php index eeee26a2..bd9e1b4b 100644 --- a/admin/inst_upgrade.php +++ b/admin/inst_upgrade.php @@ -121,26 +121,16 @@ function upgrade_step($index, $conn) return $ret; } -function db_open($conn) -{ - $db = mysql_connect($conn["host"] ,$conn["dbuser"], $conn["dbpassword"]); - if (!$db) - return false; - if (!mysql_select_db($conn["dbname"], $db)) - return false; - return $db; -} - $installers = get_installers(); if (get_post('Upgrade')) { $ret = true; - foreach ($db_connections as $conn) + foreach ($db_connections as $comp => $conn) { // connect to database - if (!($db = db_open($conn))) + if (!(set_global_connection($comp))) { display_error(_("Cannot connect to database for company") ." '".$conn['name']."'"); @@ -162,6 +152,7 @@ if (get_post('Upgrade')) // db_close($conn); ? if (!$ret) break; } + set_global_connection(); if($ret) { // re-read the prefs global $path_to_root; diff --git a/includes/current_user.inc b/includes/current_user.inc index ef14c821..8b94366f 100644 --- a/includes/current_user.inc +++ b/includes/current_user.inc @@ -9,8 +9,11 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License here . ***********************************************************************/ -include_once($path_to_root . "/includes/prefs/userprefs.inc"); +include_once($path_to_root . "/includes/prefs/userprefs.inc"); +if (!defined('TB_PREF')) { + define('TB_PREF', '&TB_PREF&'); +} //-------------------------------------------------------------------------- class current_user @@ -19,17 +22,18 @@ class current_user var $loginname; var $username; var $name; - var $company; + var $company; // user's company var $pos; var $access; var $timeout; var $last_act; var $role_set = false; - var $old_db; + var $old_db; var $logged; var $ui_mode = 0; var $prefs; + var $cur_con; // current db connection (can be different from $company for superuser) function current_user() { @@ -153,16 +157,13 @@ class current_user { global $db_connections; - $connection = $db_connections[$id == -1 ? $this->company : $id]; + $this->cur_con = $id == -1 ? $this->company : $id; + $connection = $db_connections[$this->cur_con]; //print_r($connection); - $db = mysql_connect($connection["host"] , - $connection["dbuser"], $connection["dbpassword"]); - mysql_select_db($connection["dbname"],$db); - - if (!defined('TB_PREF')) - define('TB_PREF', $connection["tbpref"]); + $db = mysql_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]); + mysql_select_db($connection["dbname"], $db); return $db; } diff --git a/includes/db/connect_db.inc b/includes/db/connect_db.inc index 5babac1b..1e6c2c4a 100644 --- a/includes/db/connect_db.inc +++ b/includes/db/connect_db.inc @@ -10,14 +10,16 @@ See the License here . ***********************************************************************/ -function set_global_connection() +function set_global_connection($company=-1) { global $db, $transaction_level; cancel_transaction(); // cancel all aborted transactions if any $transaction_level = 0; - $db = $_SESSION["wa_current_user"]->get_db_connection(); + $db = $_SESSION["wa_current_user"]->get_db_connection($company); + + return $db; } $db_duplicate_error_code = 1062; @@ -26,8 +28,13 @@ $db_duplicate_error_code = 1062; function db_query($sql, $err_msg=null) { - global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax; + global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax, + $db_connections; + // set current db prefix + $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref']; + $sql = str_replace(TB_PREF, $cur_prefix, $sql); + if ($show_sql) { $Ajax->activate('footer_debug'); @@ -38,7 +45,7 @@ function db_query($sql, $err_msg=null) if($sql_trail) { if ($select_trail || (strstr($sql, 'SELECT') === false)) { mysql_query( - "INSERT INTO ".TB_PREF."sql_trail + "INSERT INTO ".$cur_prefix."sql_trail (`sql`, `result`, `msg`) VALUES(".db_escape($sql).",".($result ? 1 : 0).", ".db_escape($err_msg).")", $db); diff --git a/includes/errors.inc b/includes/errors.inc index cafa86f4..c584d175 100644 --- a/includes/errors.inc +++ b/includes/errors.inc @@ -108,7 +108,7 @@ function end_flush() { function display_db_error($msg, $sql_statement=null, $exit=true) { - global $db, $debug, $go_debug; + global $db, $debug, $go_debug, $db_connections; $warning = $msg==null; $db_error = db_error_no(); @@ -127,7 +127,9 @@ function display_db_error($msg, $sql_statement=null, $exit=true) if ($debug == 1) { - $str .= "sql that failed was : " . $sql_statement . "
"; + $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref']; + + $str .= "sql that failed was : ".str_replace(TB_PREF, $cur_prefix, $sql_statement)."
"; if ($go_debug > 1) display_backtrace(); }