Completed
Pull Request — master (#138)
by
unknown
10:05
created

Connection::multiQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
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
    /**
18
     * close connection
19
     */
20
    public function close()
21
    {
22
        $this->connection = null;
23
    }
24
25
    /**
26
     * Performs a query on the Sphinx server.
27
     *
28
     * @param string $query The query string
29
     *
30
     * @throws DatabaseException
31
     * @return array|int The result array or number of rows affected
32
     */
33
    public function query($query)
34
    {
35
        $this->ensureConnection();
36
37
        $stm = $this->connection->prepare($query);
38
39
        try{
40
            $stm->execute();
41
        }
42
        catch(\PDOException $exception){
43
            throw new DatabaseException($exception->getMessage() . ' [' . $query . ']');
44
        }
45
46
        return ResultSet::make($stm);
47
    }
48
49
    /**
50
     * @return bool
51
     * @throws ConnectionException
52
     */
53
    public function connect($suppress_error = false)
54
    {
55
        $params = $this->getParams();
56
57
        $dsn = 'mysql:';
58
        if (isset($params['host']) && $params['host'] != '') {
59
            $dsn .= 'host=' . $params['host'] . ';';
60
        }
61
        if (isset($params['port'])) {
62
            $dsn .= 'port=' . $params['port'] . ';';
63
        }
64
        if (isset($params['charset'])) {
65
            $dsn .= 'charset=' . $params['charset'] . ';';
66
        }
67
68
        if (isset($params['socket']) && $params['socket'] != '') {
69
            $dsn .= 'unix_socket=' . $params['socket'] . ';';
70
        }
71
72
        try {
73
            $con = new \Pdo($dsn);
74
        } catch (\PDOException $exception) {
75
            if (!$suppress_error && !$this->silence_connection_warning) {
76
                trigger_error('connection error', E_USER_WARNING);
77
            }
78
79
            throw new ConnectionException($exception->getMessage());
80
        }
81
82
        $this->connection = $con;
83
        $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
84
85
        return true;
86
    }
87
88
    public function ping()
89
    {
90
        $this->ensureConnection();
91
92
        return $this->connection !== null;
93
    }
94
95
    /**
96
     * @param array $queue
97
     * @return \Foolz\SphinxQL\Drivers\Pdo\MultiResultSet
98
     * @throws DatabaseException
99
     * @throws SphinxQLException
100
     */
101
    public function multiQuery(array $queue)
102
    {
103
        $this->ensureConnection();
104
105
        if (count($queue) === 0) {
106
            throw new SphinxQLException('The Queue is empty.');
107
        }
108
109
        try {
110
            $statement = $this->connection->query(implode(';', $queue));
111
        } catch (\PDOException $exception) {
112
            throw new DatabaseException($exception->getMessage() .' [ '.implode(';', $queue).']');
113
        }
114
115
        return MultiResultSet::make($statement);
116
    }
117
118
    /**
119
     * @param string $value
120
     * @return string
121
     */
122
    public function escape($value)
123
    {
124
        $this->ensureConnection();
125
126
        return $this->connection->quote($value);
127
    }
128
}
129