diff options
Diffstat (limited to 'system/database')
-rw-r--r-- | system/database/DB_driver.php | 9 | ||||
-rw-r--r-- | system/database/DB_export.php | 132 | ||||
-rw-r--r-- | system/database/DB_utility.php | 121 | ||||
-rw-r--r-- | system/database/drivers/mysql/mysql_utility.php | 48 |
4 files changed, 150 insertions, 160 deletions
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 94db84bbf..275d51c53 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -45,6 +45,7 @@ class CI_DB_driver { var $query_count = 0; var $bind_marker = '?'; var $queries = array(); + var $cache = array(); var $trans_enabled = TRUE; var $_trans_depth = 0; var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur @@ -563,6 +564,12 @@ class CI_DB_driver { */ function field_names($table = '') { + // Is there a cached result? + if (isset($this->cache['field_names'][$table])) + { + return $this->cache['field_names'][$table]; + } + if ($table == '') { if ($this->db_debug) @@ -596,7 +603,7 @@ class CI_DB_driver { } } - return $retval; + return $this->cache['field_names'][$table] =& $retval; } // -------------------------------------------------------------------- diff --git a/system/database/DB_export.php b/system/database/DB_export.php deleted file mode 100644 index 1704f0dfd..000000000 --- a/system/database/DB_export.php +++ /dev/null @@ -1,132 +0,0 @@ -<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); -/** - * Code Igniter - * - * An open source application development framework for PHP 4.3.2 or newer - * - * @package CodeIgniter - * @author Rick Ellis - * @copyright Copyright (c) 2006, pMachine, Inc. - * @license http://www.codeignitor.com/user_guide/license.html - * @link http://www.codeigniter.com - * @since Version 1.0 - * @filesource - */ - -// ------------------------------------------------------------------------ - -/** - * DB Exporting Class - * - * @category Database - * @author Rick Ellis - * @link http://www.codeigniter.com/user_guide/database/ - */ -class CI_DB_export { - - - /** - * Constructor. Simply calls the log function - */ - function CI_DB_export() - { - log_message('debug', "Database Export Class Initialized"); - } - - // -------------------------------------------------------------------- - - /** - * Generate CVS from a query result object - * - * @access public - * @param object The query result object - * @param string The delimiter - tab by default - * @param string The newline character - \n by default - * @return string - */ - function cvs_from_result($query, $delim = "\t", $newline = "\n") - { - if ( ! is_object($query) OR ! method_exists($query, 'field_names')) - { - show_error('You must submit a valid result object'); - } - - $out = ''; - - // First generate the headings from the table column names - foreach ($query->field_names() as $name) - { - $out .= $name.$delim; - } - - $out = rtrim($out); - $out .= $newline; - - // Next blast through the result array and build out the rows - foreach ($query->result_array() as $row) - { - foreach ($row as $item) - { - $out .= $item.$delim; - } - $out = rtrim($out); - $out .= $newline; - } - - return $out; - } - - // -------------------------------------------------------------------- - - - /** - * Generate XML data from a query result object - * - * @access public - * @param object The query result object - * @param array Any preferences - * @return string - */ - function xml_from_result($query, $params = array()) - { - if ( ! is_object($query) OR ! method_exists($query, 'field_names')) - { - show_error('You must submit a valid result object'); - } - - // Set our default values - foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val) - { - if ( ! isset($params[$key])) - { - $params[$key] = $val; - } - } - - // Create variables for convenience - extract($params); - - // Load the xml helper - $obj =& get_instance(); - $obj->load->helper('xml'); - - // Generate the result - $xml = "<{$root}/>".$newline; - foreach ($query->result_array() as $row) - { - $xml .= $tab."<{$element}/>".$newline; - - foreach ($row as $key => $val) - { - $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline; - } - $xml .= $tab."</{$element}>".$newline; - } - $xml .= "</$root>".$newline; - - return $xml; - } - -} - -?>
\ No newline at end of file diff --git a/system/database/DB_utility.php b/system/database/DB_utility.php index 764f10cb7..ff9407ebd 100644 --- a/system/database/DB_utility.php +++ b/system/database/DB_utility.php @@ -25,6 +25,7 @@ class CI_DB_utility { var $db; + var $cache = array(); /** * Constructor @@ -93,6 +94,12 @@ class CI_DB_utility { */ function list_databases() { + // Is there a cached result? + if (isset($this->cache['db_names'])) + { + return $this->cache['db_names']; + } + $query = $this->db->query($this->_list_database()); $dbs = array(); if ($query->num_rows() > 0) @@ -103,7 +110,7 @@ class CI_DB_utility { } } - return $dbs; + return $this->cache['db_names'] =& $dbs; } // -------------------------------------------------------------------- @@ -116,6 +123,12 @@ class CI_DB_utility { */ function list_tables() { + // Is there a cached result? + if (isset($this->cache['table_names'])) + { + return $this->cache['table_names']; + } + if (FALSE === ($sql = $this->_list_tables())) { if ($this->db->db_debug) @@ -143,7 +156,7 @@ class CI_DB_utility { } } - return $retval; + return $this->cache['table_names'] =& $retval; } // -------------------------------------------------------------------- @@ -258,7 +271,111 @@ class CI_DB_utility { return $this->db->query($sql); } + // -------------------------------------------------------------------- + + /** + * Generate CVS from a query result object + * + * @access public + * @param object The query result object + * @param string The delimiter - tab by default + * @param string The newline character - \n by default + * @return string + */ + function cvs_from_result($query, $delim = "\t", $newline = "\n") + { + if ( ! is_object($query) OR ! method_exists($query, 'field_names')) + { + show_error('You must submit a valid result object'); + } + + $out = ''; + + // First generate the headings from the table column names + foreach ($query->field_names() as $name) + { + $out .= $name.$delim; + } + + $out = rtrim($out); + $out .= $newline; + + // Next blast through the result array and build out the rows + foreach ($query->result_array() as $row) + { + foreach ($row as $item) + { + $out .= $item.$delim; + } + $out = rtrim($out); + $out .= $newline; + } + + return $out; + } + + // -------------------------------------------------------------------- + + /** + * Generate XML data from a query result object + * + * @access public + * @param object The query result object + * @param array Any preferences + * @return string + */ + function xml_from_result($query, $params = array()) + { + if ( ! is_object($query) OR ! method_exists($query, 'field_names')) + { + show_error('You must submit a valid result object'); + } + + // Set our default values + foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val) + { + if ( ! isset($params[$key])) + { + $params[$key] = $val; + } + } + + // Create variables for convenience + extract($params); + + // Load the xml helper + $obj =& get_instance(); + $obj->load->helper('xml'); + + // Generate the result + $xml = "<{$root}/>".$newline; + foreach ($query->result_array() as $row) + { + $xml .= $tab."<{$element}/>".$newline; + + foreach ($row as $key => $val) + { + $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline; + } + $xml .= $tab."</{$element}>".$newline; + } + $xml .= "</$root>".$newline; + + return $xml; + } + + // -------------------------------------------------------------------- + /** + * Database Backup + * + * @access public + * @return void + */ + function export() + { + // The individual driver overloads this method + } } diff --git a/system/database/drivers/mysql/mysql_utility.php b/system/database/drivers/mysql/mysql_utility.php index 65fb87f92..f43299382 100644 --- a/system/database/drivers/mysql/mysql_utility.php +++ b/system/database/drivers/mysql/mysql_utility.php @@ -128,19 +128,18 @@ class CI_DB_mysql_utility extends CI_DB_utility { /** * MySQL Export * - * @access public - * @param object The query result object + * @access private * @param array Any preferences - * @return string + * @return mixed */ - function export($params = array()) + function _export($params = array()) { // Set up our default preferences $prefs = array( 'tables' => array(), 'ignore' => array(), 'format' => 'gzip', - 'download' => TRUE, + 'action' => 'download', // download, archive, echo, return 'filename' => date('Y-m-d-H:i', time()), 'filepath' => '', 'add_drop' => TRUE, @@ -169,8 +168,6 @@ class CI_DB_mysql_utility extends CI_DB_utility { $tables = $this->list_tables(); } - - // Start buffering the output ob_start(); @@ -193,7 +190,6 @@ class CI_DB_mysql_utility extends CI_DB_utility { } // Write out the table schema - echo $newline.$newline.'#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline; if ($add_drop == TRUE) @@ -211,13 +207,13 @@ class CI_DB_mysql_utility extends CI_DB_utility { } } - // Build the insert statements - + // If inserts are not needed we're done... if ($add_insert == FALSE) { continue; } - + + // Grab all the data from the current table $query = $this->db->query("SELECT * FROM $table"); if ($query->num_rows() == 0) @@ -225,37 +221,39 @@ class CI_DB_mysql_utility extends CI_DB_utility { continue; } - // Grab the field names and determine if the field is an + // Fetch the field names and determine if the field is an // integer type. We use this info to decide whether to // surround the data with quotes or not $i = 0; - $fields = ''; + $field_str = ''; $is_int = array(); while ($field = mysql_fetch_field($query->result_id)) { $is_int[$i] = (in_array( - mysql_field_type($query->result_id, $i), + strtolower(mysql_field_type($query->result_id, $i)), array('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'timestamp'), TRUE) ) ? TRUE : FALSE; // Create a string of field names - $fields .= $field->name.', '; + $field_str .= $field->name.', '; $i++; } - - $fields = preg_replace( "/, $/" , "" , $fields); + + // Trim off the end comma + $field_str = preg_replace( "/, $/" , "" , $field_str); - // Build the inserts + // Build the insert string foreach ($query->result_array() as $row) { - $values = ''; + $val_str = ''; $i = 0; foreach ($row as $v) { + // Do a little formatting... $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v); $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v); $v = str_replace('\\', '\\\\', $v); @@ -265,21 +263,21 @@ class CI_DB_mysql_utility extends CI_DB_utility { $v = str_replace('\\\t', '\t', $v); // Escape the data if it's not an integer type - $values .= ($is_int[$i] == FALSE) ? $this->db->escape($v) : $v; - $values .= ', '; + $val_str .= ($is_int[$i] == FALSE) ? $this->db->escape($v) : $v; + $val_str .= ', '; $i++; } - $values = preg_replace( "/, $/" , "" , $values); + $val_str = preg_replace( "/, $/" , "" , $val_str); - if ($download == FALSE) + if ($action == 'echo') { - $values = htmlspecialchars($values); + $val_str = htmlspecialchars($val_str); } // Build the INSERT string - echo 'INSERT INTO '.$table.' ('.$fields.') VALUES ('.$values.');'.$newline; + echo 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline; } |