Added inline company switching with set_global_connection()
[fa-stable.git] / includes / db / connect_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 function set_global_connection($company=-1)
14 {
15         global $db, $transaction_level;
16
17         cancel_transaction(); // cancel all aborted transactions if any
18         $transaction_level = 0;
19
20         $db = $_SESSION["wa_current_user"]->get_db_connection($company);
21
22         return $db;
23 }
24
25 $db_duplicate_error_code = 1062;
26
27 //DB wrapper functions to change only once for whole application
28
29 function db_query($sql, $err_msg=null)
30 {
31         global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax,
32                 $db_connections;
33         
34         // set current db prefix
35         $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref'];
36         $sql = str_replace(TB_PREF, $cur_prefix, $sql);
37
38         if ($show_sql)
39         {
40                 $Ajax->activate('footer_debug');
41                 $sql_queries .= "<pre>$sql</pre>\n<hr>";
42         }
43
44         $result = mysql_query($sql, $db);
45         if($sql_trail) {
46                 if ($select_trail || (strstr($sql, 'SELECT') === false)) {
47                         mysql_query(
48                         "INSERT INTO ".$cur_prefix."sql_trail
49                                 (`sql`, `result`, `msg`)
50                                 VALUES(".db_escape($sql).",".($result ? 1 : 0).",
51                                 ".db_escape($err_msg).")", $db);
52                 }
53         }
54
55         if ($err_msg != null || $go_debug) {
56                 $exit = $err_msg != null;
57                 if (function_exists('xdebug_call_file'))
58                         check_db_error('<br>At file '.xdebug_call_file().':'.xdebug_call_line().':<br>'.$err_msg, $sql, $exit);
59                 else
60                         check_db_error($err_msg, $sql, $exit);
61         }
62         return $result;
63 }
64
65 function db_fetch_row ($result)
66 {
67
68         return mysql_fetch_row($result);
69 }
70
71 function db_fetch_assoc ($result)
72 {
73
74         return mysql_fetch_assoc($result);
75 }
76
77 function db_fetch ($result)
78 {
79
80         return mysql_fetch_array($result);
81 }
82
83 function db_seek (&$result,$record)
84 {
85         return mysql_data_seek($result, $record);
86 }
87
88 function db_free_result ($result)
89 {
90         if ($result)
91                 mysql_free_result($result);
92 }
93
94 function db_num_rows ($result)
95 {
96         return mysql_num_rows($result);
97 }
98
99 function db_num_fields ($result)
100 {
101         return mysql_num_fields($result);
102 }
103
104 function db_escape($value = "", $nullify = false)
105 {
106         $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION['language']->encoding);
107         $value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding);
108
109         //reset default if second parameter is skipped
110         $nullify = ($nullify === null) ? (false) : ($nullify);
111
112         //check for null/unset/empty strings
113         if ((!isset($value)) || (is_null($value)) || ($value === "")) {
114                 $value = ($nullify) ? ("NULL") : ("''");
115         } else {
116                 if (is_string($value)) {
117                 //value is a string and should be quoted; determine best method based on available extensions
118                         if (function_exists('mysql_real_escape_string')) {
119                                 $value = "'" . mysql_real_escape_string($value) . "'";
120                         } else {
121                           $value = "'" . mysql_escape_string($value) . "'";
122                         }
123                 } else if (!is_numeric($value)) {
124                         //value is not a string nor numeric
125                         display_error("ERROR: incorrect data type send to sql query");
126                         echo '<br><br>';
127                         exit();
128                 }
129         }
130         return $value;
131 }
132
133 function db_error_no ()
134 {
135         global $db;
136         return mysql_errno($db);
137 }
138
139 function db_error_msg($conn)
140 {
141         return mysql_error($conn);
142 }
143
144 function db_insert_id()
145 {
146         global $db;
147         return mysql_insert_id($db);
148 }
149
150 function db_num_affected_rows()
151 {
152         global $db;
153         return mysql_affected_rows($db);
154 }
155
156 ?>