Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions publish/dtm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'barrier' => [
'db' => [
'type' => DbType::MySQL,
// 'type' => DbType::DB,
],
'apply' => [],
],
Expand Down
7 changes: 6 additions & 1 deletion src/Barrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ class Barrier

protected MySqlBarrier $mySqlBarrier;

public function __construct(ConfigInterface $config, MySqlBarrier $mySqlBarrier)
protected DbBarrier $dbBarrier;

public function __construct(ConfigInterface $config, MySqlBarrier $mySqlBarrier, DbBarrier $dbBarrier)
{
$this->config = $config;
$this->mySqlBarrier = $mySqlBarrier;
$this->dbBarrier = $dbBarrier;
}

public function call(callable $businessCall)
Expand Down Expand Up @@ -77,6 +80,8 @@ protected function getBarrier(): BarrierInterface
switch ($this->config->get('dtm.barrier.db.type', DbType::MySQL)) {
case DbType::MySQL:
return $this->mySqlBarrier;
case DbType::DB:
return $this->dbBarrier;
default:
throw new UnsupportedException('Barrier DB type is unsupported.');
}
Expand Down
2 changes: 2 additions & 0 deletions src/Constants/DbType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ class DbType
{
public const MySQL = 'mysql';

public const DB = 'db';

public const Redis = 'redis';
}
61 changes: 61 additions & 0 deletions src/DbBarrier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);
/**
* This file is part of DTM-PHP.
*
* @license https://github.com/dtm-php/dtm-client/blob/master/LICENSE
*/
namespace DtmClient;

use DtmClient\Constants\Branch;
use DtmClient\Constants\Operation;
use DtmClient\Exception\FailureException;

class DbBarrier extends MySqlBarrier
{
public function queryPrepared(string $transType, string $gid): bool
{
$db = $this->DBTransaction->connection();
$table = $db->raw('barrier');
$db->table($table)->insertOrIgnore([
'trans_type' => $transType,
'gid' => $gid,
'branch_id' => Branch::MsgDoBranch0,
'op' => Branch::MsgDoOp,
'barrier_id' => Branch::MsgDoBarrier1,
'reason' => Operation::ROLLBACK,
]);

$reason = $db->table($table)
->select('reason')
->where([
'gid' => $gid,
'branch_id' => Branch::MsgDoBranch0,
'op' => Branch::MsgDoOp,
'barrier_id' => Branch::MsgDoBarrier1,
])->first();

if ($reason->reason == Operation::ROLLBACK) {
throw new FailureException();
}
return true;
}

protected function insertBarrier(string $transType, string $gid, string $branchId, string $op, string $barrierID, string $reason): int
{
if (empty($op)) {
return 0;
}
$db = $this->DBTransaction->connection();
$table = $db->raw('barrier');
return $db->table($table)->insertOrIgnore([
'trans_type' => $transType,
'gid' => $gid,
'branch_id' => $branchId,
'op' => $op,
'barrier_id' => $barrierID,
'reason' => $reason,
]);
}
}
5 changes: 5 additions & 0 deletions src/DbTransaction/DBTransactionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function execute(string $sql, array $bindings = []): int;

public function query(string $sql, array $bindings = []): bool|array;

/**
* @return \Hyperf\Database\ConnectionInterface|\Illuminate\Database\ConnectionInterface
*/
public function connection();

public function xaExecute(string $sql, array $bindings = []): int;

public function xaQuery(string $sql, array $bindings = []): bool|array;
Expand Down
5 changes: 5 additions & 0 deletions src/DbTransaction/HyperfDbTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public function query(string $sql, array $bindings = []): bool|array
{
return Db::select($sql, $bindings);
}

public function connection()
{
return Db::connection();
}
}
7 changes: 6 additions & 1 deletion src/DbTransaction/HyperfSimpleDbTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace DtmClient\DbTransaction;

use Exception;
use Hyperf\Contract\ConfigInterface;
use Hyperf\DB\DB;

Expand Down Expand Up @@ -42,5 +43,9 @@ public function query(string $sql, array $bindings = []): bool|array
{
return DB::query($sql, $bindings);
}


public function connection()
{
throw new Exception('Not implemented');
}
}
11 changes: 6 additions & 5 deletions src/DbTransaction/LaravelDbTransaction.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<?php

declare(strict_types=1);
/**
* This file is part of DTM-PHP.
*
* @license https://github.com/dtm-php/dtm-client/blob/master/LICENSE
*/

namespace DtmClient\DbTransaction;

use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -39,4 +35,9 @@ public function query(string $sql, array $bindings = []): bool|array
{
return DB::select($sql, $bindings);
}

public function connection()
{
return DB::connection();
}
}
Loading