Permalink
Browse files

Change table engine for MySQL database, issue #41

Small functions to allow developers choose table engines. Works with
MySQL and MySQLi drivers (other ignore it).
  • Loading branch information...
1 parent ed6485b commit 67151d05bfe3d8251bb87873c376db7de4ad83bd @MunGell MunGell committed Jan 27, 2012
View
68 system/database/DB_forge.php
@@ -40,6 +40,7 @@ class CI_DB_forge {
public $keys = array();
public $primary_keys = array();
public $db_char_set = '';
+ public $engine = '';
public function __construct()
{
@@ -165,6 +166,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
*
* @param string the table name
@@ -181,8 +202,22 @@ public function create_table($table = '', $if_not_exists = FALSE)
{
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();
return is_bool($sql) ? $sql : $this->db->query($sql);
}
@@ -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));
}
+
+ // --------------------------------------------------------------------
+
+ /**
+ * 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;
+ }
+ }
// --------------------------------------------------------------------
@@ -341,6 +404,7 @@ public function modify_column($table = '', $field = array())
protected function _reset()
{
$this->fields = $this->keys = $this->primary_keys = array();
+ $this->engine = '';
}
}
View
32 system/database/drivers/mysql/mysql_forge.php
@@ -167,9 +167,10 @@ function _process_fields($fields)
* @param mixed primary key(s)
* @param mixed key(s)
* @param boolean should 'IF NOT EXISTS' be added to the SQL
+ * @param string the table engine
* @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 ';
@@ -207,8 +208,15 @@ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
$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;
}
@@ -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);
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;
+ }
}
View
32 system/database/drivers/mysqli/mysqli_forge.php
@@ -152,9 +152,10 @@ function _process_fields($fields)
* @param mixed primary key(s)
* @param mixed key(s)
* @param boolean should 'IF NOT EXISTS' be added to the SQL
+ * @param string the table engine
* @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 ';
@@ -192,8 +193,15 @@ function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
$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;
}
@@ -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);
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;
+ }
}
View
26 user_guide_src/source/database/forge.rst
@@ -169,6 +169,17 @@ below is for MySQL.
// 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
================
@@ -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);
- // 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.