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