package MySQL::easy;

=head1 NAME 

mysql_easy - eine kleine vereinfachung und auch Hilfe der DBI Schnittstelle zu mysql

=head1 SYNOPSIS

=head2 Functions

=head3 new

my $object = mysql_easy->new($user,$passwd,$db,$host);

=cut

sub new($$$$) {
        my ($class, @login) = @_;
        my $self = [];

        use DBI;

        my $user = $login[0];
        my $passwd = $login[1];
        my $db = $login[2];
        my $host = $login[3];

        my $driver = "DBI:mysql:$db:$host";
        my $dbh = DBI->connect($driver,$user,$passwd) || die $DBI::errstr;
        push @{$self}, $dbh;

        return bless $self, $class;
}

=head3 query

$object->query($query);

=cut

sub query {
        my $self = shift;
        my $dbh = $self->[0];
        my $query = shift;
        my $sth = $dbh->prepare($query) || die $DBI::errstr;
        $sth->execute() || die $DBI::errstr;
}

=head3 get_values

my $AoH = $object->get_values($query);

for my $row (@$AoH) {

        for my $k (keys %$row) {
        
        print "$k ", $row->{$k}, "\n";
        
        }
}


=cut

sub get_values {
        my $self = shift;
        my $query = shift;
        my $field = shift;
        my $dbh = $self->[0];
        my $sth = $self->[0]->prepare($query);
        $sth->execute();
        my $result = [];
        my $row;
        push @$result, $row while ($row = $sth->fetchrow_hashref());
        return $result;
}

=head3 get_cols

my @cols = $object->get_cols("table");

=cut

sub get_cols {
        my $self = shift;
        my $table = shift;
        my $query = "SELECT * FROM $table";
        my $dbh = $self->[0];
        my $sth = $self->[0]->prepare($query);
        $sth->execute();
        my $result = [];
        my $row;
        my @cols;
        push @$result, $row while ($row = $sth->fetchrow_hashref());
        for my $i (@$result) {
                for my $k (keys %$i) {
                        push @cols, $k;
                }
        }
        my %col = ();
        my @cols = grep { ! $seen{$_} ++ } @cols;
        return @cols;
}

=head1 BUGS

I thank everybody, who reports me bugs in this module !

=head1 AUTHOR

Ingo Walz, [ ingo@j-jayz-z.de ]

=head1 COPYRIGHT

Copyright (c) 2005 Ingo Walz. All rights reserved. This program is free
software; you can redistribute it and/or modify it under the same terms
as Perl itself.

=cut

1;

