blob: 38feaaff11dc524039f2e8e000eb8b237e9f220a [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * MySQL Utility Class
20 *
21 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000022 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +000023 * @link http://www.codeigniter.com/user_guide/database/
24 */
25class CI_DB_mysql_utility extends CI_DB_utility {
Derek Allardd2df9bc2007-04-15 17:41:17 +000026
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 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000041 * 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_table($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_table($table);
68 }
69
70 // --------------------------------------------------------------------
Derek Allardd2df9bc2007-04-15 17:41:17 +000071 /**
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 {
Derek Allard39b622d2008-01-16 21:10:09 +0000148 // Most versions of MySQL store timestamp as a string
Derek Allardd2df9bc2007-04-15 17:41:17 +0000149 $is_int[$i] = (in_array(
150 strtolower(mysql_field_type($query->result_id, $i)),
Derek Allard39b622d2008-01-16 21:10:09 +0000151 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'),
Derek Allardd2df9bc2007-04-15 17:41:17 +0000152 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 {
Derek Allarddb438582007-10-13 17:00:16 +0000172 // Is the value NULL?
Derek Jones84357712008-01-18 15:52:48 +0000173 if ($v === NULL)
Derek Allarddb438582007-10-13 17:00:16 +0000174 {
175 $val_str .= 'NULL';
176 }
177 else
178 {
Derek Jones4a9cb962008-01-18 18:13:13 +0000179 // Do a little formatting...
180 $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v);
181 $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v);
182 $v = str_replace('\\', '\\\\', $v);
183 $v = str_replace('\'', '\\\'', $v);
184 $v = str_replace('\\\n', '\n', $v);
185 $v = str_replace('\\\r', '\r', $v);
186 $v = str_replace('\\\t', '\t', $v);
187
Derek Allarddb438582007-10-13 17:00:16 +0000188 // Escape the data if it's not an integer
189 if ($is_int[$i] == FALSE)
190 {
191 $val_str .= $this->db->escape($v);
192 }
193 else
194 {
195 $val_str .= $v;
196 }
197 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000198
Derek Allarddb438582007-10-13 17:00:16 +0000199 // Append a comma
200 $val_str .= ', ';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000201 $i++;
202 }
203
Derek Allarddb438582007-10-13 17:00:16 +0000204 // Remove the comma at the end of the string
Derek Allardd2df9bc2007-04-15 17:41:17 +0000205 $val_str = preg_replace( "/, $/" , "" , $val_str);
206
207 // Build the INSERT string
208 $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
209 }
210
211 $output .= $newline.$newline;
212 }
213
214 return $output;
215 }
216
Derek Allard39b622d2008-01-16 21:10:09 +0000217 /**
218 *
219 * The functions below have been deprecated as of 1.6, and are only here for backwards
220 * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
221 * is STRONGLY discouraged in favour if using dbforge.
222 *
223 */
224
225 /**
226 * Create database
227 *
228 * @access private
229 * @param string the database name
230 * @return bool
231 */
232 function _create_database($name)
233 {
234 return "CREATE DATABASE ".$name;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Drop database
241 *
242 * @access private
243 * @param string the database name
244 * @return bool
245 */
246 function _drop_database($name)
247 {
248 return "DROP DATABASE ".$name;
249 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000250
251}
admin7b613c72006-09-24 18:05:17 +0000252?>