diff options
author | Mark Wells <mark@freeside.biz> | 2014-10-16 16:23:11 -0700 |
---|---|---|
committer | Mark Wells <mark@freeside.biz> | 2014-10-16 16:23:11 -0700 |
commit | 98ea15536afc6896cce08a41b877d6cb52444d14 (patch) | |
tree | 55dc39e6c6fddc530adb764e3d1f4558feb2532f /FS/FS/h_Common.pm | |
parent | 83f29f7300305134cb0c2e680ca7346927d4e9fe (diff) |
make package churn report actually show package churn, #7990
Diffstat (limited to 'FS/FS/h_Common.pm')
-rw-r--r-- | FS/FS/h_Common.pm | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/FS/FS/h_Common.pm b/FS/FS/h_Common.pm index ca13e1b..9b5ad09 100644 --- a/FS/FS/h_Common.pm +++ b/FS/FS/h_Common.pm @@ -110,6 +110,55 @@ sub sql_h_searchs { ($select, $where, $cacheobj, $as); } +=item sql_diff START_TIMESTAMP, END_TIMESTAMP[, WHERE] + +Returns a complete SQL statement to find all records that were changed +between START_TIMESTAMP and END_TIMESTAMP. This finds only replacements, +not new or deleted records. + +For each modified record, this will return I<one> row (not two rows as in +the history table) with the primary key of the record, "old_historynum" +(the historynum of the last modification before START_TIMESTAMP), and +"new_historynum" (the last modification before END_TIMESTAMP). Join these +back to the h_* table to retrieve the actual field values. + +Within the query, the last history records as of START and END are aliased +as "old" and "new"; you can append a WHERE clause to take advantage of this. + +=cut + +sub sql_diff { + my $class = shift; + my $table = $class->table; + my ($real_table) = ($table =~ /^h_(\w+)$/); + my $pkey = dbdef->table($real_table)->primary_key; + my @fields = "FS::$real_table"->fields; + + my ($sdate, $edate) = @_; + ($sdate, $edate) = ($edate, $sdate) if $edate < $sdate; + + my @select = ( + "old.$pkey", + 'old.historynum AS old_historynum', + 'new.historynum AS new_historynum', + ); + my $new = + "SELECT DISTINCT ON ($pkey) * FROM $table + WHERE history_action = 'replace_new' + AND history_date >= $sdate AND history_date < $edate + ORDER BY $pkey ASC, history_date DESC"; + my $old = + "SELECT DISTINCT ON ($pkey) * FROM $table + WHERE (history_action = 'replace_new' OR history_action = 'insert') + AND history_date < $sdate + ORDER BY $pkey ASC, history_date DESC"; + + my $from = "($new) AS new JOIN ($old) AS old USING ($pkey)"; + + return "SELECT ".join(',', @select)." FROM $from"; +} + + =back =head1 BUGS |