Skip to content

Commit

Permalink
Change table engine for MySQL database, issue #41
Browse files Browse the repository at this point in the history
Small functions to allow developers choose table engines. Works with
MySQL and MySQLi drivers (other ignore it).
  • Loading branch information
MunGell committed Jan 27, 2012
1 parent ed6485b commit 67151d0
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 9 deletions.
68 changes: 66 additions & 2 deletions system/database/DB_forge.php
Expand Up @@ -40,6 +40,7 @@ class CI_DB_forge {
public $keys = array(); public $keys = array();
public $primary_keys = array(); public $primary_keys = array();
public $db_char_set = ''; public $db_char_set = '';
public $engine = '';


public function __construct() public function __construct()
{ {
Expand Down Expand Up @@ -164,6 +165,26 @@ public function add_field($field = '')


// -------------------------------------------------------------------- // --------------------------------------------------------------------


/**
* Add engine
*
* @param string the table engine
* @return object
*/
public function add_engine($engine = '')
{
if ($engine == '')
{
show_error('An engine name is required for that operation.');
}

$this->engine = $engine;

return $this;
}

// --------------------------------------------------------------------

/** /**
* Create Table * Create Table
* *
Expand All @@ -181,8 +202,22 @@ public function create_table($table = '', $if_not_exists = FALSE)
{ {
show_error('Field information is required.'); show_error('Field information is required.');
} }


$sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists); if ($this->engine == '')
{
$sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
}
else
{
if (method_exists($this, '_set_table_engine'))
{
$sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists, $this->engine);
}
else
{
show_error('Your database does not support different engines.');
}
}
$this->_reset(); $this->_reset();
return is_bool($sql) ? $sql : $this->db->query($sql); return is_bool($sql) ? $sql : $this->db->query($sql);
} }
Expand Down Expand Up @@ -219,6 +254,34 @@ public function rename_table($table_name, $new_table_name)


return $this->db->query($this->_rename_table($this->db->dbprefix.$table_name, $this->db->dbprefix.$new_table_name)); return $this->db->query($this->_rename_table($this->db->dbprefix.$table_name, $this->db->dbprefix.$new_table_name));
} }

// --------------------------------------------------------------------

/**
* Change table engine
*
* @param string the table name
* @param string the table engine (MyISAM, InnoDb, etc.)
* @return bool
*/
public function set_table_engine($table_name, $engine)
{
if (method_exists($this, '_set_table_engine'))
{
if ($table_name == '' OR $engine == '')
{
show_error('A table name is required for that operation.');
}

return $this->db->query($this->_set_table_engine($this->db->dbprefix.$table_name, $engine));
}
else
{
log_message('debug', 'Table change engine function does not exist.');

return FALSE;
}
}


// -------------------------------------------------------------------- // --------------------------------------------------------------------


Expand Down Expand Up @@ -341,6 +404,7 @@ public function modify_column($table = '', $field = array())
protected function _reset() protected function _reset()
{ {
$this->fields = $this->keys = $this->primary_keys = array(); $this->fields = $this->keys = $this->primary_keys = array();
$this->engine = '';
} }


} }
Expand Down
32 changes: 29 additions & 3 deletions system/database/drivers/mysql/mysql_forge.php
Expand Up @@ -167,9 +167,10 @@ function _process_fields($fields)
* @param mixed primary key(s) * @param mixed primary key(s)
* @param mixed key(s) * @param mixed key(s)
* @param boolean should 'IF NOT EXISTS' be added to the SQL * @param boolean should 'IF NOT EXISTS' be added to the SQL
* @param string the table engine
* @return bool * @return bool
*/ */
function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists, $engine = NULL)
{ {
$sql = 'CREATE TABLE '; $sql = 'CREATE TABLE ';


Expand Down Expand Up @@ -207,8 +208,15 @@ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
$sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")"; $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
} }
} }


$sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};"; if (is_null($engine))
{
$sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
}
else
{
$sql .= "\n) ENGINE {$engine} DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
}


return $sql; return $sql;
} }
Expand Down Expand Up @@ -278,6 +286,24 @@ function _rename_table($table_name, $new_table_name)
$sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name); $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
return $sql; return $sql;
} }

// --------------------------------------------------------------------

/**
* Set engine of a table
*
* Generates a platform-specific query so engine of that a table can be changed
*
* @access private
* @param string the table name
* @param string the table engine
* @return string
*/
function _set_table_engine($table_name, $engine)
{
$sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name).' ENGINE '.$engine;
return $sql;
}


} }


Expand Down
32 changes: 29 additions & 3 deletions system/database/drivers/mysqli/mysqli_forge.php
Expand Up @@ -152,9 +152,10 @@ function _process_fields($fields)
* @param mixed primary key(s) * @param mixed primary key(s)
* @param mixed key(s) * @param mixed key(s)
* @param boolean should 'IF NOT EXISTS' be added to the SQL * @param boolean should 'IF NOT EXISTS' be added to the SQL
* @param string the table engine
* @return bool * @return bool
*/ */
function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists, $engine = NULL)
{ {
$sql = 'CREATE TABLE '; $sql = 'CREATE TABLE ';


Expand Down Expand Up @@ -192,8 +193,15 @@ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
$sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")"; $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
} }
} }


$sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};"; if (is_null($engine))
{
$sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
}
else
{
$sql .= "\n) ENGINE {$engine} DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
}


return $sql; return $sql;
} }
Expand Down Expand Up @@ -263,6 +271,24 @@ function _rename_table($table_name, $new_table_name)
$sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name); $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
return $sql; return $sql;
} }

// --------------------------------------------------------------------

/**
* Set engine of a table
*
* Generates a platform-specific query so engine of that a table can be changed
*
* @access private
* @param string the table name
* @param string the table engine
* @return string
*/
function _set_table_engine($table_name, $engine)
{
$sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name).' ENGINE '.$engine;
return $sql;
}


} }


Expand Down
26 changes: 25 additions & 1 deletion user_guide_src/source/database/forge.rst
Expand Up @@ -169,6 +169,17 @@ below is for MySQL.
// gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`) // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`)




Adding table engine
===================

If you need to pre-define table engine you can simply use function add_engine().
Make sure the function is placed before create_table() calling.

::

$this->dbforge->add_engine('MyISAM');


Creating a table Creating a table
================ ================


Expand Down Expand Up @@ -265,4 +276,17 @@ change the name you can add a "name" key into the field defining array.
), ),
); );
$this->dbforge->modify_column('table_name', $fields); $this->dbforge->modify_column('table_name', $fields);
// gives ALTER TABLE table_name CHANGE old_name new_name TEXT // gives ALTER TABLE table_name CHANGE old_name new_name TEXT

$this->dbforge->set_table_engine()
==================================

The set_table_engine() function is used to change current table engine.
It requires table name and new engine name as parameters.

::

$this->dbforge->set_table_engine('table_name', 'MyISAM');
// This will produce and execute code: ALTER TABLE table_name ENGINE MyISAM

0 comments on commit 67151d0

Please sign in to comment.