Skip to content

Commit

Permalink
#5 increate tests and the part of unused code was removed
Browse files Browse the repository at this point in the history
fixed Insert query and updated regarded docs
improved phpdocs
  • Loading branch information
KonstantinKuklin committed Oct 7, 2014
1 parent e9797c5 commit 4dd52db
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 102 deletions.
6 changes: 3 additions & 3 deletions docs/eng/Insert.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Insert command is modifying database and can only be done through writing socket

Open an index with columns `'key', 'date', 'float', 'varchar', 'text', 'set', 'union'`.

Paste the data on the index, certainly pass an array of values, where a value for insertion is also an array.
Paste the data on the index, certainly pass an array of values.

```php
$writer = new \HS\Writer('localhost', 9999);
Expand All @@ -16,7 +16,7 @@ $indexId = $writer->getIndexId(
);
$insertQuery = $writer->insertByIndex(
$indexId,
array(array('467', '0000-00-01', '1.02', 'char', 'text467', '1', '1'))
array('467', '0000-00-01', '1.02', 'char', 'text467', '1', '1')
);
```

Expand Down Expand Up @@ -49,7 +49,7 @@ $insertQuery = $writer->insert(
$this->getDatabase(),
$this->getTableName(),
'PRIMARY',
array(array('468', '0000-00-01', '1.02', 'char', 'text468', '1', '1'))
array('468', '0000-00-01', '1.02', 'char', 'text468', '1', '1')
);
```

Expand Down
6 changes: 3 additions & 3 deletions docs/rus/Insert.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Insert в открытый индекс

Открываем индекс с колонками `'key', 'date', 'float', 'varchar', 'text', 'set', 'union'`.

Вставляем данные по индексу, обязательно передаем массив значений, где значение для вставки тоже является массивом.
Вставляем данные по индексу, обязательно передаем массив значений.
```php
$writer = new \HS\Writer('localhost', 9999);
$indexId = $writer->getIndexId(
Expand All @@ -15,7 +15,7 @@ $indexId = $writer->getIndexId(
);
$insertQuery = $writer->insertByIndex(
$indexId,
array(array('467', '0000-00-01', '1.02', 'char', 'text467', '1', '1'))
array('467', '0000-00-01', '1.02', 'char', 'text467', '1', '1')
);
```
Если вы полностью уверены в работоспособности вашей команды, мы можем ее просто отослать серверу и не читать ответ, тем самым сэкономить время и память:
Expand Down Expand Up @@ -43,7 +43,7 @@ $insertQuery = $writer->insert(
$this->getDatabase(),
$this->getTableName(),
'PRIMARY',
array(array('468', '0000-00-01', '1.02', 'char', 'text468', '1', '1'))
array('468', '0000-00-01', '1.02', 'char', 'text468', '1', '1')
);
```
Другой способ выполнить запрос:
Expand Down
24 changes: 0 additions & 24 deletions src/HS/Builder/InsertQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,4 @@ public function getQueryClassPath()
{
return 'HS\Query\InsertQuery';
}

/**
* @param int $limit
*
* @return InsertQueryBuilder
*/
public function limit($limit)
{
$this->limit = $limit;

return $this;
}

/**
* @param int $offset
*
* @return InsertQueryBuilder
*/
public function offset($offset)
{
$this->offset = $offset;

return $this;
}
}
16 changes: 10 additions & 6 deletions src/HS/Builder/QueryBuilderAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ public function getQuery($indexId, $socket, $openIndexQuery = null)
$this->openIndexQuery = $openIndexQuery;
$classPath = $this->getQueryClassPath();

