Skip to content

Commit 1bff7da

Browse files
committed
Refactor duplicate code into base classes; add a few more tests
1 parent 3de4ef3 commit 1bff7da

23 files changed

+934
-1421
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ composer.lock
22
tests/searchd.log
33
tests/searchd.pid
44
tests/data/rt.*
5+
tests/data/test_udf.so
56
vendor/

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ before_install:
3333
before_script:
3434
- composer dump-autoload
3535
- cd tests
36+
- gcc -shared -o data/test_udf.so test_udf.c
3637
- $HOME/sphinx/usr/bin/searchd -c sphinx.conf
3738

3839
script: phpunit --configuration travis/$DRIVER.phpunit.xml --coverage-text

src/Drivers/ConnectionBase.php

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
* Wraps the input with identifiers when necessary.
89+
*
90+
* @param Expression|string $value The string to be quoted, or an Expression to leave it untouched
91+
*
92+
* @return Expression|string The untouched Expression or the quoted string
93+
*/
94+
public function quoteIdentifier($value)
95+
{
96+
if ($value instanceof Expression) {
97+
return $value->value();
98+
}
99+
100+
if ($value === '*') {
101+
return $value;
102+
}
103+
104+
$pieces = explode('.', $value);
105+
106+
foreach ($pieces as $key => $piece) {
107+
$pieces[$key] = '`'.$piece.'`';
108+
}
109+
110+
return implode('.', $pieces);
111+
}
112+
113+
/**
114+
* Calls $this->quoteIdentifier() on every element of the array passed.
115+
*
116+
* @param array $array An array of strings to be quoted
117+
*
118+
* @return array The array of quoted strings
119+
*/
120+
public function quoteIdentifierArr(Array $array = array())
121+
{
122+
$result = array();
123+
124+
foreach ($array as $key => $item) {
125+
$result[$key] = $this->quoteIdentifier($item);
126+
}
127+
128+
return $result;
129+
}
130+
131+
/**
132+
* Adds quotes around values when necessary.
133+
* Based on FuelPHP's quoting function.
134+
*
135+
* @param Expression|string $value The input string, eventually wrapped in an expression to leave it untouched
136+
*
137+
* @return Expression|string|int The untouched Expression or the quoted string
138+
*/
139+
public function quote($value)
140+
{
141+
if ($value === null) {
142+
return 'null';
143+
} elseif ($value === true) {
144+
return "'1'";
145+
} elseif ($value === false) {
146+
return "'0'";
147+
} elseif ($value instanceof Expression) {
148+
// Use the raw expression
149+
return $value->value();
150+
} elseif (is_int($value)) {
151+
return (int) $value;
152+
} elseif (is_float($value)) {
153+
// Convert to non-locale aware float to prevent possible commas
154+
return sprintf('%F', $value);
155+
} elseif (is_array($value)) {
156+
// Supports MVA attributes
157+
return '('.implode(',', $this->quoteArr($value)).')';
158+
}
159+
160+
return $this->escape($value);
161+
}
162+
163+
/**
164+
* Calls $this->quote() on every element of the array passed.
165+
*
166+
* @param array $array The array of strings to quote
167+
*
168+
* @return array The array of quotes strings
169+
*/
170+
public function quoteArr(Array $array = array())
171+
{
172+
$result = array();
173+
174+
foreach ($array as $key => $item) {
175+
$result[$key] = $this->quote($item);
176+
}
177+
178+
return $result;
179+
}
180+
}

src/Drivers/MultiResultSetBase.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
namespace Foolz\SphinxQL\Drivers;
3+
4+
abstract class MultiResultSetBase implements MultiResultSetInterface
5+
{
6+
/**
7+
* @var null|array
8+
*/
9+
public $stored = null;
10+
11+
/**
12+
* @var int
13+
*/
14+
public $cursor = null;
15+
16+
public function getStored()
17+
{
18+
$this->store();
19+
return $this->stored;
20+
}
21+
22+
/**
23+
* (PHP 5 &gt;= 5.0.0)<br/>
24+
* Whether a offset exists
25+
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
26+
* @param mixed $offset <p>
27+
* An offset to check for.
28+
* </p>
29+
* @return boolean true on success or false on failure.
30+
* </p>
31+
* <p>
32+
* The return value will be casted to boolean if non-boolean was returned.
33+
*/
34+
public function offsetExists($offset)
35+
{
36+
$this->store();
37+
return $offset >= 0 && $offset < count($this->stored);
38+
}
39+
40+
/**
41+
* (PHP 5 &gt;= 5.0.0)<br/>
42+
* Offset to retrieve
43+
* @link http://php.net/manual/en/arrayaccess.offsetget.php
44+
* @param mixed $offset <p>
45+
* The offset to retrieve.
46+
* </p>
47+
* @return mixed Can return all value types.
48+
*/
49+
public function offsetGet($offset)
50+
{
51+
$this->store();
52+
return $this->stored[$offset];
53+
}
54+
55+
/**
56+
* (PHP 5 &gt;= 5.0.0)<br/>
57+
* Offset to set
58+
* @link http://php.net/manual/en/arrayaccess.offsetset.php
59+
* @param mixed $offset <p>
60+
* The offset to assign the value to.
61+
* </p>
62+
* @param mixed $value <p>
63+
* The value to set.
64+
* </p>
65+
* @return void
66+
*
67+
* @codeCoverageIgnore
68+
*/
69+
public function offsetSet($offset, $value)
70+
{
71+
throw new \BadMethodCallException('Not implemented');
72+
}
73+
74+
/**
75+
* (PHP 5 &gt;= 5.0.0)<br/>
76+
* Offset to unset
77+
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
78+
* @param mixed $offset <p>
79+
* The offset to unset.
80+
* </p>
81+
* @return void
82+
*
83+
* @codeCoverageIgnore
84+
*/
85+
public function offsetUnset($offset)
86+
{
87+
throw new \BadMethodCallException('Not implemented');
88+
}
89+
90+
/**
91+
* (PHP 5 &gt;= 5.0.0)<br/>
92+
* Move forward to next element
93+
* @link http://php.net/manual/en/iterator.next.php
94+
* @return void Any returned value is ignored.
95+
*/
96+
public function next()
97+
{
98+
if ($this->cursor === null) {
99+
$this->cursor = 0;
100+
} else {
101+
$this->cursor++;
102+
}
103+
}
104+
105+
/**
106+
* (PHP 5 &gt;= 5.0.0)<br/>
107+
* Return the key of the current element
108+
* @link http://php.net/manual/en/iterator.key.php
109+
* @return mixed scalar on success, or null on failure.
110+
*/
111+
public function key()
112+
{
113+
return (int)$this->cursor;
114+
}
115+
116+
/**
117+
* (PHP 5 &gt;= 5.0.0)<br/>
118+
* Rewind the Iterator to the first element
119+
* @link http://php.net/manual/en/iterator.rewind.php
120+
* @return void Any returned value is ignored.
121+
*/
122+
public function rewind()
123+
{
124+
// we actually can't roll this back unless it was stored first
125+
$this->cursor = null;
126+
}
127+
128+
/**
129+
* (PHP 5 &gt;= 5.1.0)<br/>
130+
* Count elements of an object
131+
* @link http://php.net/manual/en/countable.count.php
132+
* @return int The custom count as an integer.
133+
* </p>
134+
* <p>
135+
* The return value is cast to an integer.
136+
*/
137+
public function count()
138+
{
139+
$this->store();
140+
return count($this->stored);
141+
}
142+
}

0 commit comments

Comments
 (0)