blob: a81c915f6f6eb347e5639d42df253e512a049a21 [file] [log] [blame]
admin7b613c72006-09-24 18:05:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
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, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine5bb9362006-09-27 00:31:22 +000015
admin7b613c72006-09-24 18:05:17 +000016// ------------------------------------------------------------------------
17
18/**
19 * MySQL Utility Class
20 *
21 * @category Database
22 * @author Rick Ellis
23 * @link http://www.codeigniter.com/user_guide/database/
24 */
admina5e812c2006-09-25 02:17:30 +000025class CI_DB_mysql_utility extends CI_DB_utility {
admin7b613c72006-09-24 18:05:17 +000026
admin6ca6f942006-09-25 02:51:08 +000027 /**
28 * Create database
29 *
admin6cec6a52006-09-25 06:56:49 +000030 * @access private
admin6ca6f942006-09-25 02:51:08 +000031 * @param string the database name
32 * @return bool
33 */
admin6cec6a52006-09-25 06:56:49 +000034 function _create_database($name)
admin6ca6f942006-09-25 02:51:08 +000035 {
admin6cec6a52006-09-25 06:56:49 +000036 return "CREATE DATABASE ".$name;
admin6ca6f942006-09-25 02:51:08 +000037 }
38
39 // --------------------------------------------------------------------
40
41 /**
42 * Drop database
43 *
admin83b05a82006-09-25 21:06:46 +000044 * @access private
admin6ca6f942006-09-25 02:51:08 +000045 * @param string the database name
46 * @return bool
47 */
admin83b05a82006-09-25 21:06:46 +000048 function _drop_database($name)
admin6ca6f942006-09-25 02:51:08 +000049 {
admin83b05a82006-09-25 21:06:46 +000050 return "DROP DATABASE ".$name;
admin6ca6f942006-09-25 02:51:08 +000051 }
52
53 // --------------------------------------------------------------------
admina5e812c2006-09-25 02:17:30 +000054
admin7b613c72006-09-24 18:05:17 +000055 /**
admin4ceac2d2006-09-25 06:40:16 +000056 * Drop Table
57 *
admin83b05a82006-09-25 21:06:46 +000058 * @access private
admin4ceac2d2006-09-25 06:40:16 +000059 * @return bool
60 */
admin83b05a82006-09-25 21:06:46 +000061 function _drop_table($table)
admin4ceac2d2006-09-25 06:40:16 +000062 {
admin83b05a82006-09-25 21:06:46 +000063 return "DROP TABLE IF EXISTS ".$this->db->_escape_table($name);
admin4ceac2d2006-09-25 06:40:16 +000064 }
65
66 // --------------------------------------------------------------------
67
68 /**
adminab4f61b2006-09-25 22:12:32 +000069 * Optimize table query
70 *
71 * Generates a platform-specific query so that a table can be optimized
72 *
73 * @access private
74 * @param string the table name
75 * @return object
76 */
77 function _optimize_table($table)
78 {
79 return "OPTIMIZE TABLE ".$this->db->_escape_table($table);
80 }
81
82 // --------------------------------------------------------------------
83
84 /**
85 * Repair table query
86 *
87 * Generates a platform-specific query so that a table can be repaired
88 *
89 * @access private
90 * @param string the table name
91 * @return object
92 */
93 function _repair_table($table)
94 {
95 return "REPAIR TABLE ".$this->db->_escape_table($table);
96 }
97
admin051402b2006-09-29 23:11:40 +000098 // --------------------------------------------------------------------
adminab4f61b2006-09-25 22:12:32 +000099
admin051402b2006-09-29 23:11:40 +0000100 /**
101 * MySQL Export
102 *
admin3ed8c512006-09-29 23:26:28 +0000103 * @access private
admin051402b2006-09-29 23:11:40 +0000104 * @param array Any preferences
admin3ed8c512006-09-29 23:26:28 +0000105 * @return mixed
admin051402b2006-09-29 23:11:40 +0000106 */
admin3dd978f2006-09-30 19:24:45 +0000107 function _backup($params = array())
admin051402b2006-09-29 23:11:40 +0000108 {
admin3dd978f2006-09-30 19:24:45 +0000109 if (count($params) == 0)
admin051402b2006-09-29 23:11:40 +0000110 {
admin3dd978f2006-09-30 19:24:45 +0000111 return FALSE;
admin051402b2006-09-29 23:11:40 +0000112 }
113
114 // Extract the prefs for simplicity
admin3dd978f2006-09-30 19:24:45 +0000115 extract($params);
admin051402b2006-09-29 23:11:40 +0000116
117 // Build the output
admin3dd978f2006-09-30 19:24:45 +0000118 $output = '';
119 foreach ((array)$tables as $table)
admin051402b2006-09-29 23:11:40 +0000120 {
121 // Is the table in the "ignore" list?
admin3dd978f2006-09-30 19:24:45 +0000122 if (in_array($table, (array)$ignore, TRUE))
admin051402b2006-09-29 23:11:40 +0000123 {
124 continue;
125 }
126
127 // Get the table schema
128 $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table);
129
130 // No result means the table name was invalid
131 if ($query === FALSE)
132 {
133 continue;
134 }
135
136 // Write out the table schema
admin3dd978f2006-09-30 19:24:45 +0000137 $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
admin051402b2006-09-29 23:11:40 +0000138
139 if ($add_drop == TRUE)
140 {
admin3dd978f2006-09-30 19:24:45 +0000141 $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline;
admin051402b2006-09-29 23:11:40 +0000142 }
143
144 $i = 0;
145 $result = $query->result_array();
146 foreach ($result[0] as $val)
147 {
148 if ($i++ % 2)
149 {
admin3dd978f2006-09-30 19:24:45 +0000150 $output .= $val.';'.$newline.$newline;
admin051402b2006-09-29 23:11:40 +0000151 }
152 }
153
admin3ed8c512006-09-29 23:26:28 +0000154 // If inserts are not needed we're done...
admin051402b2006-09-29 23:11:40 +0000155 if ($add_insert == FALSE)
156 {
157 continue;
158 }
admin3ed8c512006-09-29 23:26:28 +0000159
160 // Grab all the data from the current table
admin051402b2006-09-29 23:11:40 +0000161 $query = $this->db->query("SELECT * FROM $table");
162
163 if ($query->num_rows() == 0)
164 {
165 continue;
166 }
167
admin3ed8c512006-09-29 23:26:28 +0000168 // Fetch the field names and determine if the field is an
admin051402b2006-09-29 23:11:40 +0000169 // integer type. We use this info to decide whether to
170 // surround the data with quotes or not
171
172 $i = 0;
admin3ed8c512006-09-29 23:26:28 +0000173 $field_str = '';
admin051402b2006-09-29 23:11:40 +0000174 $is_int = array();
175 while ($field = mysql_fetch_field($query->result_id))
176 {
177 $is_int[$i] = (in_array(
admin3ed8c512006-09-29 23:26:28 +0000178 strtolower(mysql_field_type($query->result_id, $i)),
admin051402b2006-09-29 23:11:40 +0000179 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'timestamp'),
180 TRUE)
181 ) ? TRUE : FALSE;
182
183 // Create a string of field names
admin3ed8c512006-09-29 23:26:28 +0000184 $field_str .= $field->name.', ';
admin051402b2006-09-29 23:11:40 +0000185 $i++;
186 }
admin3ed8c512006-09-29 23:26:28 +0000187
188 // Trim off the end comma
189 $field_str = preg_replace( "/, $/" , "" , $field_str);
admin051402b2006-09-29 23:11:40 +0000190
191
admin3ed8c512006-09-29 23:26:28 +0000192 // Build the insert string
admin051402b2006-09-29 23:11:40 +0000193 foreach ($query->result_array() as $row)
194 {
admin3ed8c512006-09-29 23:26:28 +0000195 $val_str = '';
admin051402b2006-09-29 23:11:40 +0000196
197 $i = 0;
198 foreach ($row as $v)
199 {
admin3ed8c512006-09-29 23:26:28 +0000200 // Do a little formatting...
admin051402b2006-09-29 23:11:40 +0000201 $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v);
202 $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v);
203 $v = str_replace('\\', '\\\\', $v);
204 $v = str_replace('\'', '\\\'', $v);
205 $v = str_replace('\\\n', '\n', $v);
206 $v = str_replace('\\\r', '\r', $v);
207 $v = str_replace('\\\t', '\t', $v);
208
209 // Escape the data if it's not an integer type
admin3ed8c512006-09-29 23:26:28 +0000210 $val_str .= ($is_int[$i] == FALSE) ? $this->db->escape($v) : $v;
211 $val_str .= ', ';
admin051402b2006-09-29 23:11:40 +0000212
213 $i++;
214 }
215
admin3ed8c512006-09-29 23:26:28 +0000216 $val_str = preg_replace( "/, $/" , "" , $val_str);
admin3dd978f2006-09-30 19:24:45 +0000217
admin051402b2006-09-29 23:11:40 +0000218 // Build the INSERT string
admin3dd978f2006-09-30 19:24:45 +0000219 $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
admin051402b2006-09-29 23:11:40 +0000220
221 }
222
admin3dd978f2006-09-30 19:24:45 +0000223 $output .= $newline.$newline;
admin051402b2006-09-29 23:11:40 +0000224 }
225
admin3dd978f2006-09-30 19:24:45 +0000226 return $output;
admin051402b2006-09-29 23:11:40 +0000227 }
admina5e812c2006-09-25 02:17:30 +0000228
admin6ca6f942006-09-25 02:51:08 +0000229
admin7b613c72006-09-24 18:05:17 +0000230}
231
232?>