DBUtil クラス

DBUtilクラスは、データベースに対するルーチン操作を管理し、行なうことを可能にします。

Managing functions

DBUtilは、データベースのカラムを作成、リネーム、変更、および削除することを可能にします。

set_connection($connection)

set_connectionメソッドは、すべてのメソッドのための、デフォルトの データベース接続をセットします。

静的 はい
パラメータ
パラメータ 規定値 説明
$connection required データベース接続
返り値 Returns the number of affected rows.
例外 \Database_Exception on failure
// Set an alternative database connection for DBUtil
DBUtil::set_connection('my_database_group');

// Do other DBUtil calls with that connection.
...

// And set it back to default.
DBUtil::set_connection(null);

create_database($database, $charset = null, $if_not_exists = true, $db = null)

create_database メソッドは、データベースを作成します。もし失敗した場合には、例外 Database_Exception がスローされます。

静的 はい
パラメータ
パラメータ 規定値 説明
$database 必須 データベース名
$charset null デフォルトの文字コード
$if_not_exists true IF NOT EXISTSをつけるかどうか
$db
null
データベース接続
返り値 Returns the number of affected rows.
例外 \Database_Exception on failure
// `my_database` というデータベースを作成します。
DBUtil::create_database('my_database');

// Catch the exception
try
{
	DBUtil::create_database('my_database');
}
catch(\Database_Exception $e)
{
	// Creation failed...
}

// デフォルトの文字コードもセットできます。
// CREATE DATABASE IF NOT EXISTS `new_database` DEFAULT CHARACTER SET 'utf8'
DBUtil::create_database('new_database', 'utf8');

// CREATE DATABASE IF NOT EXISTS `new_database  DEFAULT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci'
DBUtil::create_database('new_database', 'utf8_unicode_ci');

もし、文字コードが指定されなかった場合、アクティブなDBグループの文字コードが適用されます。

drop_database($database, $db = null)

drop_database メソッドは、データベースを削除します。もし失敗した場合には、例外 Database_Exception がスローされます。

静的 はい
パラメータ
パラメータ 規定値 説明
$database 必須 データベース名
$db
null
データベース接続
返り値 Returns the number of affected rows.
例外 \Database_Exception on failure
// 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...
}

table_exists($table, $db = null)

table_exists メソッドは、与えられたテーブルが存在するかチェックします。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$db
null
データベース接続
返り値 テーブルが存在する場合 true、存在しない場合 falseが返ります
// Check if table named 'my_table' exists
if(DBUtil::table_exists('my_table'))
{
	// Table exists
} else
{
	// Table does NOT exist, create it!
}
								

drop_table($table, $db = null)

drop_tableメソッドは、テーブルを削除します。もし失敗した場合には、例外 Database_Exception がスローされます。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$db
null
データベース接続
返り値 Returns the number of affected rows.
例外 \Database_Exception on failure
//  `my_table` というテーブルを削除します。
DBUtil::drop_table('my_table');

// Catch the exception
try
{
	DBUtil::drop_table('my_table');
}
catch(\Database_Exception $e)
{
	// Drop failed...
}

rename_table($table, $new_table_name, $db = null)

rename_table メソッドは、テーブルをリネームします。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 The old table name
$new_table_name 必須 The new table name
返り値 Returns the number of affected rows.
例外 \Database_Exception on failure
//  `my_table` というテーブルを `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...
}

create_table($table, $fields, $primary_keys = array(), $if_not_exists = true, $engine = false, $charset = null, $foreign_keys = array(), $db = null)

create_table メソッドは、テーブルを作成します。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$fields 必須 array containing fields
$primary_keys array() array containing primary keys
$if_not_exists true whether to use IF NOT EXISTS
$engine false which storage engine to use (MyISAM, InnoDB, ...)
$charset
null
the default charset, falls back to the active db group charset
$foreign_keys array() array of foreign key constraints definitions. The array keys 'key' and 'reference' are required and throw an error if missing, all others are optional.
$db
null
データベース接続
返り値 Returns the number of affected rows.
例外 \Database_Exception on failure
\DBUtil::create_table(
	'users',
	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'), false, 'InnoDB', 'utf8_unicode_ci',
	array(
		array(
			'constraint' => 'constraintA',
			'key' => 'keyA',
			'reference' => array(
				'table' => 'table',
				'column' => 'field',
			),
			'on_update' => 'CASCADE',
			'on_delete' => 'RESTRICT'
		),
		array(
			'key' => 'keyB',
			'reference' => array(
				'table' => 'table',
				'column' => array(
					'fieldA',
					'fieldB'
				),
			),
		),
	),
);

