Completed
Push — master ( 7e591a...ced319 )
by James Ekow Abaka
02:48
created

DriverAdapter::getDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 $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)
37
    {
38
        $this->data = $data;
39
    }
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
88
    {
89
        $result = self::$db->query(
90
            $this->getQueryEngine()->getCountQuery($parameters),
91
            $parameters->getBoundData()
92
        );
93
        return $result[0]['count'];
94
    }
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)
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142
        return new ModelDescription(
143
            $this->getDriver()->describeTable($table)[$table],
0 ignored issues
show
Bug introduced by
The variable $table does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
144
            $relationships, function($type) { return $this->mapDataTypes($type); }
0 ignored issues
show
Unused Code introduced by
The call to ModelDescription::__construct() has too many arguments starting with $relationships.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
145
        );
146
    }
147
148
    /**
149
     * Set the settings used for creating default datastores.
150
     * @param array $settings
151
     */
152 36
    public static function setDefaultSettings($settings)
153
    {
154 36
        self::$defaultSettings = $settings;
155 36
    }
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");
166
        }
167 34
        return $instance;
168
    }
169
170
    /**
171
     *
172
     * @return \ntentan\nibii\QueryEngine
173
     */
174 32
    private function getQueryEngine()
175
    {
176 32
        if ($this->queryEngine === null) {
177 32
            $this->queryEngine = new QueryEngine();
178 32
            $this->queryEngine->setDriver(self::$db);
179 32
        }
180 32
        return $this->queryEngine;
181
    }
182
183
    /**
184
     *
185
     * @return \ntentan\atiaa\Driver
186
     */
187 26
    public function getDriver()
188
    {
189 26
        return self::$db;
190
    }
191
192 36
    public static function reset()
193
    {
194 36
        if(self::$db !== null) {
195 34
            self::$db->disconnect();
196 34
            self::$db = null;
197 34
        }
198 36
    }
199
200 10
    public function setModel($model)
201
    {
202 10
        $this->modelInstance = $model;
203 10
    }
204
}
205