From 3ed8c51254a5b26d951fa675802fcf69adf9638e Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 29 Sep 2006 23:26:28 +0000 Subject: --- system/database/DB_driver.php | 9 +- system/database/DB_export.php | 132 -------------------- system/database/DB_utility.php | 121 +++++++++++++++++- system/database/drivers/mysql/mysql_utility.php | 48 ++++--- system/libraries/Controller.php | 12 +- system/libraries/Encrypt.php | 35 +++--- system/libraries/Loader.php | 14 --- user_guide/database/export.html | 158 ------------------------ user_guide/database/index.html | 1 - user_guide/database/utilities.html | 57 ++++++++- 10 files changed, 229 insertions(+), 358 deletions(-) delete mode 100644 system/database/DB_export.php delete mode 100644 user_guide/database/export.html 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 @@ -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)."".$newline; - } - $xml .= $tab."".$newline; - } - $xml .= "".$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)."".$newline; + } + $xml .= $tab."".$newline; + } + $xml .= "".$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; } diff --git a/system/libraries/Controller.php b/system/libraries/Controller.php index be66b19b7..51f455023 100644 --- a/system/libraries/Controller.php +++ b/system/libraries/Controller.php @@ -189,8 +189,6 @@ class Controller extends CI_Base { } $remap = array( - 'DB_export' => 'dbexport', - 'DB_utility' => 'dbutility', 'Unit_test' => 'unit' ); @@ -516,19 +514,15 @@ class Controller extends CI_Base { { if ( ! $this->_ci_is_loaded('db')) { - $this->_init_database(); + $this->_ci_init_database(); } if ($class == 'dbutil') { require_once(BASEPATH.'database/DB_utility'.EXT); require_once(BASEPATH.'database/drivers/'.$this->db->dbdriver.'/'.$this->db->dbdriver.'_utility'.EXT); - $this->init_class('CI_DB_'.$this->db->dbdriver.'_utility', 'dbutil'); - } - elseif ($class == 'dbexport') - { - require_once(BASEPATH.'database/DB_export'.EXT); - $this->init_class('CI_DB_export', 'dbexport'); + $class = 'CI_DB_'.$this->db->dbdriver.'_utility'; + $this->dbutil = new $class(); } } diff --git a/system/libraries/Encrypt.php b/system/libraries/Encrypt.php index abc769460..537b1ab20 100644 --- a/system/libraries/Encrypt.php +++ b/system/libraries/Encrypt.php @@ -27,6 +27,7 @@ * @link http://www.codeigniter.com/user_guide/libraries/encryption.html */ class CI_Encrypt { + var $encryption_key = ''; var $_hash_type = 'sha1'; var $_mcrypt_exists = FALSE; var $_mcrypt_cipher; @@ -43,7 +44,6 @@ class CI_Encrypt { $this->_mcrypt_exists = ( ! function_exists('mcrypt_encrypt')) ? FALSE : TRUE; log_message('debug', "Encrypt Class Initialized"); } - // END CI_Encrypt() // -------------------------------------------------------------------- @@ -61,6 +61,11 @@ class CI_Encrypt { { if ($key == '') { + if ($this->encryption_key != '') + { + return $this->encryption_key; + } + $obj =& get_instance(); $key = $obj->config->item('encryption_key'); @@ -72,7 +77,20 @@ class CI_Encrypt { return md5($key); } - // END get_key() + + // -------------------------------------------------------------------- + + /** + * Set the encryption key + * + * @access public + * @param string + * @return void + */ + function set_key($key = '') + { + $this->encryption_key = $key; + } // -------------------------------------------------------------------- @@ -103,7 +121,6 @@ class CI_Encrypt { } return base64_encode($enc); } - // END encode() // -------------------------------------------------------------------- @@ -134,7 +151,6 @@ class CI_Encrypt { return $this->_xor_decode($dec, $key); } - // END decode() // -------------------------------------------------------------------- @@ -167,7 +183,6 @@ class CI_Encrypt { return $this->_xor_merge($enc, $key); } - // END _xor_encode() // -------------------------------------------------------------------- @@ -194,7 +209,6 @@ class CI_Encrypt { return $dec; } - // END _xor_decode() // -------------------------------------------------------------------- @@ -219,7 +233,6 @@ class CI_Encrypt { return $str; } - // END _xor_merge() // -------------------------------------------------------------------- @@ -238,7 +251,6 @@ class CI_Encrypt { $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND); return mcrypt_encrypt($this->_mcrypt_cipher, $key, $data, $this->_mcrypt_mode, $init_vect); } - // END mcrypt_encode() // -------------------------------------------------------------------- @@ -257,7 +269,6 @@ class CI_Encrypt { $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND); return rtrim(mcrypt_decrypt($this->_mcrypt_cipher, $key, $data, $this->_mcrypt_mode, $init_vect), "\0"); } - // END mcrypt_decode() // -------------------------------------------------------------------- @@ -272,7 +283,6 @@ class CI_Encrypt { { $this->_mcrypt_cipher = $cypher; } - // END set_cypher() // -------------------------------------------------------------------- @@ -287,7 +297,6 @@ class CI_Encrypt { { $this->_mcrypt_mode = $mode; } - // END set_mode() // -------------------------------------------------------------------- @@ -309,7 +318,6 @@ class CI_Encrypt { $this->_mcrypt_mode = MCRYPT_MODE_ECB; } } - // END _get_mcrypt() // -------------------------------------------------------------------- @@ -324,7 +332,6 @@ class CI_Encrypt { { $this->_hash_type = ($type != 'sha1' AND $type != 'md5') ? 'sha1' : $type; } - // END set_hash() // -------------------------------------------------------------------- @@ -339,7 +346,6 @@ class CI_Encrypt { { return ($this->_hash_type == 'sha1') ? $this->sha1($str) : md5($str); } - // END hash() // -------------------------------------------------------------------- @@ -370,7 +376,6 @@ class CI_Encrypt { return sha1($str); } } - // END sha1() } diff --git a/system/libraries/Loader.php b/system/libraries/Loader.php index fff9e78d3..2534e6965 100644 --- a/system/libraries/Loader.php +++ b/system/libraries/Loader.php @@ -135,20 +135,6 @@ class CI_Loader { $obj =& get_instance(); $obj->_ci_init_dbextra('dbutil'); } - - // -------------------------------------------------------------------- - - /** - * Database Export Loader - * - * @access public - * @return object - */ - function dbexport() - { - $obj =& get_instance(); - $obj->_ci_init_dbextra('dbexport'); - } // -------------------------------------------------------------------- diff --git a/user_guide/database/export.html b/user_guide/database/export.html deleted file mode 100644 index 6932fbcb8..000000000 --- a/user_guide/database/export.html +++ /dev/null @@ -1,158 +0,0 @@ - - - - -Code Igniter User Guide - - - - - - - - - - - - - - - - - - - - - - -
- - - - - -

Code Igniter User Guide Version 1.5.0

-
- - - - - - - - - -
- - - -
- - - -
- -

Database Export Class

- -

The Database Utilities Class contains functions that help you export your data.

- -

Important:  This class must be initialized independently since it is a separate class from the main Database class. -More info below...

- - -

Initializing the Export Class

- -

To initialize this class please use the following code:

- -$this->load->dbexport() - -

You can also autoload this class from within your config/autoload.php file by specifying dbexport in the $autoload['libraries'] array.

- -

Once initialized you will access the functions using the $this->dbexport object:

- -$this->dbexport->some_function() - - - -

$this->dbexport->cvs_from_result($db_result)

- -

Permits you to generate a CVS file from a query result. The first parameter of the function must contain the result object from your query. -Example:

- - -$this->load->dbexport();
-
-$query = $this->db->query("SELECT * FROM mytable");
-
-echo $this->dbexport->cvs_from_result($query); -
- -

The second and third parameters allows you to -set the delimiter and newline character. By default tabs are used as the delimiter and "\n" is used as a new line. Example: - - -$delimiter = ",";
-$newline = "\r\n";
-
-echo $this->dbexport->cvs_from_result($query, $delimiter, $newline); -
- -

Important:  This function will NOT write the CVS file for you. It simply creates the CVS layout. -If you need to write the file use the File Helper.

- - - -

$this->dbexport->xml_from_result($db_result)

- -

Permits you to generate an XML file from a query result. The first parameter expects a query result object, the second -may contain an optional array of config parameters. Example:

- - -$this->load->dbexport();
-
-$query = $this->db->query("SELECT * FROM mytable");
-
-$config = array (
-                  'root'    => 'root',
-                  'element' => 'element',
-                  'newline' => "\n",
-                  ';tab'    => "\t"
-                );
-
-echo $this->dbexport->cvs_from_result($query, $config); -
- -

Important:  This function will NOT write the CVS file for you. It simply creates the CVS layout. -If you need to write the file use the File Helper.

- - - - - -
- - - - - - - \ No newline at end of file diff --git a/user_guide/database/index.html b/user_guide/database/index.html index 61864221e..78f90478f 100644 --- a/user_guide/database/index.html +++ b/user_guide/database/index.html @@ -79,7 +79,6 @@ structures and Active Record patterns. The database functions offer clear, simpl
  • Field MetaData
  • Custom Function Calls
  • Database Utilities Class
  • -
  • Database Export Class
  • Database Caching Class
  • diff --git a/user_guide/database/utilities.html b/user_guide/database/utilities.html index e23f1f575..b064c7983 100644 --- a/user_guide/database/utilities.html +++ b/user_guide/database/utilities.html @@ -193,6 +193,61 @@ if ($result !== FALSE)
    +

    $this->dbutil->cvs_from_result($db_result)

    + +

    Permits you to generate a CVS file from a query result. The first parameter of the function must contain the result object from your query. +Example:

    + + +$this->load->dbutil();
    +
    +$query = $this->db->query("SELECT * FROM mytable");
    +
    +echo $this->dbutil->cvs_from_result($query); +
    + +

    The second and third parameters allows you to +set the delimiter and newline character. By default tabs are used as the delimiter and "\n" is used as a new line. Example: + + +$delimiter = ",";
    +$newline = "\r\n";
    +
    +echo $this->dbutil->cvs_from_result($query, $delimiter, $newline); +
    + +

    Important:  This function will NOT write the CVS file for you. It simply creates the CVS layout. +If you need to write the file use the File Helper.

    + + + +

    $this->dbutil->xml_from_result($db_result)

    + +

    Permits you to generate an XML file from a query result. The first parameter expects a query result object, the second +may contain an optional array of config parameters. Example:

    + + +$this->load->dbutil();
    +
    +$query = $this->db->query("SELECT * FROM mytable");
    +
    +$config = array (
    +                  'root'    => 'root',
    +                  'element' => 'element',
    +                  'newline' => "\n",
    +                  ';tab'    => "\t"
    +                );
    +
    +echo $this->dbutil->cvs_from_result($query, $config); +
    + +

    Important:  This function will NOT write the CVS file for you. It simply creates the CVS layout. +If you need to write the file use the File Helper.

    + + + + + @@ -204,7 +259,7 @@ Previous Topic:  Custom Function CallsTop of Page   ·   User Guide Home   ·   -Next Topic:  Database Export Class +Next Topic:  Database Caching Class

    Code Igniter  ·  Copyright © 2006  ·  pMachine, Inc.

    -- cgit v1.2.3-24-g4f1b