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
|
|
|
* Sets one or more connection parameters. |
23
|
|
|
* |
24
|
|
|
* @param array $params Associative array of parameters and values. |
25
|
|
|
*/ |
26
|
|
|
public function setParams(Array $params) |
27
|
|
|
{ |
28
|
|
|
foreach ($params as $param => $value) { |
29
|
|
|
$this->setParam($param, $value); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set a single connection parameter. Valid parameters include: |
35
|
|
|
* |
36
|
|
|
* * string host - The hostname, IP address, or unix socket |
37
|
|
|
* * int port - The port to the host |
38
|
|
|
* * array options - MySQLi options/values, as an associative array. Example: array(MYSQLI_OPT_CONNECT_TIMEOUT => 2) |
39
|
|
|
* |
40
|
|
|
* @param string $param Name of the parameter to modify. |
41
|
|
|
* @param mixed $value Value to which the parameter will be set. |
42
|
|
|
*/ |
43
|
|
|
public function setParam($param, $value) |
44
|
|
|
{ |
45
|
|
|
if ($param === 'host') { |
46
|
|
|
if ($value === 'localhost') { |
47
|
|
|
$value = '127.0.0.1'; |
48
|
|
|
} elseif (stripos($value, 'unix:') === 0) { |
49
|
|
|
$param = 'socket'; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
if ($param === 'socket') { |
53
|
|
|
if (stripos($value, 'unix:') === 0) { |
54
|
|
|
$value = substr($value, 5); |
55
|
|
|
} |
56
|
|
|
$this->connection_params['host'] = null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->connection_params[$param] = $value; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the connection parameters (host, port, connection timeout) for the current instance. |
64
|
|
|
* |
65
|
|
|
* @return array $params The current connection parameters |
66
|
|
|
*/ |
67
|
|
|
public function getParams() |
68
|
|
|
{ |
69
|
|
|
return $this->connection_params; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the current connection established. |
74
|
|
|
* |
75
|
|
|
* @return object Internal connection object |
76
|
|
|
* @throws ConnectionException If no connection has been established or open |
77
|
|
|
*/ |
78
|
|
|
public function getConnection() |
79
|
|
|
{ |
80
|
|
|
if (!is_null($this->connection)) { |
81
|
|
|
return $this->connection; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
throw new ConnectionException('The connection to the server has not been established yet.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Adds quotes around values when necessary. |
89
|
|
|
* Based on FuelPHP's quoting function. |
90
|
|
|
* |
91
|
|
|
* @param Expression|string $value The input string, eventually wrapped in an expression to leave it untouched |
92
|
|
|
* |
93
|
|
|
* @return Expression|string|int The untouched Expression or the quoted string |
94
|
|
|
*/ |
95
|
|
|
public function quote($value) |
96
|
|
|
{ |
97
|
|
|
if ($value === null) { |
98
|
|
|
return 'null'; |
99
|
|
|
} elseif ($value === true) { |
100
|
|
|
return 1; |
101
|
|
|
} elseif ($value === false) { |
102
|
|
|
return 0; |
103
|
|
|
} elseif ($value instanceof Expression) { |
104
|
|
|
// Use the raw expression |
105
|
|
|
return $value->value(); |
106
|
|
|
} elseif (is_int($value)) { |
107
|
|
|
return (int) $value; |
108
|
|
|
} elseif (is_float($value)) { |
109
|
|
|
// Convert to non-locale aware float to prevent possible commas |
110
|
|
|
return sprintf('%F', $value); |
111
|
|
|
} elseif (is_array($value)) { |
112
|
|
|
// Supports MVA attributes |
113
|
|
|
return '('.implode(',', $this->quoteArr($value)).')'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->escape($value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Calls $this->quote() on every element of the array passed. |
121
|
|
|
* |
122
|
|
|
* @param array $array The array of strings to quote |
123
|
|
|
* |
124
|
|
|
* @return array The array of quotes strings |
125
|
|
|
*/ |
126
|
|
|
public function quoteArr(Array $array = array()) |
127
|
|
|
{ |
128
|
|
|
$result = array(); |
129
|
|
|
|
130
|
|
|
foreach ($array as $key => $item) { |
131
|
|
|
$result[$key] = $this->quote($item); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $result; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|