66a781d25d868c21286076411c8f559df3394ff4
[fa-stable.git] / includes / db / sql_functions.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 //      General database functions common for all modules.
14 //
15 //-------------------------------------------------------------------
16 // Multilevel transaction control.
17 //
18 $transaction_level = 0;
19
20 function begin_transaction()
21 {
22         global $transaction_level;
23
24         if (!$transaction_level) {
25                 db_query("BEGIN", "could not start a transaction");
26         }
27         $transaction_level++;
28 }
29
30 function commit_transaction()
31 {
32         global $transaction_level;
33
34         $transaction_level--;
35
36         if (!$transaction_level) {
37                 db_query("COMMIT", "could not commit a transaction");
38         }
39 }
40
41 /*
42         This function is called on end of script execution to cancel
43         all aborted transactions (if any)
44 */
45 function cancel_transaction()
46 {
47         global $transaction_level;
48
49         if ($transaction_level) {
50                 db_query("ROLLBACK", "could not cancel a transaction"); 
51         }
52         $transaction_level = 0;
53 }
54
55 //-----------------------------------------------------------------------------
56 //      Update record activity status.
57 //
58 function update_record_status($id, $status, $table, $key) {
59         $sql = "UPDATE ".TB_PREF.$table." SET inactive = "
60                 . ((int)$status)." WHERE $key=".db_escape($id);
61                 
62         db_query($sql, "Can't update record status");
63 }
64 //-----------------------------------------------------------------------------
65 //
66 //      Helper for sql subquery returning running totals from delta tables like stock_moves or bank_trans
67 //
68 //      $table - table name with optional WHERE clause
69 //  $column - delta column
70 //      $index  - comma delimited list of columns for total grouping and order
71 //  Returns running totals with respective index column
72 //
73 function running_total_sql($table, $column, $index)
74 {
75
76         return "SELECT daily.$index, daily.$column, (@total:=@total+daily.$column) total 
77                 FROM
78                         (SELECT $index, sum($column) $column FROM $table GROUP BY $index ORDER BY $index) daily,
79                         (SELECT @total:=0) total_var";
80 }
81