|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base db-adapter class |
|
4
|
|
|
* |
|
5
|
|
|
* @file PdoConnection.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 5.4+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2015 Alexander Yancharuk <alex at itvault at info> |
|
11
|
|
|
* @date 2013-12-31 15:44 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\DataBase\Adapters; |
|
17
|
|
|
|
|
18
|
|
|
use Veles\DataBase\ConnectionPools\ConnectionPool; |
|
19
|
|
|
use Veles\Traits\SingletonInstance; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class DbAdapterBase |
|
23
|
|
|
* |
|
24
|
|
|
* Base class for Db adapters |
|
25
|
|
|
* |
|
26
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
27
|
|
|
*/ |
|
28
|
|
|
class DbAdapterBase |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var ConnectionPool */ |
|
31
|
|
|
protected static $pool; |
|
32
|
|
|
/** @var \PDO */ |
|
33
|
|
|
protected static $connection; |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
protected static $connection_name; |
|
36
|
|
|
|
|
37
|
|
|
use SingletonInstance; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Add connection pool |
|
41
|
|
|
* |
|
42
|
|
|
* @param ConnectionPool $pool |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public static function setPool(ConnectionPool $pool) |
|
45
|
|
|
{ |
|
46
|
1 |
|
static::$pool = $pool; |
|
47
|
1 |
|
static::$connection_name = $pool->getDefaultConnectionName(); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get connection pool |
|
52
|
|
|
* |
|
53
|
|
|
* @return ConnectionPool $pool |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public static function getPool() |
|
56
|
|
|
{ |
|
57
|
1 |
|
return static::$pool; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set default connection |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $name Connection name |
|
64
|
|
|
* |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function setConnection($name) |
|
68
|
|
|
{ |
|
69
|
1 |
|
static::$connection_name = $name; |
|
70
|
1 |
|
static::$connection = null; |
|
71
|
|
|
|
|
72
|
1 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get default connection resource |
|
77
|
|
|
* |
|
78
|
|
|
* return \PDO |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function getConnection() |
|
81
|
|
|
{ |
|
82
|
1 |
|
if (null === static::$connection) { |
|
83
|
1 |
|
$conn = static::$pool->getConnection(static::$connection_name); |
|
84
|
1 |
|
static::$connection = (null === $conn->getResource()) |
|
85
|
1 |
|
? $conn->create() |
|
86
|
1 |
|
: $conn->getResource(); |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
return static::$connection; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|