Dump a call trace if something calls FS::h_Common::sql_h_search without END_TIMESTAMP.
[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" and "EXTRA_SQL" SQL fragments to
34 search for the appropriate history records created before END_TIMESTAMP
35 and (optionally) not cancelled before START_TIMESTAMP.
36
37 =cut
38
39 sub sql_h_search {
40   my( $self, $end ) = ( shift, shift );
41
42   my $table = $self->table;
43   my $pkey = dbdef->table($table)->primary_key
44     or die "can't (yet) search history table $table without a primary key";
45
46   unless ($end) {
47     confess 'Called sql_h_search without END_TIMESTAMP';
48   }
49
50   my $notcancelled = '';
51   if ( scalar(@_) && $_[0] ) {
52     $notcancelled = "AND 0 = ( SELECT COUNT(*) FROM $table as notdel
53                                 WHERE notdel.$pkey = maintable.$pkey
54                                 AND notdel.history_action = 'delete'
55                                 AND notdel.history_date > maintable.history_date
56                                 AND notdel.history_date <= $_[0]
57                              )";
58   }
59
60   (
61     "DISTINCT ON ( $pkey ) *",
62
63     "AND history_date <= $end
64      AND (    history_action = 'insert'
65            OR history_action = 'replace_new'
66          )
67      $notcancelled
68      ORDER BY $pkey ASC, history_date DESC",
69
70      '',
71
72      'AS maintable',
73   );
74
75 }
76
77 =item sql_h_searchs END_TIMESTAMP [ START_TIMESTAMP ] 
78
79 Like sql_h_search, but limited to the single most recent record (before
80 END_TIMESTAMP)
81
82 =cut
83
84 sub sql_h_searchs {
85   my $self = shift;
86   my($select, $where, $cacheobj, $as) = $self->sql_h_search(@_);
87   $where .= ' LIMIT 1';
88   ($select, $where, $cacheobj, $as);
89 }
90
91 =back
92
93 =head1 BUGS
94
95 =head1 SEE ALSO
96
97 L<FS::Record>, schema.html from the base documentation
98
99 =cut
100
101 1;
102