-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgsql.php
More file actions
80 lines (66 loc) · 1.92 KB
/
Pgsql.php
File metadata and controls
80 lines (66 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace Hyperf\Pgsql;
use Hyperf\Pgsql\Pool\PoolFactory;
use Hyperf\Utils\Context;
class Pgsql
{
/**
* @var PoolFactory
*/
protected $factory;
/**
* @var string
*/
protected $poolName="default";
public function __construct(PoolFactory $factory)
{
$this->factory = $factory;
}
public function beginTransaction(){
$this->getConnection()->call('beginTransaction');
}
public function rollback(){
$this->getConnection()->call('rollBack');
}
public function commit(){
$this->getConnection()->call('commit');
}
public function insert(string $query,array $bindings = []): int
{
return $this->getConnection()->insert($query,$bindings);
}
public function execute(string $query,array $bindings = []): int
{
return $this->getConnection()->execute($query,$bindings);
}
public function query(string $query,array $bindings = []): array
{
return $this->getConnection()->query($query,$bindings);
}
public function fetch(string $query,array $bindings = [])
{
return $this->getConnection()->fetch($query,$bindings);
}
public function getConnection()
{
$connection = null;
$hasContextConnection = Context::has($this->getContextKey());
if ($hasContextConnection) {
$connection = Context::get($this->getContextKey());
}
if (!$connection instanceof PgsqlConnection) {
$pool = $this->factory->getPool($this->poolName);
$connection = $pool->get()->getConnection();
Context::set($this->getContextKey(),$connection);
}
return $connection;
}
/**
* The key to identify the connection object in coroutine context.
*/
private function getContextKey(): string
{
return sprintf('pgsql.connection.%s', $this->poolName);
}
}