blob: ae3c26cba7631bd271b30781ec0e42880673b118 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev2caf2892012-01-27 00:12:03 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev2caf2892012-01-27 00:12:03 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * MySQL Utility Class
42 *
43 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050044 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020045 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000046 */
47class CI_DB_mysql_utility extends CI_DB_utility {
48
Andrey Andreevc98e93a2012-11-02 02:04:59 +020049 /**
50 * List databases statement
51 *
52 * @var string
53 */
Andrey Andreevb457a402012-04-09 16:11:56 +030054 protected $_list_databases = 'SHOW DATABASES';
Derek Allard2067d1a2008-11-13 22:59:24 +000055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 /**
Andrey Andreevc98e93a2012-11-02 02:04:59 +020057 * OPTIMIZE TABLE statement
Derek Allard2067d1a2008-11-13 22:59:24 +000058 *
Andrey Andreevc98e93a2012-11-02 02:04:59 +020059 * @var string
60 */
61 protected $_optimize_table = 'OPTIMIZE TABLE %s';
62
63 /**
64 * REPAIR TABLE statement
65 *
66 * @var string
67 */
68 protected $_repair_table = 'REPAIR TABLE %s';
69
70 // --------------------------------------------------------------------
71
72 /**
73 * Export
74 *
75 * @param array $params Preferences
Derek Allard2067d1a2008-11-13 22:59:24 +000076 * @return mixed
77 */
Andrey Andreevb457a402012-04-09 16:11:56 +030078 protected function _backup($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000079 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020080 if (count($params) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 return FALSE;
83 }
84
85 // Extract the prefs for simplicity
86 extract($params);
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 // Build the output
89 $output = '';
Andrew Podner79494dd2012-12-19 14:15:41 -050090
Andrew Podner48512172012-12-20 07:56:19 -050091 // Do we need to include a statement to disable foreign key checks?
92 if ($foreign_key_checks === FALSE)
Andrew Podner79494dd2012-12-19 14:15:41 -050093 {
Andrey Andreev61c4d0a2012-12-20 16:29:29 +020094 $output .= 'SET foreign_key_checks = 0;'.$newline;
Andrew Podner79494dd2012-12-19 14:15:41 -050095 }
96
Andrey Andreev2caf2892012-01-27 00:12:03 +020097 foreach ( (array) $tables as $table)
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
99 // Is the table in the "ignore" list?
Andrey Andreev2caf2892012-01-27 00:12:03 +0200100 if (in_array($table, (array) $ignore, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
102 continue;
103 }
104
105 // Get the table schema
Andrey Andreev9637b402012-06-08 15:39:24 +0300106 $query = $this->db->query('SHOW CREATE TABLE '.$this->db->escape_identifiers($this->db->database.'.'.$table));
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // No result means the table name was invalid
109 if ($query === FALSE)
110 {
111 continue;
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // Write out the table schema
115 $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
116
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100117 if ($add_drop === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200118 {
Taufan Aditya24dd2f92012-02-17 17:08:31 +0700119 $output .= 'DROP TABLE IF EXISTS '.$this->db->protect_identifiers($table).';'.$newline.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 $i = 0;
123 $result = $query->result_array();
124 foreach ($result[0] as $val)
125 {
126 if ($i++ % 2)
Barry Mienydd671972010-10-04 16:33:58 +0200127 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 $output .= $val.';'.$newline.$newline;
129 }
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // If inserts are not needed we're done...
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100133 if ($add_insert === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
135 continue;
136 }
137
138 // Grab all the data from the current table
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200139 $query = $this->db->query('SELECT * FROM '.$this->db->protect_identifiers($table));
Barry Mienydd671972010-10-04 16:33:58 +0200140
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100141 if ($query->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 continue;
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // Fetch the field names and determine if the field is an
Andrey Andreev2caf2892012-01-27 00:12:03 +0200147 // integer type. We use this info to decide whether to
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // surround the data with quotes or not
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 $i = 0;
151 $field_str = '';
152 $is_int = array();
153 while ($field = mysql_fetch_field($query->result_id))
154 {
155 // Most versions of MySQL store timestamp as a string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200156 $is_int[$i] = in_array(strtolower(mysql_field_type($query->result_id, $i)),
157 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'),
158 TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 // Create a string of field names
Andrey Andreev9637b402012-06-08 15:39:24 +0300161 $field_str .= $this->db->escape_identifiers($field->name).', ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 $i++;
163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 // Trim off the end comma
Andrey Andreev2caf2892012-01-27 00:12:03 +0200166 $field_str = preg_replace('/, $/' , '', $field_str);
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // Build the insert string
169 foreach ($query->result_array() as $row)
170 {
171 $val_str = '';
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 $i = 0;
174 foreach ($row as $v)
175 {
176 // Is the value NULL?
177 if ($v === NULL)
178 {
179 $val_str .= 'NULL';
180 }
181 else
182 {
183 // Escape the data if it's not an integer
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100184 $val_str .= ($is_int[$i] === FALSE) ? $this->db->escape($v) : $v;
Barry Mienydd671972010-10-04 16:33:58 +0200185 }
186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // Append a comma
188 $val_str .= ', ';
189 $i++;
190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 // Remove the comma at the end of the string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200193 $val_str = preg_replace('/, $/' , '', $val_str);
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // Build the INSERT string
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200196 $output .= 'INSERT INTO '.$this->db->protect_identifiers($table).' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Andrey Andreevc51816d2012-02-14 16:55:42 +0200199 $output .= $newline.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
201
Andrew Podner48512172012-12-20 07:56:19 -0500202 // Do we need to include a statement to re-enable foreign key checks?
203 if ($foreign_key_checks === FALSE)
Andrew Podner79494dd2012-12-19 14:15:41 -0500204 {
Andrey Andreev61c4d0a2012-12-20 16:29:29 +0200205 $output .= 'SET foreign_key_checks = 1;'.$newline;
Andrew Podner79494dd2012-12-19 14:15:41 -0500206 }
207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 return $output;
209 }
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211}