Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * MySQLi Utility Class |
| 31 | * |
| 32 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 33 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 34 | * @link http://codeigniter.com/user_guide/database/ |
| 35 | */ |
| 36 | class CI_DB_mysqli_utility extends CI_DB_utility { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 37 | |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame^] | 38 | /** |
| 39 | * List databases statement |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame] | 43 | protected $_list_databases = 'SHOW DATABASES'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame^] | 46 | * OPTIMIZE TABLE statement |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | * |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame^] | 48 | * @var string |
| 49 | */ |
| 50 | protected $_optimize_table = 'OPTIMIZE TABLE %s'; |
| 51 | |
| 52 | /** |
| 53 | * REPAIR TABLE statement |
| 54 | * |
| 55 | * @var string |
| 56 | */ |
| 57 | protected $_repair_table = 'REPAIR TABLE %s'; |
| 58 | |
| 59 | // -------------------------------------------------------------------- |
| 60 | |
| 61 | /** |
| 62 | * Export |
| 63 | * |
| 64 | * @param array $params Preferences |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | * @return mixed |
| 66 | */ |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame] | 67 | protected function _backup($params = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | { |
Andrey Andreev | 782de11 | 2012-06-12 03:04:50 +0300 | [diff] [blame] | 69 | if (count($params) === 0) |
| 70 | { |
| 71 | return FALSE; |
| 72 | } |
| 73 | |
| 74 | // Extract the prefs for simplicity |
| 75 | extract($params); |
| 76 | |
| 77 | // Build the output |
| 78 | $output = ''; |
| 79 | foreach ( (array) $tables as $table) |
| 80 | { |
| 81 | // Is the table in the "ignore" list? |
| 82 | if (in_array($table, (array) $ignore, TRUE)) |
| 83 | { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | // Get the table schema |
| 88 | $query = $this->db->query('SHOW CREATE TABLE '.$this->db->escape_identifiers($this->db->database.'.'.$table)); |
| 89 | |
| 90 | // No result means the table name was invalid |
| 91 | if ($query === FALSE) |
| 92 | { |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | // Write out the table schema |
| 97 | $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline; |
| 98 | |
| 99 | if ($add_drop === TRUE) |
| 100 | { |
| 101 | $output .= 'DROP TABLE IF EXISTS '.$this->db->protect_identifiers($table).';'.$newline.$newline; |
| 102 | } |
| 103 | |
| 104 | $i = 0; |
| 105 | $result = $query->result_array(); |
| 106 | foreach ($result[0] as $val) |
| 107 | { |
| 108 | if ($i++ % 2) |
| 109 | { |
| 110 | $output .= $val.';'.$newline.$newline; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // If inserts are not needed we're done... |
| 115 | if ($add_insert === FALSE) |
| 116 | { |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | // Grab all the data from the current table |
| 121 | $query = $this->db->query('SELECT * FROM '.$this->db->protect_identifiers($table)); |
| 122 | |
| 123 | if ($query->num_rows() === 0) |
| 124 | { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | // Fetch the field names and determine if the field is an |
| 129 | // integer type. We use this info to decide whether to |
| 130 | // surround the data with quotes or not |
| 131 | |
| 132 | $i = 0; |
| 133 | $field_str = ''; |
| 134 | $is_int = array(); |
| 135 | while ($field = $query->result_id->fetch_field()) |
| 136 | { |
| 137 | // Most versions of MySQL store timestamp as a string |
| 138 | $is_int[$i] = in_array(strtolower($field->type), |
| 139 | array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'), |
| 140 | TRUE); |
| 141 | |
| 142 | // Create a string of field names |
| 143 | $field_str .= $this->db->escape_identifiers($field->name).', '; |
| 144 | $i++; |
| 145 | } |
| 146 | |
| 147 | // Trim off the end comma |
| 148 | $field_str = preg_replace('/, $/' , '', $field_str); |
| 149 | |
| 150 | // Build the insert string |
| 151 | foreach ($query->result_array() as $row) |
| 152 | { |
| 153 | $val_str = ''; |
| 154 | |
| 155 | $i = 0; |
| 156 | foreach ($row as $v) |
| 157 | { |
| 158 | // Is the value NULL? |
| 159 | if ($v === NULL) |
| 160 | { |
| 161 | $val_str .= 'NULL'; |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | // Escape the data if it's not an integer |
| 166 | $val_str .= ($is_int[$i] === FALSE) ? $this->db->escape($v) : $v; |
| 167 | } |
| 168 | |
| 169 | // Append a comma |
| 170 | $val_str .= ', '; |
| 171 | $i++; |
| 172 | } |
| 173 | |
| 174 | // Remove the comma at the end of the string |
| 175 | $val_str = preg_replace('/, $/' , '', $val_str); |
| 176 | |
| 177 | // Build the INSERT string |
| 178 | $output .= 'INSERT INTO '.$this->db->protect_identifiers($table).' ('.$field_str.') VALUES ('.$val_str.');'.$newline; |
| 179 | } |
| 180 | |
| 181 | $output .= $newline.$newline; |
| 182 | } |
| 183 | |
| 184 | return $output; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | } |
Andrey Andreev | 782de11 | 2012-06-12 03:04:50 +0300 | [diff] [blame] | 186 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* End of file mysqli_utility.php */ |
Timothy Warren | 9cc5c60 | 2012-03-19 18:36:37 -0400 | [diff] [blame] | 190 | /* Location: ./system/database/drivers/mysqli/mysqli_utility.php */ |