1
|
|
|
<?php |
2
|
|
|
namespace Foolz\SphinxQL\Drivers; |
3
|
|
|
|
4
|
|
|
use Foolz\SphinxQL\Exception\ConnectionException; |
5
|
|
|
use Foolz\SphinxQL\Expression; |
6
|
|
|
|
7
|
|
|
abstract class ConnectionBase implements ConnectionInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The connection parameters for the database server. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $connection_params = array('host' => '127.0.0.1', 'port' => 9306, 'socket' => null); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Internal connection object. |
18
|
|
|
*/ |
19
|
|
|
protected $connection = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Disables any warning outputs returned on the connection with @ prefix. |
23
|
|
|
* |
24
|
|
|
* @var boolean |
25
|
|
|
*/ |
26
|
|
|
protected $silence_connection_warning = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Sets one or more connection parameters. |
30
|
|
|
* |
31
|
|
|
* @param array $params Associative array of parameters and values. |
32
|
|
|
*/ |
33
|
|
|
public function setParams(Array $params) |
34
|
|
|
{ |
35
|
|
|
foreach ($params as $param => $value) { |
36
|
|
|
$this->setParam($param, $value); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set a single connection parameter. Valid parameters include: |
42
|
|
|
* |
43
|
|
|
* * string host - The hostname, IP address, or unix socket |
44
|
|
|
* * int port - The port to the host |
45
|
|
|
* * array options - MySQLi options/values, as an associative array. Example: array(MYSQLI_OPT_CONNECT_TIMEOUT => 2) |
46
|
|
|
* |
47
|
|
|
* @param string $param Name of the parameter to modify. |
48
|
|
|
* @param mixed $value Value to which the parameter will be set. |
49
|
|
|
*/ |
50
|
|
|
public function setParam($param, $value) |
51
|
|
|
{ |
52
|
|
|
if ($param === 'host') { |
53
|
|
|
if ($value === 'localhost') { |
54
|
|
|
$value = '127.0.0.1'; |
55
|
|
|
} elseif (stripos($value, 'unix:') === 0) { |
56
|
|
|
$param = 'socket'; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
if ($param === 'socket') { |
60
|
|
|
if (stripos($value, 'unix:') === 0) { |
61
|
|
|
$value = substr($value, 5); |
62
|
|
|
} |
63
|
|
|
$this->connection_params['host'] = null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->connection_params[$param] = $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the connection parameters (host, port, connection timeout) for the current instance. |
71
|
|
|
* |
72
|
|
|
* @return array $params The current connection parameters |
73
|
|
|
*/ |
74
|
|
|
public function getParams() |
75
|
|
|
{ |
76
|
|
|
return $this->connection_params; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the current connection established. |
81
|
|
|
* |
82
|
|
|
* @return object Internal connection object |
83
|
|
|
* @throws ConnectionException If no connection has been established or open |
84
|
|
|
*/ |
85
|
|
|
public function getConnection() |
86
|
|
|
{ |
87
|
|
|
if (!is_null($this->connection)) { |
88
|
|
|
return $this->connection; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
throw new ConnectionException('The connection to the server has not been established yet.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds quotes around values when necessary. |
96
|
|
|
* Based on FuelPHP's quoting function. |
97
|
|
|
* |
98
|
|
|
* @param Expression|string $value The input string, eventually wrapped in an expression to leave it untouched |
99
|
|
|
* |
100
|
|
|
* @return Expression|string|int The untouched Expression or the quoted string |
101
|
|
|
*/ |
102
|
|
|
public function quote($value) |
103
|
|
|
{ |
104
|
|
|
if ($value === null) { |
105
|
|
|
return 'null'; |
106
|
|
|
} elseif ($value === true) { |
107
|
|
|
return 1; |
108
|
|
|
} elseif ($value === false) { |
109
|
|
|
return 0; |
110
|
|
|
} elseif ($value instanceof Expression) { |
111
|
|
|
// Use the raw expression |
112
|
|
|
return $value->value(); |
113
|
|
|
} elseif (is_int($value)) { |
114
|
|
|
return (int) $value; |
115
|
|
|
} elseif (is_float($value)) { |
116
|
|
|
// Convert to non-locale aware float to prevent possible commas |
117
|
|
|
return sprintf('%F', $value); |
118
|
|
|
} elseif (is_array($value)) { |
119
|
|
|
// Supports MVA attributes |
120
|
|
|
return '('.implode(',', $this->quoteArr($value)).')'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->escape($value); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Calls $this->quote() on every element of the array passed. |
128
|
|
|
* |
129
|
|
|
* @param array $array The array of strings to quote |
130
|
|
|
* |
131
|
|
|
* @return array The array of quotes strings |
132
|
|
|
*/ |
133
|
|
|
public function quoteArr(Array $array = array()) |
134
|
|
|
{ |
135
|
|
|
$result = array(); |
136
|
|
|
|
137
|
|
|
foreach ($array as $key => $item) { |
138
|
|
|
$result[$key] = $this->quote($item); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $result; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Establishes a connection if needed |
146
|
|
|
* @throws ConnectionException |
147
|
|
|
*/ |
148
|
|
|
protected function ensureConnection() |
149
|
|
|
{ |
150
|
|
|
try { |
151
|
|
|
$this->getConnection(); |
152
|
|
|
} catch (ConnectionException $e) { |
153
|
|
|
$this->connect(); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Forces the connection to suppress all errors returned. This should only be used |
159
|
|
|
* when the production server is running with high error reporting settings. |
160
|
|
|
* |
161
|
|
|
* @param boolean $enable True if it should be enabled, false if it should be disabled |
162
|
|
|
* @deprecated |
163
|
|
|
* not good |
164
|
|
|
*/ |
165
|
|
|
public function silenceConnectionWarning($enable = true) |
166
|
|
|
{ |
167
|
|
|
$this->silence_connection_warning = $enable; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.