データベースのダンプ
この節では、現在のデータベース定義を使用してデータベースの構造、内容、さらにデータベース全体を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 />';
}
?>
この記事は参考になりましたか?
- japan.internet.com翻訳記事連載記事一覧
- この記事の著者
-
japan.internet.com(ジャパンインターネットコム)
japan.internet.com は、1999年9月にオープンした、日本初のネットビジネス専門ニュースサイト。月間2億以上のページビューを誇る米国 Jupitermedia Corporation (Nasdaq: JUPM) のニュースサイト internet.com や EarthWeb.com からの最新記事を日本語に翻訳して掲載するとともに、日本独自のネットビジネス関連記事やレポートを配信。
※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です
-
Octavia Andreea Anghel(Octavia Andreea Anghel)
経験豊富なPHP開発者。現在は、国内外のソフトウェア開発コンテストに参加するプログラミングチームの主任トレーナーを務める。国レベルの教育プロジェクト開発のコンサルティングも担当している。共著書に『XML technologies?XML in Java』があり、XML部分の執筆を担当。PHPやXML...
※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です
この記事は参考になりましたか?
この記事をシェア
イベント
CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。
