blob: 8aa051755478a0ac5dcc04b2ffb8c5b7db7b2349 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * MySQL Utility Class
31 *
32 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050033 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * @link http://codeigniter.com/user_guide/database/
35 */
36class CI_DB_mysql_utility extends CI_DB_utility {
37
Andrey Andreevc98e93a2012-11-02 02:04:59 +020038 /**
39 * List databases statement
40 *
41 * @var string
42 */
Andrey Andreevb457a402012-04-09 16:11:56 +030043 protected $_list_databases = 'SHOW DATABASES';
Derek Allard2067d1a2008-11-13 22:59:24 +000044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 /**
Andrey Andreevc98e93a2012-11-02 02:04:59 +020046 * OPTIMIZE TABLE statement
Derek Allard2067d1a2008-11-13 22:59:24 +000047 *
Andrey Andreevc98e93a2012-11-02 02:04:59 +020048 * @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 Allard2067d1a2008-11-13 22:59:24 +000065 * @return mixed
66 */
Andrey Andreevb457a402012-04-09 16:11:56 +030067 protected function _backup($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020069 if (count($params) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
71 return FALSE;
72 }
73
74 // Extract the prefs for simplicity
75 extract($params);
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // Build the output
78 $output = '';
Andrey Andreev2caf2892012-01-27 00:12:03 +020079 foreach ( (array) $tables as $table)
Derek Allard2067d1a2008-11-13 22:59:24 +000080 {
81 // Is the table in the "ignore" list?
Andrey Andreev2caf2892012-01-27 00:12:03 +020082 if (in_array($table, (array) $ignore, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
84 continue;
85 }
86
87 // Get the table schema
Andrey Andreev9637b402012-06-08 15:39:24 +030088 $query = $this->db->query('SHOW CREATE TABLE '.$this->db->escape_identifiers($this->db->database.'.'.$table));
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 // No result means the table name was invalid
91 if ($query === FALSE)
92 {
93 continue;
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 // Write out the table schema
97 $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
98
Alex Bilbie48a2baf2012-06-02 11:09:54 +010099 if ($add_drop === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200100 {
Taufan Aditya24dd2f92012-02-17 17:08:31 +0700101 $output .= 'DROP TABLE IF EXISTS '.$this->db->protect_identifiers($table).';'.$newline.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 $i = 0;
105 $result = $query->result_array();
106 foreach ($result[0] as $val)
107 {
108 if ($i++ % 2)
Barry Mienydd671972010-10-04 16:33:58 +0200109 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 $output .= $val.';'.$newline.$newline;
111 }
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // If inserts are not needed we're done...
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100115 if ($add_insert === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 continue;
118 }
119
120 // Grab all the data from the current table
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200121 $query = $this->db->query('SELECT * FROM '.$this->db->protect_identifiers($table));
Barry Mienydd671972010-10-04 16:33:58 +0200122
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100123 if ($query->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
125 continue;
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 // Fetch the field names and determine if the field is an
Andrey Andreev2caf2892012-01-27 00:12:03 +0200129 // integer type. We use this info to decide whether to
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // surround the data with quotes or not
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 $i = 0;
133 $field_str = '';
134 $is_int = array();
135 while ($field = mysql_fetch_field($query->result_id))
136 {
137 // Most versions of MySQL store timestamp as a string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200138 $is_int[$i] = in_array(strtolower(mysql_field_type($query->result_id, $i)),
139 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'),
140 TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 // Create a string of field names
Andrey Andreev9637b402012-06-08 15:39:24 +0300143 $field_str .= $this->db->escape_identifiers($field->name).', ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 $i++;
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // Trim off the end comma
Andrey Andreev2caf2892012-01-27 00:12:03 +0200148 $field_str = preg_replace('/, $/' , '', $field_str);
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // Build the insert string
151 foreach ($query->result_array() as $row)
152 {
153 $val_str = '';
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 $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
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100166 $val_str .= ($is_int[$i] === FALSE) ? $this->db->escape($v) : $v;
Barry Mienydd671972010-10-04 16:33:58 +0200167 }
168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // Append a comma
170 $val_str .= ', ';
171 $i++;
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // Remove the comma at the end of the string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200175 $val_str = preg_replace('/, $/' , '', $val_str);
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 // Build the INSERT string
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200178 $output .= 'INSERT INTO '.$this->db->protect_identifiers($table).' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Andrey Andreevc51816d2012-02-14 16:55:42 +0200181 $output .= $newline.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183
184 return $output;
185 }
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187}
188
189/* End of file mysql_utility.php */
Timothy Warren9cc5c602012-03-19 18:36:37 -0400190/* Location: ./system/database/drivers/mysql/mysql_utility.php */