CHips L MINI SHELL

CHips L pro

Current Path : /proc/2/root/usr/local/rvglobalsoft/rvglobalsoft/lib/RVL/
Upload File :
Current File : //proc/2/root/usr/local/rvglobalsoft/rvglobalsoft/lib/RVL/DB.pm

#!/usr/bin/perl
package RVL::DB;
##LICENSE##
use strict;
use warnings;
use DBI;
use define RVL_DSN_HASH    => 0;
use define RVL_DSN_STRING   => 1;
use Class::Std::Utils;
{
    sub new {
        RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
        my ($class, $opt) = @_;
        my (%hash);
        # dns = dbi:TYPE:database=DATABASE_NAME;host=HOSTNAME:PORT://USERNAME@PASSOWD
        my ($this) = bless( \%hash, $class);
        
        my ($dsn, $userpass) = split(/:\/\//, $opt->{dsn}, 2);
        my ($user, $passwd) = split(/@/, $userpass, 2);
        $this->{DBI} = DBI->connect($dsn, $user, $passwd, {
            Callbacks => {
                PrintError => 1,
                PrintWarn  => 1,
                connected  => sub {
                    my ($c) = RVL::Config::singleton();
                    my ($conf) = $c->getAll();
                    if (defined $conf->{'db'}->{'postConnect'} && $conf->{'db'}->{'postConnect'} ne '') {
                        shift->do(q{$conf->{'db'}->{'postConnect'};});
                    }
                    return;
                },
                
            }
        });
        return $this;
    }

    sub singleton {
        RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
        my ($dsn) = @_;
        my ($class) = __PACKAGE__;
        my ($msg) = 'Cannot connect to DB, check your credentials, exiting ...';
        
        my ($c) = RVL::Config::singleton();
        my ($conf) = $c->getAll();
        $dsn = (!defined($dsn) || $dsn eq '' || !$dsn) ? RVL::DB::_getDsnAsString($conf) : $dsn;
        if (!defined $RVL::INSTANCE{$class}{$dsn}) {
            $RVL::INSTANCE{$class}{$dsn} = $class->new({'dsn' => $dsn});
        }
        return $RVL::INSTANCE{$class}{$dsn}
    }
    
    sub  _getDsnAsString {
        RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
        my ($conf) = @_;
        my ($type) = (defined $conf->{'db'}->{'type'}) ? $conf->{'db'}->{'type'} : 'mysql';
        my ($database) = $conf->{'db'}->{'name'};
        my ($hostname) = (defined $conf->{'db'}->{'host'}) ? $conf->{'db'}->{'host'} : 'localhost';
        my ($port) = (defined $conf->{'db'}->{'port'}) ? $conf->{'db'}->{'port'} : 3306;
        my ($user) = $conf->{'db'}->{'user'};
        my ($passwd) = $conf->{'db'}->{'pass'};
        return 'dbi:' . $type . ':database=' . $database . ';host=' . $hostname . ':' . $port . '://' . $user . '@' . $passwd;
    }
    
    sub query {
        my ($this, $query) = @_;
        if ($this->{sqldebug}) {
            print "$query\n";
            return 1;
        }
        my ($q) = $this->{DBI}->prepare($query);
        $q->execute;
        return 1;
    }
    
    sub getRow {
        my ($this, $query) = @_;
        my (@row, @result);
        my $q = $this->{DBI}->prepare($query);
        $q->execute;
        while (@row = $q->fetchrow_array()){
            next if ($row[0] =~/^$/);
            push(@result, $row[0]);
        }    
        return @result;
    }
    
    sub getAllHash {
        my ($this, $query) = @_;
        my $q = $this->{DBI}->prepare($query);
        $q->execute;
        my @hashRows = ();
        while (my $row_ref = $q->fetchrow_hashref()) {
            push (@hashRows, $row_ref); 
        }
        return @hashRows;
    }
}

package RVL::ServiceLocator;
use strict;
use warnings;
use Class::Std::Utils;
{
    sub new {
        my ($class, $opt) = @_;
        my (%hash);
        my ($this) = bless( \%hash, $class);
        
        return $this;
    }
    
    sub singleton {
        my ($class) = __PACKAGE__;
        if (!defined $RVL::INSTANCE{$class}) {
            $RVL::INSTANCE{$class} = $class->new();
        }
        return $RVL::INSTANCE{$class}
    }
    
    sub register {
        my ($this, $serviceName, $oService) = @_;
        $this->{aServices}->{$serviceName} = $oService;
        return 1;
    }
    
    sub remove {
        my ($this, $serviceName) = @_;
        delete $this->{aServices}->{$serviceName};
    }
    
    sub get {
        my ($this, $serviceName) = @_;
        if (defined $this->{aServices}->{$serviceName}) {
            return ${$this->{aServices}->{$serviceName}};
        } else {
            return 0;
        }
    }
    
    sub staticGet
    {
        my ($serviceName) = @_;
        my ($oServiceLocator) = RVL::ServiceLocator::singleton();
        return $oServiceLocator->get($serviceName);
    }
}

1;



Copyright 2K16 - 2K18 Indonesian Hacker Rulez