|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Soluble\DbWrapper\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Soluble\DbWrapper\Exception; |
|
6
|
|
|
use ArrayObject; |
|
7
|
|
|
use PDO; |
|
8
|
|
|
|
|
9
|
|
|
class PdoMysqlAdapter implements AdapterInterface |
|
10
|
|
|
{ |
|
11
|
|
|
use Mysql\MysqlCommonTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
* @var PDO |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $connection; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor |
|
22
|
|
|
* |
|
23
|
|
|
* @throws Exception\InvalidArgumentException |
|
24
|
|
|
* @param PDO $connection |
|
25
|
|
|
*/ |
|
26
|
5 |
|
public function __construct(PDO $connection) |
|
27
|
|
|
{ |
|
28
|
5 |
|
if ($connection->getAttribute(\PDO::ATTR_DRIVER_NAME) != 'mysql') { |
|
29
|
|
|
$msg = __CLASS__ . " requires pdo connection to be 'mysql'"; |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
5 |
|
$this->connection = $connection; |
|
32
|
5 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function quoteValue($value) |
|
39
|
|
|
{ |
|
40
|
1 |
|
return $this->connection->quote($value); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function execute($query) |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
2 |
|
$ret = $this->connection->exec($query); |
|
50
|
2 |
|
if ($ret === false) { |
|
51
|
1 |
|
throw new Exception\InvalidArgumentException("Cannot execute [$query]."); |
|
52
|
|
|
} |
|
53
|
2 |
|
} catch (Exception\InvalidArgumentException $e) { |
|
54
|
1 |
|
throw $e; |
|
55
|
|
|
} catch (\Exception $e) { |
|
56
|
|
|
$msg = "PDOException : {$e->getMessage()} [$query]"; |
|
57
|
|
|
throw new Exception\InvalidArgumentException($msg); |
|
58
|
|
|
} |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function query($query) |
|
65
|
|
|
{ |
|
66
|
|
|
try { |
|
67
|
2 |
|
$stmt = $this->connection->query($query, \PDO::FETCH_ASSOC); |
|
68
|
2 |
|
if ($stmt === false) { |
|
69
|
1 |
|
throw new Exception\InvalidArgumentException("Query cannot be executed [$query]."); |
|
70
|
|
|
} |
|
71
|
2 |
|
$results = new ArrayObject(); |
|
72
|
2 |
|
foreach ($stmt as $row) { |
|
73
|
2 |
|
$results->append($row); |
|
74
|
2 |
|
} |
|
75
|
2 |
|
} catch (Exception\InvalidArgumentException $e) { |
|
76
|
1 |
|
throw $e; |
|
77
|
|
|
} catch (\Exception $e) { |
|
78
|
|
|
$msg = "PDOException : {$e->getMessage()} [$query]"; |
|
79
|
|
|
throw new Exception\InvalidArgumentException($msg); |
|
80
|
|
|
} |
|
81
|
2 |
|
return $results; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.