Tripletail

 view release on metacpan or  search on metacpan

lib/Tripletail/DB/Backend/MySQL.pm  view on Meta::CPAN

package Tripletail::DB::Backend::MySQL::Dbh;
use strict;
use warnings;
use Hash::Util qw(lock_hash);
use Scalar::Lazy;
use Tripletail;
our @ISA = qw(Tripletail::DB::Dbh);

sub connect {
    my $this = shift;

    my $opts = {
        dbname => $TL->INI->get($this->{inigroup} => 'dbname'),
    };

    my $host = $TL->INI->get($this->{inigroup} => 'host' => undef);
    if (defined($host) && $host ne '') {
        $opts->{host} = $host;
    }

    my $port = $TL->INI->get($this->{inigroup} => 'port' => undef);
    if (defined($port) && $port ne '') {
        $opts->{port} = $port;
    }

    # mysql_read_default_file, mysql_read_default_group オプションを渡す
    if (defined(my $default_file = $TL->INI->get_reloc($this->{inigroup} => 'mysql_read_default_file' => undef))) {
        if (!-e $default_file) {
            die __PACKAGE__."#connect: file $default_file does not exist. ($default_file が存在しません)".
              " ('mysql_read_default_file' in [$this->{inigroup}])\n";
        }
        $opts->{mysql_read_default_file} = $default_file;

        if (defined(my $default_group = $TL->INI->get($this->{inigroup} => 'mysql_read_default_group' => undef))) {
            $opts->{mysql_read_default_group} = $default_group;
        }
    }

    no warnings 'redefine';
    if (!$DBI::installed_drh{mysql}) {
        DBI->install_driver('mysql');
    }
    my $orig = \&DBD::mysql::db::_login;
    local $Tripletail::Error::LAST_DB_ERROR;
    local *DBD::mysql::db::_login = sub {
        my @ret = wantarray ? &$orig : scalar(&$orig);
        if (!$ret[0]) {
            # $_[0]がdbh.
            # 保持してしまうとその後のエラーメッセージがでなくなり,
            # リファレンスではundefに消されてしまうので
            # ここでエラー情報を作成.
            my $e = $this->_errinfo($_[0]);
            $Tripletail::Error::LAST_DB_ERROR = lazy { $e } 'init';
        }
        return wantarray ? @ret : $ret[0];
    };

    $this->{type} = 'mysql';
    $this->{dbh } = DBI->connect(
        'dbi:mysql:' . join(';', map { "$_=$opts->{$_}" } keys %$opts),
        $TL->INI->get($this->{inigroup} => 'user'    , undef),
        $TL->INI->get($this->{inigroup} => 'password', undef), {
            AutoCommit => 1,
            PrintError => 0,
            RaiseError => 1,
        });

    if (!$this->{dbh}) {
        die __PACKAGE__."#connect: DBI->connect failed. (DBI->connectに失敗しました)\n";
    }

    return $this;
}

sub _mk_locking_query {
    my $this   = shift;
    my $tables = shift;

    return 'LOCK TABLES ' .
      join(
          ', ',
          map {
              my $table = $_->[0];
              my $alias = $_->[1];
              my $mode  = $_->[2];

              defined $alias
                ? sprintf(
                    '%s AS %s %s',
                    $this->symquote($table),
                    $this->symquote($alias),
                    $mode



( run in 2.085 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )