source: branches/rsr.v5.1.1/web/punbb/include/dblayer/mysql.php @ 4

Last change on this file since 4 was 4, checked in by dj3c1t, 12 years ago

passage en v5.1.1

File size: 10.2 KB
Line 
1<?php
2
3/**
4 * Copyright (C) 2008-2011 FluxBB
5 * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
6 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
7 */
8
9// Make sure we have built in support for MySQL
10if (!function_exists('mysql_connect'))
11        exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL database to run this forum. Consult the PHP documentation for further assistance.');
12
13
14class DBLayer
15{
16        var $prefix;
17        var $link_id;
18        var $query_result;
19
20        var $saved_queries = array();
21        var $num_queries = 0;
22
23        var $error_no = false;
24        var $error_msg = 'Unknown';
25
26        var $datatype_transformations = array(
27                '%^SERIAL$%'    =>      'INT(10) UNSIGNED AUTO_INCREMENT'
28        );
29
30
31        function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
32        {
33                $this->prefix = $db_prefix;
34
35                if ($p_connect)
36                        $this->link_id = @mysql_pconnect($db_host, $db_username, $db_password);
37                else
38                        $this->link_id = @mysql_connect($db_host, $db_username, $db_password);
39
40                if ($this->link_id)
41                {
42                        if (!@mysql_select_db($db_name, $this->link_id))
43                                error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
44                }
45                else
46                        error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
47
48                // Setup the client-server character set (UTF-8)
49                if (!defined('FORUM_NO_SET_NAMES'))
50                        $this->set_names('utf8');
51
52                return $this->link_id;
53        }
54
55
56        function start_transaction()
57        {
58                return;
59        }
60
61
62        function end_transaction()
63        {
64                return;
65        }
66
67
68        function query($sql, $unbuffered = false)
69        {
70                if (defined('PUN_SHOW_QUERIES'))
71                        $q_start = get_microtime();
72
73                if ($unbuffered)
74                        $this->query_result = @mysql_unbuffered_query($sql, $this->link_id);
75                else
76                        $this->query_result = @mysql_query($sql, $this->link_id);
77
78                if ($this->query_result)
79                {
80                        if (defined('PUN_SHOW_QUERIES'))
81                                $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
82
83                        ++$this->num_queries;
84
85                        return $this->query_result;
86                }
87                else
88                {
89                        if (defined('PUN_SHOW_QUERIES'))
90                                $this->saved_queries[] = array($sql, 0);
91
92                        $this->error_no = @mysql_errno($this->link_id);
93                        $this->error_msg = @mysql_error($this->link_id);
94
95                        return false;
96                }
97        }
98
99
100        function result($query_id = 0, $row = 0, $col = 0)
101        {
102                return ($query_id) ? @mysql_result($query_id, $row, $col) : false;
103        }
104
105
106        function fetch_assoc($query_id = 0)
107        {
108                return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
109        }
110
111
112        function fetch_row($query_id = 0)
113        {
114                return ($query_id) ? @mysql_fetch_row($query_id) : false;
115        }
116
117
118        function num_rows($query_id = 0)
119        {
120                return ($query_id) ? @mysql_num_rows($query_id) : false;
121        }
122
123
124        function affected_rows()
125        {
126                return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
127        }
128
129
130        function insert_id()
131        {
132                return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
133        }
134
135
136        function get_num_queries()
137        {
138                return $this->num_queries;
139        }
140
141
142        function get_saved_queries()
143        {
144                return $this->saved_queries;
145        }
146
147
148        function free_result($query_id = false)
149        {
150                return ($query_id) ? @mysql_free_result($query_id) : false;
151        }
152
153
154        function escape($str)
155        {
156                if (is_array($str))
157                        return '';
158                else if (function_exists('mysql_real_escape_string'))
159                        return mysql_real_escape_string($str, $this->link_id);
160                else
161                        return mysql_escape_string($str);
162        }
163
164
165        function error()
166        {
167                $result['error_sql'] = @current(@end($this->saved_queries));
168                $result['error_no'] = $this->error_no;
169                $result['error_msg'] = $this->error_msg;
170
171                return $result;
172        }
173
174
175        function close()
176        {
177                if ($this->link_id)
178                {
179                        if ($this->query_result)
180                                @mysql_free_result($this->query_result);
181
182                        return @mysql_close($this->link_id);
183                }
184                else
185                        return false;
186        }
187
188        function get_names()
189        {
190                $result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\'');
191                return $this->result($result, 0, 1);
192        }
193
194
195        function set_names($names)
196        {
197                return $this->query('SET NAMES \''.$this->escape($names).'\'');
198        }
199
200
201        function get_version()
202        {
203                $result = $this->query('SELECT VERSION()');
204
205                return array(
206                        'name'          => 'MySQL Standard',
207                        'version'       => preg_replace('%^([^-]+).*$%', '\\1', $this->result($result))
208                );
209        }
210
211
212        function table_exists($table_name, $no_prefix = false)
213        {
214                $result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\'');
215                return $this->num_rows($result) > 0;
216        }
217
218
219        function field_exists($table_name, $field_name, $no_prefix = false)
220        {
221                $result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\'');
222                return $this->num_rows($result) > 0;
223        }
224
225
226        function index_exists($table_name, $index_name, $no_prefix = false)
227        {
228                $exists = false;
229
230                $result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name);
231                while ($cur_index = $this->fetch_assoc($result))
232                {
233                        if (strtolower($cur_index['Key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name))
234                        {
235                                $exists = true;
236                                break;
237                        }
238                }
239
240                return $exists;
241        }
242
243
244        function create_table($table_name, $schema, $no_prefix = false)
245        {
246                if ($this->table_exists($table_name, $no_prefix))
247                        return true;
248
249                $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
250
251                // Go through every schema element and add it to the query
252                foreach ($schema['FIELDS'] as $field_name => $field_data)
253                {
254                        $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
255
256                        $query .= $field_name.' '.$field_data['datatype'];
257
258                        if (isset($field_data['collation']))
259                                $query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation'];
260
261                        if (!$field_data['allow_null'])
262                                $query .= ' NOT NULL';
263
264                        if (isset($field_data['default']))
265                                $query .= ' DEFAULT '.$field_data['default'];
266
267                        $query .= ",\n";
268                }
269
270                // If we have a primary key, add it
271                if (isset($schema['PRIMARY KEY']))
272                        $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
273
274                // Add unique keys
275                if (isset($schema['UNIQUE KEYS']))
276                {
277                        foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
278                                $query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n";
279                }
280
281                // Add indexes
282                if (isset($schema['INDEXES']))
283                {
284                        foreach ($schema['INDEXES'] as $index_name => $index_fields)
285                                $query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n";
286                }
287
288                // We remove the last two characters (a newline and a comma) and add on the ending
289                $query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'MyISAM').' CHARACTER SET utf8';
290
291                return $this->query($query) ? true : false;
292        }
293
294
295        function drop_table($table_name, $no_prefix = false)
296        {
297                if (!$this->table_exists($table_name, $no_prefix))
298                        return true;
299
300                return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
301        }
302
303
304        function rename_table($old_table, $new_table, $no_prefix = false)
305        {
306                // If there new table exists and the old one doesn't, then we're happy
307                if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix))
308                        return true;
309
310                return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false;
311        }
312
313
314        function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
315        {
316                if ($this->field_exists($table_name, $field_name, $no_prefix))
317                        return true;
318
319                $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
320
321                if ($default_value !== null && !is_int($default_value) && !is_float($default_value))
322                        $default_value = '\''.$this->escape($default_value).'\'';
323
324                return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) ? true : false;
325        }
326
327
328        function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
329        {
330                if (!$this->field_exists($table_name, $field_name, $no_prefix))
331                        return true;
332
333                $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
334
335                if ($default_value !== null && !is_int($default_value) && !is_float($default_value))
336                        $default_value = '\''.$this->escape($default_value).'\'';
337
338                return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' MODIFY '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) ? true : false;
339        }
340
341
342        function drop_field($table_name, $field_name, $no_prefix = false)
343        {
344                if (!$this->field_exists($table_name, $field_name, $no_prefix))
345                        return true;
346
347                return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false;
348        }
349
350
351        function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
352        {
353                if ($this->index_exists($table_name, $index_name, $no_prefix))
354                        return true;
355
356                return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.($unique ? 'UNIQUE ' : '').'INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.' ('.implode(',', $index_fields).')') ? true : false;
357        }
358
359
360        function drop_index($table_name, $index_name, $no_prefix = false)
361        {
362                if (!$this->index_exists($table_name, $index_name, $no_prefix))
363                        return true;
364
365                return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
366        }
367
368        function truncate_table($table_name, $no_prefix = false)
369        {
370                return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
371        }
372}
Note: See TracBrowser for help on using the repository browser.