1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A DriverAdaptr is a generic database adapter. |
7
|
|
|
* This adapter implements a lot of its operations through the atiaa library. |
8
|
|
|
* Driver specific implementation of this class only handle the conversion of |
9
|
|
|
* data types from the native datatypes of the database to the generic types |
10
|
|
|
* used in the nibii library. |
11
|
|
|
*/ |
12
|
|
|
abstract class DriverAdapter { |
13
|
|
|
|
14
|
|
|
protected $data; |
15
|
|
|
private $insertQuery; |
16
|
|
|
private $updateQuery; |
17
|
|
|
private $modelInstance; |
18
|
|
|
protected $queryEngine; |
19
|
|
|
private $driver; |
20
|
|
|
|
21
|
37 |
|
public function setContext(ORMContext $context) { |
22
|
37 |
|
$this->driver = $context->getDbContext()->getDriver(); |
23
|
37 |
|
} |
24
|
|
|
|
25
|
|
|
public function setData($data) { |
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 QueryParameters $parameters |
42
|
|
|
* @return type |
43
|
|
|
*/ |
44
|
25 |
|
public function select($parameters) { |
45
|
25 |
|
$result = $this->driver->query( |
46
|
25 |
|
$this->getQueryEngine()->getSelectQuery($parameters), |
47
|
25 |
|
$parameters->getBoundData() |
|
|
|
|
48
|
|
|
); |
49
|
|
|
|
50
|
25 |
|
if ($parameters->getFirstOnly() && isset($result[0])) { |
51
|
20 |
|
$result = $result[0]; |
52
|
|
|
} |
53
|
|
|
|
54
|
25 |
|
return $result; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param QueryParameters $parameters |
59
|
|
|
*/ |
60
|
|
|
public function count($parameters) { |
|
|
|
|
61
|
|
|
$result = $this->driver->query( |
62
|
|
|
$this->getQueryEngine()->getCountQuery($parameters), $parameters->getBoundData() |
|
|
|
|
63
|
|
|
); |
64
|
|
|
return $result[0]['count']; |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
private function initInsert() { |
68
|
4 |
|
$this->insertQuery = $this->getQueryEngine() |
69
|
4 |
|
->getInsertQuery($this->modelInstance); |
70
|
4 |
|
} |
71
|
|
|
|
72
|
2 |
|
private function initUpdate() { |
73
|
2 |
|
$this->updateQuery = $this->getQueryEngine()->getUpdateQuery($this->modelInstance); |
74
|
2 |
|
} |
75
|
|
|
|
76
|
4 |
|
public function insert($record) { |
77
|
4 |
|
if ($this->insertQuery === null) { |
78
|
4 |
|
$this->initInsert(); |
79
|
|
|
} |
80
|
4 |
|
return $this->driver->query($this->insertQuery, $record); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function update($record) { |
84
|
2 |
|
if ($this->updateQuery === null) { |
85
|
2 |
|
$this->initUpdate(); |
86
|
|
|
} |
87
|
2 |
|
return $this->driver->query($this->updateQuery, $record); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param QueryParameters $parameters |
92
|
|
|
*/ |
93
|
6 |
|
public function bulkUpdate($data, $parameters) { |
94
|
6 |
|
return $this->driver->query( |
95
|
6 |
|
$this->getQueryEngine()->getBulkUpdateQuery($data, $parameters), array_merge($data, $parameters->getBoundData()) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param QueryParameters $parameters |
101
|
|
|
*/ |
102
|
2 |
|
public function delete($parameters) { |
103
|
2 |
|
return $this->driver->query( |
104
|
2 |
|
$this->getQueryEngine()->getDeleteQuery($parameters), $parameters->getBoundData() |
|
|
|
|
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function describe($model, $relationships) { |
|
|
|
|
109
|
|
|
return new ModelDescription( |
110
|
|
|
$this->driver->describeTable($table)[$table], $relationships, function($type) { |
|
|
|
|
111
|
|
|
return $this->mapDataTypes($type); |
112
|
|
|
} |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
* @return \ntentan\nibii\QueryEngine |
119
|
|
|
*/ |
120
|
33 |
|
private function getQueryEngine() { |
121
|
33 |
|
if ($this->queryEngine === null) { |
122
|
33 |
|
$this->queryEngine = new QueryEngine(); |
123
|
33 |
|
$this->queryEngine->setDriver($this->driver); |
124
|
|
|
} |
125
|
33 |
|
return $this->queryEngine; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param RecordWrapper $model |
130
|
|
|
*/ |
131
|
37 |
|
public function setModel($model) { |
132
|
37 |
|
$this->modelInstance = $model; |
133
|
37 |
|
} |
134
|
|
|
|
135
|
|
|
public function getDriver() { |
136
|
|
|
return $this->driver; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: