1 package DBIx::Class::Schema::Loader;
6 use UNIVERSAL::require;
8 use base qw/DBIx::Class::Schema/;
9 use base qw/Class::Data::Accessor/;
11 __PACKAGE__->mk_classaccessor('loader');
13 use vars qw($VERSION);
15 # Always remember to do all digits for the version even if they're 0
16 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
17 # brain damage and presumably various other packaging systems too
22 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
27 use base qw/DBIx::Class::Schema::Loader/;
29 __PACKAGE__->load_from_connection(
30 dsn => "dbi:mysql:dbname",
33 additional_classes => [qw/DBIx::Class::Foo/],
34 additional_base_classes => [qw/My::Stuff/],
35 left_base_classes => [qw/DBIx::Class::Bar/],
36 constraint => '^foo.*',
38 options => { AutoCommit => 1 },
39 inflect => { child => 'children' },
43 # in seperate application code ...
47 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
49 my $schema1 = "My::Schema";
50 # ^^ defaults to dsn/user/pass from load_from_connection()
52 # Get a list of the original (database) names of the tables that
54 my @tables = $schema1->loader->tables;
56 # Get a hashref of table_name => 'TableName' table-to-moniker
58 my $monikers = $schema1->loader->monikers;
60 # Get a hashref of table_name => 'My::Schema::TableName'
61 # table-to-classname mappings.
62 my $classes = $schema1->loader->classes;
64 # Use the schema as per normal for L<DBIx::Class::Schema>
65 my $rs = $schema1->resultset($monikers->{table_table})->search(...);
69 DBIx::Class::Schema::Loader automates the definition of a
70 DBIx::Class::Schema by scanning table schemas and setting up
71 columns and primary keys.
73 DBIx::Class::Schema::Loader supports MySQL, Postgres, SQLite and DB2. See
74 L<DBIx::Class::Schema::Loader::Generic> for more, and
75 L<DBIx::Class::Schema::Loader::Writing> for notes on writing your own
76 db-specific subclass for an unsupported db.
78 This module requires DBIx::Class::Loader 0.5 or later, and obsoletes
79 L<DBIx::Class::Loader> for L<DBIx::Class> version 0.5 and later.
85 =head2 load_from_connection
87 Example in Synopsis above demonstrates the available arguments. For
88 detailed information on the arguments, see the
89 L<DBIx::Class::Schema::Loader::Generic> documentation.
93 sub load_from_connection {
94 my ( $class, %args ) = @_;
96 croak 'dsn argument is required' if ! $args{dsn};
98 my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i;
99 $driver = 'SQLite' if $driver eq 'SQLite2';
100 my $impl = "DBIx::Class::Schema::Loader::" . $driver;
103 croak qq/Couldn't require loader class "$impl",/ .
104 qq/"$UNIVERSAL::require::ERROR"/;
106 $args{schema} = $class;
108 $class->loader($impl->new(%args));
109 $class->loader->load;
114 Brandon Black, C<bblack@gmail.com>
116 Based on L<DBIx::Class::Loader> by Sebastian Riedel
118 Based upon the work of IKEBE Tomohiro
122 Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
123 Randal Schwartz, Simon Flack and all the others who've helped.
127 This library is free software; you can redistribute it and/or modify it under
128 the same terms as Perl itself.