データベースのダンプ
この節では、現在のデータベース定義を使用してデータベースの構造、内容、さらにデータベース全体を3つの異なるファイルに抽出する方法を紹介します。そのためには、dumpDatabase()メソッドとgetDefinitionFromDatabase()メソッドを使用します。dumpDatabase()メソッドのプロトタイプを次に示します。
bool|MDB2_Error dumpDatabase( array $database_definition, array $arguments, [int $dump = MDB2_SCHEMA_DUMP_ALL])
dumpDatabase()メソッドは、解析済みのデータベース構造をメタベーススキーマXML形式でファイルに書き出します。引数$database_definitionは現在のデータベース定義を表し、引数$argumentsは表1に示すさまざまなオプションを定義する配列を表します。
| オプション | 型 | 説明 |
| 'output_mode' | String | 'file':指定のファイルに出力をダンプする。省略した場合は関数を使用してデータをダンプする |
| 'output' | String | 出力のファイル名(オプションoutput_modeの値による) |
| 'end_of_line' | String | 出力で使用する行末区切り。デフォルトは\n |
引数$dumpはダンプするデータの種類を表し、次のいずれかを指定できます。MDB2_SCHEMA_DUMP_ALL(データベース全体をダンプ)、MDB2_SCHEMA_DUMP_STRUCTURE(データベース構造のみダンプ)、MDB2_SCHEMA_DUMP_CONTENT(データベースの内容のみダンプ)。
この節で使用するもう1つのメソッドはgetDefinitionFromDatabase()です。プロトタイプは次のとおりです。
array|MDB2_Error getDefinitionFromDatabase( )
このメソッドは、XMLスキーマファイルがある場合に、既存のMDB2からスキーマ構造をリバースエンジニアリングするときに使用します。
詳しい背景を説明しておきましょう。今回の例では、bookstoreデータベースとbooksテーブルを使用します。booksテーブルは次のSQLコマンドで作成したものです。
create table books (id int not null auto_increment primary key, title varchar(50), author varchar(50), yearofpublication int, publisher varchar(50), price int);
実際にデータを操作できるように、次のコードを実行してbooksテーブルに3件のレコードを挿入します。
insert into books values( 1,"Annabel Lee","Edgar Allan Poe", 1849,"The Literature Page",26); insert into books values( 2,"The Ballad of Reading Gaol","Oscar Wilde", 1898,"The Literature Page",45); insert into books values(3,"The Sonnets","Edgar Allan Poe", 1602,"The Literature Page",34);
リスト1に、このデータベースの全体、構造、内容を抽出するPHPアプリケーションを示します。このアプリケーションはall.xml、structure.xml、content.xmlという3つのスキーマドキュメントを出力します。
リスト1を実行すると、次のようなメッセージが表示されます。
Database structure and contents successfully dumped to all.xml. Database structure successfully dumped to structure.xml. Database contents successfully dumped to content.xml.
リスト1のアプリケーションから出力された3つのXMLスキーマドキュメントをリスト2(all.xml)、リスト3(structure.xml)、リスト4(content.xml)に示します。
<?php
// Include class
include_once 'MDB2/Schema.php';
//Set the output mode
$options['output_mode'] = 'file';
// Initialize an MDB connection to the database
$schema = MDB2_Schema::factory(MDB2::factory(
'mysql://root@localhost/bookstore'),$options);
// Get data definitions
$data = $schema->getDefinitionFromDatabase();
// Set the output variable
$options['output'] = 'all.xml';
// Dump the structure and content into
// the all.xml schema document
$all = $schema->dumpDatabase($data, $options,
MDB2_SCHEMA_DUMP_ALL);
if (!$all instanceof MDB2_Error) {
echo 'Database structure and contents successfully
dumped to all.xml.<br />';
}
// Set the output variable
$options['output'] = 'structure.xml';
// Dump only the database structure into the
// structure.xml schema document
$structure = $schema->dumpDatabase(
$data, $options, MDB2_SCHEMA_DUMP_STRUCTURE);
if (!$structure instanceof MDB2_Error) {
echo 'Database structure successfully
dumped to structure.xml.<br />';
}
// Set the output variable
$options['output'] = 'content.xml';
// Dump only the database content into the
// content.xml schema document
$content = $schema->dumpDatabase(
$data, $options, MDB2_SCHEMA_DUMP_CONTENT);
if (!$content instanceof MDB2_Error) {
echo 'Database contents successfully dumped
to content.xml.<br />';
}
?>
