Passed
Pull Request — master (#197)
by
unknown
01:36
created

Connection::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
namespace Foolz\SphinxQL\Drivers\Pdo;
3
4
use Foolz\SphinxQL\Drivers\ConnectionBase;
5
use Foolz\SphinxQL\Drivers\MultiResultSet;
6
use Foolz\SphinxQL\Drivers\ResultSet;
7
use Foolz\SphinxQL\Exception\ConnectionException;
8
use Foolz\SphinxQL\Exception\DatabaseException;
9
use Foolz\SphinxQL\Exception\SphinxQLException;
10
use mysqli;
11
use PDO;
12
use PDOException;
13
use RuntimeException;
14
15
class Connection extends ConnectionBase
16
{
17
18
	/**
19
	 * @return PDO
20
	 * @throws ConnectionException
21
	 */
22
	public function getConnection(): PDO{
23
		$connection = parent::getConnection();
24
25
		if($connection instanceof mysqli){
26
			throw new RuntimeException('Connection type mismatch');
27
		}
28
29
		return $connection;
30
	}
31
32
33
		/**
34
     * @inheritdoc
35
     */
36
    public function query($query)
37
    {
38
        $this->ensureConnection();
39
40
        $statement = $this->getConnection()->prepare($query);
41
42
        try {
43
            $statement->execute();
44
        } catch (PDOException $exception) {
45
            throw new DatabaseException($exception->getMessage() . ' [' . $query . ']', $exception->getCode(), $exception);
46
        }
47
48
        return new ResultSet(new ResultSetAdapter($statement));
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function connect(): bool
55
    {
56
        $params = $this->getParams();
57
58
        $dsn = 'mysql:';
59
        if (isset($params['host']) && $params['host'] != '') {
60
            $dsn .= 'host=' . $params['host'] . ';';
61
        }
62
        if (isset($params['port'])) {
63
            $dsn .= 'port=' . $params['port'] . ';';
64
        }
65
        if (isset($params['charset'])) {
66
            $dsn .= 'charset=' . $params['charset'] . ';';
67
        }
68
69
        if (isset($params['socket']) && $params['socket'] != '') {
70
            $dsn .= 'unix_socket=' . $params['socket'] . ';';
71
        }
72
73
        try {
74
            $con = new PDO($dsn);
75
        } catch (PDOException $exception) {
76
            throw new ConnectionException($exception->getMessage(), $exception->getCode(), $exception);
77
        }
78
79
        $this->connection = $con;
80
        $this->getConnection()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
81
82
        return true;
83
    }
84
85
    /**
86
     * @return bool
87
     * @throws ConnectionException
88
     */
89
    public function ping()
90
    {
91
        $this->ensureConnection();
92
93
        return $this->getConnection() !== null;
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function multiQuery(array $queue)
100
    {
101
        $this->ensureConnection();
102
103
        if (count($queue) === 0) {
104
            throw new SphinxQLException('The Queue is empty.');
105
        }
106
107
        try {
108
            $statement = $this->getConnection()->query(implode(';', $queue));
109
        } catch (PDOException $exception) {
110
            throw new DatabaseException($exception->getMessage() .' [ '.implode(';', $queue).']', $exception->getCode(), $exception);
111
        }
112
113
        return new MultiResultSet(new MultiResultSetAdapter($statement));
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    public function escape($value)
120
    {
121
        $this->ensureConnection();
122
123
        return $this->getConnection()->quote($value);
124
    }
125
}
126