storage->disconnect and new tests for out-of-line implicit rels
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader.pm
CommitLineData
18fca96a 1package DBIx::Class::Schema::Loader;
a78e3fed 2
3use strict;
a4a19f3c 4use warnings;
3385ac62 5use Carp;
a78e3fed 6use UNIVERSAL::require;
7
3980d69c 8use vars qw($VERSION);
9
a4a19f3c 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
a4a19f3c 13$VERSION = '0.01000';
a78e3fed 14
3980d69c 15use base qw/DBIx::Class::Schema/;
16
17__PACKAGE__->mk_classaccessor('loader');
18
a78e3fed 19=head1 NAME
20
18fca96a 21DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
a78e3fed 22
23=head1 SYNOPSIS
24
a4a19f3c 25 package My::Schema;
26 use base qw/DBIx::Class::Schema::Loader/;
a78e3fed 27
a4a19f3c 28 __PACKAGE__->load_from_connection(
a78e3fed 29 dsn => "dbi:mysql:dbname",
30 user => "root",
31 password => "",
a78e3fed 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.*',
36 relationships => 1,
37 options => { AutoCommit => 1 },
38 inflect => { child => 'children' },
39 debug => 1,
40 );
af6c2665 41
a4a19f3c 42 # in seperate application code ...
a78e3fed 43
a4a19f3c 44 use My::Schema;
a78e3fed 45
a4a19f3c 46 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
47 # -or-
fbd83464 48 my $schema1 = "My::Schema";
a4a19f3c 49 # ^^ defaults to dsn/user/pass from load_from_connection()
a78e3fed 50
3980d69c 51 # Get a list of the original (database) names of the tables that
52 # were loaded
53 my @tables = $schema1->loader->tables;
54
55 # Get a hashref of table_name => 'TableName' table-to-moniker
56 # mappings.
57 my $monikers = $schema1->loader->monikers;
58
59 # Get a hashref of table_name => 'My::Schema::TableName'
60 # table-to-classname mappings.
61 my $classes = $schema1->loader->classes;
62
63 # Use the schema as per normal for L<DBIx::Class::Schema>
64 my $rs = $schema1->resultset($monikers->{table_table})->search(...);
65
a78e3fed 66=head1 DESCRIPTION
67
fbd83464 68DBIx::Class::Schema::Loader automates the definition of a
18fca96a 69DBIx::Class::Schema by scanning table schemas and setting up
70columns and primary keys.
a78e3fed 71
18fca96a 72DBIx::Class::Schema::Loader supports MySQL, Postgres, SQLite and DB2. See
73L<DBIx::Class::Schema::Loader::Generic> for more, and
74L<DBIx::Class::Schema::Loader::Writing> for notes on writing your own
75db-specific subclass for an unsupported db.
a78e3fed 76
3385ac62 77This module requires DBIx::Class::Loader 0.5 or later, and obsoletes
78L<DBIx::Class::Loader> for L<DBIx::Class> version 0.5 and later.
a78e3fed 79
80=cut
81
82=head1 METHODS
83
fbd83464 84=head2 load_from_connection
a78e3fed 85
86Example in Synopsis above demonstrates the available arguments. For
87detailed information on the arguments, see the
18fca96a 88L<DBIx::Class::Schema::Loader::Generic> documentation.
a78e3fed 89
90=cut
91
a4a19f3c 92sub load_from_connection {
a78e3fed 93 my ( $class, %args ) = @_;
af6c2665 94
3385ac62 95 croak 'dsn argument is required' if ! $args{dsn};
a78e3fed 96 my $dsn = $args{dsn};
97 my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i;
98 $driver = 'SQLite' if $driver eq 'SQLite2';
18fca96a 99 my $impl = "DBIx::Class::Schema::Loader::" . $driver;
af6c2665 100
a78e3fed 101 $impl->require or
3385ac62 102 croak qq/Couldn't require loader class "$impl",/ .
103 qq/"$UNIVERSAL::require::ERROR"/;
af6c2665 104
3980d69c 105 $args{schema} = $class;
106
107 $class->loader($impl->new(%args));
a78e3fed 108}
109
110=head1 AUTHOR
111
fbd83464 112Brandon Black, C<bblack@gmail.com>
113
114Sebastian Riedel, C<sri@oook.de> (DBIx::Class::Loader, which this module is branched from)
a78e3fed 115
116Based upon the work of IKEBE Tomohiro
117
118=head1 THANK YOU
119
120Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
121Randal Schwartz, Simon Flack and all the others who've helped.
122
123=head1 LICENSE
124
125This library is free software; you can redistribute it and/or modify it under
126the same terms as Perl itself.
127
128=head1 SEE ALSO
129
130L<DBIx::Class>
131
132=cut
133
1341;