blob: 1d910638fc549437c64d24ad489936ab61b7778f [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
8 * @author Rick Ellis
9 * @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 * MySQLi Utility Class
20 *
21 * @category Database
22 * @author Rick Ellis
23 * @link http://www.codeigniter.com/user_guide/database/
24 */
25class CI_DB_mysqli_utility extends CI_DB_utility {
26
27 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000028 * 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_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 // --------------------------------------------------------------------
71
72 /**
73 * MySQLi Export
74 *
75 * @access private
76 * @param array Preferences
77 * @return mixed
78 */
79 function _backup($params = array())
80 {
81 if (count($params) == 0)
82 {
83 return FALSE;
84 }
85
86 // Extract the prefs for simplicity
87 extract($params);
88
89 // Build the output
90 $output = '';
91 foreach ((array)$tables as $table)
92 {
93 // Is the table in the "ignore" list?
94 if (in_array($table, (array)$ignore, TRUE))
95 {
96 continue;
97 }
98
99 // Get the table schema
100 $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table);
101
102 // No result means the table name was invalid
103 if ($query === FALSE)
104 {
105 continue;
106 }
107
108 // Write out the table schema
109 $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
110
111 if ($add_drop == TRUE)
112 {
113 $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline;
114 }
115
116 $i = 0;
117 $result = $query->result_array();
118 foreach ($result[0] as $val)
119 {
120 if ($i++ % 2)
121 {
122 $output .= $val.';'.$newline.$newline;
123 }
124 }
125
126 // If inserts are not needed we're done...
127 if ($add_insert == FALSE)
128 {
129 continue;
130 }
131
132 // Grab all the data from the current table
133 $query = $this->db->query("SELECT * FROM $table");
134
135 if ($query->num_rows() == 0)
136 {
137 continue;
138 }
139
140 // Fetch the field names and determine if the field is an
141 // integer type. We use this info to decide whether to
142 // surround the data with quotes or not
143
144 $i = 0;
145 $field_str = '';
146 $is_int = array();
147 while ($field = mysqli_fetch_field($query->result_id))
148 {
Derek Allard39b622d2008-01-16 21:10:09 +0000149 // Most versions of MySQL store timestamp as a string
Derek Allardd2df9bc2007-04-15 17:41:17 +0000150 $is_int[$i] = (in_array(
151 strtolower(mysql_field_type($query->result_id, $i)),
Derek Allard39b622d2008-01-16 21:10:09 +0000152 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), // 'timestamp'),
Derek Allardd2df9bc2007-04-15 17:41:17 +0000153 TRUE)
154 ) ? TRUE : FALSE;
155
156 // Create a string of field names
157 $field_str .= $field->name.', ';
158 $i++;
159 }
160
161 // Trim off the end comma
162 $field_str = preg_replace( "/, $/" , "" , $field_str);
163
164
165 // Build the insert string
166 foreach ($query->result_array() as $row)
167 {
168 $val_str = '';
169
170 $i = 0;
171 foreach ($row as $v)
172 {
173 // Do a little formatting...
174 $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v);
175 $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v);
176 $v = str_replace('\\', '\\\\', $v);
177 $v = str_replace('\'', '\\\'', $v);
178 $v = str_replace('\\\n', '\n', $v);
179 $v = str_replace('\\\r', '\r', $v);
180 $v = str_replace('\\\t', '\t', $v);
181
Derek Allard694b5b82007-12-18 15:58:03 +0000182 // 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 if ($is_int[$i] == FALSE)
191 {
192 $val_str .= $this->db->escape($v);
193 }
194 else
195 {
196 $val_str .= $v;
197 }
198 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000199
Derek Allard694b5b82007-12-18 15:58:03 +0000200 // Append a comma
201 $val_str .= ', ';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000202 $i++;
203 }
204
Derek Allard694b5b82007-12-18 15:58:03 +0000205 // Remove the comma at the end of the string
Derek Allardd2df9bc2007-04-15 17:41:17 +0000206 $val_str = preg_replace( "/, $/" , "" , $val_str);
207
208 // Build the INSERT string
209 $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
210 }
211
212 $output .= $newline.$newline;
213 }
214
215 return $output;
216 }
217
Derek Allard39b622d2008-01-16 21:10:09 +0000218 /**
219 *
220 * The functions below have been deprecated as of 1.6, and are only here for backwards
221 * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
222 * is STRONGLY discouraged in favour if using dbforge.
223 *
224 */
Derek Allardd2df9bc2007-04-15 17:41:17 +0000225
Derek Allard39b622d2008-01-16 21:10:09 +0000226 /**
227 * Create database
228 *
229 * @access private
230 * @param string the database name
231 * @return bool
232 */
233 function _create_database($name)
234 {
235 return "CREATE DATABASE ".$name;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Drop database
242 *
243 * @access private
244 * @param string the database name
245 * @return bool
246 */
247 function _drop_database($name)
248 {
249 return "DROP DATABASE ".$name;
250 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000251
252}
admin7b613c72006-09-24 18:05:17 +0000253?>