Schema::Loader converted to better inheritance model, no longer pollutes user schema...
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader.pm
1 package DBIx::Class::Schema::Loader;
2
3 use strict;
4 use warnings;
5 use Carp;
6 use UNIVERSAL::require;
7
8 use vars qw($VERSION);
9
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 $VERSION = '0.01000';
14
15 use base qw/DBIx::Class::Schema/;
16
17 __PACKAGE__->mk_classaccessor('loader');
18
19 =head1 NAME
20
21 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
22
23 =head1 SYNOPSIS
24
25   package My::Schema;
26   use base qw/DBIx::Class::Schema::Loader/;
27
28   __PACKAGE__->load_from_connection(
29     dsn                     => "dbi:mysql:dbname",
30     user                    => "root",
31     password                => "",
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   );
41
42   # in seperate application code ...
43
44   use My::Schema;
45
46   my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
47   # -or-
48   my $schema1 = "My::Schema";
49   # ^^ defaults to dsn/user/pass from load_from_connection()
50
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
66 =head1 DESCRIPTION
67
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.
71
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.
76
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.
79
80 =cut
81
82 =head1 METHODS
83
84 =head2 load_from_connection
85
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.
89
90 =cut
91
92 sub load_from_connection {
93     my ( $class, %args ) = @_;
94
95     croak 'dsn argument is required' if ! $args{dsn};
96     my $dsn = $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;
100
101     $impl->require or
102       croak qq/Couldn't require loader class "$impl",/ .
103             qq/"$UNIVERSAL::require::ERROR"/;
104
105     $args{schema} = $class;
106
107     $class->loader($impl->new(%args));
108 }
109
110 =head1 AUTHOR
111
112 Brandon Black, C<bblack@gmail.com>
113
114 Sebastian Riedel, C<sri@oook.de> (DBIx::Class::Loader, which this module is branched from)
115
116 Based upon the work of IKEBE Tomohiro
117
118 =head1 THANK YOU
119
120 Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
121 Randal Schwartz, Simon Flack and all the others who've helped.
122
123 =head1 LICENSE
124
125 This library is free software; you can redistribute it and/or modify it under
126 the same terms as Perl itself.
127
128 =head1 SEE ALSO
129
130 L<DBIx::Class>
131
132 =cut
133
134 1;