first functional commit of non-subclassed-style Schema::Loader
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader.pm
1 package DBIx::Class::Schema::Loader;
2
3 use strict;
4 use UNIVERSAL::require;
5
6 our $VERSION = '0.01';
7
8 =head1 NAME
9
10 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
11
12 =head1 SYNOPSIS
13
14   use DBIx::Class::Schema::Loader;
15
16   my $loader = DBIx::Class::Schema::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   );
30
31   my $conn = $loader->connection($dsn, $user, $password); #
32   my $conn = $loader->connection(); # uses same dsn as ->new();
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 $conn = $loader->connection();
53   my $film_moniker = $loader->moniker('film');
54   my $a_film = $conn->resultset($film_moniker)->find($id);
55
56 =head1 DESCRIPTION
57
58 DBIx::Class::Schema::Loader automate the definition of a
59 DBIx::Class::Schema by scanning table schemas and setting up
60 columns and primary keys.
61
62 DBIx::Class::Schema::Loader supports MySQL, Postgres, SQLite and DB2.  See
63 L<DBIx::Class::Schema::Loader::Generic> for more, and
64 L<DBIx::Class::Schema::Loader::Writing> for notes on writing your own
65 db-specific subclass for an unsupported db.
66
67 L<Class::DBI::Loader>, L<Class::DBI>, and L<DBIx::Class::Loader> are now
68 obsolete, use L<DBIx::Class> and this module instead. ;)
69
70 =cut
71
72 =head1 METHODS
73
74 =head2 new
75
76 Example in Synopsis above demonstrates the available arguments.  For
77 detailed information on the arguments, see the
78 L<DBIx::Class::Schema::Loader::Generic> documentation.
79
80 =cut
81
82 sub new {
83     my ( $class, %args ) = @_;
84
85     foreach (qw/namespace dsn/) {
86        die qq/Argument $_ is required/ if ! $args{$_};
87     }
88
89     $args{namespace} =~ s/(.*)::$/$1/;
90
91     my $dsn = $args{dsn};
92     my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i;
93     $driver = 'SQLite' if $driver eq 'SQLite2';
94     my $impl = "DBIx::Class::Schema::Loader::" . $driver;
95
96     $impl->require or
97     die qq/Couldn't require loader class "$impl", "$UNIVERSAL::require::ERROR"/;
98
99     return $impl->new(%args);
100 }
101
102 =head1 AUTHOR
103
104 Sebastian Riedel, C<sri@oook.de>
105
106 Based upon the work of IKEBE Tomohiro
107
108 =head1 THANK YOU
109
110 Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
111 Randal Schwartz, Simon Flack and all the others who've helped.
112
113 =head1 LICENSE
114
115 This library is free software; you can redistribute it and/or modify it under
116 the same terms as Perl itself.
117
118 =head1 SEE ALSO
119
120 L<DBIx::Class>
121
122 =cut
123
124 1;