|
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
|
|
|
|
|
18
|
|
|
protected $data; |
|
19
|
|
|
private $insertQuery; |
|
20
|
|
|
private $updateQuery; |
|
21
|
|
|
private $modelInstance; |
|
22
|
|
|
protected $queryEngine; |
|
23
|
|
|
|
|
24
|
|
|
public function setData($data) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->data = $data; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Convert datatypes from the database system's native type to a generic type |
|
31
|
|
|
* supported by nibii. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $nativeType The native datatype |
|
34
|
|
|
* @return string The generic datatype for use in nibii. |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract public function mapDataTypes($nativeType); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
* |
|
41
|
|
|
* @param type $parameters |
|
42
|
|
|
* @return type |
|
43
|
|
|
*/ |
|
44
|
24 |
|
public function select($parameters) |
|
45
|
|
|
{ |
|
46
|
24 |
|
$result = Db::getDriver()->query( |
|
47
|
24 |
|
$this->getQueryEngine()->getSelectQuery($parameters), |
|
48
|
24 |
|
$parameters->getBoundData() |
|
49
|
24 |
|
); |
|
50
|
|
|
|
|
51
|
24 |
|
if ($parameters->getFirstOnly() && isset($result[0])) { |
|
52
|
20 |
|
$result = $result[0]; |
|
53
|
20 |
|
} |
|
54
|
|
|
|
|
55
|
24 |
|
return $result; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function count($parameters) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$result = Db::getDriver()->query( |
|
61
|
|
|
$this->getQueryEngine()->getCountQuery($parameters), |
|
62
|
|
|
$parameters->getBoundData() |
|
63
|
|
|
); |
|
64
|
|
|
return $result[0]['count']; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
4 |
|
private function initInsert() |
|
68
|
|
|
{ |
|
69
|
4 |
|
$this->insertQuery = $this->getQueryEngine() |
|
70
|
4 |
|
->getInsertQuery($this->modelInstance); |
|
71
|
4 |
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
private function initUpdate() |
|
74
|
|
|
{ |
|
75
|
2 |
|
$this->updateQuery = $this->getQueryEngine() |
|
76
|
2 |
|
->getUpdateQuery($this->modelInstance); |
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
public function insert($record) |
|
80
|
|
|
{ |
|
81
|
4 |
|
if($this->insertQuery === null) { |
|
82
|
4 |
|
$this->initInsert(); |
|
83
|
4 |
|
} |
|
84
|
4 |
|
return Db::getDriver()->query($this->insertQuery, $record); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
public function update($record) |
|
88
|
|
|
{ |
|
89
|
2 |
|
if($this->updateQuery === null) { |
|
90
|
2 |
|
$this->initUpdate(); |
|
91
|
2 |
|
} |
|
92
|
2 |
|
return Db::getDriver()->query($this->updateQuery, $record); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
public function bulkUpdate($data, $parameters) |
|
96
|
|
|
{ |
|
97
|
6 |
|
return Db::getDriver()->query( |
|
98
|
6 |
|
$this->getQueryEngine()->getBulkUpdateQuery($data, $parameters), |
|
99
|
6 |
|
array_merge($data, $parameters->getBoundData()) |
|
100
|
6 |
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
public function delete($parameters) |
|
104
|
|
|
{ |
|
105
|
2 |
|
return Db::getDriver()->query( |
|
106
|
2 |
|
$this->getQueryEngine()->getDeleteQuery($parameters), |
|
107
|
2 |
|
$parameters->getBoundData() |
|
108
|
2 |
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function describe($model, $relationships) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
return new ModelDescription( |
|
114
|
|
|
Db::getDriver()->describeTable($table)[$table], |
|
|
|
|
|
|
115
|
|
|
$relationships, function($type) { return $this->mapDataTypes($type); } |
|
|
|
|
|
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
32 |
|
public static function getDefaultInstance() |
|
120
|
|
|
{ |
|
121
|
32 |
|
return \ntentan\panie\InjectionContainer::resolve(DriverAdapter::class); |
|
122
|
|
|
/*$driver = Db::getDefaultSettings()['driver']; |
|
|
|
|
|
|
123
|
|
|
if ($driver) { |
|
124
|
|
|
$class = "\\ntentan\\nibii\\adapters\\" . Text::ucamelize($driver) . "Adapter"; |
|
125
|
|
|
$instance = new $class(); |
|
126
|
|
|
} else { |
|
127
|
|
|
throw new \Exception("No datastore specified"); |
|
128
|
|
|
} |
|
129
|
|
|
return $instance;*/ |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* |
|
134
|
|
|
* @return \ntentan\nibii\QueryEngine |
|
135
|
|
|
*/ |
|
136
|
32 |
|
private function getQueryEngine() |
|
137
|
|
|
{ |
|
138
|
32 |
|
if ($this->queryEngine === null) { |
|
139
|
32 |
|
$this->queryEngine = new QueryEngine(); |
|
140
|
32 |
|
$this->queryEngine->setDriver(Db::getDriver()); |
|
141
|
32 |
|
} |
|
142
|
32 |
|
return $this->queryEngine; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
10 |
|
public function setModel($model) |
|
146
|
|
|
{ |
|
147
|
10 |
|
$this->modelInstance = $model; |
|
148
|
10 |
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
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.