|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
|
4
|
|
|
|
|
5
|
|
|
use ntentan\utils\Text; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A DriverAdaptr is a generic database adapter. |
|
9
|
|
|
* This adapter implements a lot of its operations through the atiaa library. |
|
10
|
|
|
* Driver specific implementation of this class only handle the conversion of |
|
11
|
|
|
* data types from the native datatypes of the database to the generic types |
|
12
|
|
|
* used in the nibii library. |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class DriverAdapter |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
protected $data; |
|
18
|
|
|
private static $defaultSettings; |
|
19
|
|
|
private $insertQuery; |
|
20
|
|
|
private $updateQuery; |
|
21
|
|
|
private $modelInstance; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* An instance of an atiaa driver. |
|
25
|
|
|
* @var \ntentan\atiaa\Driver |
|
26
|
|
|
*/ |
|
27
|
|
|
private static $db; |
|
28
|
|
|
protected $queryEngine; |
|
29
|
|
|
|
|
30
|
|
|
public function setData($data) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->data = $data; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Convert datatypes from the database system's native type to a generic type |
|
37
|
|
|
* supported by nibii. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $nativeType The native datatype |
|
40
|
|
|
* @return string The generic datatype for use in nibii. |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract public function mapDataTypes($nativeType); |
|
43
|
|
|
|
|
44
|
34 |
|
public static function getDriver() |
|
45
|
|
|
{ |
|
46
|
34 |
|
if(self::$db == null) { |
|
47
|
34 |
|
self::$db = \ntentan\atiaa\Driver::getConnection(self::$defaultSettings); |
|
48
|
34 |
|
self::$db->setCleanDefaults(true); |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
34 |
|
self::$db->getPDO()->setAttribute(\PDO::ATTR_AUTOCOMMIT, false); |
|
52
|
34 |
|
} catch (\PDOException $e) { |
|
53
|
|
|
// Just do nothing for drivers which do not allow turning off autocommit |
|
54
|
|
|
} |
|
55
|
34 |
|
} |
|
56
|
34 |
|
return self::$db; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* |
|
61
|
|
|
* |
|
62
|
|
|
* @param type $parameters |
|
63
|
|
|
* @return type |
|
64
|
|
|
*/ |
|
65
|
24 |
|
public function select($parameters) |
|
66
|
|
|
{ |
|
67
|
24 |
|
$result = self::getDriver()->query( |
|
68
|
24 |
|
$this->getQueryEngine()->getSelectQuery($parameters), |
|
69
|
24 |
|
$parameters->getBoundData() |
|
70
|
24 |
|
); |
|
71
|
|
|
|
|
72
|
24 |
|
if ($parameters->getFirstOnly() && isset($result[0])) { |
|
73
|
20 |
|
$result = $result[0]; |
|
74
|
20 |
|
} |
|
75
|
|
|
|
|
76
|
24 |
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function count($parameters) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
$result = self::getDriver()->query( |
|
82
|
|
|
$this->getQueryEngine()->getCountQuery($parameters), |
|
83
|
|
|
$parameters->getBoundData() |
|
84
|
|
|
); |
|
85
|
|
|
return $result[0]['count']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
private function initInsert() |
|
89
|
|
|
{ |
|
90
|
4 |
|
$this->insertQuery = $this->getQueryEngine() |
|
91
|
4 |
|
->getInsertQuery($this->modelInstance); |
|
92
|
4 |
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
private function initUpdate() |
|
95
|
2 |
|
{ |
|
96
|
2 |
|
$this->updateQuery = $this->getQueryEngine() |
|
97
|
2 |
|
->getUpdateQuery($this->modelInstance); |
|
98
|
2 |
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
public function insert($record) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
4 |
|
if($this->insertQuery === null) { |
|
103
|
4 |
|
$this->initInsert(); |
|
104
|
4 |
|
} |
|
105
|
4 |
|
return self::getDriver()->query($this->insertQuery, $record); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
public function update($record) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
2 |
|
if($this->updateQuery === null) { |
|
111
|
2 |
|
$this->initUpdate(); |
|
112
|
2 |
|
} |
|
113
|
2 |
|
return self::getDriver()->query($this->updateQuery, $record); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
6 |
|
public function bulkUpdate($data, $parameters) |
|
117
|
|
|
{ |
|
118
|
6 |
|
return self::getDriver()->query( |
|
119
|
6 |
|
$this->getQueryEngine()->getBulkUpdateQuery($data, $parameters), |
|
120
|
6 |
|
array_merge($data, $parameters->getBoundData()) |
|
121
|
6 |
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
2 |
|
public function delete($parameters) |
|
125
|
|
|
{ |
|
126
|
2 |
|
return self::getDriver()->query( |
|
127
|
2 |
|
$this->getQueryEngine()->getDeleteQuery($parameters), |
|
128
|
2 |
|
$parameters->getBoundData() |
|
129
|
2 |
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function describe($model, $relationships) |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
return new ModelDescription( |
|
135
|
|
|
self::getDriver()->describeTable($table)[$table], |
|
|
|
|
|
|
136
|
|
|
$relationships, function($type) { return $this->mapDataTypes($type); } |
|
|
|
|
|
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Set the settings used for creating default datastores. |
|
142
|
|
|
* @param array $settings |
|
143
|
|
|
*/ |
|
144
|
36 |
|
public static function setDefaultSettings($settings) |
|
145
|
|
|
{ |
|
146
|
36 |
|
self::$defaultSettings = $settings; |
|
147
|
36 |
|
} |
|
148
|
|
|
|
|
149
|
34 |
|
public static function getDefaultInstance() |
|
150
|
|
|
{ |
|
151
|
34 |
|
if (self::$defaultSettings['driver']) { |
|
152
|
34 |
|
$class = "\\ntentan\\nibii\\adapters\\" . Text::ucamelize(self::$defaultSettings['driver']) . "Adapter"; |
|
153
|
34 |
|
$instance = new $class(); |
|
154
|
34 |
|
} else { |
|
155
|
|
|
throw new \Exception("No datastore specified"); |
|
156
|
|
|
} |
|
157
|
34 |
|
return $instance; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* |
|
162
|
|
|
* @return \ntentan\nibii\QueryEngine |
|
163
|
|
|
*/ |
|
164
|
32 |
|
private function getQueryEngine() |
|
165
|
|
|
{ |
|
166
|
32 |
|
if ($this->queryEngine === null) { |
|
167
|
32 |
|
$this->queryEngine = new QueryEngine(); |
|
168
|
32 |
|
$this->queryEngine->setDriver(self::getDriver()); |
|
169
|
32 |
|
} |
|
170
|
32 |
|
return $this->queryEngine; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
36 |
|
public static function reset() |
|
174
|
|
|
{ |
|
175
|
36 |
|
if(self::$db !== null) { |
|
176
|
34 |
|
self::$db->disconnect(); |
|
177
|
34 |
|
self::$db = null; |
|
178
|
34 |
|
} |
|
179
|
36 |
|
} |
|
180
|
|
|
|
|
181
|
10 |
|
public function setModel($model) |
|
182
|
|
|
{ |
|
183
|
10 |
|
$this->modelInstance = $model; |
|
184
|
10 |
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
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.