Completed
Push — master ( ced319...978c91 )
by James Ekow Abaka
10:45
created

DriverAdapter::select()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4286
cc 3
eloc 7
nc 2
nop 1
crap 3
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)
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...
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)
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...
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)
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...
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)
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...
133
    {
134
        return new ModelDescription(
135
            self::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...
136
            $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...
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