1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Foolz\SphinxQL\Drivers\Pdo; |
4
|
|
|
|
5
|
|
|
use Foolz\SphinxQL\Drivers\ConnectionBase; |
6
|
|
|
use Foolz\SphinxQL\Exception\ConnectionException; |
7
|
|
|
use Foolz\SphinxQL\Exception\DatabaseException; |
8
|
|
|
use Foolz\SphinxQL\Exception\SphinxQLException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class PdoConnection |
12
|
|
|
* @package Foolz\SphinxQL\Drivers |
13
|
|
|
*/ |
14
|
|
|
class Connection extends ConnectionBase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Performs a query on the Sphinx server. |
18
|
|
|
* |
19
|
|
|
* @param string $query The query string |
20
|
|
|
* |
21
|
|
|
* @throws DatabaseException |
22
|
|
|
* @return array|int The result array or number of rows affected |
23
|
|
|
*/ |
24
|
|
|
public function query($query) |
25
|
|
|
{ |
26
|
|
|
$this->ensureConnection(); |
27
|
|
|
|
28
|
|
|
$stm = $this->connection->prepare($query); |
29
|
|
|
|
30
|
|
|
try{ |
31
|
|
|
$stm->execute(); |
32
|
|
|
} |
33
|
|
|
catch(\PDOException $exception){ |
34
|
|
|
throw new DatabaseException($exception->getMessage() . ' [' . $query . ']'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return ResultSet::make($stm); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function connect($suppress_error = false) |
44
|
|
|
{ |
45
|
|
|
$params = $this->getParams(); |
46
|
|
|
|
47
|
|
|
$dsn = 'mysql:'; |
48
|
|
|
if (isset($params['host']) && $params['host'] != '') { |
49
|
|
|
$dsn .= 'host=' . $params['host'] . ';'; |
50
|
|
|
} |
51
|
|
|
if (isset($params['port'])) { |
52
|
|
|
$dsn .= 'port=' . $params['port'] . ';'; |
53
|
|
|
} |
54
|
|
|
if (isset($params['charset'])) { |
55
|
|
|
$dsn .= 'charset=' . $params['charset'] . ';'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (isset($params['socket']) && $params['socket'] != '') { |
59
|
|
|
$dsn .= 'unix_socket=' . $params['socket'] . ';'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
try { |
63
|
|
|
$con = new \Pdo($dsn); |
64
|
|
|
} catch (\PDOException $exception) { |
65
|
|
|
if (!$suppress_error && !$this->silence_connection_warning) { |
66
|
|
|
trigger_error('connection error', E_USER_WARNING); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new ConnectionException($exception->getMessage()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->connection = $con; |
73
|
|
|
$this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
74
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function ping() |
79
|
|
|
{ |
80
|
|
|
$this->ensureConnection(); |
81
|
|
|
|
82
|
|
|
return $this->connection !== null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param array $queue |
87
|
|
|
* @return \Foolz\SphinxQL\Drivers\Pdo\MultiResultSet |
88
|
|
|
* @throws DatabaseException |
89
|
|
|
* @throws SphinxQLException |
90
|
|
|
*/ |
91
|
|
|
public function multiQuery(array $queue) |
92
|
|
|
{ |
93
|
|
|
$this->ensureConnection(); |
94
|
|
|
|
95
|
|
|
if (count($queue) === 0) { |
96
|
|
|
throw new SphinxQLException('The Queue is empty.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
$statement = $this->connection->query(implode(';', $queue)); |
101
|
|
|
} catch (\PDOException $exception) { |
102
|
|
|
throw new DatabaseException($exception->getMessage() .' [ '.implode(';', $queue).']'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return MultiResultSet::make($statement); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $value |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function escape($value) |
113
|
|
|
{ |
114
|
|
|
$this->ensureConnection(); |
115
|
|
|
|
116
|
|
|
return $this->connection->quote($value); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|