Skip to content

Commit 158a9b7

Browse files
authored
Merge pull request #118 from bavix/dbs
add support select database
2 parents b8a6d9d + fd15059 commit 158a9b7

File tree

10 files changed

+96
-25
lines changed

10 files changed

+96
-25
lines changed

src/Commands/RefreshBalance.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace Bavix\Wallet\Commands;
44

55
use Bavix\Wallet\Models\Wallet;
6+
use Bavix\Wallet\Services\DbService;
67
use Bavix\Wallet\Services\ProxyService;
78
use Illuminate\Console\Command;
89
use Illuminate\Database\Query\JoinClause;
910
use Illuminate\Database\SQLiteConnection;
10-
use Illuminate\Support\Facades\DB;
1111
use function config;
1212

1313
/**
@@ -38,11 +38,14 @@ class RefreshBalance extends Command
3838
public function handle(): void
3939
{
4040
app(ProxyService::class)->fresh();
41-
DB::transaction(function () {
41+
app(DbService::class)->transaction(function () {
4242
$wallet = config('wallet.wallet.table');
43-
DB::table($wallet)->update(['balance' => 0]);
43+
app(DbService::class)
44+
->connection()
45+
->table($wallet)
46+
->update(['balance' => 0]);
4447

45-
if (DB::connection() instanceof SQLiteConnection) {
48+
if (app(DbService::class)->connection() instanceof SQLiteConnection) {
4649
$this->sqliteUpdate();
4750
} else {
4851
$this->multiUpdate();
@@ -71,18 +74,22 @@ protected function multiUpdate(): void
7174
{
7275
$wallet = config('wallet.wallet.table');
7376
$trans = config('wallet.transaction.table');
74-
$availableBalance = DB::table($trans)
75-
->select('wallet_id', DB::raw('sum(amount) balance'))
77+
$availableBalance = app(DbService::class)
78+
->connection()
79+
->table($trans)
80+
->select('wallet_id', app(DbService::class)->raw('sum(amount) balance'))
7681
->where('confirmed', true)
7782
->groupBy('wallet_id');
7883

7984
$joinClause = static function (JoinClause $join) use ($wallet) {
8085
$join->on("$wallet.id", '=', 'b.wallet_id');
8186
};
8287

83-
DB::table($wallet)
88+
app(DbService::class)
89+
->connection()
90+
->table($wallet)
8491
->joinSub($availableBalance, 'b', $joinClause, null, null, 'left')
85-
->update(['balance' => DB::raw('b.balance')]);
92+
->update(['balance' => app(DbService::class)->raw('b.balance')]);
8693
}
8794

8895
}

src/Services/CommonService.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Bavix\Wallet\Objects\Bring;
1313
use Bavix\Wallet\Objects\Operation;
1414
use Bavix\Wallet\Traits\HasWallet;
15-
use Illuminate\Support\Facades\DB;
1615
use function app;
1716
use function compact;
1817

@@ -186,7 +185,7 @@ public function assemble(array $brings): array
186185
{
187186
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($brings) {
188187
$self = $this;
189-
return DB::transaction(static function () use ($self, $brings) {
188+
return app(DbService::class)->transaction(static function () use ($self, $brings) {
190189
return $self->multiBrings($brings);
191190
});
192191
});

src/Services/DbService.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Services;
4+
5+
use Closure;
6+
use Illuminate\Database\ConnectionInterface;
7+
use Illuminate\Database\Query\Expression;
8+
use Illuminate\Support\Facades\DB;
9+
10+
/**
11+
* Class DbService
12+
* @package Bavix\Wallet\Services
13+
* @codeCoverageIgnore
14+
*/
15+
class DbService
16+
{
17+
18+
/**
19+
* @return ConnectionInterface
20+
*/
21+
public function connection(): ConnectionInterface
22+
{
23+
return DB::connection(config('wallet.database.connection'));
24+
}
25+
26+
/**
27+
* Execute a Closure within a transaction.
28+
*
29+
* @param Closure $callback
30+
* @param int $attempts
31+
* @return mixed
32+
*
33+
* @throws \Throwable
34+
*/
35+
public function transaction(Closure $callback, $attempts = 1)
36+
{
37+
return $this->connection()->transaction($callback, $attempts);
38+
}
39+
40+
/**
41+
* Get a new raw query expression.
42+
*
43+
* @param mixed $value
44+
* @return Expression
45+
*/
46+
public function raw($value): Expression
47+
{
48+
return $this->connection()->raw($value);
49+
}
50+
51+
}

