Moving towards instance-based schemas
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / DB.pm
1 package DBIx::Class::DB;
2
3 use base qw/DBIx::Class/;
4 use DBIx::Class::Storage::DBI;
5 use DBIx::Class::ClassResolver::PassThrough;
6 use DBI;
7
8 __PACKAGE__->load_components(qw/ResultSetInstance/);
9
10 *dbi_commit = \&txn_commit;
11 *dbi_rollback = \&txn_rollback;
12
13 sub storage { shift->storage_instance(@_); }
14
15 sub resultset_instance {
16   my $class = shift;
17   my $table = $class->table_instance->new($class->table_instance);
18   $table->schema($class);
19   $table->result_class($class);
20   return $table->resultset;
21 }
22
23 sub result_source {
24   my $class = shift;
25   my $table = $class->table_instance->new($class->table_instance);
26   $table->schema($class);
27   $table->result_class($class);
28   return $table;
29 }
30
31 =head1 NAME 
32
33 DBIx::Class::DB - Simple DBIx::Class Database connection by class inheritance
34
35 =head1 SYNOPSIS
36
37   package MyDB;
38
39   use base qw/DBIx::Class/;
40   __PACKAGE__->load_components('DB');
41
42   __PACKAGE__->connection('dbi:...', 'user', 'pass', \%attrs);
43
44   package MyDB::MyTable;
45
46   use base qw/MyDB/;
47   __PACKAGE__->load_components('Core'); # just load this in MyDB if it will always be there
48
49   ...
50
51 =head1 DESCRIPTION
52
53 This class provides a simple way of specifying a database connection.
54
55 =head1 METHODS
56
57 =head2 storage
58
59 Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
60
61 =head2 class_resolver
62
63 Sets or gets the class to use for resolving a class. Defaults to 
64 L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
65 it. See resolve_class below.
66
67 =cut
68
69 __PACKAGE__->mk_classdata('class_resolver' =>
70                             'DBIx::Class::ClassResolver::PassThrough');
71
72 =head2 connection
73
74   __PACKAGE__->connection($dsn, $user, $pass, $attrs);
75
76 Specifies the arguments that will be passed to DBI->connect(...) to
77 instantiate the class dbh when required.
78
79 =cut
80
81 sub connection {
82   my ($class, @info) = @_;
83   my $storage = DBIx::Class::Storage::DBI->new;
84   $storage->connect_info(\@info);
85   $class->mk_classdata('storage_instance' => $storage);
86 }
87
88 =head2 txn_begin
89
90 Begins a transaction (does nothing if AutoCommit is off).
91
92 =cut
93
94 sub txn_begin { $_[0]->storage->txn_begin }
95
96 =head2 txn_commit
97
98 Commits the current transaction.
99
100 =cut
101
102 sub txn_commit { $_[0]->storage->txn_commit }
103
104 =head2 txn_rollback
105
106 Rolls back the current transaction.
107
108 =cut
109
110 sub txn_rollback { $_[0]->storage->txn_rollback }
111
112 sub resolve_class { return shift->class_resolver->class(@_); }
113
114 1;
115
116 =head1 AUTHORS
117
118 Matt S. Trout <mst@shadowcatsystems.co.uk>
119
120 =head1 LICENSE
121
122 You may distribute this code under the same terms as Perl itself.
123
124 =cut
125