1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WebHemi. |
4
|
|
|
* |
5
|
|
|
* PHP version 7.1 |
6
|
|
|
* |
7
|
|
|
* @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com) |
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* @link http://www.gixx-web.com |
11
|
|
|
*/ |
12
|
|
|
declare(strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace WebHemi\Data\Connector\PDO\SQLite; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use RuntimeException; |
18
|
|
|
use WebHemi\Data\DriverInterface; |
19
|
|
|
use WebHemi\Data\Connector\PDO\MySQL\ConnectorAdapter as MySQLAdapter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ConnectorAdapter. |
23
|
|
|
* |
24
|
|
|
* @codeCoverageIgnore - Used by unit test only. |
25
|
|
|
*/ |
26
|
|
|
class ConnectorAdapter extends MySQLAdapter |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* ConnectorAdapter constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $name |
32
|
|
|
* @param DriverInterface $dataDriver |
33
|
|
|
* @throws InvalidArgumentException |
34
|
|
|
*/ |
35
|
|
|
public function __construct(string $name, DriverInterface $dataDriver) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($name, $dataDriver); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Runs initialization. |
42
|
|
|
*/ |
43
|
|
|
protected function init() |
44
|
|
|
{ |
45
|
|
|
if (!$this->dataDriver instanceof DriverAdapter) { |
46
|
|
|
$type = gettype($this->dataDriver); |
47
|
|
|
|
48
|
|
|
if ($type == 'object') { |
49
|
|
|
$type = get_class($this->dataDriver); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$message = sprintf( |
53
|
|
|
'Can\'t create %s instance. The parameter must be an instance of SQLiteDriver, %s given.', |
54
|
|
|
__CLASS__, |
55
|
|
|
$type |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
throw new InvalidArgumentException($message, 1001); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Insert or update entity in the storage. |
64
|
|
|
* |
65
|
|
|
* @param int $identifier |
66
|
|
|
* @param array $data |
67
|
|
|
* @throws RuntimeException |
68
|
|
|
* @return int The ID of the saved entity in the storage |
69
|
|
|
*/ |
70
|
|
|
public function saveData(? int $identifier = null, array $data = []) : int |
71
|
|
|
{ |
72
|
|
|
return empty($identifier) ? $this->insertData($data) : $this->updateData($identifier, $data); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Insert data. |
77
|
|
|
* |
78
|
|
|
* @param array $data |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
protected function insertData(array $data) : int |
82
|
|
|
{ |
83
|
|
|
$query = "INSERT INTO {$this->dataGroup}"; |
84
|
|
|
|
85
|
|
|
$queryColumns = []; |
86
|
|
|
$queryData = []; |
87
|
|
|
$queryBind = []; |
88
|
|
|
|
89
|
|
|
foreach ($data as $fieldName => $value) { |
90
|
|
|
$queryColumns[] = $fieldName; |
91
|
|
|
$queryData[] = "?"; |
92
|
|
|
$queryBind[] = $value; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$query .= ' ('.implode(', ', $queryColumns).') VALUES ('.implode(', ', $queryData).')'; |
96
|
|
|
|
97
|
|
|
$statement = $this->dataDriver->prepare($query); |
98
|
|
|
|
99
|
|
|
if (!$statement) { |
100
|
|
|
throw new RuntimeException('Query error', 1002); |
101
|
|
|
} |
102
|
|
|
$this->bindValuesToStatement($statement, $queryBind); |
103
|
|
|
$statement->execute(); |
104
|
|
|
|
105
|
|
|
return (int) $this->dataDriver->lastInsertId(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Update data. |
110
|
|
|
* |
111
|
|
|
* @param int $identifier |
112
|
|
|
* @param array $data |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
protected function updateData(int $identifier, array $data) : int |
116
|
|
|
{ |
117
|
|
|
$query = "UPDATE {$this->dataGroup}"; |
118
|
|
|
|
119
|
|
|
$queryData = []; |
120
|
|
|
$queryBind = []; |
121
|
|
|
|
122
|
|
|
foreach ($data as $fieldName => $value) { |
123
|
|
|
$queryData[] = "{$fieldName} = ?"; |
124
|
|
|
$queryBind[] = $value; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$query .= ' SET '.implode(', ', $queryData)." WHERE {$this->idKey} = ?"; |
128
|
|
|
$queryBind[] = $identifier; |
129
|
|
|
$statement = $this->dataDriver->prepare($query); |
130
|
|
|
|
131
|
|
|
if (!$statement) { |
132
|
|
|
throw new RuntimeException('Query error', 1003); |
133
|
|
|
} |
134
|
|
|
$this->bindValuesToStatement($statement, $queryBind); |
135
|
|
|
$statement->execute(); |
136
|
|
|
|
137
|
|
|
return $identifier; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|