Add automatic naming of unique constraints
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / DB.pm
CommitLineData
ea2e61bf 1package DBIx::Class::DB;
2
bf5ecff9 3use strict;
4use warnings;
5
1edd1722 6use base qw/DBIx::Class/;
7fb16f1a 7use DBIx::Class::Schema;
8b445e33 8use DBIx::Class::Storage::DBI;
11b78bd6 9use DBIx::Class::ClassResolver::PassThrough;
604d9f38 10use DBI;
ea2e61bf 11
80c90f5d 12__PACKAGE__->load_components(qw/ResultSetProxy/);
2d679367 13
bf5ecff9 14{
15 no warnings 'once';
16 *dbi_commit = \&txn_commit;
17 *dbi_rollback = \&txn_rollback;
18}
8091aa91 19
7fb16f1a 20sub storage { shift->schema_instance(@_)->storage; }
2d679367 21
f6858c33 22sub resultset_instance {
1225fc4d 23 my $class = ref $_[0] || $_[0];
8c49f629 24 my $source = $class->result_source_instance;
7fb16f1a 25 if ($source->result_class ne $class) {
26 $source = $source->new($source);
27 $source->result_class($class);
28 }
29 return $source->resultset;
88cb6a1d 30}
31
75d07914 32=head1 NAME
34d52be2 33
1c81f831 34DBIx::Class::DB - (DEPRECATED) classdata schema component
34d52be2 35
36=head1 SYNOPSIS
37
8b445e33 38 package MyDB;
39
40 use base qw/DBIx::Class/;
503536d5 41 __PACKAGE__->load_components('DB');
8b445e33 42
43 __PACKAGE__->connection('dbi:...', 'user', 'pass', \%attrs);
44
45 package MyDB::MyTable;
46
47 use base qw/MyDB/;
bc0c9800 48 __PACKAGE__->load_components('Core'); # just load this in MyDB if it will
49 # always be there
4a9d7cae 50
51 ...
8b445e33 52
34d52be2 53=head1 DESCRIPTION
54
66d9ef6b 55This class is designed to support the Class::DBI connection-as-classdata style
56for DBIx::Class. You are *strongly* recommended to use a DBIx::Class::Schema
1c81f831 57instead; DBIx::Class::DB will not undergo new development and will be moved
58to being a CDBICompat-only component before 1.0.
34d52be2 59
60=head1 METHODS
61
8091aa91 62=head2 storage
076652e8 63
8091aa91 64Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
076652e8 65
87c4e602 66=head2 class_resolver
67
68****DEPRECATED****
076652e8 69
75d07914 70Sets or gets the class to use for resolving a class. Defaults to
8091aa91 71L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
72it. See resolve_class below.
076652e8 73
34d52be2 74=cut
75
11b78bd6 76__PACKAGE__->mk_classdata('class_resolver' =>
181a28f4 77 'DBIx::Class::ClassResolver::PassThrough');
8fe001e1 78
8091aa91 79=head2 connection
39fe0e65 80
81 __PACKAGE__->connection($dsn, $user, $pass, $attrs);
82
83Specifies the arguments that will be passed to DBI->connect(...) to
84instantiate the class dbh when required.
85
86=cut
87
8fe001e1 88sub connection {
89 my ($class, @info) = @_;
66d9ef6b 90 $class->setup_schema_instance unless $class->can('schema_instance');
91 $class->schema_instance->connection(@info);
92}
93
94=head2 setup_schema_instance
95
96Creates a class method ->schema_instance which contains a DBIx::Class::Schema;
97all class-method operations are proxies through to this object. If you don't
98call ->connection in your DBIx::Class::DB subclass at load time you *must*
99call ->setup_schema_instance in order for subclasses to find the schema and
100register themselves with it.
101
102=cut
103
104sub setup_schema_instance {
105 my $class = shift;
106 my $schema = bless({}, 'DBIx::Class::Schema');
7fb16f1a 107 $class->mk_classdata('schema_instance' => $schema);
ea2e61bf 108}
109
8091aa91 110=head2 txn_begin
39fe0e65 111
8091aa91 112Begins a transaction (does nothing if AutoCommit is off).
39fe0e65 113
114=cut
115
181a28f4 116sub txn_begin { shift->schema_instance->txn_begin(@_); }
a29644e1 117
8091aa91 118=head2 txn_commit
39fe0e65 119
8091aa91 120Commits the current transaction.
39fe0e65 121
8091aa91 122=cut
123
181a28f4 124sub txn_commit { shift->schema_instance->txn_commit(@_); }
8091aa91 125
126=head2 txn_rollback
127
128Rolls back the current transaction.
39fe0e65 129
130=cut
131
181a28f4 132sub txn_rollback { shift->schema_instance->txn_rollback(@_); }
133
134=head2 txn_do
135
136Executes a block of code transactionally. If this code reference
137throws an exception, the transaction is rolled back and the exception
bc0c9800 138is rethrown. See L<DBIx::Class::Schema/"txn_do"> for more details.
181a28f4 139
140=cut
141
142sub txn_do { shift->schema_instance->txn_do(@_); }
8b445e33 143
8452e496 144{
145 my $warn;
146
147 sub resolve_class {
148 warn "resolve_class deprecated as of 0.04999_02" unless $warn++;
149 return shift->class_resolver->class(@_);
150 }
7fb16f1a 151}
11b78bd6 152
ea2e61bf 1531;
34d52be2 154
34d52be2 155=head1 AUTHORS
156
daec44b8 157Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 158
159=head1 LICENSE
160
161You may distribute this code under the same terms as Perl itself.
162
163=cut
164