1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soluble\DbWrapper\Adapter\Pdo; |
4
|
|
|
|
5
|
|
|
use Soluble\DbWrapper\Exception; |
6
|
|
|
use Soluble\DbWrapper\Adapter\AdapterInterface; |
7
|
|
|
use Soluble\DbWrapper\Result\Resultset; |
8
|
|
|
|
9
|
|
|
abstract class GenericPdo implements AdapterInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \PDO |
13
|
|
|
*/ |
14
|
|
|
protected $resource; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
10 |
|
public function query($query, $resultsetType = Resultset::TYPE_ARRAY) |
20
|
|
|
{ |
21
|
|
|
try { |
22
|
|
|
//$query = "select * from product"; |
23
|
10 |
|
$stmt = $this->resource->prepare($query); |
24
|
|
|
|
25
|
10 |
|
if ($stmt === false) { |
26
|
1 |
|
$error = $this->resource->errorInfo(); |
27
|
1 |
|
throw new Exception\InvalidArgumentException($error[2]); |
28
|
|
|
} |
29
|
|
|
|
30
|
10 |
|
if (!$stmt->execute()) { |
31
|
4 |
|
throw new Exception\InvalidArgumentException( |
32
|
4 |
|
sprintf('Statement could not be executed (%s)', implode(' - ', $this->resource->errorInfo())) |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
10 |
|
$results = new Resultset($resultsetType); |
37
|
10 |
|
if ($stmt->columnCount() > 0) { |
38
|
6 |
|
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
39
|
6 |
|
$results->append($row); |
40
|
|
|
} |
41
|
|
|
} |
42
|
10 |
|
$stmt->closeCursor(); |
43
|
5 |
|
} catch (Exception\InvalidArgumentException $e) { |
44
|
5 |
|
throw $e; |
45
|
2 |
|
} catch (\Exception $e) { |
46
|
2 |
|
$eclass = get_class($e); |
47
|
2 |
|
$msg = sprintf("GenericPdo '%s' : %s [%s]", $eclass, $e->getMessage(), $query); |
48
|
2 |
|
throw new Exception\InvalidArgumentException($msg); |
49
|
|
|
} |
50
|
|
|
|
51
|
10 |
|
return $results; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
3 |
|
public function quoteValue($value) |
58
|
|
|
{ |
59
|
3 |
|
return $this->resource->quote($value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check extension. |
64
|
|
|
* |
65
|
|
|
* @throws Exception\RuntimeException |
66
|
|
|
*/ |
67
|
20 |
|
protected function checkEnvironment() |
68
|
|
|
{ |
69
|
20 |
|
if (!extension_loaded('PDO')) { |
70
|
|
|
throw new Exception\RuntimeException('The PDO extension is not loaded'); |
71
|
|
|
} |
72
|
20 |
|
} |
73
|
|
|
} |
74
|
|
|
|