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