Completed
Pull Request — master (#140)
by
unknown
02:05
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
     * @return bool
42
     * @throws ConnectionException
43
     */
44
    public function connect($suppress_error = false)
45
    {
46
        $params = $this->getParams();
47
48
        $dsn = 'mysql:';
49
        if (isset($params['host']) && $params['host'] != '') {
50
            $dsn .= 'host=' . $params['host'] . ';';
51
        }
52
        if (isset($params['port'])) {
53
            $dsn .= 'port=' . $params['port'] . ';';
54
        }
55
        if (isset($params['charset'])) {
56
            $dsn .= 'charset=' . $params['charset'] . ';';
57
        }
58
59
        if (isset($params['socket']) && $params['socket'] != '') {
60
            $dsn .= 'unix_socket=' . $params['socket'] . ';';
61
        }
62
63
        try {
64
            $con = new \Pdo($dsn);
65
        } catch (\PDOException $exception) {
66
            if (!$suppress_error && !$this->silence_connection_warning) {
67
                trigger_error('connection error', E_USER_WARNING);
68
            }
69
70
            throw new ConnectionException($exception->getMessage());
71
        }
72
73
        $this->connection = $con;
74
        $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
75
76
        return true;
77
    }
78
79
    public function ping()
80
    {
81
        $this->ensureConnection();
82
83
        return $this->connection !== null;
84
    }
85
86
    /**
87
     * @param array $queue
88
     * @return \Foolz\SphinxQL\Drivers\Pdo\MultiResultSet
89
     * @throws DatabaseException
90
     * @throws SphinxQLException
91
     */
92
    public function multiQuery(array $queue)
93
    {
94
        $this->ensureConnection();
95
96
        if (count($queue) === 0) {
97
            throw new SphinxQLException('The Queue is empty.');
98
        }
99
100
        $result = array();
101
        $count = 0;
102
103
        if(version_compare(PHP_VERSION, '5.4.0', '>='))
104
        {
105
            try {
106
                $statement = $this->connection->query(implode(';', $queue));
107
            } catch (\PDOException $exception) {
108
                throw new DatabaseException($exception->getMessage() .' [ '.implode(';', $queue).']');
109
            }
110
111
            return MultiResultSet::make($statement);
112
        }
113
        else
114
        {
115
            foreach($queue as $sql)
116
            {
117
                try {
118
                    $statement = $this->connection->query($sql);
119
                } catch (\PDOException $exception) {
120
                    throw new DatabaseException($exception->getMessage() .' [ '.implode(';', $queue).']');
121
                }
122
                if ($statement->columnCount()) {
123
                    $set = ResultSet::make($statement);
124
                    $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...
125
                } else {
126
                    $rowset = $statement->rowCount();
127
                }
128
129
                $result[$count] = $rowset;
130
                $count++;
131
            }
132
133
            return MultiResultSet::make($result);
134
        }
135
    }
136
137
    /**
138
     * @param string $value
139
     * @return string
140
     */
141
    public function escape($value)
142
    {
143
        $this->ensureConnection();
144
145
        return $this->connection->quote($value);
146
    }
147
}
148