mysql_utility.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 class CI_DB_mysql_utility extends CI_DB_utility {
00026
00027
00028
00029
00030
00031
00032
00033 function _list_databases()
00034 {
00035 return "SHOW DATABASES";
00036 }
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 function _optimize_table($table)
00050 {
00051 return "OPTIMIZE TABLE ".$this->db->_escape_table($table);
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 function _repair_table($table)
00066 {
00067 return "REPAIR TABLE ".$this->db->_escape_table($table);
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 function _backup($params = array())
00079 {
00080 if (count($params) == 0)
00081 {
00082 return FALSE;
00083 }
00084
00085
00086 extract($params);
00087
00088
00089 $output = '';
00090 foreach ((array)$tables as $table)
00091 {
00092
00093 if (in_array($table, (array)$ignore, TRUE))
00094 {
00095 continue;
00096 }
00097
00098
00099 $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table);
00100
00101
00102 if ($query === FALSE)
00103 {
00104 continue;
00105 }
00106
00107
00108 $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
00109
00110 if ($add_drop == TRUE)
00111 {
00112 $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline;
00113 }
00114
00115 $i = 0;
00116 $result = $query->result_array();
00117 foreach ($result[0] as $val)
00118 {
00119 if ($i++ % 2)
00120 {
00121 $output .= $val.';'.$newline.$newline;
00122 }
00123 }
00124
00125
00126 if ($add_insert == FALSE)
00127 {
00128 continue;
00129 }
00130
00131
00132 $query = $this->db->query("SELECT * FROM $table");
00133
00134 if ($query->num_rows() == 0)
00135 {
00136 continue;
00137 }
00138
00139
00140
00141
00142
00143 $i = 0;
00144 $field_str = '';
00145 $is_int = array();
00146 while ($field = mysql_fetch_field($query->result_id))
00147 {
00148
00149 $is_int[$i] = (in_array(
00150 strtolower(mysql_field_type($query->result_id, $i)),
00151 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'),
00152 TRUE)
00153 ) ? TRUE : FALSE;
00154
00155
00156 $field_str .= $field->name.', ';
00157 $i++;
00158 }
00159
00160
00161 $field_str = preg_replace( "/, $/" , "" , $field_str);
00162
00163
00164
00165 foreach ($query->result_array() as $row)
00166 {
00167 $val_str = '';
00168
00169 $i = 0;
00170 foreach ($row as $v)
00171 {
00172
00173 if ($v === NULL)
00174 {
00175 $val_str .= 'NULL';
00176 }
00177 else
00178 {
00179
00180 $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v);
00181 $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v);
00182 $v = str_replace('\\', '\\\\', $v);
00183 $v = str_replace('\'', '\\\'', $v);
00184 $v = str_replace('\\\n', '\n', $v);
00185 $v = str_replace('\\\r', '\r', $v);
00186 $v = str_replace('\\\t', '\t', $v);
00187
00188
00189 if ($is_int[$i] == FALSE)
00190 {
00191 $val_str .= $this->db->escape($v);
00192 }
00193 else
00194 {
00195 $val_str .= $v;
00196 }
00197 }
00198
00199
00200 $val_str .= ', ';
00201 $i++;
00202 }
00203
00204
00205 $val_str = preg_replace( "/, $/" , "" , $val_str);
00206
00207
00208 $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
00209 }
00210
00211 $output .= $newline.$newline;
00212 }
00213
00214 return $output;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 function _create_database($name)
00233 {
00234 return "CREATE DATABASE ".$name;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 function _drop_database($name)
00247 {
00248 return "DROP DATABASE ".$name;
00249 }
00250
00251 }
00252
00253
00254