package DBI::LZE;
use strict;
use warnings;
no warnings 'redefine';
use vars qw( $dbh $dsn $DefaultClass $settings @EXPORT_OK @ISA %functions $style $right $install);
$DefaultClass = 'DBI::LZE' unless defined $DBI::LZE::DefaultClass;
@DBI::LZE::EXPORT_OK = qw( useexecute quote void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute tableLength tableExists initDB $dsn $dbh);
%DBI::LZE::EXPORT_TAGS = (
'all' => [qw( useexecute quote void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute tableLength tableExists initDB)],
'dynamic' => [qw( useexecute void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute)],
'independent' => [qw(tableLength tableExists initDB useexecute void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute)],
);
$DBI::LZE::VERSION = '0.2.1';
require Exporter;
use DBI;
@DBI::LZE::ISA = qw( Exporter DBI);
$install = 0;
=head1 NAME
DBI::LZE
=head1 SYNOPSIS
FO Syntax
use DBI::LZE qw(:all);
my $dbh = initDB({name => 'LZE',host => 'localhost',user => 'root',password =>'',style=> 'Crystal'});
OO Syntax
use DBI::LZE;
my $database = new DBI::LZE(
{
name =>'LZE',
host => 'localhost',
user => 'root',
password =>'',
style=> 'Crystal'
}
);
my %execute = (
title => 'showTables',
description => 'description',
sql => "show tables",
return => "fetch_array",
);
$database->addexecute(\%execute);
$database->showTables();
=head2 Export Tags
:all execute useexecute quote void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute tableLength tableExists initDB
:dynamic execute useexecute void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute
independent: tableLength tableExists initDB :dynamic
=head1 DESCRIPTION
DBI::LZE
=head1 BUGS & LIMITATIONS
you can`t use the dynamic statements under mod_perl.
=head2 new()
my $database = new DBI::LZE(optional \%initializer);
see initDB()
=cut
sub new {
my ($class, @initializer) = @_;
my $self = {};
bless $self, ref $class || $class || $DefaultClass;
$self->initDB(@initializer) if(@initializer);
return $self;
}
=head2 initDB()
my $dbh = initDB({name => 'LZE',host => 'localhost',user => 'root',password =>'',style=> 'Crystal'});
=cut
sub initDB {
my ($self, @p) = getSelf(@_);
my $hash = $p[0];
my $database = defined $hash->{name} ? $hash->{name} : 'LZE';
my $host = defined $hash->{host} ? $hash->{host} : 'localhost';
my $user = defined $hash->{user} ? $hash->{user} : 'root';
my $pass = defined $hash->{password} ? $hash->{password} : '';
$style = defined $hash->{style} ? $hash->{style} : 'Crystal';
$dsn = "DBI:mysql:database=$database;host=$host";
$dbh = DBI::LZE->connect($dsn, $user, $pass, {RaiseError => 0, PrintError => 0, AutoCommit => 1,}) or warn "DBI::LZE::errs";
unless ($install) {
my @q = $self->fetch_array("select title from querys");
$functions{$_} = $_ foreach (@q);
}
return $dbh;
}
=head1 independent functions
=head2 tableExists()
$bool = $database->tableExists($table);
=cut
sub tableExists {
my ($self, @p) = getSelf(@_);
my $table = $p[0];
my $db_clause = "";
($db_clause, $table) = (" FROM $1", $2) if $table =~ /(.*)\.(.*)/;
return ($dbh->selectrow_array("SHOW TABLES $db_clause LIKE '$table'"));
}
=head2 tableLength
$length = $database->tableLength($table);
=cut
sub tableLength {
my ($self, @p) = getSelf(@_);
my $table = $p[0];
my $sql = "select count(*) from `$table`";
if($self->tableExists($p[0])) {
if(defined $p[1]) {
$sql = "select count(*) from `$table`";
}
my $sth = $dbh->prepare($sql) or warn $dbh->errstr;
$sth->execute() or warn $dbh->errstr;
my $length = $sth->fetchrow_array;
$sth->finish();
return $length;
} else {
return 0;
}
}
=head1 dynamic statements
=head2 addexecute()
add sql statments to yourdatabase for later use witdh useexecute();
my %execute = (
title => 'showTables',
description => 'description',
sql => "show tables",
return => "fetch_array",
);
$database->addexecute(\%execute);
print join '<br/>' ,$database->showTables();
Fo Syntax:
print join '<br/>' , useexecute('showTables');
=cut
sub addexecute {
my ($self, @p) = getSelf(@_);
my $hash = $p[0];
my $title = ((defined $hash->{title})) ? $hash->{title} : 0;
my $sql = $hash->{sql} if((defined $hash->{sql}));
my $description = $hash->{description} if(defined $hash->{description});
my $return = $hash->{'return'} if(defined $hash->{'return'});
unless ($functions{$title}) {
my $sql_addexecute = qq/INSERT INTO querys(`title`,`sql`,`description`,`return`) VALUES(?,?,?,?);/;
my $sth = $dbh->prepare($sql_addexecute);
$sth->execute($title, $sql, $description, $return) or warn $dbh->errstr;
$sth->finish();
$self->updateModules();
} else {
return 0;
}
}
=head2 editexecute
my %hash = (
title => 'Titel',
newTitle => 'New Titel',
description => 'querys Abfragen',
sql => "sql statement",
return => 'fetch_hashref', #subname
);
editexecute(\%hash);
=cut
sub editexecute {
my ($self, @p) = getSelf(@_);
my $hash = $p[0];
my $title = ((defined $hash->{title})) ? $hash->{title} : 0;
my $newTitle = ((defined $hash->{newTitle})) ? $hash->{newTitle} : $title;
my $sql = $hash->{sql} if((defined $hash->{sql}));
my $description = $hash->{description} if(defined $hash->{description});
my $return = (defined $hash->{'return'}) ? $hash->{'return'} : 'array';
if($functions{$title}) {
my $sql_edit = qq(update querys set title = ?, sql=? ,description=?,return=? where title = ? );
my $sth = $dbh->prepare($sql_edit);
$sth->execute($newTitle, $sql, $description, $return, $title) or warn $dbh->errstr;
$sth->finish();
} else {
return 0;
}
}
=head2 useexecute()
useexecute($title, @parameter);
=cut
sub useexecute {
my ($self, @p) = getSelf(@_);
my $title = shift(@p);
my $sql = "select `sql`,`return` from querys where `title` = ?";
my $sth = $dbh->prepare($sql);
$sth->execute($title) or warn $dbh->errstr;
my ($sqlexec, $return) = $sth->fetchrow_array();
$sth->finish();
return eval(" \$self->$return(\$sqlexec,\@p)");
}
=head2 deleteexecute()
deleteexecute($title);
=cut
sub deleteexecute {
my ($self, @p) = getSelf(@_);
my $id = $p[0];
my $sql_delete = "DELETE FROM querys Where title = ?";
my $sth = $dbh->prepare($sql_delete);
$sth->execute($id) or warn $dbh->errstr;
$sth->finish();
}
=head2 fetch_array()
@AoA = $database->fetch_array($sql);
=cut
sub fetch_array {
my ($self, @p) = getSelf(@_);
my $sql = shift @p;
my @r;
eval('
my $sth = $dbh->prepare($sql);
if(defined $p[0]) {
$sth->execute(@p) or warn $dbh->errstr;
}else {
$sth->execute() or warn $dbh->errstr;
}
while(my @comms = $sth->fetchrow_array()) {
push(@r, @comms);
}
$sth->finish();');
@r = $@ if $@;
return @r;
}