データベースのリバースエンジニアリング
データベースをダンプする方法を説明しましたが、その逆、つまりダンプしたスキーマドキュメント「structure.xml」を使用してデータベースを作成することも可能です。そのためには、parseDatabaseDefinitionFileメソッドと createDatabaseメソッドを使用します。
データベース定義ファイルを解析するには、スキーマ形式パーサーオブジェクトを作成し、ファイルの内容をこのパーサーに入力データストリームとして渡します。parseDatabaseDefinitionFileメソッドのプロトタイプを次に示します。
</b>void parseDatabaseDefinitionFile(string $input_file, [array $variables = array()], [bool $fail_on_invalid_names = true], [array $structure = false])
引数$input_fileはデータベーススキーマファイルを表します。引数$variablesは、スキーマ定義で使われている変数を置き換えるためのテキスト文字列値を定義する連想配列です。引数$fail_on_invalid_namesを指定すると、パーサーが無効な名前を発見した場合に関数の実行が失敗します。引数$structureはデータベース構造定義を表します。
スキーマファイルの解析が終わったら、createDatabaseメソッドを呼び出して実際にデータベースを作成します。
bool|MDB2_Error createDatabase( array $database_definition, [array $options = array()]):
createDatabaseメソッドはデータベースを作成するだけでなく、スキーマ内に定義されているテーブル、インデックス、シーケンスなどすべてのオブジェクトを作成します。引数$database_definitionは、現在のデータベース定義を含んでいる多次元配列を表します。また引数$optionsは、各データベースのドライバに対応するMDB2_Driver_Manager_Common::createTable()に引き渡すオプションの配列を表します。
実際の例を見てみましょう。次のアプリケーションは、少し修正を加えたバージョンのスキーマドキュメント「structure.xml」(データベース名とテーブル名の部分を変更)を解析して、books2テーブルを含むbookstore2データベースを作成します。
<?php
// Include class
include_once 'MDB2/Schema.php';
// Initialize an MDB connection to the database
$schema = MDB2_Schema::factory(MDB2::factory(
'mysql://root@localhost/bookstore2'));
// Parse a database definition file
$data = $schema->parseDatabaseDefinitionFile(
'structure.xml');
//Create/import database
$ret = $schema->createDatabase($data);
if ($ret instanceof MDB2_Error){
die ($ret->getMessage());
} else {
echo 'Database schema successfully imported. <br />';
}
?>
このコードを実行すると、入力ファイル「structure.xml」が解析されて、books2テーブルを含むbookstore2データベースが作成されます。この新しいデータベースはbookstoreデータベースと同じ構造を持ちますが、books2テーブルにはレコードが1件も含まれていません。
配列を使用したデータベース作成
既存のスキーマファイルがない場合のために、MDB2 PEARには新しいデータベースを作成するcreateDatabaseメソッドが用意されています。この方法でデータベースを作成するには、データベース名、各種プロパティ、テーブル、フィールド名などのパラメータを設定する多次元配列を作成します(リスト5に一般的な例を示します)。
リスト5に示したデータベースとテーブルを定義する多次元配列は、MDB2_Driver_mysql-1.4.1\MDB2\Driver\Manager\mysql.phpファイルから抽出したものです。
/** * create a new table * * @param string $name Name of the database that should be created * @param array $fields Associative array that contains the definition of each field * of the new table * The indexes of the array entries are the names of the fields of the table and * the array entry values are associative arrays like those that are meant to be * passed with the field definitions to get[Type]Declaration() functions. * array( * 'id' => array( * 'type' => 'integer', * 'unsigned' => 1 * 'notnull' => 1 * 'default' => 0 * ), * 'name' => array( * 'type' => 'text', * 'length' => 12 * ), * 'password' => array( * 'type' => 'text', * 'length' => 12 * ) * ); * @param array $options An associative array of table options: * array( * 'comment' => 'Foo', * 'charset' => 'utf8', * 'collate' => 'utf8_unicode_ci', * 'type' => 'innodb', * ); * * Example: * array( * 'name' => 'userlist', * 'add' => array( * 'quota' => array( * 'type' => 'integer', * 'unsigned' => 1 * ) * ), * 'remove' => array( * 'file_limit' => array(), * 'time_limit' => array() * ), * 'change' => array( * 'name' => array( * 'length' => '20', * 'definition' => array( * 'type' => 'text', * 'length' => 20, * ), * ) * ), * 'rename' => array( * 'sex' => array( * 'name' => 'gender', * 'definition' => array( * 'type' => 'text', * 'length' => 1, * 'default' => 'M', * ), * ) * ) * )
データベースを定義する多次元配列をセットアップしたら、DSNを作成し、createDatabaseメソッドを呼び出します。リスト6 に示すアプリケーションは、リスト5内の構造と同様の構造を持つ多次元配列を使用して、books3およびbooks4テーブルを含む bookstore3データベースを作成します。
リスト6のコードを実行すると、bookstore3という新しいデータベースが作成されます。このデータベースには2つのテーブルがあり、books3 テーブルにはid、title、authorフィールド、books4テーブルにはid、yearofpublication、publisherフィールドが含まれます。
<?php
require 'MDB2/Schema.php';
// Define the database and the tables
$definition = array(
'name' => 'bookstore3',
'create' => 1,
'overwrite' => 0,
'tables' => array(
'books3' => array(
'fields' => array(
'id' => array(
'type' => 'integer',
'notnull' => 1,
'length' => 2,
'unsigned' => 1,
'default' => 0
),
'title' => array(
'type' => 'text',
'length' => 50,
'default' => 'None'
),
'author' => array(
'type' => 'text',
'length' => 50
)
)
),
'books4' => array(
'fields' => array(
'id' => array(
'type' => 'integer',
'notnull' => 1,
'length' => 2,
'unsigned' => 1,
'default' => 0
),
'yearofpublication' => array(
'type' => 'integer',
'length' => 4,
'default' => 'None'
),
'publisher' => array(
'type' => 'text',
'length' => 30
)
)
)
)
);
// Define the DSN
$dsn = 'mysql://root:@localhost/test';
// Initialize an MDB connection to the database
$schema = MDB2_Schema::factory($dsn);
// Create a database using the $definition array
$ret = $schema->createDatabase($definition);
if ($ret instanceof MDB2_Error){
die ($ret->getMessage());
} else {
echo 'Database successfully created. <br />';
}
?>
