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