-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathConnection.php
More file actions
108 lines (88 loc) · 2.72 KB
/
Connection.php
File metadata and controls
108 lines (88 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Foolz\SphinxQL\Drivers\Pdo;
use Foolz\SphinxQL\Drivers\ConnectionBase;
use Foolz\SphinxQL\Drivers\MultiResultSet;
use Foolz\SphinxQL\Drivers\ResultSet;
use Foolz\SphinxQL\Exception\ConnectionException;
use Foolz\SphinxQL\Exception\DatabaseException;
use Foolz\SphinxQL\Exception\SphinxQLException;
use PDO;
use PDOException;
class Connection extends ConnectionBase
{
/**
* @inheritdoc
*/
public function query($query)
{
$this->ensureConnection();
$statement = $this->getConnection()->prepare($query);
try {
$statement->execute();
} catch (PDOException $exception) {
throw new DatabaseException($exception->getMessage() . ' [' . $query . ']', $exception->getCode(), $exception);
}
return new ResultSet(new ResultSetAdapter($statement));
}
/**
* @inheritdoc
*/
public function connect()
{
$params = $this->getParams();
$dsn = 'mysql:';
if (isset($params['host']) && $params['host'] != '') {
$dsn .= 'host=' . $params['host'] . ';';
}
if (isset($params['port'])) {
$dsn .= 'port=' . $params['port'] . ';';
}
if (isset($params['charset'])) {
$dsn .= 'charset=' . $params['charset'] . ';';
}
if (isset($params['socket']) && $params['socket'] != '') {
$dsn .= 'unix_socket=' . $params['socket'] . ';';
}
try {
$con = new PDO($dsn);
} catch (PDOException $exception) {
throw new ConnectionException($exception->getMessage(), $exception->getCode(), $exception);
}
$this->connection = $con;
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return true;
}
/**
* @return bool
* @throws ConnectionException
*/
public function ping()
{
$this->ensureConnection();
return $this->getConnection() !== null;
}
/**
* @inheritdoc
*/
public function multiQuery(array $queue)
{
$this->ensureConnection();
if (count($queue) === 0) {
throw new SphinxQLException('The Queue is empty.');
}
try {
$statement = $this->getConnection()->query(implode(';', $queue));
} catch (PDOException $exception) {
throw new DatabaseException($exception->getMessage() .' [ '.implode(';', $queue).']', $exception->getCode(), $exception);
}
return new MultiResultSet(new MultiResultSetAdapter($statement));
}
/**
* @inheritdoc
*/
public function escape($value)
{
$this->ensureConnection();
return $this->getConnection()->quote($value);
}
}