DBUtil Class
The DBUtil class allowes you to manage and perform routine opperations on your databases.
Managing functions
DBUtil allows you to create, rename, alter and drop databases field
DBUtil::create_database
The create_database method creates a database. Will throw a Database_Exception if it cannot.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$database |
required |
the database name |
|
Returns |
Returns the number of affected rows. |
Throws |
\Database_Exception on failure |
Example |
// Create a database named `my_database`
DBUtil::create_database('my_database');
// Catch the exception
try
{
DBUtil::create_database('my_database');
}
catch(\Database_Exception $e)
{
// Creation failed...
}
|
DBUtil::drop_database
The drop_database method drops a database. Will throw a Database_Exception if it cannot.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$database |
required |
the database name |
|
Returns |
Returns the number of affected rows. |
Throws |
\Database_Exception on failure |
Example |
// Drop a database named `my_database`
DBUtil::drop_database('my_database');
// Catch the exception
try
{
DBUtil::drop_database('my_database');
}
catch(\Database_Exception $e)
{
// Drop failed...
}
|
DBUtil::drop_table
The drop_table method drops a table. Will throw a Database_Exception if it cannot.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$table |
required |
the table name |
|
Returns |
Returns the number of affected rows. |
Throws |
\Database_Exception on failure |
Example |
// Drop a table named `my_table`
DBUtil::drop_table('my_table');
// Catch the exception
try
{
DBUtil::drop_table('my_table');
}
catch(\Database_Exception $e)
{
// Drop failed...
}
|
DBUtil::rename_table
The rename_table method renames a table.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$table |
required |
The old table name |
$new_table_name |
required |
The old table name |
|
Returns |
Returns the number of affected. |
Throws |
\Database_Exception on failure |
Example |
// Rename `my_table` to `my_new_table`
DBUtil::rename_table('my_table', 'my_new_table');
// Catch the exception
try
{
DBUtil::rename_table('my_table', 'my_new_table');
}
catch(\Database_Exception $e)
{
// Rename failed...
}
|
DBUtil::create_table
The create_table method creates a table.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$table |
required |
the table name |
$fields |
required |
array containing fields |
$primary_keys |
array() |
array containing primary keys |
$if_not_exists |
true |
whether to use IF NOT EXISTS |
|
Returns |
Returns the number of affected rows. |
Throws |
\Database_Exception on failure |
Example |
\DBUtil::create_table('users2', array(
'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),
'name' => array('type' => 'text'),
'email' => array('constraint' => 50, 'type' => 'varchar'),
'title' => array('constraint' => 50, 'type' => 'varchar', 'default' => 'mr.'),
'password' => array('constraint' => 125, 'type' => 'varchar'),
), array('id'));
|
Default fields values are escaped by default. In some cases you might want not to escape this value. If so, you can use DB::escape.
\DBUtil::create_table('users2', array(
'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),
'name' => array('type' => 'text'),
'email' => array('constraint' => 50, 'type' => 'varchar'),
'title' => array('constraint' => 50, 'type' => 'varchar', 'default' => 'mr.'),
'created' => array('type' => 'timestamp', 'default' => \DB::expr('CURRENT_TIMESTAMP')),
'password' => array('constraint' => 125, 'type' => 'varchar'),
), array('id'));
Database operations
DBUtil::truncate_table
The truncate_table method truncates a table.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$table |
required |
the table name. |
|
Returns |
Returns the number of affected rows. |
Throws |
\Database_Exception when not supported |
Example |
\DBUtil::truncate_table('my_table');
|
DBUtil::analyze_table
The analyze_table method analyzes a table.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$table |
required |
the table name. |
|
Returns |
True if the table is OK. False when needs attention. If not supported it logs the error message. |
Example |
if(\DBUtil::analyze_table('table_name') === false)
{
// Do something
}
|
DBUtil::check_table
The check_table method checks a table.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$table |
required |
the table name. |
|
Returns |
True if the table is OK. False when needs attention. If not supported it logs the error message. |
Example |
if(\DBUtil::check_table('table_name') === false)
{
// Do something
}
|
DBUtil::optimize_table
The check_table method optimizes a table.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$table |
required |
the table name. |
|
Returns |
True if the table is OK or optimized. False on failure. If not supported or failed it logs the error message. |
Example |
if(\DBUtil::optimize_table('table_name') === false)
{
// Do something
}
|
DBUtil::repair_table
The check_table method repairs a table.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$table |
required |
the table name. |
|
Returns |
True if the table is OK or repaired. False on failure. If not supported or failed it logs the error message. |
Example |
if(\DBUtil::repair_table('table_name') === false)
{
// Do something
}
|