$query = null;
$query = array();
switch ($classPath) {
case 'HS\Query\InsertQuery':
$query = new InsertQuery($indexId, $this->valueList, $socket, $openIndexQuery);
foreach ($this->valueList as $rowList) {
$query[] = new InsertQuery($indexId, $rowList, $socket, $openIndexQuery);
}
break;
case 'HS\Query\DeleteQuery':
$query = new DeleteQuery(
$query[] = new DeleteQuery(
$indexId,
$this->comparison,
$this->keyList,
Expand All @@ -70,7 +72,7 @@ public function getQuery($indexId, $socket, $openIndexQuery = null)
);
break;
case 'HS\Query\SelectQuery':
$query = new SelectQuery(
$queryTmp = new SelectQuery(
$indexId,
$this->comparison,
$this->keyList,
Expand All @@ -83,9 +85,11 @@ public function getQuery($indexId, $socket, $openIndexQuery = null)
$this->filterList, null

);
$queryTmp->setReturnType($this->returnType);
$query[] = $queryTmp;
break;
case 'HS\Query\UpdateQuery':
$query = new UpdateQuery(
$query[] = new UpdateQuery(
$indexId,
$this->comparison,
$this->keyList,
Expand All @@ -101,7 +105,7 @@ public function getQuery($indexId, $socket, $openIndexQuery = null)
);
break;
default:
$query = new $classPath(
$query[] = new $classPath(
$indexId,
$this->comparison,
$this->keyList,
Expand Down
16 changes: 1 addition & 15 deletions src/HS/Builder/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,10 @@ interface QueryBuilderInterface
* @param null|OpenIndexQuery $openIndexQuery
*
*
* @return QueryInterface
* @return QueryInterface[]
*/
public function getQuery($indexId, $socket, $openIndexQuery = null);

/**
* @param int $limit
*
* @return QueryBuilderInterface
*/
public function limit($limit);

/**
* @param int $offset
*
* @return QueryBuilderInterface
*/
public function offset($offset);

/**
* @return boolean
*/
Expand Down
50 changes: 34 additions & 16 deletions src/HS/CommonClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ abstract class CommonClient
*/
abstract public function authenticate($authKey);

/**
* @param string $dbName
* @param string $tableName
* @param string $indexName
* @param array $columnList
* @param bool $returnOnlyId
* @param array $filterColumnList
*
* @return int|OpenIndexQuery
*/
abstract public function getIndexId(
$dbName, $tableName, $indexName, array $columnList, $returnOnlyId = true, array $filterColumnList = array()
);
Expand All @@ -77,15 +87,17 @@ public function __construct($url, $port, $authKey = null, $debug = false)
}

/**
* {@inheritdoc}
* @return boolean
*/
public function isDebug()
{
return $this->debug;
}

/**
* {@inheritdoc}
* @return \HS\Result\ResultAbstract[]
* @throws Exception
* @throws \Stream\Exception\StreamException
*/
public function getResultList()
{
Expand Down Expand Up @@ -131,39 +143,42 @@ public function getResultList()
}

/**
* {@inheritdoc}
* @return int
*/
public function getCountQueriesInQueue()
{
return count($this->queryListNotSent);
}

/**
* {@inheritdoc}
* @return int
*/
public function getCountQueries()
{
return $this->countQueries;
}

/**
* {@inheritdoc}
* @return float
*/
public function getTimeQueries()
{
return $this->timeQueries;
}

/**
* {@inheritdoc}
* @return string
*/
public function getUrlConnection()
{
return $this->getStream()->getConnection()->getUrlConnection();
}

/**
* {@inheritdoc}
* @param QueryBuilderInterface $queryBuilder
*
* @throws Exception
* @return \HS\Query\QueryAbstract
*/
public function addQueryBuilder(QueryBuilderInterface $queryBuilder)
{
Expand All @@ -178,19 +193,25 @@ public function addQueryBuilder(QueryBuilderInterface $queryBuilder)

// if returned int
if (is_int($openIndexQuery)) {
/** @var int $openIndexQuery */
$queryForAdd = $queryBuilder->getQuery($openIndexQuery, $this);
$queryForAddList = $queryBuilder->getQuery($openIndexQuery, $this);
} else {
/** @var OpenIndexQuery $openIndexQuery */
$queryForAdd = $queryBuilder->getQuery($openIndexQuery->getIndexId(), $this, $openIndexQuery);
$queryForAddList = $queryBuilder->getQuery($openIndexQuery->getIndexId(), $this, $openIndexQuery);
}

if (count($queryForAddList) === 0) {
throw new Exception("Returned empty list queries.");
}

$queryForAdd = null;
foreach ($queryForAddList as $queryForAdd) {
$this->addQuery($queryForAdd);
}
$this->addQuery($queryForAdd);

return $queryForAdd;
}

/**
* {@inheritdoc}
* @param QueryInterface $query
*/
public function addQuery(QueryInterface $query)
{
Expand All @@ -208,9 +229,6 @@ public function sendQueries()
}
}

/**
* {@inheritdoc}
*/
public function reOpen()
{
$this->close();
Expand Down
32 changes: 7 additions & 25 deletions src/HS/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@

namespace HS;

use Stream\StreamDriverInterface;

class Driver implements StreamDriverInterface
class Driver
{
const EOL = "\x0a"; // "\n" char
const DELIMITER = "\x09"; // "\t" char
const NULL = "\0";

private static $encodeMap = array(
// NULL is expressed as a single NUL(0x00).
null => "\x00",
// A character in the range [0x00 - 0x0f] is prefixed by 0x01 and shifted by 0x40
"\x00" => "\x01\x40",
"\x01" => "\x01\x41",
Expand Down Expand Up @@ -67,14 +63,6 @@ public static function prepareSendDataStatic($data)
return implode(self::DELIMITER, $encodedData);
}

/**
* @{inheritdoc}
*/
public function prepareSendData($data)
{
return self::prepareSendDataStatic($data);
}

/**
* @param string $data
*
Expand All @@ -87,14 +75,6 @@ public static function prepareReceiveDataStatic($data)
return array_map('self::decodeData', $dataList);
}

/**
* @{inheritdoc}
*/
public function prepareReceiveData($data)
{
return self::prepareReceiveDataStatic($data);
}

/**
* @param string $data
*
Expand All @@ -112,10 +92,12 @@ public static function decodeData($data)
*/
public static function encodeData($data)
{
if (false === $newStr = strtr($data, self::$encodeMap)) {
return $data;
} else {
return $newStr;
// NULL is expressed as a single NUL(0x00).
// null => "\x00",
if ($data === null) {
return "\x00";
}

return strtr($data, self::$encodeMap);
}
}
9 changes: 3 additions & 6 deletions src/HS/Query/InsertQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ public function getQueryString()
$queryString = sprintf(
"%d" . Driver::DELIMITER . "+" . Driver::DELIMITER . "%d",
$this->getIndexId(),
count($this->valueList[0])
count($this->valueList)
);

foreach ($this->valueList as $row) {
$queryString .= "\t" . Driver::prepareSendDataStatic($row);
}

$queryString .= "\t" . Driver::prepareSendDataStatic($this->valueList);

return $queryString;
}

Expand Down
Loading

0 comments on commit 4dd52db

Please sign in to comment.