|
NAMEClass::DBI::DDL - Combined with Class::DBI to create and dispose of tablesSYNOPSISpackage My::DBI; use base 'Class::DBI::DDL'; # __PACKAGE__->set_db('Main', 'dbi:Pg:dbname=test', 'test', 'test'); __PACKAGE->set_db('Main', 'dbi:mysql:test', 'test', 'test'); package My::Folk; use base 'My::DBI'; # Regular Class::DBI definitions... __PACKAGE__->table('folks'); __PACKAGE__->columns(Primary => 'id'); __PACKAGE__->columns(Essential => qw(first_name last_name age)); __PACKAGE__->has_many(favorite_colors => 'My::Favorite'); # DDL methods __PACKAGE__->column_definitions([ [ id => 'int', 'not null', 'auto_increment' ], [ first_name => 'varchar(20)', 'not null' ], [ last_name => 'varchar(20)', 'not null' ], [ age => 'numeric(3)', 'not null' ], ]); __PACKAGE__->index_definitions([ [ Unique => qw(last_name first_name) ], ]); __PACKAGE__->create_table; package My::Favorite; use base 'My::DBI'; # Class::DBI definitions... __PACKAGE__->table('favorites'); __PACKAGE__->columns(Primary => 'id'); __PACKAGE__->columns(Essential => qw(folk color)); __PACKAGE__->has_a(folk => 'My::Folk'); # DDL methods __PACKAGE__->column_definitions([ [ id => 'int', 'not null', 'auto_increment' ], [ folk => 'numeric(5)', 'not null' ], [ color => 'varchar(20)', 'not null' ], ]); __PACKAGE__->index_definitions([ [ Unique => qw(folk color) ], [ Foreign => 'folk', 'My::Folk', 'id' ], ]); __PACKAGE__->create_table; DESCRIPTIONThis module is used to added to a Class::DBI class to allow it to automatically generate DDL calls to create a table if it doesn't exist in the database already. It attempts to do so in such a way as to be database independent whenever possible.Use the typical "Class::DBI" methods to build your class methods. Then, use the "column_definitions" and "index_definitions" methods to define the structure of the table. Finally, call "create_table" and the system will attempt to create the table if the table cannot be found. DBI DEPENDENCEThe functionality provided by this library attempts to depend on as little that is database or driver specific as possible. However, it does, at this time, require that the DBD driver have a functioning "tables" method for listing tables in the database. Such dependence may later be emulated in the same way "DRIVER DEPENDENT OPERATIONS" is done, if necessary, but it is not at this time.DRIVER DEPENDENT OPERATIONSIt also has some special support for situations where standard SQL generation will fail for a given database. The primary use of this facility is to make sure that auto-increment fields are properly handled. This system uses the the "auto_increment" property notation used by MySQL to handle this. This system does not work well with the "sequence" method of "Class::DBI".METHODSIn addition to the method found in Class::DBI, this package defines the following:
HELPER METHODSThe "Class::DBI::DDL" package uses helper methods named "pre_create_table", "post_create_table", "pre_drop_table", and "post_drop_table" to take care of work that is specific to a database driver--specifically setting up auto_increment columns or stripping out unsupported constraints or indexes.As of this writing, "Class::DBI::DDL" supports "DBD::Pg" and "DBD::mysql" directly, but provides a default that is general enough to work under most other environments. To define a new helper for another database driver, just create a package named "Class::DBI::DDL::Driver", where "Driver" is the name of the database driver name returned by: $dbh->{Driver}->{Name} After this class is installed somewhere in the Perl include path, it will be automatically loaded. If you create such a driver, please send it to me and I will consider its inclusion in the next release. Here are described the workings of the default helper methods--please let me know if this could be improved to be more general as this is largely untested!
SEE ALSOClass::DBI, DBIAUTHORAndrew Sterling Hanenkamp <sterling@hanenkamp.com>LICENSE AND COPYRIGHTCopyright 2003 Andrew Sterling Hanenkamp. All Rights Reserved.This module is free software and is distributed under the same license as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |