warnings go byebye
[dbsrgits/DBIx-Class-Journal.git] / lib / DBIx / Class / Schema / Journal.pm
1 package DBIx::Class::Schema::Journal;
2
3 use base qw/DBIx::Class/;
4
5 use Scalar::Util 'blessed';
6 use DBIx::Class::Schema::Journal::DB;
7
8 __PACKAGE__->mk_classdata('journal_storage_type');
9 __PACKAGE__->mk_classdata('journal_connection');
10 __PACKAGE__->mk_classdata('journal_sources'); ## [ source names ]
11 __PACKAGE__->mk_classdata('journal_user'); ## [ class, field for user id ]
12 __PACKAGE__->mk_classdata('_journal_schema');
13
14 our $VERSION = '0.01';
15
16 sub throw_exception
17 {
18 }
19
20 sub exception_action
21 {
22     my $self = shift;
23 #    print STDERR Carp::longmess;
24     
25     $self->next::method(@_);
26 }
27
28 # sub load_classes
29 # {
30 #     my $class = shift;
31
32
33 #     $class->next::method(@_);
34     
35 # }
36
37 sub connection
38 {
39     my $self = shift;
40     $self->next::method(@_);
41
42 #   print STDERR join(":", $self->sources), "\n";
43
44     my $journal_schema;
45     if(!defined($self->journal_connection))
46     {
47         ## If no connection, use the same schema/storage etc as the user
48         DBIx::Class::Componentised->inject_base(ref $self, 'DBIx::Class::Schema::Journal::DB');
49           $journal_schema = $self;
50     }
51     else
52     {
53         $journal_schema = DBIx::Class::Schema::Journal::DB->connect(@{ $self->journal_connection });
54         if($self->journal_storage_type)
55         {
56             $journal_schema->storage_type($self->journal_storage_type);
57         }
58     }
59
60     ## get our own private version of the journaling sources
61    $self->_journal_schema($journal_schema->compose_namespace(blessed($self) . '::Journal'));
62
63     ## Create auditlog+history per table
64     my %j_sources = map { $_ => 1 } $self->journal_sources
65                                       ? @{$self->journal_sources}
66                                       : $self->sources;
67     foreach my $s_name ($self->sources)
68     {
69         next unless($j_sources{$s_name});
70         $self->create_journal_for($s_name);
71         $self->class($s_name)->load_components('Journal');
72 #        print STDERR "$s_name :", $self->class($s_name), "\n";
73     }
74
75     ## Set up relationship between changeset->user_id and this schema's user
76     if(!@{$self->journal_user})
77     {
78         warn "No Journal User set!";
79         return;
80     }
81
82     $self->_journal_schema->deploy();
83     $self->_journal_schema->class('ChangeSet')->belongs_to('user', @{$self->journal_user});
84     $self->_journal_schema->storage->disconnect();
85 }
86
87 sub get_audit_log_class_name
88 {
89     my ($self, $sourcename) = @_;
90
91     return blessed($self->_journal_schema) . "::${sourcename}AuditLog";
92 }
93
94 sub get_audit_history_class_name
95 {
96     my ($self, $sourcename) = @_;
97
98     return blessed($self->_journal_schema) . "::${sourcename}AuditHistory";
99 }
100
101 sub create_journal_for
102 {
103     my ($self, $s_name) = @_;
104
105     my $source = $self->source($s_name);
106     my $newclass = $self->get_audit_log_class_name($s_name);
107     DBIx::Class::Componentised->inject_base($newclass, 'DBIx::Class::Schema::Journal::DB::AuditLog');
108     $newclass->table(lc($s_name) . "_audit_log");
109     $self->_journal_schema->register_class("${s_name}AuditLog", $newclass);
110                            
111
112     my $histclass = $self->get_audit_history_class_name($s_name);
113     DBIx::Class::Componentised->inject_base($histclass, 'DBIx::Class::Schema::Journal::DB::AuditHistory');
114     $histclass->table(lc($s_name) . "_audit_history");
115 #    $histclass->result_source_instance->name(lc($s_name) . "_audit_hisory");
116     $histclass->add_columns(
117                             map { $_ => $source->column_info($_) } $source->columns
118                            );
119                            
120     $self->_journal_schema->register_class("${s_name}AuditHistory", $histclass);
121 }
122
123 sub txn_do
124 {
125     my ($self, $code) = @_;
126
127     ## Create a new changeset, then run $code as a transaction
128     my $cs = $self->_journal_schema->resultset('ChangeSet');
129
130     $self->txn_begin;
131     my $changeset = $cs->create({ ( $self->_journal_schema->current_user() ? ( user_id => $self->_journal_schema->current_user()) : () ),
132 #        user_id => $self->_journal_schema->current_user(),
133         session_id => $self->_journal_schema->current_session(),
134     });
135     $self->_journal_schema->current_changeset($changeset->ID);
136
137     $self->next::method($code);
138 }
139
140 1;