docs and reqs update for 0.01000 release
[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 use Carp;
6 use UNIVERSAL::require;
7
8 use base qw/DBIx::Class::Schema/;
9 use base qw/Class::Data::Accessor/;
10
11 __PACKAGE__->mk_classaccessor('loader');
12
13 use vars qw($VERSION);
14
15 # Always remember to do all digits for the version even if they're 0
16 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
17 # brain damage and presumably various other packaging systems too
18 $VERSION = '0.01000';
19
20 =head1 NAME
21
22 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
23
24 =head1 SYNOPSIS
25
26   package My::Schema;
27   use base qw/DBIx::Class::Schema::Loader/;
28
29   __PACKAGE__->load_from_connection(
30     dsn                     => "dbi:mysql:dbname",
31     user                    => "root",
32     password                => "",
33     additional_classes      => [qw/DBIx::Class::Foo/],
34     additional_base_classes => [qw/My::Stuff/],
35     left_base_classes       => [qw/DBIx::Class::Bar/],
36     constraint              => '^foo.*',
37     relationships           => 1,
38     options                 => { AutoCommit => 1 }, 
39     inflect                 => { child => 'children' },
40     debug                   => 1,
41   );
42
43   # in seperate application code ...
44
45   use My::Schema;
46
47   my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
48   # -or-
49   my $schema1 = "My::Schema";
50   # ^^ defaults to dsn/user/pass from load_from_connection()
51
52   # Get a list of the original (database) names of the tables that
53   #  were loaded
54   my @tables = $schema1->loader->tables;
55
56   # Get a hashref of table_name => 'TableName' table-to-moniker
57   #   mappings.
58   my $monikers = $schema1->loader->monikers;
59
60   # Get a hashref of table_name => 'My::Schema::TableName'
61   #   table-to-classname mappings.
62   my $classes = $schema1->loader->classes;
63
64   # Use the schema as per normal for L<DBIx::Class::Schema>
65   my $rs = $schema1->resultset($monikers->{table_table})->search(...);
66
67 =head1 DESCRIPTION
68
69 THIS IS A DEVELOPMENT RELEASE.  This is 0.01000, the first public
70 release.  Expect things to be broken in various ways.  Expect the
71 entire design to be fatally flawed.  Expect the interfaces to change if
72 it becomes neccessary.  It's mostly here for people to poke at it and
73 find the flaws in it.  0.02 will hopefully have some sanity when we get
74 there.
75
76 DBIx::Class::Schema::Loader automates the definition of a
77 DBIx::Class::Schema by scanning table schemas and setting up
78 columns and primary keys.
79
80 DBIx::Class::Schema::Loader supports MySQL, Postgres, SQLite and DB2.  See
81 L<DBIx::Class::Schema::Loader::Generic> for more, and
82 L<DBIx::Class::Schema::Loader::Writing> for notes on writing your own
83 db-specific subclass for an unsupported db.
84
85 This module requires DBIx::Class::Loader 0.5 or later, and obsoletes
86 L<DBIx::Class::Loader> for L<DBIx::Class> version 0.5 and later.
87
88 =cut
89
90 =head1 METHODS
91
92 =head2 load_from_connection
93
94 Example in Synopsis above demonstrates the available arguments.  For
95 detailed information on the arguments, see the
96 L<DBIx::Class::Schema::Loader::Generic> documentation.
97
98 =cut
99
100 sub load_from_connection {
101     my ( $class, %args ) = @_;
102
103     croak 'dsn argument is required' if ! $args{dsn};
104     my $dsn = $args{dsn};
105     my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i;
106     $driver = 'SQLite' if $driver eq 'SQLite2';
107     my $impl = "DBIx::Class::Schema::Loader::" . $driver;
108
109     $impl->require or
110       croak qq/Couldn't require loader class "$impl",/ .
111             qq/"$UNIVERSAL::require::ERROR"/;
112
113     $args{schema} = $class;
114
115     $class->loader($impl->new(%args));
116     $class->loader->load;
117 }
118
119 =head1 AUTHOR
120
121 Brandon Black, C<bblack@gmail.com>
122
123 Based on L<DBIx::Class::Loader> by Sebastian Riedel
124
125 Based upon the work of IKEBE Tomohiro
126
127 =head1 THANK YOU
128
129 Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
130 Randal Schwartz, Simon Flack and all the others who've helped.
131
132 =head1 LICENSE
133
134 This library is free software; you can redistribute it and/or modify it under
135 the same terms as Perl itself.
136
137 =head1 SEE ALSO
138
139 L<DBIx::Class>
140
141 =cut
142
143 1;