1 package DBIx::Class::Schema::Loader;
5 use base qw/DBIx::Class::Schema/;
6 use base qw/Class::Data::Accessor/;
8 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
13 use vars qw($VERSION);
16 __PACKAGE__->mk_classaccessor('loader');
20 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
25 use base qw/DBIx::Class::Schema::Loader/;
27 __PACKAGE__->load_from_connection(
28 dsn => "dbi:mysql:dbname",
31 additional_classes => [qw/DBIx::Class::Foo/],
32 additional_base_classes => [qw/My::Stuff/],
33 left_base_classes => [qw/DBIx::Class::Bar/],
34 constraint => '^foo.*',
36 options => { AutoCommit => 1 },
37 inflect => { child => 'children' },
41 # in seperate application code ...
45 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
47 my $schema1 = "My::Schema";
48 # ^^ defaults to dsn/user/pass from load_from_connection()
50 # Get a list of the original (database) names of the tables that
52 my @tables = $schema1->loader->tables;
54 # Get a hashref of table_name => 'TableName' table-to-moniker
56 my $monikers = $schema1->loader->monikers;
58 # Get a hashref of table_name => 'My::Schema::TableName'
59 # table-to-classname mappings.
60 my $classes = $schema1->loader->classes;
62 # Use the schema as per normal for DBIx::Class::Schema
63 my $rs = $schema1->resultset($monikers->{table_table})->search(...);
67 THIS IS A DEVELOPMENT RELEASE. This is 0.01xxx, the first public
68 releases. Expect things to be broken in various ways. Expect the
69 entire design to be fatally flawed. Expect the interfaces to change if
70 it becomes neccessary. It's mostly here for people to poke at it and
71 find the flaws in it. 0.02 will hopefully have some sanity when we get
74 DBIx::Class::Schema::Loader automates the definition of a
75 DBIx::Class::Schema by scanning table schemas and setting up
76 columns and primary keys.
78 DBIx::Class::Schema::Loader supports MySQL, Postgres, SQLite and DB2. See
79 L<DBIx::Class::Schema::Loader::Generic> for more, and
80 L<DBIx::Class::Schema::Loader::Writing> for notes on writing your own
81 db-specific subclass for an unsupported db.
83 This module requires L<DBIx::Class> 0.05 or later, and obsoletes
84 L<DBIx::Class::Loader> for L<DBIx::Class> version 0.05 and later.
88 =head2 load_from_connection
90 Example in Synopsis above demonstrates the available arguments. For
91 detailed information on the arguments, see the
92 L<DBIx::Class::Schema::Loader::Generic> documentation.
96 sub load_from_connection {
97 my ( $class, %args ) = @_;
99 croak 'dsn argument is required' if ! $args{dsn};
100 my $dsn = $args{dsn};
101 my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i;
102 $driver = 'SQLite' if $driver eq 'SQLite2';
103 my $impl = "DBIx::Class::Schema::Loader::" . $driver;
106 croak qq/Couldn't require loader class "$impl",/ .
107 qq/"$UNIVERSAL::require::ERROR"/;
109 $args{schema} = $class;
111 $class->loader($impl->new(%args));
112 $class->loader->load;
117 This is an accessor in the generated Schema class for accessing
118 the L<DBIx::Class::Schema::Loader::Generic> -based loader object
119 that was used during construction. See the
120 L<DBIx::Class::Schema::Loader::Generic> docs for more information
121 on the available loader methods there.
125 Brandon Black, C<bblack@gmail.com>
127 Based on L<DBIx::Class::Loader> by Sebastian Riedel
129 Based upon the work of IKEBE Tomohiro
133 Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
134 Randal Schwartz, Simon Flack and all the others who've helped.
138 This library is free software; you can redistribute it and/or modify it under
139 the same terms as Perl itself.