#!/usr/bin/perl
# dbipg_clean_table
#    $Id: dbipg_clean_table,v 1.1.1.1 2002/12/26 04:37:47 nakahira Exp $
#    Last updated: 12/22/2002
#
# Copyright (C) 2002 The Nagoya University Consumers' Co-operative Association
#   Written by K.Nakahira
#
#  This program is licensed under the GNU GPL.
#  See the following URL for more details:
#    http://www.gnu.org/licenses/gpl.txt
#

use strict;
use File::Basename;
use Getopt::Long;

use DBIPgSystem::DB;
use DBIPgSystem::Passwd;
use DBSession;

my $VERSION = '1.1.01';

# 
my $opt = { interval => '3 days',
		  };
my $optlist = [ 'help!', 'interval=s' ];
my $init = { basename => basename($0),
		   };

sub usage {
  my $ver = "(version $VERSION)" if($VERSION);
print <<USAGE;
$init->{basename} $ver
Usage: $init->{basename} [ץ] basefile [filename]
ARGS:
  basefile     ե
  filename     Υե
Options:
  --interval   ޤǤδ (default: '$opt->{interval}')
USAGE
  exit;
}

{
  GetOptions($opt, @$optlist);
  &usage if(@ARGV != 1 and @ARGV != 2 or $opt->{help});

  # 
  my $basefile = shift;
  die "new: Cannot open $basefile'" unless(-f $basefile and -r $basefile);
  require $basefile;
  my $baseconf = &baseconf;

  my $file = shift;
  my $fh = \*STDOUT;
  open($fh, "> $file") or "Couldn't open $file: $!" if(defined $file);

  # إåν
  if(defined $file) {
	print $fh <<HEADER;
--
-- This file was made by $init->{basename}. Don\'t edit this file !
--\n
HEADER
  }

  my $dbi = DBIPgSystem::DB->new
	($baseconf->{conffile}, 'staff', 'root', test => 1, no_db => 1);
  my $session = DBSession->new
	("dbi:Pg:$dbi->{global}{db}{data_source}",
	 $dbi->{global}{db}{username}, $dbi->{global}{db}{passwd}, undef,
	 noconnect => 1);

  # å
  print $fh <<SESSION;
-- 郎 $opt->{interval} Υå
DELETE FROM dbsession WHERE ldate < now() - INTERVAL '$opt->{interval}';\n
SESSION

  # draftѥơ֥
  my $subsql;
  my $primary = $dbi->{global}{primary};
  foreach my $dbt (keys %{ $dbi->{dbcol} }) {
	next if($dbt eq $primary);
	$subsql .= "DELETE FROM ${dbt}_dft WHERE system_rid NOT IN\n"
	  . "\t( SELECT system_next_rid FROM ${primary}_dft );\n";
  }
  chomp($subsql);
  print $fh <<DRAFT;
-- $opt->{interval} ˺줿 draftѥơ֥Υ쥳ɤ
BEGIN;
DELETE FROM ${primary}_dft WHERE
	system_date < now() - INTERVAL '$opt->{interval}';
$subsql
COMMIT;\n
DRAFT

  my $dbname = $dbi->{global}{db}{data_source};
  $dbname =~ s/^(?:.*;)?dbname=([^;]+)(?:;.*)?$/$1/;
  print STDERR <<COMMENT;
You can remove the old records in the database dbsession and *_dft
using the following command:
  \$ psql -f $file -U $dbi->{global}{db}{username} $dbname\n
COMMENT

  close($fh) if(defined $file);
}
exit;

__END__

=for html
<div class="header">
<div class="bar">
<a href="../index.html">Top</a>
<a href="index.html">ޥ˥奢</a>
</div>
<h1>dbipg_clean_table</h1></div>

=for html
<div class="pod">

=head1 NAME

dbipg_clean_table - ådraftѥơ֥Υߤ

=head1 DESCRIPTION

  $ dbipg_clean_table basefile clean.sql
  $ psql -U pguser -f clean.sql dbname

ե뤫ɤ߹򸵤ˡ
ådraftѥơ֥פʥǡ뤿 SQL ʸϤ롣

=head1 SEE ALSO

F<DBIPgSystem>, F<DBIPgSystem::DB>

=head1 COPYRIGHT

Copyright (C) 2002 The Nagoya University Consumers' Co-operative Association

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the following URL for more details:
  http://www.gnu.org/licenses/gpl.txt

Written by Kenji Nakahira <nakahira@coop.nagoya-u.ac.jp>

=for html
</div>

=cut
