Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / dbmysql.class.php @ b67d8923

Historique | Voir | Annoter | Télécharger (8,33 ko)

1
<?php
2
/*
3
 * @version $Id: dbmysql.class.php 7876 2009-01-23 15:50:37Z moyo $
4
 -------------------------------------------------------------------------
5
 GLPI - Gestionnaire Libre de Parc Informatique
6
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
7

8
 http://indepnet.net/   http://glpi-project.org
9
 -------------------------------------------------------------------------
10

11
 LICENSE
12

13
 This file is part of GLPI.
14

15
 GLPI is free software; you can redistribute it and/or modify
16
 it under the terms of the GNU General Public License as published by
17
 the Free Software Foundation; either version 2 of the License, or
18
 (at your option) any later version.
19

20
 GLPI is distributed in the hope that it will be useful,
21
 but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 GNU General Public License for more details.
24

25
 You should have received a copy of the GNU General Public License
26
 along with GLPI; if not, write to the Free Software
27
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 --------------------------------------------------------------------------
29
 */
30

    
31
if (!defined('GLPI_ROOT')){
32
        die("Sorry. You can't access directly to this file");
33
        }
34

    
35
/**
36
 *  Database class for Mysql
37
 */
38
class DBmysql {
39

    
40
        //! Database Host
41
        var $dbhost        = ""; 
42
        //! Database User
43
        var $dbuser = "";
44
        //! Database Password 
45
        var $dbpassword        = "";
46
        //! Default Database
47
        var $dbdefault        = "";
48
        //! Database Handler
49
        var $dbh;
50
        //! Database Error
51
        var $error = 0;
52

    
53
        /// Slave management
54
        var $slave = false;
55
        /** Is it a first connection ?
56
        * Indicates if the first connection attempt is successful or not
57
        * if first attempt fail -> display a warning which indicates
58
        * that glpi is in readonly
59
        */
60
        var $first_connection = true;
61
        /// Is connected to the DB ?
62
        var $connected = false;
63
        
64
        /**
65
         * Constructor / Connect to the MySQL Database
66
         *
67
         * Use dbhost, dbuser, dbpassword and dbdefault
68
         * Die if connection or database selection failed
69
         *
70
         * @return nothing 
71
         */
72
        function DBmysql(){
73
                $this->connected=false;
74
                $this->dbh = @mysql_connect($this->dbhost, $this->dbuser, rawurldecode($this->dbpassword)) or $this->error = 1;
75
                if ($this->dbh){
76
                        @mysql_query("SET NAMES '" . (isset($this->dbenc) ? $this->dbenc : "utf8") . "'",$this->dbh);
77
                        $select= mysql_select_db($this->dbdefault) or $this->error = 1;
78
                        if ($select){ // select ok
79
                                $this->connected=true;
80
                        }else{ // select wrong
81
                                $this->connected=false;
82
                        }
83
                } else {
84
                        $this->connected=false;
85
                }
86
        }
87
        /**
88
         * Execute a MySQL query
89
         * @param $query Query to execute
90
         * @return Query result handler
91
         */
92
        function query($query) {
93
                global $CFG_GLPI,$DEBUG_SQL,$SQL_TOTAL_REQUEST;
94
                //pseudo debug ryxeo
95
                //print $query;
96
                if ($CFG_GLPI["debug"]) {
97
                        if ($CFG_GLPI["debug_sql"]){                
98
                                $SQL_TOTAL_REQUEST++;
99
                                $DEBUG_SQL["queries"][$SQL_TOTAL_REQUEST]=$query;
100

    
101
                                if ($CFG_GLPI["debug_profile"]){                
102
                                        $TIMER=new Script_Timer;
103
                                        $TIMER->Start_Timer();
104
                                }
105
                        }
106
                }
107

    
108
                //mysql_ping($this->dbh);
109
                $res=@mysql_query($query,$this->dbh);
110
                
111
                if (!$res) {
112
                        $this->DBmysql();
113
                        $res=mysql_query($query,$this->dbh);
114
                        
115
                        if (!$res) {
116
                                $error = "*** MySQL query error : \n***\nSQL: ".addslashes($query)."\nError: ". mysql_error()."\n";
117
                                if (function_exists("debug_backtrace")) {
118
                                        $error .= "Backtrace :\n";
119
                                        $traces=debug_backtrace();
120
                                        foreach ($traces as $trace) {
121
                                                $error .= $trace["file"] . ":" . $trace["line"] . "\t\t"
122
                                                                . (isset($trace["class"]) ? $trace["class"] : "")
123
                                                                . (isset($trace["type"]) ? $trace["type"] : "")
124
                                                                . (isset($trace["function"]) ? $trace["function"]."()" : "")
125
                                                                . "\n";
126
                                        }
127
                                } else {
128
                                        $error .= "Script: " ; 
129
                                }
130
                                $error .= $_SERVER["SCRIPT_FILENAME"]. "\n";
131
                                logInFile("sql-errors",$error);
132
                
133
                                if ($CFG_GLPI["debug"]&&$CFG_GLPI["debug_sql"]){
134
                                        $DEBUG_SQL["errors"][$SQL_TOTAL_REQUEST]=$this->error();
135
                                }
136
                        }
137
                }
138

    
139
                if ($CFG_GLPI["debug"]) {
140
                        if ($CFG_GLPI["debug_profile"]&&$CFG_GLPI["debug_sql"]){                
141
                                $TIME=$TIMER->Get_Time();
142
                                $DEBUG_SQL["times"][$SQL_TOTAL_REQUEST]=$TIME;
143
                        }
144
                }
145

    
146
                return $res;
147
        }
148
        /**
149
         * Give result from a mysql result
150
         * @param $result MySQL result handler
151
         * @param $i Row to give
152
         * @param $field Field to give
153
         * @return Value of the Row $i and the Field $field of the Mysql $result
154
         */
155
        function result($result, $i, $field) {
156
                $value=get_magic_quotes_runtime()?stripslashes_deep(mysql_result($result, $i, $field)):mysql_result($result, $i, $field);
157
                return $value;
158
        }
159
        /**
160
         * Give number of rows of a Mysql result
161
         * @param $result MySQL result handler
162
         * @return number of rows
163
         */
164
        function numrows($result) {
165
                return mysql_num_rows($result);
166
        }
167
        /**
168
         * Fetch array of the next row of a Mysql query
169
         * @param $result MySQL result handler
170
         * @return result array
171
         */
172
        function fetch_array($result) {
173
                $value=get_magic_quotes_runtime()?stripslashes_deep(mysql_fetch_array($result)):mysql_fetch_array($result);
174
                return $value;
175
        }
176
        /**
177
         * Fetch row of the next row of a Mysql query
178
         * @param $result MySQL result handler
179
         * @return result row
180
         */
181
        function fetch_row($result) {
182
                $value=get_magic_quotes_runtime()?stripslashes_deep(mysql_fetch_row($result)):mysql_fetch_row($result);
183
                return $value;
184
        }
185
        /**
186
         * Fetch assoc of the next row of a Mysql query
187
         * @param $result MySQL result handler
188
         * @return result associative array
189
         */
190
        function fetch_assoc($result) {
191
                $value=get_magic_quotes_runtime()?stripslashes_deep(mysql_fetch_assoc($result)):mysql_fetch_assoc($result);
192
                return $value;
193
        }
194
        /**
195
         * Move current pointer of a Mysql result to the specific row
196
         * @param $result MySQL result handler
197
         * @param $num row to move current pointer
198
         * @return boolean
199
         */
200
        function data_seek($result,$num){
201
                return mysql_data_seek ($result,$num);
202
        }
203
        /**
204
         * Give ID of the last insert item by Mysql
205
         * @return item ID
206
         */
207
        function insert_id() {
208
                return mysql_insert_id($this->dbh);
209
        }
210
        /**
211
         * Give number of fields of a Mysql result
212
         * @param $result MySQL result handler
213
         * @return number of fields
214
         */
215
        function num_fields($result) {
216
                return mysql_num_fields($result);
217
        }
218
        /**
219
         * Give name of a field of a Mysql result
220
         * @param $result MySQL result handler
221
         * @param $nb number of column of the field
222
         * @return name of the field
223
         */
224
        function field_name($result,$nb){
225
                return mysql_field_name($result,$nb);
226
        }
227
        /**
228
         * Get flags of a field of a mysql result 
229
         * @param $result MySQL result handler
230
         * @param $field field name
231
         * @return flags of the field
232
         */
233
        function field_flags($result,$field){
234
                return mysql_field_flags($result,$field);
235
        }
236
        /**
237
         * List tables in database 
238
         * @param $table table name condition (glpi_% as default to retrieve only glpi tables)
239
         * @return list of tables
240
         */
241
        function list_tables($table="glpi_%") {
242
                return $this->query("SHOW TABLES LIKE '".$table."'");
243
        }
244
        /**
245
         * List fields of a table
246
         * @param $table table name condition
247
         * @return list of fields
248
         */
249
        function list_fields($table) {
250
                $result = $this->query("SHOW COLUMNS FROM `$table`");
251
                if ($result) {
252
                        if ($this->numrows($result) > 0) {
253
                                while ($data = mysql_fetch_assoc($result)){
254
                                        $ret[$data["Field"]]= $data;
255
                                }
256
                                return $ret;
257
                        } else return array();
258
                } else return false;
259

    
260

    
261
        }
262
        /**
263
         * Get number of affected rows in previous MySQL operation
264
         * @return number of affected rows on success, and -1 if the last query failed.
265
         */
266
        function affected_rows() {
267
                return mysql_affected_rows($this->dbh);
268
        }
269
        /**
270
         * Free result memory
271
         * @param $result MySQL result handler
272
         * @return Returns TRUE on success or FALSE on failure.
273
         */
274
        function free_result($result) {
275
                return mysql_free_result($result);
276
        }
277
        /**
278
         * Returns the numerical value of the error message from previous MySQL operation
279
         * @return error number from the last MySQL function, or 0 (zero) if no error occurred.
280
         */
281
        function errno(){
282
                return mysql_errno($this->dbh);
283
        }
284

    
285
        /**
286
         * Returns the text of the error message from previous MySQL operation
287
         * @return error text from the last MySQL function, or '' (empty string) if no error occurred.
288
         */
289
        function error(){
290
                return mysql_error($this->dbh);
291
        }
292
        /**
293
         * Close MySQL connection
294
         * @return TRUE on success or FALSE on failure.
295
         */
296
        function close(){
297
                return @mysql_close($this->dbh);
298
        }
299
        
300
        /**
301
         * is a slave database ?
302
         * @return boolean 
303
         */
304
        function isSlave(){
305
                return $this->slave;
306
        }
307
}
308
?>
Redmine Appliance - Powered by TurnKey Linux