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