5238c78a9cf3bf8802bb7dc756887f68d8cd260b
[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
6 use vars qw($VERSION @ISA);
7 use UNIVERSAL::require;
8
9 # Always remember to do all digits for the version even if they're 0
10 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
11 # brain damage and presumably various other packaging systems too
12
13 $VERSION = '0.01000';
14
15 =head1 NAME
16
17 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
18
19 =head1 SYNOPSIS
20
21   package My::Schema;
22   use base qw/DBIx::Class::Schema::Loader/;
23
24   __PACKAGE__->load_from_connection(
25     dsn                     => "dbi:mysql:dbname",
26     user                    => "root",
27     password                => "",
28     namespace               => "My",
29     additional_classes      => [qw/DBIx::Class::Foo/],
30     additional_base_classes => [qw/My::Stuff/],
31     left_base_classes       => [qw/DBIx::Class::Bar/],
32     constraint              => '^foo.*',
33     relationships           => 1,
34     options                 => { AutoCommit => 1 }, 
35     inflect                 => { child => 'children' },
36     debug                   => 1,
37   );
38
39   # in seperate application code ...
40
41   use My::Schema;
42
43   my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
44   # -or-
45   my $schema1 = My::Schema->connect();
46   # ^^ defaults to dsn/user/pass from load_from_connection()
47
48 =head1 DESCRIPTION
49
50 DBIx::Class::Schema::Loader automate the definition of a
51 DBIx::Class::Schema by scanning table schemas and setting up
52 columns and primary keys.
53
54 DBIx::Class::Schema::Loader supports MySQL, Postgres, SQLite and DB2.  See
55 L<DBIx::Class::Schema::Loader::Generic> for more, and
56 L<DBIx::Class::Schema::Loader::Writing> for notes on writing your own
57 db-specific subclass for an unsupported db.
58
59 L<Class::DBI::Loader>, L<Class::DBI>, and L<DBIx::Class::Loader> are now
60 obsolete, use L<DBIx::Class> and this module instead. ;)
61
62 =cut
63
64 =head1 METHODS
65
66 =head2 new
67
68 Example in Synopsis above demonstrates the available arguments.  For
69 detailed information on the arguments, see the
70 L<DBIx::Class::Schema::Loader::Generic> documentation.
71
72 =cut
73
74 sub load_from_connection {
75     my ( $class, %args ) = @_;
76
77     foreach (qw/namespace dsn/) {
78        die qq/Argument $_ is required/ if ! $args{$_};
79     }
80
81     $args{namespace} =~ s/(.*)::$/$1/;
82
83     my $dsn = $args{dsn};
84     my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i;
85     $driver = 'SQLite' if $driver eq 'SQLite2';
86     my $impl = "DBIx::Class::Schema::Loader::" . $driver;
87
88     $impl->require or
89     die qq/Couldn't require loader class "$impl", "$UNIVERSAL::require::ERROR"/;
90
91     push(@ISA, $impl);
92     $class->_load_from_connection(%args);
93 }
94
95 =head1 AUTHOR
96
97 Sebastian Riedel, C<sri@oook.de>
98
99 Based upon the work of IKEBE Tomohiro
100
101 =head1 THANK YOU
102
103 Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
104 Randal Schwartz, Simon Flack and all the others who've helped.
105
106 =head1 LICENSE
107
108 This library is free software; you can redistribute it and/or modify it under
109 the same terms as Perl itself.
110
111 =head1 SEE ALSO
112
113 L<DBIx::Class>
114
115 =cut
116
117 1;