communigate provisioning phase 2: Domain:Account Defaults:Settings: RulesAllowed...
[freeside.git] / FS / FS / h_Common.pm
1 package FS::h_Common;
2
3 use strict;
4 use FS::Record qw(dbdef);
5 use Carp qw(confess);
6
7 =head1 NAME
8
9 FS::h_Common - History table "mixin" common base class
10
11 =head1 SYNOPSIS
12
13 package FS::h_tablename;
14 @ISA = qw( FS::h_Common FS::tablename ); 
15
16 sub table { 'h_table_name'; }
17
18 sub insert { return "can't insert history records manually"; }
19 sub delete { return "can't delete history records"; }
20 sub replace { return "can't modify history records"; }
21
22 =head1 DESCRIPTION
23
24 FS::h_Common is intended as a "mixin" base class for history table classes to
25 inherit from.
26
27 =head1 METHODS
28
29 =over 4
30
31 =item sql_h_search END_TIMESTAMP [ START_TIMESTAMP ] 
32
33 Returns an a list consisting of the "SELECT", "EXTRA_SQL", SQL fragments, a
34 placeholder for "CACHE_OBJ" and an "AS" SQL fragment, to search for the
35 appropriate history records created before END_TIMESTAMP and (optionally) not
36 deleted before START_TIMESTAMP.
37
38 =cut
39
40 sub sql_h_search {
41   my( $self, $end ) = ( shift, shift );
42
43   my $table = $self->table;
44   my $real_table = ($table =~ /^h_(.*)$/) ? $1 : $table;
45   my $pkey = dbdef->table($real_table)->primary_key
46     or die "can't (yet) search history table $real_table without a primary key";
47
48   unless ($end) {
49     confess 'Called sql_h_search without END_TIMESTAMP';
50   }
51
52   my( $notdeleted, $notdeleted_mr ) = ( '', '' );
53   if ( scalar(@_) && $_[0] ) {
54     $notdeleted =
55       "AND 0 = ( SELECT COUNT(*) FROM $table as notdel
56                    WHERE notdel.$pkey = maintable.$pkey
57                      AND notdel.history_action = 'delete'
58                      AND notdel.history_date > maintable.history_date
59                      AND notdel.history_date <= $_[0]
60                )";
61     $notdeleted_mr =
62       "AND 0 = ( SELECT COUNT(*) FROM $table as notdel_mr
63                    WHERE notdel_mr.$pkey = mostrecent.$pkey
64                      AND notdel_mr.history_action = 'delete'
65                      AND notdel_mr.history_date > mostrecent.history_date
66                      AND notdel_mr.history_date <= $_[0]
67                )";
68   }
69
70   (
71     #"DISTINCT ON ( $pkey ) *",
72     "*",
73
74     "AND history_date <= $end
75      AND (    history_action = 'insert'
76            OR history_action = 'replace_new'
77          )
78      $notdeleted
79      AND history_date = ( SELECT MAX(mostrecent.history_date)
80                             FROM $table AS mostrecent
81                             WHERE mostrecent.$pkey = maintable.$pkey
82                               AND mostrecent.history_date <= $end
83                               AND (    mostrecent.history_action = 'insert'
84                                     OR mostrecent.history_action = 'replace_new'
85                                   )
86                               $notdeleted_mr
87                         )
88
89      ORDER BY $pkey ASC",
90      #ORDER BY $pkey ASC, history_date DESC",
91
92      '',
93
94      'AS maintable',
95   );
96
97 }
98
99 =item sql_h_searchs END_TIMESTAMP [ START_TIMESTAMP ] 
100
101 Like sql_h_search, but limited to the single most recent record (before
102 END_TIMESTAMP)
103
104 =cut
105
106 sub sql_h_searchs {
107   my $self = shift;
108   my($select, $where, $cacheobj, $as) = $self->sql_h_search(@_);
109   $where .= ' LIMIT 1';
110   ($select, $where, $cacheobj, $as);
111 }
112
113 =back
114
115 =head1 BUGS
116
117 =head1 SEE ALSO
118
119 L<FS::Record>, schema.html from the base documentation
120
121 =cut
122
123 1;
124