Completed
Push — master ( ffc474...cf0a8f )
by Hung
05:25 queued 59s
created

Connection::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
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 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
        $result = array();
100
        $count = 0;
101
102
        if(version_compare(PHP_VERSION, '5.4.0', '>='))
103
        {
104
            try {
105
                $statement = $this->connection->query(implode(';', $queue));
106
            } catch (\PDOException $exception) {
107
                throw new DatabaseException($exception->getMessage() .' [ '.implode(';', $queue).']');
108
            }
109
110
            return MultiResultSet::make($statement);
111
        }
112
        else
113
        {
114
            foreach($queue as $sql)
115
            {
116
                try {
117
                    $statement = $this->connection->query($sql);
118
                } catch (\PDOException $exception) {
119
                    throw new DatabaseException($exception->getMessage() .' [ '.implode(';', $queue).']');
120
                }
121
                if ($statement->columnCount()) {
122
                    $set = ResultSet::make($statement);
123
                    $rowset = $set->getStored();
0 ignored issues
show
Deprecated Code introduced by
The method Foolz\SphinxQL\Drivers\ResultSetBase::getStored() has been deprecated with message: Commodity method for simple transition to version 1.0.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
124
                } else {
125
                    $rowset = $statement->rowCount();
126
                }
127
128
                $result[$count] = $rowset;
129
                $count++;
130
            }
131
132
            return MultiResultSet::make($result);
133
        }
134
    }
135
136
    /**
137
     * @param string $value
138
     * @return string
139
     */
140
    public function escape($value)
141
    {
142
        $this->ensureConnection();
143
144
        return $this->connection->quote($value);
145
    }
146
}
147