mysql_utility.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * MySQL Utility Class
00020  *
00021  * @category    Database
00022  * @author              ExpressionEngine Dev Team
00023  * @link                http://codeigniter.com/user_guide/database/
00024  */
00025 class CI_DB_mysql_utility extends CI_DB_utility {
00026 
00027         /**
00028          * List databases
00029          *
00030          * @access      private
00031          * @return      bool
00032          */
00033         function _list_databases()
00034         {
00035                 return "SHOW DATABASES";
00036         }
00037 
00038         // --------------------------------------------------------------------
00039 
00040         /**
00041          * Optimize table query
00042          *
00043          * Generates a platform-specific query so that a table can be optimized
00044          *
00045          * @access      private
00046          * @param       string  the table name
00047          * @return      object
00048          */
00049         function _optimize_table($table)
00050         {
00051                 return "OPTIMIZE TABLE ".$this->db->_escape_identifiers($table);
00052         }
00053 
00054         // --------------------------------------------------------------------
00055 
00056         /**
00057          * Repair table query
00058          *
00059          * Generates a platform-specific query so that a table can be repaired
00060          *
00061          * @access      private
00062          * @param       string  the table name
00063          * @return      object
00064          */
00065         function _repair_table($table)
00066         {
00067                 return "REPAIR TABLE ".$this->db->_escape_identifiers($table);
00068         }
00069 
00070         // --------------------------------------------------------------------
00071         /**
00072          * MySQL Export
00073          *
00074          * @access      private
00075          * @param       array   Preferences
00076          * @return      mixed
00077          */
00078         function _backup($params = array())
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                                                 // Escape the data if it's not an integer
00180                                                 if ($is_int[$i] == FALSE)
00181                                                 {
00182                                                         $val_str .= $this->db->escape($v);
00183                                                 }
00184                                                 else
00185                                                 {
00186                                                         $val_str .= $v;
00187                                                 }                                       
00188                                         }                                       
00189                                         
00190                                         // Append a comma
00191                                         $val_str .= ', ';
00192                                         $i++;
00193                                 }
00194                                 
00195                                 // Remove the comma at the end of the string
00196                                 $val_str = preg_replace( "/, $/" , "" , $val_str);
00197                                                                 
00198                                 // Build the INSERT string
00199                                 $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
00200                         }
00201                         
00202                         $output .= $newline.$newline;
00203                 }
00204 
00205                 return $output;
00206         }
00207 
00208         /**
00209          *
00210          * The functions below have been deprecated as of 1.6, and are only here for backwards
00211          * compatibility.  They now reside in dbforge().  The use of dbutils for database manipulation
00212          * is STRONGLY discouraged in favour if using dbforge.
00213          *
00214          */
00215 
00216         /**
00217          * Create database
00218          *
00219          * @access      private
00220          * @param       string  the database name
00221          * @return      bool
00222          */
00223         function _create_database($name)
00224         {
00225                 return "CREATE DATABASE ".$name;
00226         }
00227 
00228         // --------------------------------------------------------------------
00229 
00230         /**
00231          * Drop database
00232          *
00233          * @access      private
00234          * @param       string  the database name
00235          * @return      bool
00236          */
00237         function _drop_database($name)
00238         {
00239                 return "DROP DATABASE ".$name;
00240         }
00241 
00242 }
00243 
00244 /* End of file mysql_utility.php */
00245 /* Location: ./system/database/drivers/mysql/mysql_utility.php */