diff --git a/docs/eng/Decrement.md b/docs/eng/Decrement.md index 6b95e3c..414efba 100644 --- a/docs/eng/Decrement.md +++ b/docs/eng/Decrement.md @@ -31,6 +31,12 @@ if($decrementQuery->getResult()->isSuccessfully()){ } ``` +Another way to execute the query: +```php +$decrementQuery->execute(); // query was sent and the results on this query and all from the queue were parsed +$decrementResult = $decrementQuery->getResult(); +``` + Decrement with the opening index ------------ This command will check whether there is a required index if it is not, first open and then perform Decrement. diff --git a/docs/eng/Delete.md b/docs/eng/Delete.md index 3fe7b07..f40c2bf 100644 --- a/docs/eng/Delete.md +++ b/docs/eng/Delete.md @@ -48,6 +48,12 @@ $deleteQuery = $writer->delete( $writer->getResultList(); ``` +Another way to execute the query: +```php +$deleteQuery->execute(); // query was sent and the results on this query and all from the queue were parsed +$deleteResult = $deleteQuery->getResult(); +``` + Delete using QueryBuilder ------------ When initializing specify which columns and how much will be raised. If you specify a value with out number, diff --git a/docs/eng/Increment.md b/docs/eng/Increment.md index d4977ad..58f525e 100644 --- a/docs/eng/Increment.md +++ b/docs/eng/Increment.md @@ -31,6 +31,12 @@ if($incrementQuery->getResult()->isSuccessfully()){ } ``` +Another way to execute the query: +```php +$incrementQuery->execute(); // query was sent and the results on this query and all from the queue were parsed +$incrementResult = $incrementQuery->getResult(); +``` + Increment with the opening index ------------ This command will check whether there is a required index if it is not, first open and then perform Increment. diff --git a/docs/eng/Insert.md b/docs/eng/Insert.md index 2e4f5cb..4a6369b 100644 --- a/docs/eng/Insert.md +++ b/docs/eng/Insert.md @@ -53,6 +53,12 @@ $insertQuery = $writer->insert( ); ``` +Another way to execute the query: +```php +$insertQuery->execute(); // query was sent and the results on this query and all from the queue were parsed +$insertResult = $insertQuery->getResult(); +``` + Insert with QueryBuilder ------------ When adding entries to all arrays of records must contain the same keys. diff --git a/docs/eng/OpenIndex.md b/docs/eng/OpenIndex.md index 5e44d34..fe19174 100644 --- a/docs/eng/OpenIndex.md +++ b/docs/eng/OpenIndex.md @@ -21,12 +21,13 @@ $indexId = $reader->getIndexId( 'PRIMARY', array('key', 'text') ); +$reader->getResultList(); ``` If we just need to open the subscript c number 12 (for example), and we don't want to check: ```php -$indexIndexQuery = $reader->openIndex( +$openIndexQuery = $reader->openIndex( '12' 'database', 'tableName', @@ -34,7 +35,14 @@ $indexIndexQuery = $reader->openIndex( array('key', 'text'), array('num') ); +$reader->getResultList(); ``` Here we open the index with identifier 12, columns `key`,` text`, and the column to filter `num`. -Method returns us to the class OpenIndexQuery. \ No newline at end of file +Method returns us to the class OpenIndexQuery. + +Another way to execute the query: +```php +$insertQuery->execute(); // query was sent and the results on this query and all from the queue were parsed +$insertResult = $insertQuery->getResult(); +``` \ No newline at end of file diff --git a/docs/eng/Select.md b/docs/eng/Select.md index e063231..5af0074 100644 --- a/docs/eng/Select.md +++ b/docs/eng/Select.md @@ -34,6 +34,12 @@ To send all queries and get the results you need to do: $resultList = $reader->getResultList(); ``` +Another way to execute the query: +```php +$selectQuery->execute(); // query was sent and the results on this query and all from the queue were parsed +$selectResult = $selectQuery->getResult(); +``` + The variable `$resultList` contains a list of all results. Just the desired result can be obtained diff --git a/docs/eng/Update.md b/docs/eng/Update.md index 623b197..3a51f0f 100644 --- a/docs/eng/Update.md +++ b/docs/eng/Update.md @@ -40,6 +40,12 @@ $updateQuery = $writer->update( ); ``` +Another way to execute the query: +```php +$updateQuery->execute(); // query was sent and the results on this query and all from the queue were parsed +$updateResult = $updateQuery->getResult(); +``` + Update with QueryBuilder ------------ When initializing specify which columns to which data will be replaced. diff --git a/docs/rus/Decrement.md b/docs/rus/Decrement.md index 27928bd..fbb35ab 100644 --- a/docs/rus/Decrement.md +++ b/docs/rus/Decrement.md @@ -28,6 +28,13 @@ if($decrementQuery->getResult()->isSuccessfully()){ } ``` +Другой способ выполнить запрос: +```php +$decrementQuery->execute(); // отправлен запрос + получен ответ на этот запрос + все, что было в очереди на отправку +$decrementResult = $decrementQuery->getResult(); +``` + + Decrement с открытием индекса ------------ Данная команда проверит есть ли нужный индекс, если его нет, то сначала откроет, а затем выполнит Decrement. diff --git a/docs/rus/Delete.md b/docs/rus/Delete.md index e65113b..bb0192e 100644 --- a/docs/rus/Delete.md +++ b/docs/rus/Delete.md @@ -27,6 +27,12 @@ if($deleteQuery->getResult()->isSuccessfully()){ } ``` +Другой способ выполнить запрос: +```php +$deleteQuery->execute(); // отправлен запрос + получен ответ на этот запрос + все, что было в очереди на отправку +$deleteResult = $deleteQuery->getResult(); +``` + Delete с открытием индекса ------------ Данная команда проверит есть ли нужный индекс, если его нет, то сначала откроет, а затем выполнит Delete. diff --git a/docs/rus/Increment.md b/docs/rus/Increment.md index cccfc07..53fe86a 100644 --- a/docs/rus/Increment.md +++ b/docs/rus/Increment.md @@ -28,6 +28,12 @@ if($incrementQuery->getResult()->isSuccessfully()){ } ``` +Другой способ выполнить запрос: +```php +$incrementQuery->execute(); // отправлен запрос + получен ответ на этот запрос + все, что было в очереди на отправку +$incrementResult = $incrementQuery->getResult(); +``` + Increment с открытием индекса ------------ Данная команда проверит есть ли нужный индекс, если его нет, то сначала откроет, а затем выполнит Increment. diff --git a/docs/rus/Insert.md b/docs/rus/Insert.md index fcf921d..eb08210 100644 --- a/docs/rus/Insert.md +++ b/docs/rus/Insert.md @@ -31,6 +31,12 @@ if($insertQuery->getResult()->isSuccessfully()){ } ``` +Другой способ выполнить запрос: +```php +$insertQuery->execute(); // отправлен запрос + получен ответ на этот запрос + все, что было в очереди на отправку +$insertResult = $insertQuery->getResult(); +``` + Insert с открытием индекса ------------ Вставка данных происходит в `'key', 'date', 'float', 'varchar', 'text', 'set', 'union'`. diff --git a/docs/rus/OpenIndex.md b/docs/rus/OpenIndex.md index 9725f2a..f70a8c2 100644 --- a/docs/rus/OpenIndex.md +++ b/docs/rus/OpenIndex.md @@ -19,12 +19,14 @@ $indexId = $reader->getIndexId( 'PRIMARY', array('key', 'text') ); + +$reader->getResultList(); ``` Если нам нужно просто открыть индекс c номер 12(например), и мы ничего не хотим проверять: ```php -$indexIndexQuery = $reader->openIndex( +$openIndexQuery = $reader->openIndex( '12' 'database', 'tableName', @@ -32,7 +34,15 @@ $indexIndexQuery = $reader->openIndex( array('key', 'text'), array('num') ); + +$reader->getResultList(); ``` Здесь мы открываем индекс c номером 12, колонками `key`, `text`, а так же колонкой для фильтров `num`. -Метод вернет нам класс OpenIndexQuery. \ No newline at end of file +Метод вернет нам класс OpenIndexQuery. + +Другой способ выполнить запрос: +```php +$openIndexQuery->execute(); // отправлен запрос + получен ответ на этот запрос + все, что было в очереди на отправку +$openIndexResult = $openIndexQuery->getResult(); +``` \ No newline at end of file diff --git a/docs/rus/Select.md b/docs/rus/Select.md index da735bf..542c75c 100644 --- a/docs/rus/Select.md +++ b/docs/rus/Select.md @@ -27,6 +27,11 @@ $selectQuery = $reader->selectByIndex($indexId, Comparison::EQUAL, array(42)); ```php $resultList = $reader->getResultList(); ``` +Другой способ выполнить запрос: +```php +$selectQuery->execute(); // отправлен запрос + получен ответ на этот запрос + все, что было в очереди на отправку +$selectResult = $selectQuery->getResult(); +``` Переменная $resultList содержит список всех результатов. Так же нужный нам результат можно получить ```php diff --git a/docs/rus/Update.md b/docs/rus/Update.md index 607c166..6236d12 100644 --- a/docs/rus/Update.md +++ b/docs/rus/Update.md @@ -22,6 +22,12 @@ if($updateQuery->getResult()->getNumberModifiedRows() == 0){ } ``` +Другой способ выполнить запрос: +```php +$updateQuery->execute(); // отправлен запрос + получен ответ на этот запрос + все, что было в очереди на отправку +$updateResult = $updateQuery->getResult(); +``` + Update с открытием индекса ------------ Данная команда проверит есть ли нужный индекс, если его нет, то сначала откроет, а затем выполнит Update. diff --git a/src/HS/Query/QueryAbstract.php b/src/HS/Query/QueryAbstract.php index f03c5fe..74fd31f 100644 --- a/src/HS/Query/QueryAbstract.php +++ b/src/HS/Query/QueryAbstract.php @@ -7,6 +7,8 @@ use HS\Component\ParameterBag; use HS\Driver; +use HS\Exception\WrongParameterException; +use HS\ReaderInterface; use HS\Result\SelectResult; use HS\Validator; @@ -25,6 +27,11 @@ abstract class QueryAbstract implements QueryInterface 'HS\Query\DecrementQuery' => 'HS\Result\DecrementResult', ); + /** + * {@inheritdoc} + */ + abstract public function getQueryParameters(); + /** * @param array $parameterList */ @@ -56,14 +63,6 @@ public function __construct(array $parameterList) } - private function initIndexName() - { - $indexName = $this->getParameter('indexName'); - if (!$this->getParameterBag()->isExists('indexName') || empty($indexName)) { - $this->getParameterBag()->setParameter('indexName', 'PRIMARY'); - } - } - /** * @param string $parameterName * @param mixed $defaultValue @@ -91,11 +90,6 @@ public function getQueryString() return Driver::prepareSendDataStatic($this->getQueryParameters()); } - /** - * {@inheritdoc} - */ - abstract public function getQueryParameters(); - /** * @return int */ @@ -119,6 +113,48 @@ public function setResultData($data) $this->setResultObject($this->queryResultMap[$queryClassName], $data); } + /** + * @return $this + * @throws WrongParameterException + */ + public function execute() + { + $this->getSocket()->addQuery($this); + $this->getSocket()->getResultList(); + + return $this; + } + + /** + * @return string + */ + protected function getQueryClassName() + { + return $this->getParameter('queryClassName'); + } + + /** + * @return ParameterBag + */ + protected function getParameterBag() + { + return $this->parameterBag; + } + + /** + * @throws WrongParameterException + * @return ReaderInterface + */ + protected function getSocket() + { + $socket = $this->getParameter('socket'); + if (!($socket instanceof ReaderInterface)) { + throw new WrongParameterException('Socket not found'); + } + + return $socket; + } + /** * @param string $className * @param mixed $data @@ -150,19 +186,13 @@ private function setSelectResultObject($data) } /** - * @return string - */ - protected function getQueryClassName() - { - return $this->getParameter('queryClassName'); - } - - /** - * @return ParameterBag + * @return void */ - protected function getParameterBag() + private function initIndexName() { - return $this->parameterBag; + $indexName = $this->getParameter('indexName'); + if (!$this->getParameterBag()->isExists('indexName') || empty($indexName)) { + $this->getParameterBag()->setParameter('indexName', 'PRIMARY'); + } } - } \ No newline at end of file diff --git a/src/HS/Reader.php b/src/HS/Reader.php index 2e15dfd..392667d 100644 --- a/src/HS/Reader.php +++ b/src/HS/Reader.php @@ -109,7 +109,7 @@ public function authenticate($authKey) */ public function text($queryText, $queryClass) { - $textQuery = new TextQuery(array('text' => $queryText), $queryClass); + $textQuery = new TextQuery(array('text' => $queryText, 'socket' => $this), $queryClass); $this->addQuery($textQuery); return $textQuery; @@ -128,7 +128,8 @@ public function openIndex( 'tableName' => $tableName, 'indexName' => $indexName, 'columnList' => $columnList, - 'filterColumnList' => $filterColumnList + 'filterColumnList' => $filterColumnList, + 'socket' => $this, ) ); $this->addQuery($indexQuery); @@ -181,7 +182,8 @@ public function selectByIndex( 'offset' => $offset, 'limit' => $limit, 'columnList' => $this->getKeysByIndexId($indexId), - 'filterList' => $filterList + 'filterList' => $filterList, + 'socket' => $this, ) ); @@ -208,7 +210,8 @@ public function selectInByIndex($indexId, $in, $offset = null, $limit = null, ar 'limit' => ($limit !== null) ? $limit : count($in), 'columnList' => $this->getKeysByIndexId($indexId), 'inKeyList' => new InList(0, $in), - 'filterList' => $filterList + 'filterList' => $filterList, + 'socket' => $this, ) ); @@ -242,7 +245,7 @@ public function select( $indexId = $openIndexQuery->getIndexId(); } - $selectQuery= new SelectQuery( + $selectQuery = new SelectQuery( array( 'indexId' => $indexId, 'comparison' => $comparisonOperation, @@ -252,10 +255,12 @@ public function select( 'columnList' => $this->getKeysByIndexId($indexId), 'openIndexQuery' => $openIndexQuery, 'filterList' => $filterList, + 'socket' => $this, ) ); $this->addQuery($selectQuery); + return $selectQuery; } @@ -295,7 +300,8 @@ public function selectIn( 'offset' => $offset, 'limit' => ($limit !== null) ? $limit : count($in), 'columnList' => $this->getKeysByIndexId($indexId), - 'inKeyList' => new InList(0, $in) + 'inKeyList' => new InList(0, $in), + 'socket' => $this, ) ); @@ -317,12 +323,7 @@ public function isDebug() */ public function getResultList() { - $ResultsList = array(); - - if ($this->isQueryQueueEmpty()) { - // return empty array if no Queries in queue - return array(); - } + $resultsList = array(); // if debug mode enabled if (!$this->isDebug()) { @@ -352,7 +353,7 @@ public function getResultList() $this->addTimeQueries($currentQueryTime); $this->debugResultList[] = $ResultObject; } - $ResultsList[] = $ResultObject; + $resultsList[] = $ResultObject; // add time to general time counter } catch (ReadStreamException $e) { // TODO check @@ -361,7 +362,7 @@ public function getResultList() $this->queryListNotSent = array(); - return $ResultsList; + return $resultsList; } /** @@ -496,10 +497,15 @@ protected function setKeysToIndexId($indexId, $keys) /** * @param int $indexId * + * @throws WrongParameterException * @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)); + } + return $this->keysList[$indexId]; } diff --git a/src/HS/Writer.php b/src/HS/Writer.php index ed99ee2..1fe56e1 100644 --- a/src/HS/Writer.php +++ b/src/HS/Writer.php @@ -33,7 +33,8 @@ public function update( 'limit' => $limit, 'columnList' => $this->getKeysByIndexId($indexId), 'valueList' => $values, - 'openIndexQuery' => $openIndexQuery + 'openIndexQuery' => $openIndexQuery, + 'socket' => $this, ) ); @@ -56,7 +57,8 @@ public function updateByIndex( 'offset' => $offset, 'limit' => $limit, 'columnList' => $this->getKeysByIndexId($indexId), - 'valueList' => $values + 'valueList' => $values, + 'socket' => $this, ) ); $this->addQuery($updateQuery); @@ -83,7 +85,8 @@ public function delete( 'offset' => $offset, 'limit' => $limit, 'columnList' => $this->getKeysByIndexId($indexId), - 'openIndexQuery' => $openIndexQuery + 'openIndexQuery' => $openIndexQuery, + 'socket' => $this, ) ); $this->addQuery($deleteQuery); @@ -104,6 +107,7 @@ public function deleteByIndex($indexId, $comparisonOperation, array $keys, $limi 'offset' => $offset, 'limit' => $limit, 'columnList' => $this->getKeysByIndexId($indexId), + 'socket' => $this, ) ); $this->addQuery($deleteQuery); @@ -133,6 +137,7 @@ public function increment( 'columnList' => $this->getKeysByIndexId($indexId), 'openIndexQuery' => $openIndexQuery, 'valueList' => $valueList, + 'socket' => $this, ) ); $this->addQuery($incrementQuery); @@ -155,6 +160,7 @@ public function incrementByIndex( 'limit' => $limit, 'columnList' => $this->getKeysByIndexId($indexId), 'valueList' => $valueList, + 'socket' => $this, ) ); $this->addQuery($incrementQuery); @@ -185,6 +191,7 @@ public function decrement( 'columnList' => $this->getKeysByIndexId($indexId), 'openIndexQuery' => $openIndexQuery, 'valueList' => $valueList, + 'socket' => $this, ) ); $this->addQuery($decrementQuery); @@ -207,6 +214,7 @@ public function decrementByIndex( 'limit' => $limit, 'columnList' => $this->getKeysByIndexId($indexId), 'valueList' => $valueList, + 'socket' => $this, ) ); $this->addQuery($decrementQuery); @@ -231,7 +239,8 @@ public function insert( array( 'indexId' => $indexId, 'valueList' => $valueList, - 'openIndexQuery' => $openIndexQuery + 'openIndexQuery' => $openIndexQuery, + 'socket' => $this, ) ); $this->addQuery($insertQuery); @@ -247,7 +256,8 @@ public function insertByIndex($indexId, array $valueList) $updateQuery = new InsertQuery( array( 'indexId' => $indexId, - 'valueList' => $valueList + 'valueList' => $valueList, + 'socket' => $this, ) ); diff --git a/tests/HS/Reader/ReaderTest.php b/tests/HS/Reader/ReaderTest.php new file mode 100644 index 0000000..6eccb6c --- /dev/null +++ b/tests/HS/Reader/ReaderTest.php @@ -0,0 +1,95 @@ + + */ + +namespace HS\Tests\Reader; + +use HS\Component\Comparison; +use HS\Component\Filter; +use HS\Exception\WrongParameterException; +use HS\Tests\TestCommon; + +class ReaderTest extends TestCommon +{ + public function testUrlConnection() + { + $reader = $this->getReader(); + + $this->assertEquals('tcp://127.0.0.1:9998', $reader->getUrlConnection(), 'Fall wrong url connection.'); + } + + public function testIsDebug() + { + $reader = $this->getReader(); + + $this->assertFalse($reader->isDebug(), 'Fall reader with debug on.'); + } + + public function testExceptionOnMissedIndexId() + { + $reader = $this->getReader(); + $reader->reOpen(); + + try { + $reader->selectByIndex( + '9000', // some random value for get exception error + Comparison::MORE, + array(1), + 0, + 99, + array(new Filter(Comparison::EQUAL, 0, 1)) + ); + } catch (WrongParameterException $e) { + return true; + } + + $this->fail('Not fall with out opening index.'); + + return true; + } + + public function testReopen() + { + $reader = $this->getReader(); + $reader->reOpen(); + + $indexId = $reader->getIndexId( + $this->getDatabase(), + $this->getTableName(), + 'PRIMARY', + array('key', 'text'), + true, + array('num') + ); + + $this->assertEquals(1, $reader->getCountQueriesInQueue(), 'Wrong count queries.'); + + $selectQuery = $reader->selectByIndex( + $indexId, + Comparison::MORE, + array(1), + 0, + 99, + array(new Filter(Comparison::EQUAL, 0, 1)) + ); + + $this->assertEquals(2, $reader->getCountQueriesInQueue(), 'Wrong count queries.'); + $data = $selectQuery->execute()->getResult()->getData(); + + // auth query + open index + select = 3 + $this->assertEquals(3, $reader->getCountQueries(), 'Wrong count queries.'); + $this->assertEquals(0, $reader->getCountQueriesInQueue(), 'Wrong count queries.'); + + $this->assertEquals( + array( + array( + 'key' => '100', + 'text' => '' + ) + ), + $data, + 'Got wrong data.' + ); + } +} \ No newline at end of file diff --git a/tests/HS/Reader/GetResultTest.php b/tests/HS/Reader/SelectQueryTest.php similarity index 91% rename from tests/HS/Reader/GetResultTest.php rename to tests/HS/Reader/SelectQueryTest.php index 06078d5..aceabaa 100644 --- a/tests/HS/Reader/GetResultTest.php +++ b/tests/HS/Reader/SelectQueryTest.php @@ -327,4 +327,40 @@ public function testSelectByIndexExistedValueWithFilter() "Fall selectByIndex query with filter returned invalid data." ); } + + public function testSelectByIndexExistedValueExecuted() + { + $reader = $this->getReader(); + + $indexId = $reader->getIndexId( + $this->getDatabase(), + $this->getTableName(), + 'PRIMARY', + array('key', 'text'), + true, + array('num') + ); + $selectQuery = $reader->selectByIndex( + $indexId, + Comparison::MORE, + array(1), + 0, + 99, + array(new Filter(Comparison::EQUAL, 0, 1)) + ); + + $selectResult = $selectQuery->execute()->getResult(); + + $this->assertTrue($selectResult->isSuccessfully(), 'Fail selectByIndex query executed by self.'); + $this->assertEquals( + array( + array( + 'key' => '100', + 'text' => '' + ) + ), + $selectResult->getData(), + "Fall selectByIndex query executed by self." + ); + } } \ No newline at end of file diff --git a/tests/HS/Reader/TextQueryTest.php b/tests/HS/Reader/TextQueryTest.php new file mode 100644 index 0000000..0c2c0e0 --- /dev/null +++ b/tests/HS/Reader/TextQueryTest.php @@ -0,0 +1,26 @@ + + */ + +namespace HS\Tests\Reader; + +use HS\Errors\CommandError; +use HS\Tests\TestCommon; + +class TextQueryTest extends TestCommon +{ + public function testTextQueryErrorCommand() + { + $reader = $this->getReader(); + $textQuery = $reader->text("dfgsdgsd", 'HS\Query\SelectQuery'); + $result = $textQuery->execute()->getResult(); + if (!($result->getError() instanceof CommandError)) { + $this->fail('Wrong instance of error.'); + } + + $this->assertEquals('cmd', $result->getErrorMessage(), 'Wrong message.'); + $this->assertEquals(false, $result->isSuccessfully(), 'Wrong query type'); + $this->assertEquals($textQuery, $result->getQuery(), 'Wrong query class'); + } +} \ No newline at end of file