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