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