blob: ff4949046ddb4ec039d513bba80910980806c6da [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 Andreev1ff49e02012-01-27 11:30:41 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1ff49e02012-01-27 11:30:41 +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 * MySQLi 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_mysqli_utility extends CI_DB_utility {
Barry Mienydd671972010-10-04 16:33:58 +020037
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
45 /**
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 Andreev782de112012-06-12 03:04:50 +030069 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 = '';
Andrew Podner79494dd2012-12-19 14:15:41 -050079
80 // Do we need to include a statement to disable FK checks?
81 if ($fk_checks === FALSE)
82 {
83 $output .= "SET foreign_key_checks = 0;".$newline;
84 }
85
Andrey Andreev782de112012-06-12 03:04:50 +030086 foreach ( (array) $tables as $table)
87 {
88 // Is the table in the "ignore" list?
89 if (in_array($table, (array) $ignore, TRUE))
90 {
91 continue;
92 }
93
94 // Get the table schema
95 $query = $this->db->query('SHOW CREATE TABLE '.$this->db->escape_identifiers($this->db->database.'.'.$table));
96
97 // No result means the table name was invalid
98 if ($query === FALSE)
99 {
100 continue;
101 }
102
103 // Write out the table schema
104 $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
105
106 if ($add_drop === TRUE)
107 {
108 $output .= 'DROP TABLE IF EXISTS '.$this->db->protect_identifiers($table).';'.$newline.$newline;
109 }
110
111 $i = 0;
112 $result = $query->result_array();
113 foreach ($result[0] as $val)
114 {
115 if ($i++ % 2)
116 {
117 $output .= $val.';'.$newline.$newline;
118 }
119 }
120
121 // If inserts are not needed we're done...
122 if ($add_insert === FALSE)
123 {
124 continue;
125 }
126
127 // Grab all the data from the current table
128 $query = $this->db->query('SELECT * FROM '.$this->db->protect_identifiers($table));
129
130 if ($query->num_rows() === 0)
131 {
132 continue;
133 }
134
135 // Fetch the field names and determine if the field is an
136 // integer type. We use this info to decide whether to
137 // surround the data with quotes or not
138
139 $i = 0;
140 $field_str = '';
141 $is_int = array();
142 while ($field = $query->result_id->fetch_field())
143 {
144 // Most versions of MySQL store timestamp as a string
145 $is_int[$i] = in_array(strtolower($field->type),
146 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'),
147 TRUE);
148
149 // Create a string of field names
150 $field_str .= $this->db->escape_identifiers($field->name).', ';
151 $i++;
152 }
153
154 // Trim off the end comma
155 $field_str = preg_replace('/, $/' , '', $field_str);
156
157 // Build the insert string
158 foreach ($query->result_array() as $row)
159 {
160 $val_str = '';
161
162 $i = 0;
163 foreach ($row as $v)
164 {
165 // Is the value NULL?
166 if ($v === NULL)
167 {
168 $val_str .= 'NULL';
169 }
170 else
171 {
172 // Escape the data if it's not an integer
173 $val_str .= ($is_int[$i] === FALSE) ? $this->db->escape($v) : $v;
174 }
175
176 // Append a comma
177 $val_str .= ', ';
178 $i++;
179 }
180
181 // Remove the comma at the end of the string
182 $val_str = preg_replace('/, $/' , '', $val_str);
183
184 // Build the INSERT string
185 $output .= 'INSERT INTO '.$this->db->protect_identifiers($table).' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
186 }
187
188 $output .= $newline.$newline;
189 }
190
Andrew Podner79494dd2012-12-19 14:15:41 -0500191 // Do we need to include a statement to re-enable FK checks?
192 if ($fk_checks === FALSE)
193 {
194 $output .= "SET foreign_key_checks = 1;".$newline;
195 }
196
Andrey Andreev782de112012-06-12 03:04:50 +0300197 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
Andrey Andreev782de112012-06-12 03:04:50 +0300199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200}
201
202/* End of file mysqli_utility.php */
Timothy Warren9cc5c602012-03-19 18:36:37 -0400203/* Location: ./system/database/drivers/mysqli/mysqli_utility.php */