1 package DBIx::Class::Schema::Loader;
6 use UNIVERSAL::require;
10 # Always remember to do all digits for the version even if they're 0
11 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
12 # brain damage and presumably various other packaging systems too
15 use base qw/DBIx::Class::Schema/;
17 __PACKAGE__->mk_classaccessor('loader');
21 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
26 use base qw/DBIx::Class::Schema::Loader/;
28 __PACKAGE__->load_from_connection(
29 dsn => "dbi:mysql:dbname",
32 additional_classes => [qw/DBIx::Class::Foo/],
33 additional_base_classes => [qw/My::Stuff/],
34 left_base_classes => [qw/DBIx::Class::Bar/],
35 constraint => '^foo.*',
37 options => { AutoCommit => 1 },
38 inflect => { child => 'children' },
42 # in seperate application code ...
46 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
48 my $schema1 = "My::Schema";
49 # ^^ defaults to dsn/user/pass from load_from_connection()
51 # Get a list of the original (database) names of the tables that
53 my @tables = $schema1->loader->tables;
55 # Get a hashref of table_name => 'TableName' table-to-moniker
57 my $monikers = $schema1->loader->monikers;
59 # Get a hashref of table_name => 'My::Schema::TableName'
60 # table-to-classname mappings.
61 my $classes = $schema1->loader->classes;
63 # Use the schema as per normal for L<DBIx::Class::Schema>
64 my $rs = $schema1->resultset($monikers->{table_table})->search(...);
68 DBIx::Class::Schema::Loader automates the definition of a
69 DBIx::Class::Schema by scanning table schemas and setting up
70 columns and primary keys.
72 DBIx::Class::Schema::Loader supports MySQL, Postgres, SQLite and DB2. See
73 L<DBIx::Class::Schema::Loader::Generic> for more, and
74 L<DBIx::Class::Schema::Loader::Writing> for notes on writing your own
75 db-specific subclass for an unsupported db.
77 This module requires DBIx::Class::Loader 0.5 or later, and obsoletes
78 L<DBIx::Class::Loader> for L<DBIx::Class> version 0.5 and later.
84 =head2 load_from_connection
86 Example in Synopsis above demonstrates the available arguments. For
87 detailed information on the arguments, see the
88 L<DBIx::Class::Schema::Loader::Generic> documentation.
92 sub load_from_connection {
93 my ( $class, %args ) = @_;
95 croak 'dsn argument is required' if ! $args{dsn};
97 my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i;
98 $driver = 'SQLite' if $driver eq 'SQLite2';
99 my $impl = "DBIx::Class::Schema::Loader::" . $driver;
102 croak qq/Couldn't require loader class "$impl",/ .
103 qq/"$UNIVERSAL::require::ERROR"/;
105 $args{schema} = $class;
107 $class->loader($impl->new(%args));
112 Brandon Black, C<bblack@gmail.com>
114 Sebastian Riedel, C<sri@oook.de> (DBIx::Class::Loader, which this module is branched from)
116 Based upon the work of IKEBE Tomohiro
120 Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
121 Randal Schwartz, Simon Flack and all the others who've helped.
125 This library is free software; you can redistribute it and/or modify it under
126 the same terms as Perl itself.