1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
4
|
|
|
|
5
|
|
|
use ntentan\utils\Text; |
6
|
|
|
use ntentan\atiaa\Db; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A DriverAdaptr is a generic database adapter. |
10
|
|
|
* This adapter implements a lot of its operations through the atiaa library. |
11
|
|
|
* Driver specific implementation of this class only handle the conversion of |
12
|
|
|
* data types from the native datatypes of the database to the generic types |
13
|
|
|
* used in the nibii library. |
14
|
|
|
*/ |
15
|
|
|
abstract class DriverAdapter { |
16
|
|
|
|
17
|
|
|
protected $data; |
18
|
|
|
private $insertQuery; |
19
|
|
|
private $updateQuery; |
20
|
|
|
private $modelInstance; |
21
|
|
|
protected $queryEngine; |
22
|
|
|
|
23
|
|
|
public function setData($data) { |
24
|
|
|
$this->data = $data; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Convert datatypes from the database system's native type to a generic type |
29
|
|
|
* supported by nibii. |
30
|
|
|
* |
31
|
|
|
* @param string $nativeType The native datatype |
32
|
|
|
* @return string The generic datatype for use in nibii. |
33
|
|
|
*/ |
34
|
|
|
abstract public function mapDataTypes($nativeType); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* |
39
|
|
|
* @param type $parameters |
40
|
|
|
* @return type |
41
|
|
|
*/ |
42
|
26 |
|
public function select($parameters) { |
43
|
26 |
|
$result = Db::getDriver()->query( |
44
|
26 |
|
$this->getQueryEngine()->getSelectQuery($parameters), |
45
|
26 |
|
$parameters->getBoundData() |
46
|
|
|
); |
47
|
|
|
|
48
|
24 |
|
if ($parameters->getFirstOnly() && isset($result[0])) { |
49
|
20 |
|
$result = $result[0]; |
50
|
|
|
} |
51
|
|
|
|
52
|
24 |
|
return $result; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function count($parameters) { |
|
|
|
|
56
|
|
|
$result = Db::getDriver()->query( |
57
|
|
|
$this->getQueryEngine()->getCountQuery($parameters), $parameters->getBoundData() |
58
|
|
|
); |
59
|
|
|
return $result[0]['count']; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
private function initInsert() { |
63
|
4 |
|
$this->insertQuery = $this->getQueryEngine() |
64
|
4 |
|
->getInsertQuery($this->modelInstance); |
65
|
4 |
|
} |
66
|
|
|
|
67
|
2 |
|
private function initUpdate() { |
68
|
2 |
|
$this->updateQuery = $this->getQueryEngine()->getUpdateQuery($this->modelInstance); |
69
|
2 |
|
} |
70
|
|
|
|
71
|
4 |
|
public function insert($record) { |
72
|
4 |
|
if ($this->insertQuery === null) { |
73
|
4 |
|
$this->initInsert(); |
74
|
|
|
} |
75
|
4 |
|
return Db::getDriver()->query($this->insertQuery, $record); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
public function update($record) { |
79
|
2 |
|
if ($this->updateQuery === null) { |
80
|
2 |
|
$this->initUpdate(); |
81
|
|
|
} |
82
|
2 |
|
return Db::getDriver()->query($this->updateQuery, $record); |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
public function bulkUpdate($data, $parameters) { |
86
|
6 |
|
return Db::getDriver()->query( |
87
|
6 |
|
$this->getQueryEngine()->getBulkUpdateQuery($data, $parameters), array_merge($data, $parameters->getBoundData()) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
public function delete($parameters) { |
92
|
2 |
|
return Db::getDriver()->query( |
93
|
2 |
|
$this->getQueryEngine()->getDeleteQuery($parameters), $parameters->getBoundData() |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function describe($model, $relationships) { |
|
|
|
|
98
|
|
|
return new ModelDescription( |
99
|
|
|
Db::getDriver()->describeTable($table)[$table], $relationships, function($type) { |
|
|
|
|
100
|
|
|
return $this->mapDataTypes($type); |
101
|
|
|
} |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
38 |
|
public static function getDefaultInstance() { |
106
|
38 |
|
return \ntentan\panie\InjectionContainer::resolve(DriverAdapter::class); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
* @return \ntentan\nibii\QueryEngine |
112
|
|
|
*/ |
113
|
34 |
|
private function getQueryEngine() { |
114
|
34 |
|
if ($this->queryEngine === null) { |
115
|
34 |
|
$this->queryEngine = new QueryEngine(); |
116
|
34 |
|
$this->queryEngine->setDriver(Db::getDriver()); |
117
|
|
|
} |
118
|
34 |
|
return $this->queryEngine; |
119
|
|
|
} |
120
|
|
|
|
121
|
38 |
|
public function setModel($model) { |
122
|
38 |
|
$this->modelInstance = $model; |
123
|
38 |
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
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
@return
annotation as described here.