Commit | Line | Data |
a78e3fed |
1 | package DBIx::Class::Loader; |
2 | |
3 | use strict; |
4 | use UNIVERSAL::require; |
5 | |
af6c2665 |
6 | our $VERSION = '0.01'; |
a78e3fed |
7 | |
8 | =head1 NAME |
9 | |
10 | DBIx::Class::Loader - Dynamic definition of DBIx::Class sub classes. |
11 | |
12 | =head1 SYNOPSIS |
13 | |
14 | use DBIx::Class::Loader; |
15 | |
16 | my $loader = DBIx::Class::Loader->new( |
17 | dsn => "dbi:mysql:dbname", |
18 | user => "root", |
19 | password => "", |
20 | namespace => "Data", |
21 | additional_classes => [qw/DBIx::Class::Foo/], |
22 | additional_base_classes => [qw/My::Stuff/], |
23 | left_base_classes => [qw/DBIx::Class::Bar/], |
24 | constraint => '^foo.*', |
25 | relationships => 1, |
26 | options => { AutoCommit => 1 }, |
27 | inflect => { child => 'children' }, |
28 | debug => 1, |
29 | ); |
af6c2665 |
30 | |
31 | my $conn = $loader->get_connection($dsn, $user, $password); # |
32 | my $conn = $loader->get_connection(); # uses same dsn as ->new(); |
a78e3fed |
33 | |
34 | use with mod_perl |
35 | |
36 | in your startup.pl |
37 | |
38 | # load all tables |
39 | use DBIx::Class::Loader; |
40 | my $loader = DBIx::Class::Loader->new( |
41 | dsn => "dbi:mysql:dbname", |
42 | user => "root", |
43 | password => "", |
44 | namespace => "Data", |
45 | ); |
46 | |
47 | in your web application. |
48 | |
49 | use strict; |
50 | |
51 | # you can use Data::Film directly |
52 | my $film = Data::Film->retrieve($id); |
53 | |
54 | |
55 | =head1 DESCRIPTION |
56 | |
57 | DBIx::Class::Loader automate the definition of DBIx::Class sub-classes by |
58 | scanning table schemas and setting up columns and primary keys. |
59 | |
60 | Class names are defined by table names and the namespace option, which is |
61 | required. |
62 | |
63 | +---------+-----------+--------------+ |
64 | | table | namespace | class | |
65 | +---------+-----------+--------------+ |
66 | | foo | Data | Data::Foo | |
67 | | foo_bar | MyDB | MyDB::FooBar | |
68 | +---------+-----------+--------------+ |
69 | |
70 | DBIx::Class::Loader supports MySQL, Postgres, SQLite and DB2. See |
71 | L<DBIx::Class::Loader::Generic> for more, and L<DBIx::Class::Loader::Writing> |
72 | for notes on writing your own db-specific subclass for an unsupported db. |
73 | |
74 | L<Class::DBI::Loader> and L<Class::DBI> are now obsolete, use L<DBIx::Class> and this module instead. ;) |
75 | |
76 | =cut |
77 | |
78 | =head1 METHODS |
79 | |
80 | =head2 new |
81 | |
82 | Example in Synopsis above demonstrates the available arguments. For |
83 | detailed information on the arguments, see the |
84 | L<DBIx::Class::Loader::Generic> documentation. |
85 | |
86 | =cut |
87 | |
88 | sub new { |
89 | my ( $class, %args ) = @_; |
af6c2665 |
90 | |
91 | foreach (qw/namespace dsn/) { |
92 | die qq/Argument $_ is required/ if ! $args{$_}; |
93 | } |
94 | |
95 | $args{namespace} =~ s/(.*)::$/$1/; |
96 | |
a78e3fed |
97 | my $dsn = $args{dsn}; |
98 | my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i; |
99 | $driver = 'SQLite' if $driver eq 'SQLite2'; |
100 | my $impl = "DBIx::Class::Loader::" . $driver; |
af6c2665 |
101 | |
a78e3fed |
102 | $impl->require or |
103 | die qq/Couldn't require loader class "$impl", "$UNIVERSAL::require::ERROR"/; |
af6c2665 |
104 | |
a78e3fed |
105 | return $impl->new(%args); |
106 | } |
107 | |
108 | =head1 AUTHOR |
109 | |
110 | Sebastian Riedel, C<sri@oook.de> |
111 | |
112 | Based upon the work of IKEBE Tomohiro |
113 | |
114 | =head1 THANK YOU |
115 | |
116 | Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton, |
117 | Randal Schwartz, Simon Flack and all the others who've helped. |
118 | |
119 | =head1 LICENSE |
120 | |
121 | This library is free software; you can redistribute it and/or modify it under |
122 | the same terms as Perl itself. |
123 | |
124 | =head1 SEE ALSO |
125 | |
126 | L<DBIx::Class> |
127 | |
128 | =cut |
129 | |
130 | 1; |