|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2014-2018 James Ekow Abaka Ainooson |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace ntentan\nibii; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* DriverAdapter provides a generic interface through which specific database operations can be performed. |
|
31
|
|
|
* It. |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract class DriverAdapter |
|
34
|
|
|
{ |
|
35
|
|
|
protected $data; |
|
36
|
|
|
private $insertQuery; |
|
37
|
|
|
private $updateQuery; |
|
38
|
|
|
private $modelInstance; |
|
39
|
|
|
protected $queryEngine; |
|
40
|
|
|
private $driver; |
|
41
|
|
|
|
|
42
|
36 |
|
public function setContext(ORMContext $context) |
|
43
|
|
|
{ |
|
44
|
36 |
|
$this->driver = $context->getDbContext()->getDriver(); |
|
45
|
36 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function setData($data) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->data = $data; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Convert datatypes from the database system's native type to a generic type |
|
54
|
|
|
* supported by nibii. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $nativeType The native datatype |
|
57
|
|
|
* |
|
58
|
|
|
* @return string The generic datatype for use in nibii. |
|
59
|
|
|
*/ |
|
60
|
|
|
abstract public function mapDataTypes($nativeType); |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param QueryParameters $parameters |
|
64
|
|
|
* |
|
65
|
|
|
* @return type |
|
66
|
|
|
*/ |
|
67
|
24 |
|
public function select($parameters) |
|
68
|
|
|
{ |
|
69
|
24 |
|
$result = $this->driver->query( |
|
70
|
24 |
|
$this->getQueryEngine()->getSelectQuery($parameters), |
|
71
|
24 |
|
$parameters->getBoundData() |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
24 |
|
if ($parameters->getFirstOnly() && isset($result[0])) { |
|
75
|
20 |
|
$result = $result[0]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
24 |
|
return $result; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param QueryParameters $parameters |
|
83
|
|
|
*/ |
|
84
|
|
|
public function count($parameters) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
$result = $this->driver->query($this->getQueryEngine()->getCountQuery($parameters), $parameters->getBoundData()); |
|
87
|
|
|
|
|
88
|
|
|
return $result[0]['count']; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
4 |
|
private function initInsert() |
|
92
|
|
|
{ |
|
93
|
4 |
|
$this->insertQuery = $this->getQueryEngine()->getInsertQuery($this->modelInstance); |
|
94
|
4 |
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
private function initUpdate() |
|
97
|
|
|
{ |
|
98
|
2 |
|
$this->updateQuery = $this->getQueryEngine()->getUpdateQuery($this->modelInstance); |
|
99
|
2 |
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
public function insert($record) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
4 |
|
if ($this->insertQuery === null) { |
|
104
|
4 |
|
$this->initInsert(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
4 |
|
return $this->driver->query($this->insertQuery, $record); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
public function update($record) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
2 |
|
if ($this->updateQuery === null) { |
|
113
|
2 |
|
$this->initUpdate(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
return $this->driver->query($this->updateQuery, $record); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param QueryParameters $parameters |
|
121
|
|
|
*/ |
|
122
|
6 |
|
public function bulkUpdate($data, $parameters) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
6 |
|
return $this->driver->query( |
|
125
|
6 |
|
$this->getQueryEngine()->getBulkUpdateQuery($data, $parameters), array_merge($data, $parameters->getBoundData()) |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param QueryParameters $parameters |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function delete($parameters) |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
2 |
|
return $this->driver->query( |
|
135
|
2 |
|
$this->getQueryEngine()->getDeleteQuery($parameters), $parameters->getBoundData() |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function describe($model, $relationships) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
return new ModelDescription( |
|
142
|
|
|
$this->driver->describeTable($table)[$table], $relationships, function ($type) { |
|
|
|
|
|
|
143
|
|
|
return $this->mapDataTypes($type); |
|
144
|
|
|
} |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return \ntentan\nibii\QueryEngine |
|
150
|
|
|
*/ |
|
151
|
32 |
|
private function getQueryEngine() |
|
152
|
|
|
{ |
|
153
|
32 |
|
if ($this->queryEngine === null) { |
|
154
|
32 |
|
$this->queryEngine = new QueryEngine(); |
|
155
|
32 |
|
$this->queryEngine->setDriver($this->driver); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
32 |
|
return $this->queryEngine; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param RecordWrapper $model |
|
163
|
|
|
*/ |
|
164
|
36 |
|
public function setModel($model) |
|
165
|
|
|
{ |
|
166
|
36 |
|
$this->modelInstance = $model; |
|
167
|
36 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function getDriver() |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
return $this->driver; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.