blob: bc20d255643a35d7f957cf67d8c7ea322dadbd4a [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev2caf2892012-01-27 00:12:03 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev2caf2892012-01-27 00:12:03 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * MySQL Utility Class
31 *
32 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050033 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * @link http://codeigniter.com/user_guide/database/
35 */
36class CI_DB_mysql_utility extends CI_DB_utility {
37
Andrey Andreevb457a402012-04-09 16:11:56 +030038 protected $_list_databases = 'SHOW DATABASES';
39 protected $_optimize_table = 'OPTIMIZE TABLE %s';
40 protected $_repair_table = 'REPAIR TABLE %s';
Derek Allard2067d1a2008-11-13 22:59:24 +000041
Derek Allard2067d1a2008-11-13 22:59:24 +000042 /**
43 * MySQL Export
44 *
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * @param array Preferences
46 * @return mixed
47 */
Andrey Andreevb457a402012-04-09 16:11:56 +030048 protected function _backup($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000049 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020050 if (count($params) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000051 {
52 return FALSE;
53 }
54
55 // Extract the prefs for simplicity
56 extract($params);
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 // Build the output
59 $output = '';
Andrey Andreev2caf2892012-01-27 00:12:03 +020060 foreach ( (array) $tables as $table)
Derek Allard2067d1a2008-11-13 22:59:24 +000061 {
62 // Is the table in the "ignore" list?
Andrey Andreev2caf2892012-01-27 00:12:03 +020063 if (in_array($table, (array) $ignore, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +000064 {
65 continue;
66 }
67
68 // Get the table schema
Andrey Andreev9637b402012-06-08 15:39:24 +030069 $query = $this->db->query('SHOW CREATE TABLE '.$this->db->escape_identifiers($this->db->database.'.'.$table));
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 // No result means the table name was invalid
72 if ($query === FALSE)
73 {
74 continue;
75 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // Write out the table schema
78 $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
79
Alex Bilbie48a2baf2012-06-02 11:09:54 +010080 if ($add_drop === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +020081 {
Taufan Aditya24dd2f92012-02-17 17:08:31 +070082 $output .= 'DROP TABLE IF EXISTS '.$this->db->protect_identifiers($table).';'.$newline.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +000083 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 $i = 0;
86 $result = $query->result_array();
87 foreach ($result[0] as $val)
88 {
89 if ($i++ % 2)
Barry Mienydd671972010-10-04 16:33:58 +020090 {
Derek Allard2067d1a2008-11-13 22:59:24 +000091 $output .= $val.';'.$newline.$newline;
92 }
93 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 // If inserts are not needed we're done...
Alex Bilbie48a2baf2012-06-02 11:09:54 +010096 if ($add_insert === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
98 continue;
99 }
100
101 // Grab all the data from the current table
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200102 $query = $this->db->query('SELECT * FROM '.$this->db->protect_identifiers($table));
Barry Mienydd671972010-10-04 16:33:58 +0200103
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100104 if ($query->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
106 continue;
107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // Fetch the field names and determine if the field is an
Andrey Andreev2caf2892012-01-27 00:12:03 +0200110 // integer type. We use this info to decide whether to
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // surround the data with quotes or not
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 $i = 0;
114 $field_str = '';
115 $is_int = array();
116 while ($field = mysql_fetch_field($query->result_id))
117 {
118 // Most versions of MySQL store timestamp as a string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200119 $is_int[$i] = in_array(strtolower(mysql_field_type($query->result_id, $i)),
120 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'),
121 TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // Create a string of field names
Andrey Andreev9637b402012-06-08 15:39:24 +0300124 $field_str .= $this->db->escape_identifiers($field->name).', ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 $i++;
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 // Trim off the end comma
Andrey Andreev2caf2892012-01-27 00:12:03 +0200129 $field_str = preg_replace('/, $/' , '', $field_str);
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 // Build the insert string
132 foreach ($query->result_array() as $row)
133 {
134 $val_str = '';
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 $i = 0;
137 foreach ($row as $v)
138 {
139 // Is the value NULL?
140 if ($v === NULL)
141 {
142 $val_str .= 'NULL';
143 }
144 else
145 {
146 // Escape the data if it's not an integer
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100147 $val_str .= ($is_int[$i] === FALSE) ? $this->db->escape($v) : $v;
Barry Mienydd671972010-10-04 16:33:58 +0200148 }
149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // Append a comma
151 $val_str .= ', ';
152 $i++;
153 }
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 // Remove the comma at the end of the string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200156 $val_str = preg_replace('/, $/' , '', $val_str);
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 // Build the INSERT string
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200159 $output .= 'INSERT INTO '.$this->db->protect_identifiers($table).' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Andrey Andreevc51816d2012-02-14 16:55:42 +0200162 $output .= $newline.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164
165 return $output;
166 }
Andrey Andreev93cac5c2012-02-14 14:45:02 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168}
169
170/* End of file mysql_utility.php */
Timothy Warren9cc5c602012-03-19 18:36:37 -0400171/* Location: ./system/database/drivers/mysql/mysql_utility.php */