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