Completed
Push — master ( 488daa...64d97a )
by Hung
01:46
created

Connection::connect()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 18
nc 32
nop 0
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 new ResultSet($stm);
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function connect()
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
            throw new ConnectionException($exception->getMessage());
66
        }
67
68
        $this->connection = $con;
69
        $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
70
71
        return true;
72
    }
73
74
    public function ping()
75
    {
76
        $this->ensureConnection();
77
78
        return $this->connection !== null;
79
    }
80
81
    /**
82
     * @param array $queue
83
     * @return \Foolz\SphinxQL\Drivers\Pdo\MultiResultSet
84
     * @throws DatabaseException
85
     * @throws SphinxQLException
86
     */
87
    public function multiQuery(array $queue)
88
    {
89
        $this->ensureConnection();
90
91
        if (count($queue) === 0) {
92
            throw new SphinxQLException('The Queue is empty.');
93
        }
94
95
        try {
96
            $statement = $this->connection->query(implode(';', $queue));
97
        } catch (\PDOException $exception) {
98
            throw new DatabaseException($exception->getMessage() .' [ '.implode(';', $queue).']');
99
        }
100
101
        return new MultiResultSet($statement);
102
    }
103
104
    /**
105
     * @param string $value
106
     * @return string
107
     */
108
    public function escape($value)
109
    {
110
        $this->ensureConnection();
111
112
        return $this->connection->quote($value);
113
    }
114
}
115