Gebtag.pl

From LUGUM

Jump to: navigation, search

Datenbank


CREATE DATABASE `gebtag` DEFAULT CHARACTER SET latin1 COLLATE latin1_german1_ci;
USE `gebtag`;
 

CREATE TABLE `gebtag` (
  `id` int(10) NOT NULL auto_increment,
  `name` varchar(50) character set latin1 collate latin1_german1_ci NOT NULL,
  `gebtag` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

Script

#!/usr/bin/perl


=head1 NAME

gebtag.pl - Script zur Erinnerung an Geburtstage inkl Administration der Datenbank 

=head1 SYNTAX

gebtag.pl [-lh] [-a Name -d Datum(YYYY-MM-DD)]  [-u Name -d Datum(YYYY-MM-DD)] 
                [--del Name] [--help]

=cut

sub usage{
print <<EOF;

Usage: $0 [-lh] [-a Name -d Datum(YYYY-MM-DD)]  [-u Name -d Datum(YYYY-MM-DD)] 
                [--del Name] [--help]

Für genaueres siehe "perldoc $0"
EOF
exit 0;
}

=head1 OPTIONEN

=over 4

=item -l 

Listet alle Einträge in der Datenbank

=item -h | --help

Zeigt eine Kurze Hilfe an 

=item -a 

Übergibt den Namen des Geburtstagskindes bei "Vorname Nachname" bitte in Anführungszeichen setzen

=item -d 

Übergibt das Geburtsdatum im Datenbankformat YYYY-MM-DD

=item --del 

Löscht den Eintrag aus der Datenbank anhand des angegebenen Namens des Geburtstagkindes

=item -u 

Aktualisiert das Geburtsdatum anhand des Angegebenen Namens des Geburtstagkindes

=head1 AUTHOR

Andreas Kraus <lug@ak1301.de>

=head1 COPYRIGHT

Copyright (C) 2006 Andreas Kraus <lug@ak1301.de>

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

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
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


=cut

use strict;
use warnings;
use Getopt::Long;
use DBI;
use Date::Format;

my $dbh = DBI->connect( "DBI:mysql:gebtag","gebtag","gebtag" ) || die "Verbindung ging nicht $DBI::errstr";
my ($list,$name,$datum, $del, $update, $help,);
my $jahr = time2str("%Y", time);
GetOptions( 'l' => \$list,
            'a=s' => \$name,
            'd=s' => \$datum,
            'del=s'=> \$del,
            'u=s' => \$update,
            'help|h' => \$help,); 

usage if $help;

   if ( $list )   { geburtstagler() ;}
elsif ( $name )   { add()           ;} 
elsif ( $del )    { del()           ;}
elsif ( $update ) { update()        ;}
else              { geburtstagler() ;}




sub add{
    if (!$name || !$datum ){ die "Datum oder Name fehlen" }; 
    my $sth = $dbh->prepare(qq{ insert into gebtag values ('','$name','$datum') }); 
    $sth->execute(); 
    $sth->finish();
}

sub del{
    my $sth = $dbh->prepare(qq{ delete from gebtag where name='$del' limit 1 }); 
    $sth->execute(); 
    $sth->finish();
}

sub update{
    if (!$update || !$datum ){ die "Datum oder Name fehlen" }; 
    my $sth = $dbh->prepare(qq{ update gebtag set gebtag='$datum' where name='$update' }); 
    $sth->execute(); 
    $sth->finish();
}

sub geburtstagler {
    my $sth = $dbh->prepare(qq{ SELECT name, gebtag FROM gebtag }); 
    $sth->execute(); 

    my %vorschau = (0 => 'heute',
                    1 => 'morgen', 
                    2 => 'uebermorgen', 
                    3 => 'in 3 Tagen', 
                    4 => 'in 4 Tagen', 
                    5 => 'in 5 Tagen', 
                    6 => 'in 6 Tagen', 
                    7 => 'in einer Woche',);

    while (my @gebtag = $sth->fetchrow_array() ) {
        my @tage = split(/-/, $gebtag[1]);
        my $gebtag_datum = $tage[1]."-".$tage[2];
        my $geb_jahr = $tage[0];
        if ($list){
        my $alter = $jahr - $geb_jahr ;
        print "$gebtag[0] \t hat am  $tage[2].$tage[1].$tage[0] geburtstag und wird/wurde dieses Jahr $alter Jahre alt\n" ;
        }
        else{
            foreach my $key (keys %vorschau) {
                my $heute = time2str("%m-%d", (time + ($key * 86400 ))); # 86400 = Anzahl der sec an einem TAG
                if ($heute eq $gebtag_datum){
                    my $alter = $jahr - $geb_jahr ;
                    print "$gebtag[0] wird $vorschau{$key} $alter Jahre alt\n" ; 
                }
            }
        }
    }
    $sth->finish();
}


Persönliche Werkzeuge