GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#25)
by Charlotte
02:36
created

Statement   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Test Coverage

Coverage 72.34%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 201
ccs 34
cts 47
cp 0.7234
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getColumns() 0 2 1
A getParams() 0 2 1
A isClosed() 0 2 1
A __destruct() 0 5 2
A getID() 0 2 1
A createCursor() 0 14 3
A runQuery() 0 2 1
A close() 0 12 2
A getQuery() 0 2 1
A execute() 0 11 2
1
<?php
2
/**
3
 * Plasma Driver MySQL component
4
 * Copyright 2018-2019 PlasmaPHP, All Rights Reserved
5
 *
6
 * Website: https://github.com/PlasmaPHP
7
 * License: https://github.com/PlasmaPHP/driver-mysql/blob/master/LICENSE
8
*/
9
10
namespace Plasma\Drivers\MySQL;
11
12
/**
13
 * Represents a Prepared Statement.
14
 */
15
class Statement implements \Plasma\StatementInterface {
16
    /**
17
     * @var \Plasma\ClientInterface
18
     */
19
    protected $client;
20
    
21
    /**
22
     * @var \Plasma\Drivers\MySQL\Driver
23
     */
24
    protected $driver;
25
    
26
    /**
27
     * @var mixed
28
     */
29
    protected $id;
30
    
31
    /**
32
     * @var string
33
     */
34
    protected $query;
35
    
36
    /**
37
     * @var string
38
     */
39
    protected $rewrittenQuery;
40
    
41
    /**
42
     * @var array
43
     */
44
    protected $rewrittenParams;
45
    
46
    /**
47
     * @var \Plasma\ColumnDefinitionInterface[]
48
     */
49
    protected $params;
50
    
51
    /**
52
     * @var \Plasma\ColumnDefinitionInterface[]
53
     */
54
    protected $columns;
55
    
56
    /**
57
     * @var bool
58
     */
59
    protected $closed = false;
60
    
61
    /**
62
     * Constructor.
63
     * @param \Plasma\ClientInterface              $client
64
     * @param \Plasma\Drivers\MySQL\Driver         $driver
65
     * @param mixed                                $id
66
     * @param string                               $query
67
     * @param string                               $rQuery
68
     * @param array                                $rParams
69
     * @param \Plasma\ColumnDefinitionInterface[]  $params
70
     * @param \Plasma\ColumnDefinitionInterface[]  $columns
71
     */
72 12
    function __construct(
73
        \Plasma\ClientInterface $client, \Plasma\Drivers\MySQL\Driver $driver, $id, string $query, string $rQuery, array $rParams, array $params, array $columns
74
    ) {
75 12
        $this->client = $client;
76 12
        $this->driver = $driver;
77
        
78 12
        $this->id = $id;
79 12
        $this->query = $query;
80 12
        $this->rewrittenQuery = $rQuery;
81 12
        $this->rewrittenParams = $rParams;
82 12
        $this->params = $params;
83 12
        $this->columns = $columns;
84 12
    }
85
    
86
    /**
87
     * Destructor. Runs once the instance goes out of scope.
88
     * Please do not rely on the destructor to properly close your statement.
89
     * ALWAYS explicitely close the statement once you're done.
90
     * @codeCoverageIgnore
91
     */
92
    function __destruct() {
93
        if(!$this->closed) {
94
            $this->close()->then(null, function () {
95
                // Error during implicit close, close the session
96
                $this->driver->close();
97
            });
98
        }
99
    }
100
    
101
    /**
102
     * Get the driver-dependent ID of this statement.
103
     * The return type can be of ANY type, as the ID depends on the driver and DBMS.
104
     * @return mixed
105
     */
106 1
    function getID() {
107 1
        return $this->id;
108
    }
109
    
110
    /**
111
     * Get the prepared query.
112
     * @return string
113
     */
114 1
    function getQuery(): string {
115 1
        return $this->query;
116
    }
117
    
118
    /**
119
     * Whether the statement has been closed.
120
     * @return bool
121
     */
122 2
    function isClosed(): bool {
123 2
        return $this->closed;
124
    }
125
    
126
    /**
127
     * Closes the prepared statement and frees the associated resources on the server.
128
     * Closing a statement more than once has no effect.
129
     * @return \React\Promise\PromiseInterface
130
     */
131 12
    function close(): \React\Promise\PromiseInterface {
132 12
        if($this->closed) {
133 1
            return \React\Promise\resolve();
134
        }
135
        
136 12
        $this->closed = true;
137
        
138 12
        $close = new \Plasma\Drivers\MySQL\Commands\StatementCloseCommand($this->driver, $this->id);
139 12
        $this->driver->executeCommand($close);
140
        
141
        return $close->getPromise()->then(function () {
142
            $this->client->checkinConnection($this->driver);
143 12
        });
144
    }
145
    
146
    /**
147
     * Executes the prepared statement. Resolves with a `QueryResult` instance.
148
     * @param array  $params
149
     * @return \React\Promise\PromiseInterface
150
     * @throws \Plasma\Exception  Thrown if the statement is executed after it has been closed, or thrown for insufficent or missing parameters.
151
     * @see \Plasma\QueryResultInterface
152
     */
153 7
    function execute(array $params = array()): \React\Promise\PromiseInterface {
154 7
        if($this->closed) {
155 1
            throw new \Plasma\Exception('Statement has been closed');
156
        }
157
        
158 6
        $params = \Plasma\Utility::replaceParameters($this->rewrittenParams, $params);
159
        
160 4
        $execute = new \Plasma\Drivers\MySQL\Commands\StatementExecuteCommand($this->driver, $this->id, $this->query, $params, $this->params);
161 4
        $this->driver->executeCommand($execute);
162
        
163 4
        return $execute->getPromise();
164
    }
165
    
166
    /**
167
     * Runs the given querybuilder on the underlying driver instance. However the query will be ignored, only the parameters are used.
168
     * The driver CAN throw an exception if the given querybuilder is not supported.
169
     * An example would be a SQL querybuilder and a Cassandra driver.
170
     * @param \Plasma\QueryBuilderInterface  $query
171
     * @return \React\Promise\PromiseInterface
172
     * @throws \Plasma\Exception
173
     */
174
    function runQuery(\Plasma\QueryBuilderInterface $query): \React\Promise\PromiseInterface {
175
        return $this->execute($query->getParameters());
176
    }
177
    
178
    /**
179
     * Creates a cursor.
180
     * @param array  $params
181
     * @return \React\Promise\PromiseInterface
182
     * @throws \Plasma\Exception  Thrown if the statement is executed after it has been closed, or if it's not a SELECT query, or for insufficent or missing parameters.
183
     * @internal
184
     */
185
    function createCursor(array $params = array()): \React\Promise\PromiseInterface {
186
        if($this->closed) {
187
            throw new \Plasma\Exception('Statement has been closed');
188
        } elseif(empty($this->columns)) {
189
            throw new \Plasma\Exception('Query is not a SELECT query');
190
        }
191
        
192
        $params = \Plasma\Utility::replaceParameters($this->rewrittenParams, $params);
193
        
194
        $execute = new \Plasma\Drivers\MySQL\Commands\StatementExecuteCommand($this->driver, $this->id, $this->query, $params, $this->params, 0x05);
195
        $this->driver->executeCommand($execute);
196
        
197
        return $execute->getPromise()->then(function () {
198
            return (new \Plasma\Drivers\MySQL\StatementCursor($this->driver, $this));
199
        });
200
    }
201
    
202
    /**
203
     * Get the parsed parameters.
204
     * @return \Plasma\ColumnDefinitionInterface[]
205
     */
206 1
    function getParams(): array {
207 1
        return $this->params;
208
    }
209
    
210
    /**
211
     * Get the columns.
212
     * @return \Plasma\ColumnDefinitionInterface[]
213
     */
214 1
    function getColumns(): array {
215 1
        return $this->columns;
216
    }
217
}
218