Ignore:
Timestamp:
Nov 14, 2011, 11:17:15 PM (13 years ago)
Author:
dj3c1t
Message:

passage a Fluxbb 1.4.7

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/rsr.v5.1.dev/web/punbb/include/dblayer/mysqli.php

    r1 r3  
    11<?php
    2 /***********************************************************************
    3 
    4   Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)
    5 
    6   This file is part of PunBB.
    7 
    8   PunBB is free software; you can redistribute it and/or modify it
    9   under the terms of the GNU General Public License as published
    10   by the Free Software Foundation; either version 2 of the License,
    11   or (at your option) any later version.
    12 
    13   PunBB is distributed in the hope that it will be useful, but
    14   WITHOUT ANY WARRANTY; without even the implied warranty of
    15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16   GNU General Public License for more details.
    17 
    18   You should have received a copy of the GNU General Public License
    19   along with this program; if not, write to the Free Software
    20   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
    21   MA  02111-1307  USA
    22 
    23 ************************************************************************/
    24 
     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 */
    258
    269// Make sure we have built in support for MySQL
     
    3821        var $num_queries = 0;
    3922
    40 
    41         function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $foo)
     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)
    4232        {
    4333                $this->prefix = $db_prefix;
     
    4737                        list($db_host, $db_port) = explode(':', $db_host);
    4838
     39                // Persistent connection in MySQLi are only available in PHP 5.3 and later releases
     40                $p_connect = $p_connect && version_compare(PHP_VERSION, '5.3.0', '>=') ? 'p:' : '';
     41
    4942                if (isset($db_port))
    50                         $this->link_id = @mysqli_connect($db_host, $db_username, $db_password, $db_name, $db_port);
     43                        $this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name, $db_port);
    5144                else
    52                         $this->link_id = @mysqli_connect($db_host, $db_username, $db_password, $db_name);
     45                        $this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name);
    5346
    5447                if (!$this->link_id)
    5548                        error('Unable to connect to MySQL and select database. MySQL reported: '.mysqli_connect_error(), __FILE__, __LINE__);
     49
     50                // Setup the client-server character set (UTF-8)
     51                if (!defined('FORUM_NO_SET_NAMES'))
     52                        $this->set_names('utf8');
     53
     54                return $this->link_id;
    5655        }
    5756
     
    9089                                $this->saved_queries[] = array($sql, 0);
    9190
     91                        $this->error_no = @mysqli_errno($this->link_id);
     92                        $this->error_msg = @mysqli_error($this->link_id);
     93
    9294                        return false;
    9395                }
     
    9597
    9698
    97         function result($query_id = 0, $row = 0)
     99        function result($query_id = 0, $row = 0, $col = 0)
    98100        {
    99101                if ($query_id)
    100102                {
    101                         if ($row)
    102                                 @mysqli_data_seek($query_id, $row);
     103                        if ($row !== 0 && @mysqli_data_seek($query_id, $row) === false)
     104                                return false;
    103105
    104106                        $cur_row = @mysqli_fetch_row($query_id);
    105                         return $cur_row[0];
     107                        if ($cur_row === false)
     108                                return false;
     109
     110                        return $cur_row[$col];
    106111                }
    107112                else
     
    160165        function escape($str)
    161166        {
    162                 return mysqli_real_escape_string($this->link_id, $str);
     167                return is_array($str) ? '' : mysqli_real_escape_string($this->link_id, $str);
    163168        }
    164169
     
    167172        {
    168173                $result['error_sql'] = @current(@end($this->saved_queries));
    169                 $result['error_no'] = @mysqli_errno($this->link_id);
    170                 $result['error_msg'] = @mysqli_error($this->link_id);
     174                $result['error_no'] = $this->error_no;
     175                $result['error_msg'] = $this->error_msg;
    171176
    172177                return $result;
     
    186191                        return false;
    187192        }
     193
     194
     195        function get_names()
     196        {
     197                $result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\'');
     198                return $this->result($result, 0, 1);
     199        }
     200
     201
     202        function set_names($names)
     203        {
     204                return $this->query('SET NAMES \''.$this->escape($names).'\'');
     205        }
     206
     207
     208        function get_version()
     209        {
     210                $result = $this->query('SELECT VERSION()');
     211
     212                return array(
     213                        'name'          => 'MySQL Improved',
     214                        'version'       => preg_replace('%^([^-]+).*$%', '\\1', $this->result($result))
     215                );
     216        }
     217
     218
     219        function table_exists($table_name, $no_prefix = false)
     220        {
     221                $result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\'');
     222                return $this->num_rows($result) > 0;
     223        }
     224
     225
     226        function field_exists($table_name, $field_name, $no_prefix = false)
     227        {
     228                $result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\'');
     229                return $this->num_rows($result) > 0;
     230        }
     231
     232
     233        function index_exists($table_name, $index_name, $no_prefix = false)
     234        {
     235                $exists = false;
     236
     237                $result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name);
     238                while ($cur_index = $this->fetch_assoc($result))
     239                {
     240                        if (strtolower($cur_index['Key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name))
     241                        {
     242                                $exists = true;
     243                                break;
     244                        }
     245                }
     246
     247                return $exists;
     248        }
     249
     250
     251        function create_table($table_name, $schema, $no_prefix = false)
     252        {
     253                if ($this->table_exists($table_name, $no_prefix))
     254                        return true;
     255
     256                $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
     257
     258                // Go through every schema element and add it to the query
     259                foreach ($schema['FIELDS'] as $field_name => $field_data)
     260                {
     261                        $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
     262
     263                        $query .= $field_name.' '.$field_data['datatype'];
     264
     265                        if (isset($field_data['collation']))
     266                                $query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation'];
     267
     268                        if (!$field_data['allow_null'])
     269                                $query .= ' NOT NULL';
     270
     271                        if (isset($field_data['default']))
     272                                $query .= ' DEFAULT '.$field_data['default'];
     273
     274                        $query .= ",\n";
     275                }
     276
     277                // If we have a primary key, add it
     278                if (isset($schema['PRIMARY KEY']))
     279                        $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
     280
     281                // Add unique keys
     282                if (isset($schema['UNIQUE KEYS']))
     283                {
     284                        foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
     285                                $query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n";
     286                }
     287
     288                // Add indexes
     289                if (isset($schema['INDEXES']))
     290                {
     291                        foreach ($schema['INDEXES'] as $index_name => $index_fields)
     292                                $query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n";
     293                }
     294
     295                // We remove the last two characters (a newline and a comma) and add on the ending
     296                $query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'MyISAM').' CHARACTER SET utf8';
     297
     298                return $this->query($query) ? true : false;
     299        }
     300
     301
     302        function drop_table($table_name, $no_prefix = false)
     303        {
     304                if (!$this->table_exists($table_name, $no_prefix))
     305                        return true;
     306
     307                return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
     308        }
     309
     310
     311        function rename_table($old_table, $new_table, $no_prefix = false)
     312        {
     313                // If there new table exists and the old one doesn't, then we're happy
     314                if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix))
     315                        return true;
     316
     317                return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false;
     318        }
     319
     320
     321        function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
     322        {
     323                if ($this->field_exists($table_name, $field_name, $no_prefix))
     324                        return true;
     325
     326                $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
     327
     328                if ($default_value !== null && !is_int($default_value) && !is_float($default_value))
     329                        $default_value = '\''.$this->escape($default_value).'\'';
     330
     331                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;
     332        }
     333
     334
     335        function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
     336        {
     337                if (!$this->field_exists($table_name, $field_name, $no_prefix))
     338                        return true;
     339
     340                $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
     341
     342                if ($default_value !== null && !is_int($default_value) && !is_float($default_value))
     343                        $default_value = '\''.$this->escape($default_value).'\'';
     344
     345                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;
     346        }
     347
     348
     349        function drop_field($table_name, $field_name, $no_prefix = false)
     350        {
     351                if (!$this->field_exists($table_name, $field_name, $no_prefix))
     352                        return true;
     353
     354                return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false;
     355        }
     356
     357
     358        function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
     359        {
     360                if ($this->index_exists($table_name, $index_name, $no_prefix))
     361                        return true;
     362
     363                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;
     364        }
     365
     366
     367        function drop_index($table_name, $index_name, $no_prefix = false)
     368        {
     369                if (!$this->index_exists($table_name, $index_name, $no_prefix))
     370                        return true;
     371
     372                return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
     373        }
     374
     375        function truncate_table($table_name, $no_prefix = false)
     376        {
     377                return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
     378        }
    188379}
Note: See TracChangeset for help on using the changeset viewer.