1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
---|
2 | /** |
---|
3 | * CodeIgniter |
---|
4 | * |
---|
5 | * An open source application development framework for PHP 5.1.6 or newer |
---|
6 | * |
---|
7 | * @package CodeIgniter |
---|
8 | * @author ExpressionEngine Dev Team |
---|
9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
---|
10 | * @license http://codeigniter.com/user_guide/license.html |
---|
11 | * @link http://codeigniter.com |
---|
12 | * @since Version 1.0 |
---|
13 | * @filesource |
---|
14 | */ |
---|
15 | |
---|
16 | // ------------------------------------------------------------------------ |
---|
17 | |
---|
18 | /** |
---|
19 | * MySQL Utility Class |
---|
20 | * |
---|
21 | * @category Database |
---|
22 | * @author ExpressionEngine Dev Team |
---|
23 | * @link http://codeigniter.com/user_guide/database/ |
---|
24 | */ |
---|
25 | class CI_DB_mysql_utility extends CI_DB_utility { |
---|
26 | |
---|
27 | /** |
---|
28 | * List databases |
---|
29 | * |
---|
30 | * @access private |
---|
31 | * @return bool |
---|
32 | */ |
---|
33 | function _list_databases() |
---|
34 | { |
---|
35 | return "SHOW DATABASES"; |
---|
36 | } |
---|
37 | |
---|
38 | // -------------------------------------------------------------------- |
---|
39 | |
---|
40 | /** |
---|
41 | * Optimize table query |
---|
42 | * |
---|
43 | * Generates a platform-specific query so that a table can be optimized |
---|
44 | * |
---|
45 | * @access private |
---|
46 | * @param string the table name |
---|
47 | * @return object |
---|
48 | */ |
---|
49 | function _optimize_table($table) |
---|
50 | { |
---|
51 | return "OPTIMIZE TABLE ".$this->db->_escape_identifiers($table); |
---|
52 | } |
---|
53 | |
---|
54 | // -------------------------------------------------------------------- |
---|
55 | |
---|
56 | /** |
---|
57 | * Repair table query |
---|
58 | * |
---|
59 | * Generates a platform-specific query so that a table can be repaired |
---|
60 | * |
---|
61 | * @access private |
---|
62 | * @param string the table name |
---|
63 | * @return object |
---|
64 | */ |
---|
65 | function _repair_table($table) |
---|
66 | { |
---|
67 | return "REPAIR TABLE ".$this->db->_escape_identifiers($table); |
---|
68 | } |
---|
69 | |
---|
70 | // -------------------------------------------------------------------- |
---|
71 | /** |
---|
72 | * MySQL Export |
---|
73 | * |
---|
74 | * @access private |
---|
75 | * @param array Preferences |
---|
76 | * @return mixed |
---|
77 | */ |
---|
78 | function _backup($params = array()) |
---|
79 | { |
---|
80 | if (count($params) == 0) |
---|
81 | { |
---|
82 | return FALSE; |
---|
83 | } |
---|
84 | |
---|
85 | // Extract the prefs for simplicity |
---|
86 | extract($params); |
---|
87 | |
---|
88 | // Build the output |
---|
89 | $output = ''; |
---|
90 | foreach ((array)$tables as $table) |
---|
91 | { |
---|
92 | // Is the table in the "ignore" list? |
---|
93 | if (in_array($table, (array)$ignore, TRUE)) |
---|
94 | { |
---|
95 | continue; |
---|
96 | } |
---|
97 | |
---|
98 | // Get the table schema |
---|
99 | $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.`'.$table.'`'); |
---|
100 | |
---|
101 | // No result means the table name was invalid |
---|
102 | if ($query === FALSE) |
---|
103 | { |
---|
104 | continue; |
---|
105 | } |
---|
106 | |
---|
107 | // Write out the table schema |
---|
108 | $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline; |
---|
109 | |
---|
110 | if ($add_drop == TRUE) |
---|
111 | { |
---|
112 | $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline; |
---|
113 | } |
---|
114 | |
---|
115 | $i = 0; |
---|
116 | $result = $query->result_array(); |
---|
117 | foreach ($result[0] as $val) |
---|
118 | { |
---|
119 | if ($i++ % 2) |
---|
120 | { |
---|
121 | $output .= $val.';'.$newline.$newline; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | // If inserts are not needed we're done... |
---|
126 | if ($add_insert == FALSE) |
---|
127 | { |
---|
128 | continue; |
---|
129 | } |
---|
130 | |
---|
131 | // Grab all the data from the current table |
---|
132 | $query = $this->db->query("SELECT * FROM $table"); |
---|
133 | |
---|
134 | if ($query->num_rows() == 0) |
---|
135 | { |
---|
136 | continue; |
---|
137 | } |
---|
138 | |
---|
139 | // Fetch the field names and determine if the field is an |
---|
140 | // integer type. We use this info to decide whether to |
---|
141 | // surround the data with quotes or not |
---|
142 | |
---|
143 | $i = 0; |
---|
144 | $field_str = ''; |
---|
145 | $is_int = array(); |
---|
146 | while ($field = mysql_fetch_field($query->result_id)) |
---|
147 | { |
---|
148 | // Most versions of MySQL store timestamp as a string |
---|
149 | $is_int[$i] = (in_array( |
---|
150 | strtolower(mysql_field_type($query->result_id, $i)), |
---|
151 | array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'), |
---|
152 | TRUE) |
---|
153 | ) ? TRUE : FALSE; |
---|
154 | |
---|
155 | // Create a string of field names |
---|
156 | $field_str .= '`'.$field->name.'`, '; |
---|
157 | $i++; |
---|
158 | } |
---|
159 | |
---|
160 | // Trim off the end comma |
---|
161 | $field_str = preg_replace( "/, $/" , "" , $field_str); |
---|
162 | |
---|
163 | |
---|
164 | // Build the insert string |
---|
165 | foreach ($query->result_array() as $row) |
---|
166 | { |
---|
167 | $val_str = ''; |
---|
168 | |
---|
169 | $i = 0; |
---|
170 | foreach ($row as $v) |
---|
171 | { |
---|
172 | // Is the value NULL? |
---|
173 | if ($v === NULL) |
---|
174 | { |
---|
175 | $val_str .= 'NULL'; |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | // Escape the data if it's not an integer |
---|
180 | if ($is_int[$i] == FALSE) |
---|
181 | { |
---|
182 | $val_str .= $this->db->escape($v); |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | $val_str .= $v; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | // Append a comma |
---|
191 | $val_str .= ', '; |
---|
192 | $i++; |
---|
193 | } |
---|
194 | |
---|
195 | // Remove the comma at the end of the string |
---|
196 | $val_str = preg_replace( "/, $/" , "" , $val_str); |
---|
197 | |
---|
198 | // Build the INSERT string |
---|
199 | $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline; |
---|
200 | } |
---|
201 | |
---|
202 | $output .= $newline.$newline; |
---|
203 | } |
---|
204 | |
---|
205 | return $output; |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | /* End of file mysql_utility.php */ |
---|
210 | /* Location: ./system/database/drivers/mysql/mysql_utility.php */ |
---|