6c403e28eba91ee622365663ab2e11206ec3d7c4
[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 base qw/DBIx::Class::Schema/;
6 use base qw/Class::Data::Accessor/;
7 use Carp;
8 use UNIVERSAL::require;
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 our $VERSION = '0.02000';
14
15 __PACKAGE__->mk_classaccessor('loader');
16
17 =head1 NAME
18
19 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
20
21 =head1 SYNOPSIS
22
23   package My::Schema;
24   use base qw/DBIx::Class::Schema::Loader/;
25
26   __PACKAGE__->load_from_connection(
27     dsn                     => "dbi:mysql:dbname",
28     user                    => "root",
29     password                => "",
30     additional_classes      => [qw/DBIx::Class::Foo/],
31     additional_base_classes => [qw/My::Stuff/],
32     left_base_classes       => [qw/DBIx::Class::Bar/],
33     constraint              => '^foo.*',
34     relationships           => 1,
35     options                 => { AutoCommit => 1 }, 
36     inflect                 => { child => 'children' },
37     debug                   => 1,
38   );
39
40   # in seperate application code ...
41
42   use My::Schema;
43
44   my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
45   # -or-
46   my $schema1 = "My::Schema";
47   # ^^ defaults to dsn/user/pass from load_from_connection()
48
49   # Get a list of the original (database) names of the tables that
50   #  were loaded
51   my @tables = $schema1->loader->tables;
52
53   # Get a hashref of table_name => 'TableName' table-to-moniker
54   #   mappings.
55   my $monikers = $schema1->loader->monikers;
56
57   # Get a hashref of table_name => 'My::Schema::TableName'
58   #   table-to-classname mappings.
59   my $classes = $schema1->loader->classes;
60
61   # Use the schema as per normal for DBIx::Class::Schema
62   my $rs = $schema1->resultset($monikers->{foo_table})->search(...);
63
64 =head1 DESCRIPTION
65
66 DBIx::Class::Schema::Loader automates the definition of a
67 DBIx::Class::Schema by scanning table schemas and setting up
68 columns and primary keys.
69
70 DBIx::Class::Schema::Loader supports MySQL, Postgres, SQLite and DB2.  See
71 L<DBIx::Class::Schema::Loader::Generic> for more, and
72 L<DBIx::Class::Schema::Loader::Writing> for notes on writing your own
73 db-specific subclass for an unsupported db.
74
75 This module requires L<DBIx::Class> 0.05 or later, and obsoletes
76 L<DBIx::Class::Loader> for L<DBIx::Class> version 0.05 and later.
77
78 While on the whole, the bare table definitions are fairly straightforward,
79 relationship creation is somewhat heuristic, especially in the choosing
80 of relationship types, join types, and relationship names.  The relationships
81 generated by this module will probably never be as well-defined as
82 hand-generated ones.  Because of this, over time a complex project will
83 probably wish to migrate off of L<DBIx::Class::Schema::Loader>.
84
85 It is designed more to get you up and running quickly against an existing
86 database, or to be effective for simple situations, rather than to be what
87 you use in the long term for a complex database/project.
88
89 That being said, transitioning your code from a Schema generated by this
90 module to one that doesn't use this module should be straightforward and
91 painless, so don't shy away from it just for fears of the transition down
92 the road.
93
94 =head1 METHODS
95
96 =head2 load_from_connection
97
98 Example in Synopsis above demonstrates the available arguments.  For
99 detailed information on the arguments, see the
100 L<DBIx::Class::Schema::Loader::Generic> documentation.
101
102 =cut
103
104 sub load_from_connection {
105     my ( $class, %args ) = @_;
106
107     croak 'dsn argument is required' if ! $args{dsn};
108     my $dsn = $args{dsn};
109     my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i;
110     $driver = 'SQLite' if $driver eq 'SQLite2';
111     my $impl = "DBIx::Class::Schema::Loader::" . $driver;
112
113     $impl->require or
114       croak qq/Couldn't require loader class "$impl",/ .
115             qq/"$UNIVERSAL::require::ERROR"/;
116
117     $args{schema} = $class;
118
119     $class->loader($impl->new(%args));
120     $class->loader->load;
121 }
122
123 =head2 loader
124
125 This is an accessor in the generated Schema class for accessing
126 the L<DBIx::Class::Schema::Loader::Generic> -based loader object
127 that was used during construction.  See the
128 L<DBIx::Class::Schema::Loader::Generic> docs for more information
129 on the available loader methods there.
130
131 =head1 KNOWN BUGS
132
133 Aside from relationship definitions being less than ideal in general,
134 this version is known not to handle the case of multiple relationships
135 between the same pair of tables.  All of the relationship code will
136 be overhauled on the way to 0.03, at which time that bug will be
137 addressed.
138
139 =head1 AUTHOR
140
141 Brandon Black, C<blblack@gmail.com>
142
143 Based on L<DBIx::Class::Loader> by Sebastian Riedel
144
145 Based upon the work of IKEBE Tomohiro
146
147 =head1 THANK YOU
148
149 Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
150 Randal Schwartz, Simon Flack and all the others who've helped.
151
152 =head1 LICENSE
153
154 This library is free software; you can redistribute it and/or modify it under
155 the same terms as Perl itself.
156
157 =head1 SEE ALSO
158
159 L<DBIx::Class>
160
161 =cut
162
163 1;