src/Traits/CanConfirm.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Bavix\Wallet\Exceptions\WalletOwnerInvalid;
77
use Bavix\Wallet\Models\Transaction;
88
use Bavix\Wallet\Services\CommonService;
9+
use Bavix\Wallet\Services\DbService;
910
use Bavix\Wallet\Services\LockService;
1011
use Bavix\Wallet\Services\WalletService;
11-
use Illuminate\Support\Facades\DB;
1212

1313
trait CanConfirm
1414
{
@@ -22,7 +22,7 @@ public function confirm(Transaction $transaction): bool
2222
{
2323
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) {
2424
$self = $this;
25-
return DB::transaction(static function () use ($self, $transaction) {
25+
return app(DbService::class)->transaction(static function () use ($self, $transaction) {
2626
$wallet = app(WalletService::class)
2727
->getWallet($self);
2828

@@ -65,7 +65,7 @@ public function forceConfirm(Transaction $transaction): bool
6565
{
6666
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) {
6767
$self = $this;
68-
return DB::transaction(static function () use ($self, $transaction) {
68+
return app(DbService::class)->transaction(static function () use ($self, $transaction) {
6969

7070
$wallet = app(WalletService::class)
7171
->getWallet($self);

src/Traits/CanExchange.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use Bavix\Wallet\Models\Transfer;
77
use Bavix\Wallet\Objects\Bring;
88
use Bavix\Wallet\Services\CommonService;
9+
use Bavix\Wallet\Services\DbService;
910
use Bavix\Wallet\Services\ExchangeService;
1011
use Bavix\Wallet\Services\LockService;
1112
use Bavix\Wallet\Services\WalletService;
12-
use Illuminate\Support\Facades\DB;
1313

1414
trait CanExchange
1515
{
@@ -51,7 +51,7 @@ public function forceExchange(Wallet $to, int $amount, ?array $meta = null): Tra
5151
$from = app(WalletService::class)->getWallet($this);
5252

5353
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($from, $to, $amount, $meta) {
54-
return DB::transaction(static function () use ($from, $to, $amount, $meta) {
54+
return app(DbService::class)->transaction(static function () use ($from, $to, $amount, $meta) {
5555
$rate = app(ExchangeService::class)->rate($from, $to);
5656
$fee = app(WalletService::class)->fee($to, $amount);
5757

src/Traits/CartPay.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Bavix\Wallet\Models\Transfer;
88
use Bavix\Wallet\Objects\Cart;
99
use Bavix\Wallet\Services\CommonService;
10+
use Bavix\Wallet\Services\DbService;
1011
use Illuminate\Database\Eloquent\ModelNotFoundException;
11-
use Illuminate\Support\Facades\DB;
1212
use Throwable;
1313
use function array_unique;
1414
use function count;
@@ -32,7 +32,7 @@ public function payFreeCart(Cart $cart): array
3232
->verifyWithdraw($this, 0, true);
3333

3434
$self = $this;
35-
return DB::transaction(static function () use ($self, $cart) {
35+
return app(DbService::class)->transaction(static function () use ($self, $cart) {
3636
$results = [];
3737
foreach ($cart->getItems() as $product) {
3838
$results[] = app(CommonService::class)->forceTransfer(
@@ -75,7 +75,7 @@ public function payCart(Cart $cart, bool $force = null): array
7575
}
7676

7777
$self = $this;
78-
return DB::transaction(static function () use ($self, $cart, $force) {
78+
return app(DbService::class)->transaction(static function () use ($self, $cart, $force) {
7979
$results = [];
8080
foreach ($cart->getItems() as $product) {
8181
if ($force) {
@@ -138,7 +138,7 @@ public function safeRefundCart(Cart $cart, bool $force = null, bool $gifts = nul
138138
public function refundCart(Cart $cart, bool $force = null, bool $gifts = null): bool
139139
{
140140
$self = $this;
141-
return DB::transaction(static function () use ($self, $cart, $force, $gifts) {
141+
return app(DbService::class)->transaction(static function () use ($self, $cart, $force, $gifts) {
142142
$results = [];
143143
$transfers = $cart->alreadyBuy($self, $gifts);
144144
if (count($transfers) !== count($cart)) {

src/Traits/HasGift.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use Bavix\Wallet\Models\Transfer;
88
use Bavix\Wallet\Objects\Bring;
99
use Bavix\Wallet\Services\CommonService;
10+
use Bavix\Wallet\Services\DbService;
1011
use Bavix\Wallet\Services\LockService;
1112
use Bavix\Wallet\Services\WalletService;
12-
use Illuminate\Support\Facades\DB;
1313
use Throwable;
1414
use function app;
1515

@@ -63,7 +63,7 @@ public function gift(Wallet $to, Product $product, bool $force = null): Transfer
6363
* I think it is wrong to make the "assemble" method public.
6464
* That's why I address him like this!
6565
*/
66-
return DB::transaction(static function () use ($santa, $to, $product, $force) {
66+
return app(DbService::class)->transaction(static function () use ($santa, $to, $product, $force) {
6767
$amount = $product->getAmountProduct();
6868
$meta = $product->getMetaProduct();
6969
$fee = app(WalletService::class)

src/Traits/HasWallet.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
use Bavix\Wallet\Models\Transfer;
99
use Bavix\Wallet\Models\Wallet as WalletModel;
1010
use Bavix\Wallet\Services\CommonService;
11+
use Bavix\Wallet\Services\DbService;
1112
use Bavix\Wallet\Services\WalletService;
1213
use Illuminate\Database\Eloquent\Relations\MorphMany;
1314
use Illuminate\Support\Collection;
14-
use Illuminate\Support\Facades\DB;
1515
use Throwable;
1616
use function app;
1717
use function config;
@@ -37,11 +37,12 @@ trait HasWallet
3737
* @param bool $confirmed
3838
*
3939
* @return Transaction
40+
* @throws
4041
*/
4142
public function deposit(int $amount, ?array $meta = null, bool $confirmed = true): Transaction
4243
{
4344
$self = $this;
44-
return DB::transaction(static function () use ($self, $amount, $meta, $confirmed) {
45+
return app(DbService::class)->transaction(static function () use ($self, $amount, $meta, $confirmed) {
4546
return app(CommonService::class)
4647
->deposit($self, $amount, $meta, $confirmed);
4748
});
@@ -161,11 +162,12 @@ public function canWithdraw(int $amount, bool $allowZero = null): bool
161162
* @param bool $confirmed
162163
*
163164
* @return Transaction
165+
* @throws
164166
*/
165167
public function forceWithdraw(int $amount, ?array $meta = null, bool $confirmed = true): Transaction
166168
{
167169
$self = $this;
168-
return DB::transaction(static function () use ($self, $amount, $meta, $confirmed) {
170+
return app(DbService::class)->transaction(static function () use ($self, $amount, $meta, $confirmed) {
169171
return app(CommonService::class)
170172
->forceWithdraw($self, $amount, $meta, $confirmed);
171173
});
@@ -179,11 +181,12 @@ public function forceWithdraw(int $amount, ?array $meta = null, bool $confirmed
179181
* @param int $amount
180182
* @param array|null $meta
181183
* @return Transfer
184+
* @throws
182185
*/
183186
public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null): Transfer
184187
{
185188
$self = $this;
186-
return DB::transaction(static function () use ($self, $amount, $wallet, $meta) {
189+
return app(DbService::class)->transaction(static function () use ($self, $amount, $wallet, $meta) {
187190
return app(CommonService::class)
188191
->forceTransfer($self, $wallet, $amount, $meta);
189192
});

src/WalletServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Bavix\Wallet\Objects\EmptyLock;
1414
use Bavix\Wallet\Objects\Operation;
1515
use Bavix\Wallet\Services\CommonService;
16+
use Bavix\Wallet\Services\DbService;
1617
use Bavix\Wallet\Services\ExchangeService;
1718
use Bavix\Wallet\Services\LockService;
1819
use Bavix\Wallet\Services\ProxyService;
@@ -84,6 +85,7 @@ public function register(): void
8485
// Bind eloquent models to IoC container
8586
$this->app->singleton(Rateable::class, config('wallet.package.rateable'));
8687
$this->app->singleton(Storable::class, config('wallet.package.storable'));
88+
$this->app->singleton(DbService::class, config('wallet.services.db'));
8789
$this->app->singleton(ExchangeService::class, config('wallet.services.exchange'));
8890
$this->app->singleton(CommonService::class, config('wallet.services.common'));
8991
$this->app->singleton(ProxyService::class, config('wallet.services.proxy'));

tests/SingletonTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Bavix\Wallet\Objects\EmptyLock;
99
use Bavix\Wallet\Objects\Operation;
1010
use Bavix\Wallet\Services\CommonService;
11+
use Bavix\Wallet\Services\DbService;
1112
use Bavix\Wallet\Services\ExchangeService;
1213
use Bavix\Wallet\Services\LockService;
1314
use Bavix\Wallet\Services\ProxyService;
@@ -124,6 +125,14 @@ public function testWalletService(): void
124125
$this->assertEquals($this->getRefId(WalletService::class), $this->getRefId(WalletService::class));
125126
}
126127

128+
/**
129+
* @return void
130+
*/
131+
public function testDbService(): void
132+
{
133+
$this->assertEquals($this->getRefId(DbService::class), $this->getRefId(DbService::class));
134+
}
135+
127136
/**
128137
* @return void
129138
*/

0 commit comments

Comments
 (0)