1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Database\Drivers; |
4
|
|
|
|
5
|
|
|
use DrMVC\Config\ConfigInterface; |
6
|
|
|
|
7
|
|
|
abstract class Driver implements DriverInterface, QueryInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \PDO |
11
|
|
|
*/ |
12
|
|
|
protected $_connection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $_dsn; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ConfigInterface |
21
|
|
|
*/ |
22
|
|
|
private $_config; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Name of collection |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $_collection; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Driver constructor. |
32
|
|
|
* |
33
|
|
|
* @param ConfigInterface $config object with current configuration |
34
|
|
|
* @param string $collection name of collection |
35
|
|
|
*/ |
36
|
|
|
public function __construct(ConfigInterface $config, string $collection) |
37
|
|
|
{ |
38
|
|
|
$this->setConfig($config); |
39
|
|
|
$this->setCollection($this->getConfig()->get('prefix') . $collection); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Initiate connection with database |
44
|
|
|
* |
45
|
|
|
* @return DriverInterface |
46
|
|
|
*/ |
47
|
|
|
abstract public function connect(): DriverInterface; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Disconnect from database |
51
|
|
|
* |
52
|
|
|
* @return DriverInterface |
53
|
|
|
*/ |
54
|
|
|
abstract public function disconnect(): DriverInterface; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $collection |
58
|
|
|
* @return DriverInterface |
59
|
|
|
*/ |
60
|
|
|
public function setCollection(string $collection): DriverInterface |
61
|
|
|
{ |
62
|
|
|
$this->_collection = $collection; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getCollection(): string |
70
|
|
|
{ |
71
|
|
|
return $this->_collection; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param ConfigInterface $config |
76
|
|
|
* @return DriverInterface |
77
|
|
|
*/ |
78
|
|
|
public function setConfig(ConfigInterface $config): DriverInterface |
79
|
|
|
{ |
80
|
|
|
$this->_config = $config; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return ConfigInterface |
86
|
|
|
*/ |
87
|
|
|
public function getConfig(): ConfigInterface |
88
|
|
|
{ |
89
|
|
|
return $this->_config; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $dsn |
94
|
|
|
* @return DriverInterface |
95
|
|
|
*/ |
96
|
|
|
public function setDsn(string $dsn): DriverInterface |
97
|
|
|
{ |
98
|
|
|
$this->_dsn = $dsn; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getDsn(): string |
106
|
|
|
{ |
107
|
|
|
return $this->_dsn; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param \PDO $connection |
112
|
|
|
* @return DriverInterface |
113
|
|
|
*/ |
114
|
|
|
public function setConnection(\PDO $connection): DriverInterface |
115
|
|
|
{ |
116
|
|
|
$this->_connection = $connection; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return \PDO |
122
|
|
|
*/ |
123
|
|
|
public function getConnection(): \PDO |
124
|
|
|
{ |
125
|
|
|
return $this->_connection; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|