|
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
|
|
|
public function select($parameters) { |
|
43
|
|
|
$result = Db::getDriver()->query( |
|
44
|
|
|
$this->getQueryEngine()->getSelectQuery($parameters), |
|
45
|
|
|
$parameters->getBoundData() |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
if ($parameters->getFirstOnly() && isset($result[0])) { |
|
49
|
|
|
$result = $result[0]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
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
|
|
|
private function initInsert() { |
|
63
|
|
|
$this->insertQuery = $this->getQueryEngine() |
|
64
|
|
|
->getInsertQuery($this->modelInstance); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function initUpdate() { |
|
68
|
|
|
$this->updateQuery = $this->getQueryEngine()->getUpdateQuery($this->modelInstance); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function insert($record) { |
|
|
|
|
|
|
72
|
|
|
if ($this->insertQuery === null) { |
|
73
|
|
|
$this->initInsert(); |
|
74
|
|
|
} |
|
75
|
|
|
return Db::getDriver()->query($this->insertQuery, $record); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function update($record) { |
|
|
|
|
|
|
79
|
|
|
if ($this->updateQuery === null) { |
|
80
|
|
|
$this->initUpdate(); |
|
81
|
|
|
} |
|
82
|
|
|
return Db::getDriver()->query($this->updateQuery, $record); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function bulkUpdate($data, $parameters) { |
|
|
|
|
|
|
86
|
|
|
return Db::getDriver()->query( |
|
87
|
|
|
$this->getQueryEngine()->getBulkUpdateQuery($data, $parameters), array_merge($data, $parameters->getBoundData()) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function delete($parameters) { |
|
|
|
|
|
|
92
|
|
|
return Db::getDriver()->query( |
|
93
|
|
|
$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
|
|
|
/** |
|
106
|
|
|
* |
|
107
|
|
|
* @return \ntentan\nibii\QueryEngine |
|
108
|
|
|
*/ |
|
109
|
|
|
private function getQueryEngine() { |
|
110
|
|
|
if ($this->queryEngine === null) { |
|
111
|
|
|
$this->queryEngine = new QueryEngine(); |
|
112
|
|
|
$this->queryEngine->setDriver(Db::getDriver()); |
|
113
|
|
|
} |
|
114
|
|
|
return $this->queryEngine; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function setModel($model) { |
|
118
|
|
|
$this->modelInstance = $model; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
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.