b671945ab6e83daf6b9fe7ab8102a6d643c07bc4
[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 //$path_to_root="..";
13 define('MAX_DEADLOCK_RETRY', 3);
14
15 if (function_exists('mysqli_connect'))
16         include_once("connect_db_mysqli.inc");
17 else
18         include_once("connect_db_mysql.inc"); // use deprecated mysql_* API as fallback on old servers
19
20 function db_get_version()
21 {
22         $result = db_query("SELECT VERSION()");
23         $row = db_fetch($result);
24         return $row[0];
25 }
26
27 /*
28         Converts encoding name to mysql standard.
29 */
30 function get_mysql_encoding_name($encoding)
31 {
32         $db_encoding = array(
33                 'UTF-8' => 'utf8',
34                 'ISO-8859-1' => 'latin1',
35                 'ISO-8859-2' => 'latin2',
36                 'ISO-8859-7' => 'greek',
37                 'ISO-8859-8' => 'hebrew',
38                 'ISO-8859-9' => 'latin5',
39                 'ISO-8859-13' => 'latin7',
40                 'KOI8-R' => 'koi8r',
41                 'KOI8-U' => 'koi8u',
42                 'CP850' => 'cp850',
43                 'CP866' => 'cp866',
44                 'CP932' => 'cp932',
45                 'CP1250' => 'cp1250',
46                 'CP1251' => 'cp1251',
47                 'CP1252' => 'latin1',
48                 'CP1256' => 'cp1256',
49                 'CP1257' => 'cp1257',
50                 'GB2312' => 'gb2312',
51                 'EUC-JP' => 'ujis',
52                 'EUC-KR' => 'euckr',
53                 'BIG5' => 'big5',
54                 'GBK' => 'gbk',
55                 'SHIFT_JIS' => 'sjis',
56                 'TIS-620' => 'tis620',
57                 'ASCII' => 'ascii',
58         );
59         $encoding = strtoupper($encoding);
60
61         return isset($db_encoding[$encoding]) ? $db_encoding[$encoding] : null;
62 }
63
64 /*
65         Returns 'best' mysql collation for various FA backend language codes.
66 */
67 function get_mysql_collation($lang=null)
68 {
69         if (!$lang)
70                 $lang = 'utf8_'.substr($_SESSION['language']->code, 0, 2);
71
72         $db_collation = array(
73                 'utf8_is' => 'utf8_icelandic_ci',
74                 'utf8_lv' => 'utf8_latvian_ci',
75                 'utf8_ro' => 'utf8_romanian_ci',
76                 'utf8_sl' => 'utf8_slovenian_ci',
77                 'utf8_pl' => 'utf8_polish_ci',
78                 'utf8_et' => 'utf8_estonian_ci',
79                 'utf8_es' => 'utf8_spanish_ci', // or 'spanish2',
80                 'utf8_sw' => 'utf8_swedish_ci',
81                 'utf8_tr' => 'utf8_turkish_ci',
82                 'utf8_cs' => 'utf8_czech_ci',
83                 'utf8_da' => 'utf8_danish_ci',
84                 'utf8_lt' => 'utf8_lithuanian_ci',
85                 'utf8_sk' => 'utf8_slovak_ci',
86                 'utf8_sp' => 'utf8_spanish2_ci',
87                 'utf8_fa' => 'utf8_persian_ci',
88                 'utf8_hu' => 'utf8_hungarian_ci',
89                 'utf8_fr' => 'utf8_roman_ci',
90                 'utf8_it' => 'utf8_roman_ci',
91         );
92
93         return isset($db_collation[$lang]) ? $db_collation[$lang] : 'utf8_unicode_ci';
94 }
95
96 /*
97         Later we assume that database with version less than 2.4 is old database, 
98         which is subject to invalid encodings on text columns,
99         so no SET NAMES or equivalent should be used.
100 */
101 function db_fixed()
102 {
103         $result = db_query("SELECT value FROM ".TB_PREF."sys_prefs WHERE name='version_id'");
104         $data = db_fetch($result);
105         return !db_num_rows($result) // new database is fixed by default 
106                 || ($data[0] > "2.3rc");
107 }
108
109 /*
110         Check database default charset.
111 */
112 function db_get_default_charset()
113 {
114         $result = db_query("SELECT @@character_set_database");
115         $var = db_fetch($result);
116         return $var[0];
117 }
118
119 /*
120         SQL db profiling stub
121 */
122 if (!function_exists('db_profile'))
123 {
124         function db_profile($sql=false)
125         {
126         }
127 }