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