Skip to content

Commit

Permalink
#13 query methods now work with strings inside, was arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
KonstantinKuklin committed Sep 26, 2014
1 parent 3c043d8 commit eaa9279
Show file tree
Hide file tree
Showing 33 changed files with 244 additions and 205 deletions.
4 changes: 2 additions & 2 deletions src/HS/Builder/DeleteQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace HS\Builder;

use HS\Exception\WrongParameterException;
use HS\Exception\InvalidArgumentException;

/**
* @author KonstantinKuklin <[email protected]>
Expand All @@ -27,7 +27,7 @@ public function getQueryClassPath()
* @param array $list
*
* @return $this
* @throws WrongParameterException
* @throws InvalidArgumentException
*/
public function where($comparison, array $list)
{
Expand Down
10 changes: 5 additions & 5 deletions src/HS/Builder/FindQueryBuilderAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use HS\Component\Comparison;
use HS\Component\Filter;
use HS\Component\InList;
use HS\Exception\WrongParameterException;
use HS\Exception\InvalidArgumentException;

/**
* @author KonstantinKuklin <[email protected]>
Expand Down Expand Up @@ -68,7 +68,7 @@ public function getColumnList()
* @param array $list
*
* @return $this
* @throws WrongParameterException
* @throws InvalidArgumentException
*/
public function where($comparison, array $list)
{
Expand All @@ -79,7 +79,7 @@ public function where($comparison, array $list)
for ($i = 0, $countWhere = count($list); $i < $countWhere; $i++) {
$key = $columnList[$i];
if (!isset($list[$key])) {
throw new WrongParameterException(
throw new InvalidArgumentException(
"The key`s must be set with out skip on select( key1, key2). Where(key2,key1)"
);
}
Expand All @@ -95,7 +95,7 @@ public function where($comparison, array $list)
* @param array $values
*
* @return $this
* @throws \Exception
* @throws InvalidArgumentException
*/
public function whereIn($key, array $values)
{
Expand All @@ -104,7 +104,7 @@ public function whereIn($key, array $values)
$this->getParameterBag()->setParameter('keyList', array(1));

if (false === $index = array_search($key, $columnList)) {
throw new WrongParameterException("Can't find key in columns list.");
throw new InvalidArgumentException("Can't find key in columns list.");
}
$inList = new InList($index, $values);
$this->getParameterBag()->setParameter('inKeyList', $inList);
Expand Down
6 changes: 3 additions & 3 deletions src/HS/Builder/IncrementQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HS\Builder;

use HS\Exception\WrongParameterException;
use HS\Exception\InvalidArgumentException;

/**
* @author KonstantinKuklin <[email protected]>
Expand All @@ -12,7 +12,7 @@ class IncrementQueryBuilder extends UpdateQueryBuilder
/**
* @param array $incrementList
*
* @throws WrongParameterException
* @throws InvalidArgumentException
*/
public function __construct(array $incrementList)
{
Expand All @@ -26,7 +26,7 @@ public function __construct(array $incrementList)
$columnList[] = $value;
$valueList[] = 1;
} else {
throw new WrongParameterException("Wrong increment parameter.");
throw new InvalidArgumentException("Wrong increment parameter.");
}

}
Expand Down
31 changes: 18 additions & 13 deletions src/HS/CommonClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use HS\Builder\QueryBuilderInterface;
use HS\Exception\Exception;
use HS\Exception\WrongParameterException;
use HS\Exception\InvalidArgumentException;
use HS\Query\OpenIndexQuery;
use HS\Query\QueryInterface;
use PHP_Timer;
Expand Down Expand Up @@ -39,9 +39,6 @@ abstract class CommonClient
/** @var array */
private $keysList = array();

/** @var Driver */
private $driver = null;

/** @var null|string */
private $authKey = null;

Expand All @@ -50,6 +47,15 @@ abstract class CommonClient

public $debugResultList = array();

/**
* @param string $authKey
*/
abstract public function authenticate($authKey);

abstract public function getIndexId(
$dbName, $tableName, $indexName, array $columnList, $returnOnlyId = true, array $filterColumnList = array()
);

/**
* @param string $url
* @param int $port
Expand All @@ -63,8 +69,7 @@ public function __construct($url, $port, $authKey = null, $debug = false)
}

$this->authKey = $authKey;
$this->driver = new Driver();
$this->stream = new Stream($url, Connection::PROTOCOL_TCP, $port, $this->driver);
$this->stream = new Stream($url, Connection::PROTOCOL_TCP, $port);
$this->stream->open();
$this->stream->setBlockingOff();
$this->stream->setReadTimeOut(0, (float)500000);
Expand Down Expand Up @@ -101,8 +106,8 @@ public function getResultList()
}
$this->getStream()->isReadyForReading();
try {
$result = $this->getStream()->getContents();
$query->setResultData($result);
$resultList = Driver::prepareReceiveDataStatic($this->getStream()->getContents());
$query->setResultData($resultList);

$ResultObject = $query->getResult();

Expand All @@ -118,7 +123,7 @@ public function getResultList()
$resultsList[] = $ResultObject;
// add time to general time counter
} catch (ReadStreamException $e) {
// TODO check
throw new Exception("Read stream error. Can't read from stream. URL: " . $this->getUrlConnection());
}
}

Expand Down Expand Up @@ -239,7 +244,7 @@ protected function isQueryQueueEmpty()
*/
protected function sendQuery(QueryInterface $query)
{
if ($this->getStream()->sendContents($query->getQueryParameters()) > 0) {
if ($this->getStream()->sendContents($query->getQueryString() . Driver::EOL) > 0) {
// increment count of queries
$this->countQueries++;

Expand All @@ -261,13 +266,13 @@ protected function setKeysToIndexId($indexId, $keys)
/**
* @param int $indexId
*
* @throws WrongParameterException
* @throws InvalidArgumentException
* @return array
*/
protected function getKeysByIndexId($indexId)
{
if (!array_key_exists($indexId, $this->keysList)) {
throw new WrongParameterException(sprintf("Don't find any Index with this indexId:%d.", $indexId));
throw new InvalidArgumentException(sprintf("Don't find any Index with this indexId:%d.", $indexId));
}

return $this->keysList[$indexId];
Expand Down Expand Up @@ -319,7 +324,7 @@ protected function addIndexIdToArray($indexMapValue, $indexId)
}

/**
* @throws WrongParameterException
* @throws InvalidArgumentException
*/
private function authenticateWithConstructor()
{
Expand Down
8 changes: 4 additions & 4 deletions src/HS/Component/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace HS\Component;


use HS\Exception\WrongParameterException;
use HS\Exception\InvalidArgumentException;

class Filter
{
Expand All @@ -24,7 +24,7 @@ class Filter
* @param string $key
* @param string $type
*
* @throws WrongParameterException
* @throws InvalidArgumentException
*/
public function __construct($comparison, $position, $key, $type = self::FILTER_TYPE_SKIP)
{
Expand All @@ -35,14 +35,14 @@ public function __construct($comparison, $position, $key, $type = self::FILTER_T
}

if (!is_numeric($position)) {
throw new WrongParameterException("Position must be numeric");
throw new InvalidArgumentException("Position must be numeric");
}
$this->position = (int)$position;
$this->key = $key;
if ($type === self::FILTER_TYPE_SKIP || $type === self::FILTER_TYPE_STOP) {
$this->type = $type;
} else {
throw new WrongParameterException("Filter type is wrong must be 'F' or 'W'.");
throw new InvalidArgumentException("Filter type is wrong must be 'F' or 'W'.");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/HS/Component/InList.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace HS\Component;

use HS\Exception\WrongParameterException;
use HS\Exception\InvalidArgumentException;

class InList
{
Expand All @@ -16,12 +16,12 @@ class InList
* @param int $position
* @param array $keyList
*
* @throws WrongParameterException
* @throws InvalidArgumentException
*/
public function __construct($position, array $keyList)
{
if (!is_numeric($position)) {
throw new WrongParameterException("ColumnNumber must be a number");
throw new InvalidArgumentException("ColumnNumber must be a number");
}
$this->position = $position;
$this->keyList = $keyList;
Expand Down
2 changes: 1 addition & 1 deletion src/HS/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function prepareSendDataStatic($data)
{
$encodedData = array_map('self::encodeData', $data);

return implode(self::DELIMITER, $encodedData) . self::EOL;
return implode(self::DELIMITER, $encodedData);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/HS/Exception/ComparisonException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
*/
namespace HS\Exception;

class ComparisonException extends WrongParameterException
class ComparisonException extends InvalidArgumentException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

namespace HS\Exception;

class WrongParameterException extends Exception
class InvalidArgumentException extends Exception
{
}
10 changes: 4 additions & 6 deletions src/HS/Query/AuthQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

namespace HS\Query;

use HS\Driver;

class AuthQuery extends QueryAbstract
{
/**
* {@inheritdoc}
*/
public function getQueryParameters()
public function getQueryString()
{
return array(
'A',
'1', // <atyp>
$this->getParameter('authKey') // <akey>
);
return sprintf("A" . Driver::DELIMITER . "1" . Driver::DELIMITER, $this->getParameter('authKey'));
}
}
11 changes: 6 additions & 5 deletions src/HS/Query/InsertQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@
*/
namespace HS\Query;

use HS\Driver;
use HS\Result\InsertResult;

class InsertQuery extends QueryAbstract
{
/**
* {@inheritdoc}
*/
public function getQueryParameters()
public function getQueryString()
{
$valueList = $this->getParameter('valueList', array());

$returnList = array(
$queryString = sprintf(
"%d" . Driver::DELIMITER . "+" . Driver::DELIMITER . "%d",
$this->getIndexId(),
'+',
count($valueList[0])
);

foreach ($valueList as $row) {
$returnList = array_merge($returnList, $row);
$queryString .= "\t" . Driver::prepareSendDataStatic($row);
}

return $returnList;
return $queryString;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/HS/Query/ModifyQueryAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace HS\Query;

use HS\Driver;

abstract class ModifyQueryAbstract extends SelectQuery
{
abstract public function getModificator();
Expand All @@ -20,17 +22,16 @@ public function isSuffics()
/**
* {@inheritdoc}
*/
public function getQueryParameters()
public function getQueryString()
{
$parameters = parent::getQueryParameters();
$queryString = parent::getQueryString();
$modificator = $this->getModificator();

if ($this->isSuffics()) {
$modificator .= '?';
}
$queryString .= Driver::DELIMITER . $modificator . Driver::DELIMITER;

$parameters[] = $modificator;

return $parameters;
return $queryString;
}
}
13 changes: 9 additions & 4 deletions src/HS/Query/ModifyStepQueryAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@

namespace HS\Query;

use HS\Driver;

abstract class ModifyStepQueryAbstract extends ModifyQueryAbstract
{
/**
* {@inheritdoc}
*/
public function getQueryParameters()
public function getQueryString()
{
$parameters = parent::getQueryParameters();
$parameters = array_merge($parameters, $this->getParameter('valueList', array()));
return $parameters;
$queryString = parent::getQueryString();
if (($valueList = $this->getParameter('valueList', array())) && !empty($valueList)) {
$queryString .= Driver::prepareSendDataStatic($valueList);
}

return $queryString;
}
}
Loading

0 comments on commit eaa9279

Please sign in to comment.