DBIx-Custom

 view release on metacpan or  search on metacpan

t/mysql.t  view on Meta::CPAN

    $user,
    $password,
    DBIx::Custom->new->default_option
  );

  my $dbi = DBIx::Custom->connect(connector => $connector);
  $dbi->delete_all(table => 'table1');
  $dbi->do('insert into table1 (key1, key2) values (1, 2)');
  is($dbi->select(table => 'table1')->fetch_hash_one->{key1}, 1);
  
  $dbi = DBIx::Custom->new;
  $dbi->dbh('a');
  is($dbi->{dbh}, 'a');
}

# transaction
# dbh
{
  my $connector = DBIx::Connector->new(
    "dbi:mysql:database=$database",
    $user,
    $password,
    DBIx::Custom->new->default_option
  );

  my $dbi = DBIx::Custom->connect(connector => $connector);
  $dbi->delete_all(table => 'table1');
  
  $dbi->connector->txn(sub {
    $dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
    $dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
  });
  is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
    [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);

  $dbi->delete_all(table => 'table1');
  eval {
    $dbi->connector->txn(sub {
      $dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
      die "Error";
      $dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
    });
  };
  is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
            []);
}

use DBIx::Custom;
use Scalar::Util 'blessed';
{
  my $dbi = DBIx::Custom->connect(
    user => $user,
    password => $password,
    dsn => "dbi:mysql:dbname=$database"
  );
  $dbi->connect;
  
  ok(blessed $dbi->dbh);
  can_ok($dbi->dbh, qw/prepare/);
  ok($dbi->dbh->{AutoCommit});
  ok(!$dbi->dbh->{mysql_enable_utf8});
}

{
  my $dbi = DBIx::Custom->connect(
    user => $user,
    password => $password,
    dsn => "dbi:mysql:dbname=$database",
    option => {AutoCommit => 0, mysql_enable_utf8 => 1}
  );
  $dbi->connect;
  ok(!$dbi->dbh->{AutoCommit});
  #ok($dbi->dbh->{mysql_enable_utf8});
}

# fork
{
  my $connector = DBIx::Connector->new(
    "dbi:mysql:database=$database",
    $user,
    $password,
    DBIx::Custom->new->default_option
  );
  
  my $dbi = DBIx::Custom->new(connector => $connector);
  $dbi->delete_all(table => 'table1');
  $dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
  die "Can't fork" unless defined (my $pid = fork);

  if ($pid) {
    # Parent
    my $result = $dbi->select(table => 'table1');
    is_deeply($result->fetch_hash_one, {key1 => 1, key2 => 2});
  }
  else {
    # Child
    my $result = $dbi->select(table => 'table1');
    die "Not OK" unless $result->fetch_hash_one->{key1} == 1;
  }
}



( run in 1.259 second using v1.01-cache-2.11-cpan-5837b0d9d2c )