CI_DB_mysql_utility Class Reference

Inheritance diagram for CI_DB_mysql_utility:
Collaboration diagram for CI_DB_mysql_utility:

List of all members.


Public Member Functions

 _list_databases ()
 List databases.
 _optimize_table ($table)
 Optimize table query.
 _repair_table ($table)
 Repair table query.
 _backup ($params=array())
 MySQL Export.
 _create_database ($name)
 The functions below have been deprecated as of 1.6, and are only here for backwards compatibility.
 _drop_database ($name)
 Drop database.

Detailed Description

Definition at line 25 of file mysql_utility.php.


Member Function Documentation

CI_DB_mysql_utility::_backup ( params = array()  ) 

MySQL Export.

private

Parameters:
array Preferences
Returns:
mixed

Definition at line 78 of file mysql_utility.php.

00079         {
00080                 if (count($params) == 0)
00081                 {
00082                         return FALSE;
00083                 }
00084 
00085                 // Extract the prefs for simplicity
00086                 extract($params);
00087         
00088                 // Build the output
00089                 $output = '';
00090                 foreach ((array)$tables as $table)
00091                 {
00092                         // Is the table in the "ignore" list?
00093                         if (in_array($table, (array)$ignore, TRUE))
00094                         {
00095                                 continue;
00096                         }
00097 
00098                         // Get the table schema
00099                         $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table);
00100                         
00101                         // No result means the table name was invalid
00102                         if ($query === FALSE)
00103                         {
00104                                 continue;
00105                         }
00106                         
00107                         // Write out the table schema
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                         // If inserts are not needed we're done...
00126                         if ($add_insert == FALSE)
00127                         {
00128                                 continue;
00129                         }
00130 
00131                         // Grab all the data from the current table
00132                         $query = $this->db->query("SELECT * FROM $table");
00133                         
00134                         if ($query->num_rows() == 0)
00135                         {
00136                                 continue;
00137                         }
00138                 
00139                         // Fetch the field names and determine if the field is an
00140                         // integer type.  We use this info to decide whether to
00141                         // surround the data with quotes or not
00142                         
00143                         $i = 0;
00144                         $field_str = '';
00145                         $is_int = array();
00146                         while ($field = mysql_fetch_field($query->result_id))
00147                         {
00148                                 // Most versions of MySQL store timestamp as a string
00149                                 $is_int[$i] = (in_array(
00150                                                                                 strtolower(mysql_field_type($query->result_id, $i)),
00151                                                                                 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'), 
00152                                                                                 TRUE)
00153                                                                                 ) ? TRUE : FALSE;
00154                                                                                 
00155                                 // Create a string of field names
00156                                 $field_str .= $field->name.', ';
00157                                 $i++;
00158                         }
00159                         
00160                         // Trim off the end comma
00161                         $field_str = preg_replace( "/, $/" , "" , $field_str);
00162                         
00163                         
00164                         // Build the insert string
00165                         foreach ($query->result_array() as $row)
00166                         {
00167                                 $val_str = '';
00168                         
00169                                 $i = 0;
00170                                 foreach ($row as $v)
00171                                 {
00172                                         // Is the value NULL?
00173                                         if ($v === NULL)
00174                                         {
00175                                                 $val_str .= 'NULL';
00176                                         }
00177                                         else
00178                                         {
00179                                                 // Do a little formatting...
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                                                 // Escape the data if it's not an integer
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                                         // Append a comma
00200                                         $val_str .= ', ';
00201                                         $i++;
00202                                 }
00203                                 
00204                                 // Remove the comma at the end of the string
00205                                 $val_str = preg_replace( "/, $/" , "" , $val_str);
00206                                                                 
00207                                 // Build the INSERT string
00208                                 $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
00209                         }
00210                         
00211                         $output .= $newline.$newline;
00212                 }
00213 
00214                 return $output;
00215         }

CI_DB_mysql_utility::_create_database ( name  ) 

The functions below have been deprecated as of 1.6, and are only here for backwards compatibility.

They now reside in dbforge(). The use of dbutils for database manipulation is STRONGLY discouraged in favour if using dbforge. Create database private

Parameters:
string the database name
Returns:
bool

Definition at line 232 of file mysql_utility.php.

00233         {
00234                 return "CREATE DATABASE ".$name;
00235         }

CI_DB_mysql_utility::_drop_database ( name  ) 

Drop database.

private

Parameters:
string the database name
Returns:
bool

Definition at line 246 of file mysql_utility.php.

00247         {
00248                 return "DROP DATABASE ".$name;
00249         }

CI_DB_mysql_utility::_list_databases (  ) 

List databases.

private

Returns:
bool

Definition at line 33 of file mysql_utility.php.

00034         {
00035                 return "SHOW DATABASES";
00036         }

CI_DB_mysql_utility::_optimize_table ( table  ) 

Optimize table query.

Generates a platform-specific query so that a table can be optimized

private

Parameters:
string the table name
Returns:
object

Definition at line 49 of file mysql_utility.php.

00050         {
00051                 return "OPTIMIZE TABLE ".$this->db->_escape_table($table);
00052         }

CI_DB_mysql_utility::_repair_table ( table  ) 

Repair table query.

Generates a platform-specific query so that a table can be repaired

private

Parameters:
string the table name
Returns:
object

Definition at line 65 of file mysql_utility.php.

00066         {
00067                 return "REPAIR TABLE ".$this->db->_escape_table($table);
00068         }


The documentation for this class was generated from the following file: