1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodeIgniter |
4
|
|
|
* |
5
|
|
|
* An open source application development framework for PHP |
6
|
|
|
* |
7
|
|
|
* This content is released under the MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* Copyright (c) 2014 - 2017, British Columbia Institute of Technology |
10
|
|
|
* |
11
|
|
|
* 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: |
17
|
|
|
* |
18
|
|
|
* 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 |
31
|
|
|
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
32
|
|
|
* @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/) |
33
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
34
|
|
|
* @link https://codeigniter.com |
35
|
|
|
* @since Version 1.3.0 |
36
|
|
|
* @filesource |
37
|
|
|
*/ |
38
|
|
|
namespace Rioxygen\CiCoreDatabase\Mysql; |
39
|
|
|
|
40
|
|
|
use Rioxygen\CiCoreDatabase\CI_DB_utility; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* MySQLi Utility Class |
44
|
|
|
* |
45
|
|
|
* @package CodeIgniter |
46
|
|
|
* @subpackage Drivers |
47
|
|
|
* @category Database |
48
|
|
|
* @author EllisLab Dev Team |
49
|
|
|
* @link https://codeigniter.com/user_guide/database/ |
50
|
|
|
*/ |
51
|
|
|
class CI_DB_mysqli_utility extends CI_DB_utility { |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* List databases statement |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $_list_databases = 'SHOW DATABASES'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* OPTIMIZE TABLE statement |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $_optimize_table = 'OPTIMIZE TABLE %s'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* REPAIR TABLE statement |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $_repair_table = 'REPAIR TABLE %s'; |
73
|
|
|
|
74
|
|
|
// -------------------------------------------------------------------- |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Export |
78
|
|
|
* |
79
|
|
|
* @param array $params Preferences |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
protected function _backup($params = array()) |
83
|
|
|
{ |
84
|
|
|
if (count($params) === 0) |
85
|
|
|
{ |
86
|
|
|
return FALSE; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Extract the prefs for simplicity |
90
|
|
|
extract($params); |
91
|
|
|
|
92
|
|
|
// Build the output |
93
|
|
|
$output = ''; |
94
|
|
|
|
95
|
|
|
// Do we need to include a statement to disable foreign key checks? |
96
|
|
|
if ($foreign_key_checks === FALSE) |
97
|
|
|
{ |
98
|
|
|
$output .= 'SET foreign_key_checks = 0;'.$newline; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach ( (array) $tables as $table) |
102
|
|
|
{ |
103
|
|
|
// Is the table in the "ignore" list? |
104
|
|
|
if (in_array($table, (array) $ignore, TRUE)) |
105
|
|
|
{ |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Get the table schema |
110
|
|
|
$query = $this->db->query('SHOW CREATE TABLE '.$this->db->escape_identifiers($this->db->database.'.'.$table)); |
111
|
|
|
|
112
|
|
|
// No result means the table name was invalid |
113
|
|
|
if ($query === FALSE) |
114
|
|
|
{ |
115
|
|
|
continue; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Write out the table schema |
119
|
|
|
$output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline; |
120
|
|
|
|
121
|
|
|
if ($add_drop === TRUE) |
122
|
|
|
{ |
123
|
|
|
$output .= 'DROP TABLE IF EXISTS '.$this->db->protect_identifiers($table).';'.$newline.$newline; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$i = 0; |
127
|
|
|
$result = $query->result_array(); |
128
|
|
|
foreach ($result[0] as $val) |
129
|
|
|
{ |
130
|
|
|
if ($i++ % 2) |
131
|
|
|
{ |
132
|
|
|
$output .= $val.';'.$newline.$newline; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// If inserts are not needed we're done... |
137
|
|
|
if ($add_insert === FALSE) |
138
|
|
|
{ |
139
|
|
|
continue; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Grab all the data from the current table |
143
|
|
|
$query = $this->db->query('SELECT * FROM '.$this->db->protect_identifiers($table)); |
144
|
|
|
|
145
|
|
|
if ($query->num_rows() === 0) |
146
|
|
|
{ |
147
|
|
|
continue; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Fetch the field names and determine if the field is an |
151
|
|
|
// integer type. We use this info to decide whether to |
152
|
|
|
// surround the data with quotes or not |
153
|
|
|
|
154
|
|
|
$i = 0; |
155
|
|
|
$field_str = ''; |
156
|
|
|
$is_int = array(); |
157
|
|
|
while ($field = $query->result_id->fetch_field()) |
158
|
|
|
{ |
159
|
|
|
// Most versions of MySQL store timestamp as a string |
160
|
|
|
$is_int[$i] = ($field->type & MYSQLI_TYPE_TINY) |
161
|
|
|
OR ($field->type & MYSQLI_TYPE_SHORT) |
162
|
|
|
OR ($field->type & MYSQLI_TYPE_INT24) |
163
|
|
|
OR ($field->type & MYSQLI_TYPE_LONG) |
164
|
|
|
OR ($field->type & MYSQLI_TYPE_LONGLONG); |
165
|
|
|
|
166
|
|
|
// Create a string of field names |
167
|
|
|
$field_str .= $this->db->escape_identifiers($field->name).', '; |
168
|
|
|
$i++; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// Trim off the end comma |
172
|
|
|
$field_str = preg_replace('/, $/' , '', $field_str); |
173
|
|
|
|
174
|
|
|
// Build the insert string |
175
|
|
|
foreach ($query->result_array() as $row) |
176
|
|
|
{ |
177
|
|
|
$val_str = ''; |
178
|
|
|
|
179
|
|
|
$i = 0; |
180
|
|
|
foreach ($row as $v) |
181
|
|
|
{ |
182
|
|
|
// Is the value NULL? |
183
|
|
|
if ($v === NULL) |
184
|
|
|
{ |
185
|
|
|
$val_str .= 'NULL'; |
186
|
|
|
} |
187
|
|
|
else |
188
|
|
|
{ |
189
|
|
|
// Escape the data if it's not an integer |
190
|
|
|
$val_str .= ($is_int[$i] === FALSE) ? $this->db->escape($v) : $v; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// Append a comma |
194
|
|
|
$val_str .= ', '; |
195
|
|
|
$i++; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// Remove the comma at the end of the string |
199
|
|
|
$val_str = preg_replace('/, $/' , '', $val_str); |
200
|
|
|
|
201
|
|
|
// Build the INSERT string |
202
|
|
|
$output .= 'INSERT INTO '.$this->db->protect_identifiers($table).' ('.$field_str.') VALUES ('.$val_str.');'.$newline; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$output .= $newline.$newline; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// Do we need to include a statement to re-enable foreign key checks? |
209
|
|
|
if ($foreign_key_checks === FALSE) |
210
|
|
|
{ |
211
|
|
|
$output .= 'SET foreign_key_checks = 1;'.$newline; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $output; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
} |
218
|
|
|
|