DBI::Library - DBI subclass providing a dynamic SQL Libary.


MySQL-Admin documentation Contained in the MySQL-Admin distribution.

Index


Code Index:

NAME

Top

DBI::Library - DBI subclass providing a dynamic SQL Libary.

SYNOPSIS

Top

FO Syntax

use DBI::Library qw(:all);

my $dbh = initDB({name => 'LZE',host => 'localhost',user => 'root',password =>'',style=> 'lze'});

OO Syntax

use DBI::Library;

        my ($database,$dbh) = new DBI::Library(

                {

                name =>'LZE',

                host => 'localhost',

                user => 'root',

                password =>'',

                style=> 'lze'

                }

        );

        my %execute  = (

                title => 'showTables',

                description => 'description',

                sql => 'show tables',

                return => 'fetch_array',

        );

        $database->addexecute(\%execute);

        $database->showTables();

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

DESCRIPTION

Top

DBI::Library is a DBI subclass providing a dynamic SQL Libary.

new()

        my $database = new DBI::Library();




        my ($database,$m_dbh) = new DBI::Library(

                                        {

                                        name => $db,

                                        host => $m_sHost,

                                        user => $m_sUser,

                                        password => $m_sPassword,

                                        }

        );

initDB()

        my $dbh = initDB(

                {

                name => 'LZE',

                host => 'localhost',

                user => 'root',

                password =>'',

                }
        );

Driver()

set or get the driver

       Driver()

default: Mysql

CurrentDb()

gibt die aktuelle datenbank zurück

CurrentHost()

gibt den aktuellen host zurück

CurrentUser()

gibt den aktuellen user zurück

CurrentPass()

gibt das aktuelle passwort zurück

independent functions

Top

tableExists()

$bool = $database->tableExists($table);

tableLength

$length = $database->tableLength($table);

dynamic statements

Top

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/>' ,$m_oDatabase->showTables();

Fo Syntax:

        print join '<br/>' , useexecute('showTables');

editexecute

        my %hash = (

                title => 'Titel',

                newTitle => 'New Titel',

                description => 'querys Abfragen',

                sql => 'sql statement',

                return => 'fetch_hashref', #subname

        );

        editexecute(\%hash);

