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 |
f654c972 |
13 | our $VERSION = '0.01002'; |
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/], |
33 | constraint => '^foo.*', |
34 | relationships => 1, |
35 | options => { AutoCommit => 1 }, |
36 | inflect => { child => 'children' }, |
37 | debug => 1, |
38 | ); |
af6c2665 |
39 | |
a4a19f3c |
40 | # in seperate application code ... |
a78e3fed |
41 | |
a4a19f3c |
42 | use My::Schema; |
a78e3fed |
43 | |
a4a19f3c |
44 | my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs); |
45 | # -or- |
fbd83464 |
46 | my $schema1 = "My::Schema"; |
a4a19f3c |
47 | # ^^ defaults to dsn/user/pass from load_from_connection() |
a78e3fed |
48 | |
3980d69c |
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 | |
457eb8a6 |
61 | # Use the schema as per normal for DBIx::Class::Schema |
3980d69c |
62 | my $rs = $schema1->resultset($monikers->{table_table})->search(...); |
63 | |
a78e3fed |
64 | =head1 DESCRIPTION |
65 | |
457eb8a6 |
66 | THIS IS A DEVELOPMENT RELEASE. This is 0.01xxx, the first public |
67 | releases. Expect things to be broken in various ways. Expect the |
aec93e93 |
68 | entire design to be fatally flawed. Expect the interfaces to change if |
69 | it becomes neccessary. It's mostly here for people to poke at it and |
70 | find the flaws in it. 0.02 will hopefully have some sanity when we get |
71 | there. |
72 | |
fbd83464 |
73 | DBIx::Class::Schema::Loader automates the definition of a |
18fca96a |
74 | DBIx::Class::Schema by scanning table schemas and setting up |
75 | columns and primary keys. |
a78e3fed |
76 | |
18fca96a |
77 | DBIx::Class::Schema::Loader supports MySQL, Postgres, SQLite and DB2. See |
78 | L<DBIx::Class::Schema::Loader::Generic> for more, and |
79 | L<DBIx::Class::Schema::Loader::Writing> for notes on writing your own |
80 | db-specific subclass for an unsupported db. |
a78e3fed |
81 | |
457eb8a6 |
82 | This module requires L<DBIx::Class> 0.05 or later, and obsoletes |
83 | L<DBIx::Class::Loader> for L<DBIx::Class> version 0.05 and later. |
a78e3fed |
84 | |
85 | =head1 METHODS |
86 | |
fbd83464 |
87 | =head2 load_from_connection |
a78e3fed |
88 | |
89 | Example in Synopsis above demonstrates the available arguments. For |
90 | detailed information on the arguments, see the |
18fca96a |
91 | L<DBIx::Class::Schema::Loader::Generic> documentation. |
a78e3fed |
92 | |
93 | =cut |
94 | |
a4a19f3c |
95 | sub load_from_connection { |
a78e3fed |
96 | my ( $class, %args ) = @_; |
af6c2665 |
97 | |
3385ac62 |
98 | croak 'dsn argument is required' if ! $args{dsn}; |
a78e3fed |
99 | my $dsn = $args{dsn}; |
100 | my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i; |
101 | $driver = 'SQLite' if $driver eq 'SQLite2'; |
18fca96a |
102 | my $impl = "DBIx::Class::Schema::Loader::" . $driver; |
af6c2665 |
103 | |
a78e3fed |
104 | $impl->require or |
3385ac62 |
105 | croak qq/Couldn't require loader class "$impl",/ . |
106 | qq/"$UNIVERSAL::require::ERROR"/; |
af6c2665 |
107 | |
3980d69c |
108 | $args{schema} = $class; |
109 | |
110 | $class->loader($impl->new(%args)); |
2a4b8262 |
111 | $class->loader->load; |
a78e3fed |
112 | } |
113 | |
457eb8a6 |
114 | =head2 loader |
115 | |
116 | This is an accessor in the generated Schema class for accessing |
117 | the L<DBIx::Class::Schema::Loader::Generic> -based loader object |
118 | that was used during construction. See the |
119 | L<DBIx::Class::Schema::Loader::Generic> docs for more information |
120 | on the available loader methods there. |
121 | |
a78e3fed |
122 | =head1 AUTHOR |
123 | |
f654c972 |
124 | Brandon Black, C<blblack@gmail.com> |
fbd83464 |
125 | |
8a6b44ef |
126 | Based on L<DBIx::Class::Loader> by Sebastian Riedel |
a78e3fed |
127 | |
128 | Based upon the work of IKEBE Tomohiro |
129 | |
130 | =head1 THANK YOU |
131 | |
132 | Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton, |
133 | Randal Schwartz, Simon Flack and all the others who've helped. |
134 | |
135 | =head1 LICENSE |
136 | |
137 | This library is free software; you can redistribute it and/or modify it under |
138 | the same terms as Perl itself. |
139 | |
140 | =head1 SEE ALSO |
141 | |
142 | L<DBIx::Class> |
143 | |
144 | =cut |
145 | |
146 | 1; |