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