|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Brouzie\Sphinxy\Connection; |
|
4
|
|
|
|
|
5
|
|
|
use Brouzie\Sphinxy\Exception\ConnectionException; |
|
6
|
|
|
|
|
7
|
|
|
class PdoConnection implements ConnectionInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var \PDO |
|
11
|
|
|
*/ |
|
12
|
|
|
private $pdo; |
|
13
|
|
|
|
|
14
|
|
|
private $dsn; |
|
15
|
|
|
|
|
16
|
9 |
|
public function __construct($dsn) |
|
17
|
|
|
{ |
|
18
|
9 |
|
$this->dsn = $dsn; |
|
19
|
9 |
|
} |
|
20
|
|
|
|
|
21
|
9 |
|
public function query($query) |
|
22
|
|
|
{ |
|
23
|
9 |
|
$this->initialize(); |
|
24
|
|
|
|
|
25
|
|
|
try { |
|
26
|
6 |
|
$stmt = $this->pdo->query($query); |
|
27
|
6 |
|
} catch (\PDOException $e) { |
|
28
|
|
|
throw new ConnectionException($e->getMessage(), $e->getCode(), $e); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
6 |
|
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
|
32
|
|
|
|
|
33
|
6 |
|
return $result; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
public function multiQuery($query, array $resultSetNames = array()) |
|
37
|
|
|
{ |
|
38
|
3 |
|
$this->initialize(); |
|
39
|
|
|
|
|
40
|
|
|
try { |
|
41
|
3 |
|
$stmt = $this->pdo->query($query); |
|
42
|
3 |
|
} catch (\PDOException $e) { |
|
43
|
|
|
throw new ConnectionException($e->getMessage(), $e->getCode(), $e); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
$results = array(); |
|
47
|
3 |
|
$i = 0; |
|
48
|
|
|
do { |
|
49
|
3 |
|
$key = isset($resultSetNames[$i]) ? $resultSetNames[$i] : $i; |
|
50
|
3 |
|
$results[$key] = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
|
51
|
3 |
|
$i++; |
|
52
|
3 |
|
} while ($stmt->nextRowset()); |
|
53
|
|
|
|
|
54
|
3 |
|
return $results; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
public function exec($query) |
|
58
|
|
|
{ |
|
59
|
3 |
|
$this->initialize(); |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
3 |
|
return $this->pdo->exec($query); |
|
63
|
|
|
} catch (\PDOException $e) { |
|
64
|
|
|
throw new ConnectionException($e->getMessage(), $e->getCode(), $e); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function quote($value) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->initialize(); |
|
71
|
|
|
|
|
72
|
|
|
if (false === $value = $this->pdo->quote((string)$value)) { |
|
73
|
|
|
throw new ConnectionException($this->pdo->errorInfo(), $this->pdo->errorCode()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
9 |
|
protected function initialize() |
|
80
|
|
|
{ |
|
81
|
9 |
|
if (null === $this->pdo) { |
|
82
|
|
|
try { |
|
83
|
9 |
|
$this->pdo = new \PDO($this->dsn, null, null, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)); |
|
84
|
9 |
|
} catch (\PDOException $e) { |
|
85
|
3 |
|
throw new ConnectionException($e->getMessage(), $e->getCode(), $e); |
|
86
|
|
|
} |
|
87
|
6 |
|
} |
|
88
|
6 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|