thomascube
2005-09-25 4e17e6c9dbac8991ee8b302cb2581241247dc8bc
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_mysql.inc                                       |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2005, RoundCube Dev. - Switzerland                      |
9  | All rights reserved.                                                  |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   MySQL wrapper class that implements PHP MySQL functions             |
13  |   See http://www.php.net/manual/en/ref.mysql.php                      |
14  |                                                                       |
15  +-----------------------------------------------------------------------+
16  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17  +-----------------------------------------------------------------------+
18
19  $Id$
20
21 */
22
23
24 class rcube_mysql
25   {
26   var $db_link;
27   var $db_host = 'localhost';
28   var $db_name = '';
29   var $db_user = '';
30   var $db_pass = '';
31   var $a_query_results = array('dummy');
32   var $last_res_id = 0;
33
34
35   // PHP 5 constructor
36   function __construct($db_name='', $user='', $pass='', $host='localhost')
37     {
38     $this->db_host = $host;
39     $this->db_name = $db_name;
40     $this->db_user = $user;
41     $this->db_pass = $pass;
42     }
43
44   // PHP 4 compatibility
45   function rcube_mysql($db_name='', $user='', $pass='', $host='localhost')
46     {
47     $this->__construct($db_name, $user, $pass, $host);
48     }
49
50
51   function connect()
52     {
53     $this->db_link = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
54       
55     if (!$this->db_link)
56       {
57       raise_error(array('code' => 500,
58                         'type' => 'mysql',
59                         'line' => __LINE__,
60                         'file' => __FILE__,
61                         'message' => "Can't connect to database"), TRUE, FALSE);
62       return FALSE;
63       }
64     
65     return TRUE;
66     }
67
68
69   function select_db($name)
70     {
71     $this->db_name = $name;
72
73     if ($this->db_link)
74       mysql_select_db($name, $this->db_link);
75     }
76
77
78   function query($query)
79     {
80     // establish a connection
81     if (!$this->db_link)
82       {
83       if (!$this->connect())
84         return FALSE;
85       }
86
87     $sql_result = mysql_db_query($this->db_name, $query, $this->db_link);
88     return $this->_add_result($sql_result, $query);
89     }
90
91
92   function num_rows($res_id=NULL)
93     {
94     if (!$this->db_link)
95       return FALSE;
96
97     $sql_result = $this->_get_result($res_id);
98     
99     if ($sql_result)    
100       return mysql_num_rows($sql_result);
101     else
102       return FALSE;
103     }
104
105
106   function affected_rows()
107     {
108     if (!$this->db_link)
109       return FALSE;
110
111     return mysql_affected_rows($this->db_link);
112     }
113
114
115   function insert_id()
116     {
117     if (!$this->db_link)
118       return FALSE;
119
120     return mysql_insert_id($this->db_link);
121     }
122
123
124   function fetch_assoc($res_id=NULL)
125     {
126     $sql_result = $this->_get_result($res_id);
127     
128     if ($sql_result)
129       return mysql_fetch_assoc($sql_result);
130     else
131       return FALSE;
132     }
133
134
135   function seek($res_id=NULL, $row=0)
136     {
137     $sql_result = $this->_get_result($res_id);
138     
139     if ($sql_result)
140       return mysql_data_seek($sql_result, $row);
141     else
142       return FALSE;
143     }
144
145
146
147   function _add_result($res, $query)
148     {
149     // sql error occured
150     if ($res===FALSE)
151       {
152       $sql_error = mysql_error($this->db_link);
153       raise_error(array('code' => 500,
154                         'type' => 'mysql',
155                         'line' => __LINE__,
156                         'file' => __FILE__,
157                         'message' => $sql_error."; QUERY: ".preg_replace('/[\r\n]+\s*/', ' ', $query)), TRUE, FALSE);
158       
159       return FALSE;
160       }
161     else
162       {
163       $res_id = sizeof($this->a_query_results);
164       $this->a_query_results[$res_id] = $res;
165       $this->last_res_id = $res_id;
166
167       return $res_id;
168       }
169     }
170
171
172   function _get_result($res_id)
173     {
174     if ($res_id===NULL)
175       $res_id = $this->last_res_id;
176
177     if ($res_id && isset($this->a_query_results[$res_id]))
178       return $this->a_query_results[$res_id];
179     else
180       return FALSE;
181     }
182
183   }
184
185
186 ?>