/* produces the following SQL statement:
CREATE TABLE `users` (
	`id` int(11) NOT NULL AUTO_INCREMENT,
	`name` text NOT NULL,
	`email` varchar(50) NOT NULL,
	`title` varchar(50) DEFAULT 'mr.' NOT NULL,
	`password` varchar(125) NOT NULL,
	PRIMARY KEY `id` (`id`),
	CONSTRAINT constraintA FOREIGN KEY (keyA) REFERENCES table (field) ON UPDATE CASCADE ON DELETE RESTRICT,
	FOREIGN KEY (keyB) REFERENCES table (fieldA, fieldB)
) ENGINE = InnoDB  DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
*/
Possible parameters
パラメータ 説明
name the field name
type field type (varchar, enum, text, ...)
constraint Length or values. Values can be provided in an array
charset field charset
unsigned boolean, true if is unsigned, defaults to false
default default value
null boolean, true if nullable, defaults to false
auto_increment set to true to use auto incrementing
comment adds a comment to your field

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('users', 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'));

Escaping values work the same for DBUtil::add_fields and DBUtil::modify_fields

field_exists($table, $columns, $db = null)

field_exists メソッドは、与えられたテーブルに与えられたカラムが存在しているかチェックします。

静的 はい
パラメータ
パラメータ 規定値 説明
$table required テーブル名
$columns required array containing fields
$db
null
データベース接続
返り値 Returns true if field/column exists, false otherwise

if(DBUtil::field_exists('my_table', array('my_field_or_column')))
{
	// Fields exist
}
else
{
	// Fields are not available on the table
}
								

add_fields($table, $fields, $db = null)

add_fields メソッドは、テーブルにカラムを追加します。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$fields 必須 array containing fields
$db
null
データベース接続
返り値 Returns the number of affected rows.
例外 \Database_Exception on failure
\DBUtil::add_fields('users', array(
	'surname' => array('constraint' => 100, 'type' => 'varchar'),
	'twitter_name' => array('constraint' => 100, 'type' => 'varchar'),
));

drop_fields($table, $fields, $db = null)

drop_fieldsメソッドは、テーブルからカラムを削除します。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$fields 必須 string or array containing fields
$db
null
データベース接続
返り値 Returns the number of affected rows.
例外 \Database_Exception on failure
\DBUtil::drop_fields('users', 'surname');

modify_fields($table, $fields, $db = null)

modify_fields メソッドは、テーブルのカラムを変更します。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$fields 必須 array containing fields
$db
null
データベース接続
返り値 Returns the number of affected rows.
例外 \Database_Exception on failure
\DBUtil::modify_fields('users2', array(
	'name' => array('constraint' => 100, 'type' => 'varchar', 'charset' => 'utf8_general_ci'),
	'title' => array('constraint' => 50, 'type' => 'varchar', 'default' => 'mrs.'),
));

create_index($table, $index_columns, $index_name, $index = '', $db = null)

create_index メソッドは、テーブルにインデックスを追加します。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$index_columns 必須 mixed, string or array of strings containing fields
$index_name optional name of the index to be created
$index optional type of index to be created.
Currently supported: UNIQUE, FULLTEXT, SPATIAL, NONCLUSTERED
$db
null
データベース接続
返り値 Returns the result of the database operation.
例外 \Database_Exception on failure
\DBUtil::create_index('table', 'name');
// produces CREATE INDEX name ON table ( `name` )

\DBUtil::create_index('table', array('nameA', 'nameB'), 'name');
// produces CREATE INDEX name ON table ( `nameA`, `nameB` )

\DBUtil::create_index('table', array('nameA' => 'ASC', 'nameB'), 'name', 'fulltext');
// produces CREATE FULLTEXT INDEX name ON table ( `nameA` ASC, `nameB` )

drop_index($table, $index_name, $db = null)

drop_index メソッドは、テーブルからインデックスを削除します。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$index_name 必須 name of the index to be dropped
$db
null
データベース接続
返り値 Returns the result of the database operation.
例外 \Database_Exception on failure
\DBUtil::drop_index('table', 'name');
// produces DROP INDEX name ON table

add_foregin_key($table, $foreign_key)

add_foreign_key メソッドは、既存テーブルに外部キーを追加します。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$foreign_key 必須 array containing the foreign key definition.
返り値 Returns the result of the database operation.
例外 \InvalidArgumentException on invalid input, \Database_Exception on failure
\DBUtil::add_foreign_key('users', array(
	'constraint' => 'constraintA',
	'key' => 'keyA',
	'reference' => array(
		'table' => 'table',
		'column' => 'field',
	),
	'on_update' => 'CASCADE',
	'on_delete' => 'RESTRICT'
));

drop_foreign_key($table, $fk_name)

drop_foreign_key メソッドは、テーブルから外部キーを削除します。

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$fk_name 必須 name of the foreign key to be dropped
返り値 Returns the result of the database operation.
例外 \Database_Exception on failure
\DBUtil::drop_foreign_key('table', 'name');
// produces ALTER TABLE `table` DROP FOREIGN KEY name

Database operations

truncate_table($table, $db = null)

The truncate_table method truncates a table.

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名.
$db
null
データベース接続
返り値 Returns the number of affected rows.
例外 \Database_Exception when not supported
\DBUtil::truncate_table('my_table');

analyze_table($table, $db = null)

The analyze_table method analyzes a table.

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名.
$db
null
データベース接続
返り値 True if the table is OK. False when needs attention. If not supported it logs the error message.
if(\DBUtil::analyze_table('table_name') === false)
{
	// Do something
}

check_table($table, $db = null)

The check_table method checks a table.

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名.
$db
null
データベース接続
返り値 True if the table is OK. False when needs attention. If not supported it logs the error message.
if(\DBUtil::check_table('table_name') === false)
{
	// Do something
}

optimize_table($table, $db = null)

The optimize_table method optimizes a table.

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名.
$db
null
データベース接続
返り値 True if the table is OK or optimized. False on failure. If not supported or failed it logs the error message.
if(\DBUtil::optimize_table('table_name') === false)
{
	// Do something
}

repair_table($table, $db = null)

The repair_table method repairs a table.

静的 はい
パラメータ
パラメータ 規定値 説明
$table 必須 テーブル名
$db
null
データベース接続
返り値 True if the table is OK or repaired. false on failure. If not supported or failed it logs the error message.
if(\DBUtil::repair_table('table_name') === false)
{
	// Do something
}