1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Database; |
4
|
|
|
|
5
|
|
|
use DrMVC\Config\ConfigInterface; |
6
|
|
|
use DrMVC\Database\Drivers\QueryInterface; |
7
|
|
|
|
8
|
|
|
class Model implements ModelInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var QueryInterface |
12
|
|
|
*/ |
13
|
|
|
private $_instance; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default connection name |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $connection = Database::DEFAULT_CONNECTION; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Name of collection for query |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
protected $collection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Model constructor. |
29
|
|
|
* @param ConfigInterface $config |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ConfigInterface $config) |
32
|
|
|
{ |
33
|
|
|
// Create database object with config from above |
34
|
|
|
$config_db = $this->getConfigDB($config); |
35
|
|
|
$database = new Database($config_db); |
36
|
|
|
|
37
|
|
|
// Get current collection name |
38
|
|
|
$collection = $this->getCollection(); |
39
|
|
|
// Extract instance created by driver and put collection name |
40
|
|
|
$instance = $database->getInstance($collection); |
41
|
|
|
|
42
|
|
|
// Keep driver's instance as local parameter |
43
|
|
|
$this->setInstance($instance); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Extract configuration of current database |
48
|
|
|
* |
49
|
|
|
* @param ConfigInterface $config |
50
|
|
|
* @return ConfigInterface |
51
|
|
|
*/ |
52
|
|
|
private function getConfigDB(ConfigInterface $config): ConfigInterface |
53
|
|
|
{ |
54
|
|
|
// get current connection |
55
|
|
|
$connection = $this->getConnection(); |
56
|
|
|
// Get config of required connection |
57
|
|
|
$config_db = $config->get($connection); |
58
|
|
|
try { |
59
|
|
|
if (null === $config_db) { |
60
|
|
|
throw new Exception("Connection \"$connection\" is not found in database config"); |
61
|
|
|
} |
62
|
|
|
} catch (Exception $e) { |
63
|
|
|
// __constructor |
64
|
|
|
} |
65
|
|
|
return $config_db; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get current database name |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getConnection(): string |
74
|
|
|
{ |
75
|
|
|
return $this->connection; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get current collection |
80
|
|
|
* |
81
|
|
|
* @return string|null |
82
|
|
|
*/ |
83
|
|
|
public function getCollection() |
84
|
|
|
{ |
85
|
|
|
$collection = $this->collection; |
86
|
|
|
try { |
87
|
|
|
if (null === $collection) { |
88
|
|
|
throw new Exception('Collection is not set'); |
89
|
|
|
} |
90
|
|
|
} catch (Exception $e) { |
91
|
|
|
// __constructor |
92
|
|
|
} |
93
|
|
|
return $collection; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set database instance |
98
|
|
|
* |
99
|
|
|
* @param QueryInterface $instance |
100
|
|
|
*/ |
101
|
|
|
private function setInstance(QueryInterface $instance) |
102
|
|
|
{ |
103
|
|
|
$this->_instance = $instance; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get database instance |
108
|
|
|
* |
109
|
|
|
* @return QueryInterface |
110
|
|
|
*/ |
111
|
|
|
public function getInstance(): QueryInterface |
112
|
|
|
{ |
113
|
|
|
return $this->_instance; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function update(array $data, array $where = []) |
117
|
|
|
{ |
118
|
|
|
return $this->getInstance()->update($data, $where); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function delete(array $where) |
122
|
|
|
{ |
123
|
|
|
return $this->getInstance()->delete($where); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function exec(string $query) |
127
|
|
|
{ |
128
|
|
|
return $this->getInstance()->exec($query); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function insert(array $data) |
132
|
|
|
{ |
133
|
|
|
return $this->getInstance()->insert($data); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function select(string $query, array $data = []) |
137
|
|
|
{ |
138
|
|
|
return $this->getInstance()->select($query, $data); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function truncate() |
142
|
|
|
{ |
143
|
|
|
return $this->getInstance()->truncate(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|