blob: 538aaa0f7e9fc1ba2f89630eb497b39b10abb78f [file] [log] [blame]
Andrey Andreev2caf2892012-01-27 00:12:03 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev2caf2892012-01-27 00:12:03 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev2caf2892012-01-27 00:12:03 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * MySQL Utility Class
30 *
31 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * @link http://codeigniter.com/user_guide/database/
34 */
35class CI_DB_mysql_utility extends CI_DB_utility {
36
37 /**
38 * List databases
39 *
Andrey Andreev2caf2892012-01-27 00:12:03 +020040 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000041 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020042 public function _list_databases()
Derek Allard2067d1a2008-11-13 22:59:24 +000043 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020044 return 'SHOW DATABASES';
Derek Allard2067d1a2008-11-13 22:59:24 +000045 }
46
47 // --------------------------------------------------------------------
48
49 /**
50 * Optimize table query
51 *
52 * Generates a platform-specific query so that a table can be optimized
53 *
Derek Allard2067d1a2008-11-13 22:59:24 +000054 * @param string the table name
Andrey Andreev2caf2892012-01-27 00:12:03 +020055 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000056 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020057 public function _optimize_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +000058 {
Andrey Andreev93cac5c2012-02-14 14:45:02 +020059 return 'OPTIMIZE TABLE '.$this->db->protect_identifiers($table);
Derek Allard2067d1a2008-11-13 22:59:24 +000060 }
61
62 // --------------------------------------------------------------------
63
64 /**
65 * Repair table query
66 *
67 * Generates a platform-specific query so that a table can be repaired
68 *
Derek Allard2067d1a2008-11-13 22:59:24 +000069 * @param string the table name
Andrey Andreev2caf2892012-01-27 00:12:03 +020070 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000071 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020072 public function _repair_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
Andrey Andreev93cac5c2012-02-14 14:45:02 +020074 return 'REPAIR TABLE '.$this->db->protect_identifiers($table);
Derek Allard2067d1a2008-11-13 22:59:24 +000075 }
76
77 // --------------------------------------------------------------------
78 /**
79 * MySQL Export
80 *
Derek Allard2067d1a2008-11-13 22:59:24 +000081 * @param array Preferences
82 * @return mixed
83 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020084 public function _backup($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020086 if (count($params) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 return FALSE;
89 }
90
91 // Extract the prefs for simplicity
92 extract($params);
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // Build the output
95 $output = '';
Andrey Andreev2caf2892012-01-27 00:12:03 +020096 foreach ( (array) $tables as $table)
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
98 // Is the table in the "ignore" list?
Andrey Andreev2caf2892012-01-27 00:12:03 +020099 if (in_array($table, (array) $ignore, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 continue;
102 }
103
104 // Get the table schema
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200105 $query = $this->db->query('SHOW CREATE TABLE '.$this->db->protect_identifiers($this->db->database).'.'.$this->db->protect_identifiers($table));
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 // No result means the table name was invalid
108 if ($query === FALSE)
109 {
110 continue;
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 // Write out the table schema
114 $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
115
Barry Mienydd671972010-10-04 16:33:58 +0200116 if ($add_drop == TRUE)
117 {
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200118 $output .= 'DROP TABLE IF EXISTS '.($this->db->protect_identifiers($table).';'.$newline.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 $i = 0;
122 $result = $query->result_array();
123 foreach ($result[0] as $val)
124 {
125 if ($i++ % 2)
Barry Mienydd671972010-10-04 16:33:58 +0200126 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 $output .= $val.';'.$newline.$newline;
128 }
129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 // If inserts are not needed we're done...
132 if ($add_insert == FALSE)
133 {
134 continue;
135 }
136
137 // Grab all the data from the current table
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200138 $query = $this->db->query('SELECT * FROM '.$this->db->protect_identifiers($table));
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 if ($query->num_rows() == 0)
141 {
142 continue;
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // Fetch the field names and determine if the field is an
Andrey Andreev2caf2892012-01-27 00:12:03 +0200146 // integer type. We use this info to decide whether to
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // surround the data with quotes or not
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 $i = 0;
150 $field_str = '';
151 $is_int = array();
152 while ($field = mysql_fetch_field($query->result_id))
153 {
154 // Most versions of MySQL store timestamp as a string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200155 $is_int[$i] = in_array(strtolower(mysql_field_type($query->result_id, $i)),
156 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'),
157 TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 // Create a string of field names
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200160 $field_str .= $this->db->protect_identifiers($field->name).', ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 $i++;
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 // Trim off the end comma
Andrey Andreev2caf2892012-01-27 00:12:03 +0200165 $field_str = preg_replace('/, $/' , '', $field_str);
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 // Build the insert string
168 foreach ($query->result_array() as $row)
169 {
170 $val_str = '';
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 $i = 0;
173 foreach ($row as $v)
174 {
175 // Is the value NULL?
176 if ($v === NULL)
177 {
178 $val_str .= 'NULL';
179 }
180 else
181 {
182 // Escape the data if it's not an integer
Andrey Andreev2caf2892012-01-27 00:12:03 +0200183 $val_str .= ($is_int[$i] == FALSE) ? $this->db->escape($v) : $v;
Barry Mienydd671972010-10-04 16:33:58 +0200184 }
185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 // Append a comma
187 $val_str .= ', ';
188 $i++;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // Remove the comma at the end of the string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200192 $val_str = preg_replace('/, $/' , '', $val_str);
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // Build the INSERT string
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200195 $output .= 'INSERT INTO '.$this->db->protect_identifiers($table).' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Andrey Andreev2caf2892012-01-27 00:12:03 +0200198 return $output.$newline.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200
201 return $output;
202 }
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204}
205
206/* End of file mysql_utility.php */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200207/* Location: ./system/database/drivers/mysql/mysql_utility.php */