silence postgres create table jabber
[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;
3385ac62 5use Carp;
a78e3fed 6use UNIVERSAL::require;
7
8a6b44ef 8use base qw/DBIx::Class::Schema/;
9use base qw/Class::Data::Accessor/;
10
11__PACKAGE__->mk_classaccessor('loader');
12
3980d69c 13use vars qw($VERSION);
14
a4a19f3c 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
a4a19f3c 18$VERSION = '0.01000';
a78e3fed 19
20=head1 NAME
21
18fca96a 22DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
a78e3fed 23
24=head1 SYNOPSIS
25
a4a19f3c 26 package My::Schema;
27 use base qw/DBIx::Class::Schema::Loader/;
a78e3fed 28
a4a19f3c 29 __PACKAGE__->load_from_connection(
a78e3fed 30 dsn => "dbi:mysql:dbname",
31 user => "root",
32 password => "",
a78e3fed 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 );
af6c2665 42
a4a19f3c 43 # in seperate application code ...
a78e3fed 44
a4a19f3c 45 use My::Schema;
a78e3fed 46
a4a19f3c 47 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
48 # -or-
fbd83464 49 my $schema1 = "My::Schema";
a4a19f3c 50 # ^^ defaults to dsn/user/pass from load_from_connection()
a78e3fed 51
3980d69c 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
a78e3fed 67=head1 DESCRIPTION
68
fbd83464 69DBIx::Class::Schema::Loader automates the definition of a
18fca96a 70DBIx::Class::Schema by scanning table schemas and setting up
71columns and primary keys.
a78e3fed 72
18fca96a 73DBIx::Class::Schema::Loader supports MySQL, Postgres, SQLite and DB2. See
74L<DBIx::Class::Schema::Loader::Generic> for more, and
75L<DBIx::Class::Schema::Loader::Writing> for notes on writing your own
76db-specific subclass for an unsupported db.
a78e3fed 77
3385ac62 78This module requires DBIx::Class::Loader 0.5 or later, and obsoletes
79L<DBIx::Class::Loader> for L<DBIx::Class> version 0.5 and later.
a78e3fed 80
81=cut
82
83=head1 METHODS
84
fbd83464 85=head2 load_from_connection
a78e3fed 86
87Example in Synopsis above demonstrates the available arguments. For
88detailed information on the arguments, see the
18fca96a 89L<DBIx::Class::Schema::Loader::Generic> documentation.
a78e3fed 90
91=cut
92
a4a19f3c 93sub load_from_connection {
a78e3fed 94 my ( $class, %args ) = @_;
af6c2665 95
3385ac62 96 croak 'dsn argument is required' if ! $args{dsn};
a78e3fed 97 my $dsn = $args{dsn};
98 my ($driver) = $dsn =~ m/^dbi:(\w*?)(?:\((.*?)\))?:/i;
99 $driver = 'SQLite' if $driver eq 'SQLite2';
18fca96a 100 my $impl = "DBIx::Class::Schema::Loader::" . $driver;
af6c2665 101
a78e3fed 102 $impl->require or
3385ac62 103 croak qq/Couldn't require loader class "$impl",/ .
104 qq/"$UNIVERSAL::require::ERROR"/;
af6c2665 105
3980d69c 106 $args{schema} = $class;
107
108 $class->loader($impl->new(%args));
a78e3fed 109}
110
111=head1 AUTHOR
112
fbd83464 113Brandon Black, C<bblack@gmail.com>
114
8a6b44ef 115Based on L<DBIx::Class::Loader> by Sebastian Riedel
a78e3fed 116
117Based upon the work of IKEBE Tomohiro
118
119=head1 THANK YOU
120
121Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
122Randal Schwartz, Simon Flack and all the others who've helped.
123
124=head1 LICENSE
125
126This library is free software; you can redistribute it and/or modify it under
127the same terms as Perl itself.
128
129=head1 SEE ALSO
130
131L<DBIx::Class>
132
133=cut
134
1351;