useexecute()

        useexecute($m_sTitle,optional hashref {identifier => {1 => 'news', 2 => 'querys'}  , @parameter);

example:

        my %execute = (

                title => 'joins',

                description => 'description',

                sql => 'select * from table_1 JOIN  table_2 ',

                return => 'fetch_hashref'

        );

        $database->addexecute(\%execute5);

        my $ref = $database->joins({identifier => {1 => 'news', 2 => 'querys'}});

deleteexecute()

        deleteexecute($title);

fetch_array()

        @A = $database->fetch_array($sql);

fetch_AoH()

@aoh = $database->fetch_AoH($sql)

fetch_hashref()

$hashref = $database->fetch_hashref($sql)

void()

void(sql)

quote()

        $quotedString = $database->quote($sql);

selectTable

set a placeholder wihich is usesd by dynmaic statements.

<TABLE> will be replaced width this value.

default : querys;

Privat

Top

updateModules()

getSelf()

AUTOLOAD()

statements add by addexecute can called like

$database->showTables()

prepare()

execute()

fetch()

SEE ALSO

Top

MySQL::Admin::GUI DBI DBI::Library::Database

AUTHOR

Top

Dirk Lindner <lze@cpan.org>

LICENSE

Top

Copyright (C) 2005-2009 by Hr. Dirk Lindner

This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; 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 Lesser General Public License for more details.


MySQL-Admin documentation Contained in the MySQL-Admin distribution.
package DBI::Library;
use strict;
use warnings;
use utf8;
use vars
    qw( $m_dbh $m_dsn $m_sDefaultClass $m_hrSettings  @EXPORT_OK @ISA %m_hFunctions $m_sStyle $m_nRight $m_tbl $driver $m_oDatabase $m_sHost $m_sPass $m_sUser);
$m_sDefaultClass = 'DBI::Library' unless defined $DBI::Library::m_sDefaultClass;
@DBI::Library::EXPORT_OK
    = qw(CurrentPass CurrentUser CurrentHost CurrentDb Driver  useexecute quote void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute tableLength tableExists initDB $m_dsn $m_dbh selectTable);
%DBI::Library::EXPORT_TAGS = (
    'all' => [
        qw(CurrentPass CurrentUser CurrentHost CurrentDb Driver useexecute quote void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute tableLength tableExists initDB selectTable)
    ],
    'dynamic' => [
        qw(CurrentPass CurrentUser CurrentHost CurrentDb Driver useexecute void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute selectTable)
    ],
    'independent' => [
        qw(CurrentPass CurrentUser CurrentHost CurrentDb Driver tableLength tableExists initDB useexecute void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute selectTable)
    ],
);
$DBI::Library::VERSION = '0.62';
$m_tbl                 = 'querys';
$driver                = 'mysql';
require Exporter;
use DBI;

use base qw/Exporter DBI/;

# use Exporter qw/import/;

sub new {
    my ( $class, @initializer ) = @_;
    my $self = {};
    my $m_dbh;
    bless $self, ref $class || $class || $m_sDefaultClass;
    $m_dbh = $self->initDB(@initializer) if(@initializer);
    return ( $self, $m_dbh ) if $m_dbh;
    return $self;
}

sub initDB {
    my ( $self, @p ) = getSelf(@_);
    my $hash = $p[0];
    $m_oDatabase
        = defined $hash->{name}
        ? $hash->{name}
        : 'LZE';    #todo durch make setzen
    $m_sHost    = defined $hash->{host}     ? $hash->{host}     : 'localhost';
    $m_sUser = defined $hash->{user}     ? $hash->{user}     : 'root';
    $m_sPass    = defined $hash->{password} ? $hash->{password} : '';
    my $install = defined $hash->{install} ? $hash->{install} : 0;
    $m_sStyle = defined $hash->{style} ? $hash->{style} : 'lze';
    $m_dsn      = "DBI:$driver:database=$m_oDatabase;host=$m_sHost";
    $m_dbh    = DBI::Library->connect(
        $m_dsn, $m_sUser, $m_sPass,
        {   RaiseError => 1,
            PrintError => 0,
            AutoCommit => 1,
        }
        ) or warn "$DBI::Library::errs";
        $self->void("SET NAMES 'utf8_general_ci'");
    if( !$install && $m_oDatabase eq 'LZE' ) {
        my @q = $self->fetch_array('select title from querys');
        $m_hFunctions{$_} = $_ foreach (@q);
    }
    return $m_dbh;
}

sub Driver {
    if( $_[0] ) {
        $driver = $_[0];
    } else {
        return $driver;
    }
}

sub CurrentDb {
    return $m_oDatabase;
}

sub CurrentHost {
    return $m_sHost;
}

sub CurrentUser {
    return $m_sUser;
}

sub CurrentPass {
    return $m_sPass;
}

sub tableExists {
    my ( $self, @p ) = getSelf(@_);
    my $table     = $m_dbh->quote( $p[0] );
    my $db_clause = '';
    ( $db_clause, $table ) = ( " FROM $1", $2 ) if $table =~ /(.*)\.(.*)/;
    return ( $m_dbh->selectrow_array("SHOW TABLES $db_clause LIKE $table") );
}

sub tableLength {
    my ( $self, @p ) = getSelf(@_);
    my $table = $m_dbh->quote_identifier( $p[0] );
    my $sql   = "select count(*) from $table";
    if( $self->tableExists( $p[0] ) ) {
        my $sth = $m_dbh->prepare($sql) or warn $m_dbh->errstr;
        $sth->execute() or warn $m_dbh->errstr;
        my $length = $sth->fetchrow_array;
        $sth->finish();
        return $length;
    } else {
        return 0;
    }
}

sub addexecute {
    my ( $self, @p ) = getSelf(@_);
    my $hash        = $p[0];
    my $m_sTitle    = ( ( 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 ( $m_hFunctions{$m_sTitle} ) {
        my $sql_addexecute
            = qq/INSERT INTO querys(`title`,`sql`,`description`,`return`) VALUES(?,?,?,?);/;
        my $sth = $m_dbh->prepare($sql_addexecute);
        $sth->execute( $m_sTitle, $sql, $description, $return )
            or warn $m_dbh->errstr;
        $sth->finish();
        $self->updateModules();
    } else {
        return 0;
    }
}

sub editexecute {
    my ( $self, @p ) = getSelf(@_);
    my $hash = $p[0];
    my $m_sTitle = ( ( defined $hash->{title} ) ) ? $hash->{title} : 0;
    my $newTitle
        = ( ( defined $hash->{newTitle} ) ) ? $hash->{newTitle} : $m_sTitle;
    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( $m_hFunctions{$m_sTitle} ) {
        my $sql_edit
            = qq(update querys set title = ?, sql=? ,description=?,return=? where title = ? );
        my $sth = $m_dbh->prepare($sql_edit);
        $sth->execute( $newTitle, $sql, $description, $return, $m_sTitle )
            or warn $m_dbh->errstr;
        $sth->finish();

    } else {
        return 0;
    }
}

sub useexecute {
    my ( $self, @p ) = getSelf(@_);
    my $m_sTitle = shift(@p);
    my $sql      = 'select `sql`,`return` from querys where `title` = ?';
    my $sth      = $m_dbh->prepare($sql);
    $sth->execute($m_sTitle) or warn $m_dbh->errstr;
    my ( $sqlexec, $return ) = $sth->fetchrow_array();
    $sqlexec =~ s/<TABLE>/$m_tbl/g;
    if( ref $p[0] eq 'HASH' ) {
        my $ref = shift(@p);
        foreach my $key ( keys %{ $ref->{identifier} } ) {
            $sqlexec
                =~ s/table_$key/$m_dbh->quote_identifier($ref->{identifier}{$key})/ge;
        }
    }
    $sth->finish();
    return eval(" \$self->$return(\$sqlexec,\@p)");
}

sub deleteexecute {
    my ( $self, @p ) = getSelf(@_);
    my $id         = $p[0];
    my $sql_delete = 'DELETE FROM querys Where title  = ?';
    my $sth        = $m_dbh->prepare($sql_delete);
    $sth->execute($id) or warn $m_dbh->errstr;
    $sth->finish();
}

sub fetch_array {
    my ( $self, @p ) = getSelf(@_);
    my $sql = shift @p;
    my @r;
    eval( '
my $sth = $m_dbh->prepare($sql);
if(defined $p[0]) {
$sth->execute(@p) or warn $m_dbh->errstr;
}else {
$sth->execute() or warn $m_dbh->errstr;
}
while(my @comms = $sth->fetchrow_array()) {
push(@r, @comms);
}
$sth->finish();' );
    @r = $@ if $@;
    return @r;
}

sub fetch_AoH {
    my ( $self, @p ) = getSelf(@_);
    my $sql = shift @p;
    my @r;
    eval( '
my $sth = $m_dbh->prepare($sql);
if(defined $p[0]) {
$sth->execute(@p) or warn $m_dbh->errstr;
} else {
$sth->execute() or warn $m_dbh->errstr;
}
while(my $h = $sth->fetchrow_hashref) {
push(@r, $h);
}
$sth->finish();' );
    @r = $@ if $@;
    return @r;
}

sub fetch_hashref {
    my ( $self, @p ) = getSelf(@_);
    my $sql = shift @p;
    my $h;
    eval( '
my $sth = $m_dbh->prepare($sql);
if(defined $p[0]) {
$sth->execute(@p) or warn $m_dbh->errstr;
} else {
$sth->execute() or warn $m_dbh->errstr;
}
$h = $sth->fetchrow_hashref();
$sth->finish();
' );
    $h = "$@" if $@;
    return $h;
}

sub void {
    my ( $self, @p ) = getSelf(@_);
    my $sql = shift @p;
    my $sth = $m_dbh->prepare($sql);
    eval( '
if(defined $p[0]) {
$sth->execute(@p) or warn $m_dbh->errstr;
} else {
$sth->execute() or warn $m_dbh->errstr;
}' );
    $sth->finish();
    return "$@" if $@;
}

sub quote {
    my ( $self, @p ) = getSelf(@_);
    my $sql = $p[0];
    return $m_dbh->quote($sql);
}

sub selectTable {
    my ( $self, @p ) = getSelf(@_);
    $m_tbl = $m_dbh->quote_identifier( $p[0] );
}

sub updateModules {
    my ( $self, @p ) = getSelf(@_);
    my @q = $self->fetch_array('select title from querys');
    $m_hFunctions{$_} = $_ foreach (@q);
}

sub getSelf {
    return @_
        if defined( $_[0] )
            && ( !ref( $_[0] ) )
            && ( $_[0] eq 'DBI::Library' );
    return (
        defined( $_[0] )
            && ( ref( $_[0] ) eq 'DBI::Library'
            || UNIVERSAL::isa( $_[0], 'DBI::Library' ) )
    ) ? @_ : ( $DBI::Library::m_sDefaultClass->new, @_ );
}

sub AUTOLOAD {
    my ( $self, @p ) = getSelf(@_);
    our $AUTOLOAD;
    if( $AUTOLOAD =~ /.*::(\w+)$/ and grep $1 eq $_, %m_hFunctions ) {
        my $attr = $1;
        {
            no strict 'refs';
            *{$AUTOLOAD} = sub {
                $self->useexecute( $attr, @p );
            };
        }
        goto &{$AUTOLOAD};
    }
}

package DBI::Library::db;
use vars qw(@ISA);
@ISA = qw(DBI::db);

sub prepare {
    my ( $m_dbh, @args ) = @_;
    my $sth = $m_dbh->SUPER::prepare(@args) or return;
    return $sth;
}

package DBI::Library::st;
use vars qw(@ISA);
@ISA = qw(DBI::st);

sub execute {
    my ( $sth, @args ) = @_;
    my $rv;
    eval('$rv = $sth->SUPER::execute(@args)');
    return "$@" if $@;
    return $rv;
}

sub fetch {
    my ( $sth, @args ) = @_;

    my $row;
    eval('$row = $sth->SUPER::fetch(@args)');
    return "$@" if $@;
    if ($row) {
    utf8::encode($row->[1])  if(utf8::is_utf8($row->[1]));
    }
    return $row;
}
1;