diff --git a/README.md b/README.md index e057a6d..634418f 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,9 @@ Get a default connection and send query $posts = $presto->connection()->query('select * from posts')->get(); ``` -If set manager as global, just query directly and get data with [collection](https://github.com/tightenco/collect) +If set manager as global, just query directly and get data ```php $posts = Presto::query('SELECT * FROM posts')->get(); -$posts->toArray(); - /* [ [1, 'Good pracetice'], @@ -57,8 +55,6 @@ $posts->toArray(); */ $posts = Presto::query('SELECT * FROM posts')->getAssoc(); -$posts->toArray(); - /* [ ['id' => 1, 'title' => 'Good pracetice'], diff --git a/composer.json b/composer.json index 3b4edf8..293629d 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,7 @@ "require": { "php": ">=7.2", "ext-json": "*", - "guzzlehttp/guzzle": "~6.3", - "tightenco/collect": "^5.7" + "guzzlehttp/guzzle": "~6.3" }, "require-dev": { "blastcloud/guzzler": "^1.0", @@ -22,7 +21,8 @@ "phpstan/phpstan": "^0.11.2", "phpstan/phpstan-mockery": "^0.11.0", "phpunit/phpunit": "^8.0", - "squizlabs/php_codesniffer": "^3.4" + "squizlabs/php_codesniffer": "^3.4", + "symfony/var-dumper": "^4.2" }, "autoload": { "psr-4": { diff --git a/src/Collectors/AssocCollector.php b/src/Collectors/AssocCollector.php index 7e8860f..149c707 100644 --- a/src/Collectors/AssocCollector.php +++ b/src/Collectors/AssocCollector.php @@ -4,51 +4,50 @@ namespace Clouding\Presto\Collectors; -use Clouding\Presto\Contracts\Collectorable; -use Tightenco\Collect\Support\Collection; - class AssocCollector implements Collectorable { /** - * The collection of collect data. + * The array of collect data. * - * @var \Tightenco\Collect\Support\Collection + * @var array */ - protected $collection; + protected $collection = []; /** - * Create a new collector instance. + * Collect data from presto response. + * + * @param object $response */ - public function __construct() + public function collect(object $response) { - $this->collection = new Collection(); + if (!isset($response->data, $response->columns)) { + return; + } + + $this->collection = array_merge($this->collection, $this->getAssocData($response)); } /** - * Collect needs data from object + * Get associated data. * - * @param object $object + * @param object $response + * @return array */ - public function collect(object $object) + protected function getAssocData(object $response): array { - if (!isset($object->data, $object->columns)) { - return; - } - - $columns = (new Collection($object->columns))->pluck('name'); - $data = (new Collection($object->data))->map(function (array $row) use ($columns) { - return $columns->combine($row)->toArray(); - }); + $columns = array_column($response->columns, 'name'); - $this->collection = $this->collection->merge($data); + return array_map(function (array $data) use ($columns) { + return array_combine($columns, $data); + }, $response->data); } /** * Get collect data. * - * @return \Tightenco\Collect\Support\Collection + * @return array */ - public function get(): Collection + public function get(): array { return $this->collection; } diff --git a/src/Collectors/BasicCollector.php b/src/Collectors/BasicCollector.php new file mode 100644 index 0000000..2055b5b --- /dev/null +++ b/src/Collectors/BasicCollector.php @@ -0,0 +1,39 @@ +data)) { + return; + } + + $this->collection = array_merge($this->collection, $response->data); + } + + /** + * Get collect data. + * + * @return array + */ + public function get(): array + { + return $this->collection; + } +} diff --git a/src/Collectors/Collector.php b/src/Collectors/Collector.php deleted file mode 100644 index e7034de..0000000 --- a/src/Collectors/Collector.php +++ /dev/null @@ -1,50 +0,0 @@ -collection = new Collection(); - } - - /** - * Collect needs data from object. - * - * @param object $contents - */ - public function collect(object $contents) - { - if (!isset($contents->data)) { - return; - } - - $this->collection = $this->collection->merge($contents->data); - } - - /** - * Get collect data. - * - * @return \Tightenco\Collect\Support\Collection - */ - public function get(): Collection - { - return $this->collection; - } -} diff --git a/src/Collectors/Collectorable.php b/src/Collectors/Collectorable.php new file mode 100644 index 0000000..0a3e740 --- /dev/null +++ b/src/Collectors/Collectorable.php @@ -0,0 +1,22 @@ +collector = $collector; $this->resolve($this->sendQuery($query)); - while ($this->continue()) { - $this->resolve($this->sendNext()); + while ($this->hasNextUri()) { + $this->resolve($this->sendNextUri()); } return $this->collector->get(); @@ -91,10 +88,10 @@ public function execute(string $query, Collectorable $collector): Collection /** * Send query request. * - * @param string $query - * @return \Psr\Http\Message\ResponseInterface + * @param string $query + * @return object */ - protected function sendQuery(string $query): ResponseInterface + protected function sendQuery(string $query): object { $baseUri = $this->connection->getHost() . static::STATEMENT_URI; $headers = [ @@ -103,46 +100,48 @@ protected function sendQuery(string $query): ResponseInterface 'X-Presto-Catalog' => $this->connection->getCatalog(), ]; - return $this->client->post($baseUri, ['headers' => $headers, 'body' => $query]); + $response = $this->client->post($baseUri, ['headers' => $headers, 'body' => $query]); + + return json_decode((string) $response->getBody()); } /** * Send next query. * - * @return \Psr\Http\Message\ResponseInterface + * @return object */ - protected function sendNext(): ResponseInterface + protected function sendNextUri(): object { - return $this->client->get($this->nextUri); + $response = $this->client->get($this->nextUri); + + return json_decode((string) $response->getBody()); } /** * Resolve response. * - * @param \Psr\Http\Message\ResponseInterface $response + * @param object $response */ - protected function resolve(ResponseInterface $response) + protected function resolve(object $response) { - $contents = json_decode($response->getBody()->getContents()); - - $this->checkState($contents); + $this->checkState($response); - $this->setNextUri($contents); + $this->setNextUri($response); - $this->collector->collect($contents); + $this->collector->collect($response); } /** * Check response state. * - * @param object $contents + * @param object $response * * @throws \Clouding\Presto\Exceptions\PrestoException */ - protected function checkState(object $contents) + protected function checkState(object $response) { - if ($contents->stats->state === PrestoState::FAILED) { - $message = "{$contents->error->errorName}: {$contents->error->message}"; + if ($response->stats->state === PrestoState::FAILED) { + $message = "{$response->error->errorName}: {$response->error->message}"; throw new PrestoException($message); } } @@ -150,11 +149,11 @@ protected function checkState(object $contents) /** * Set next uri. * - * @param object $contents + * @param object $response */ - protected function setNextUri(object $contents) + protected function setNextUri(object $response) { - $this->nextUri = $contents->nextUri ?? null; + $this->nextUri = $response->nextUri ?? null; } /** @@ -162,7 +161,7 @@ protected function setNextUri(object $contents) * * @return bool */ - protected function continue(): bool + protected function hasNextUri(): bool { return isset($this->nextUri); } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index ac15ca9..dc15316 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -5,8 +5,7 @@ namespace Clouding\Presto; use Clouding\Presto\Collectors\AssocCollector; -use Clouding\Presto\Collectors\Collector; -use Tightenco\Collect\Support\Collection; +use Clouding\Presto\Collectors\BasicCollector; class QueryBuilder { @@ -37,7 +36,7 @@ public function __construct(Processor $processor) /** * Set raw query. * - * @param string $query + * @param string $query * @return \Clouding\Presto\QueryBuilder */ public function raw(string $query): QueryBuilder @@ -50,19 +49,19 @@ public function raw(string $query): QueryBuilder /** * Execute the query statement. * - * @return \Tightenco\Collect\Support\Collection + * @return array */ - public function get(): Collection + public function get(): array { - return $this->processor->execute($this->toSql(), new Collector()); + return $this->processor->execute($this->toSql(), new BasicCollector()); } /** * Execute the query statement with assoc column. * - * @return \Tightenco\Collect\Support\Collection + * @return array */ - public function getAssoc(): Collection + public function getAssoc(): array { return $this->processor->execute($this->toSql(), new AssocCollector()); } diff --git a/tests/Collector/AssocCollectorTest.php b/tests/Collector/AssocCollectorTest.php index 96a32b9..d0da9fb 100644 --- a/tests/Collector/AssocCollectorTest.php +++ b/tests/Collector/AssocCollectorTest.php @@ -6,8 +6,6 @@ use Clouding\Presto\Collectors\AssocCollector; use PHPUnit\Framework\TestCase; -use stdClass; -use Tightenco\Collect\Support\Collection; class AssocCollectorTest extends TestCase { @@ -15,32 +13,47 @@ public function testGet() { $collector = new AssocCollector(); - $this->assertInstanceOf(Collection::class, $collector->get()); - $this->assertEmpty($collector->get()->toArray()); + $this->assertEmpty($collector->get()); } public function testCollect() { - $object = new stdClass(); - $object->columns = [['name' => 'id']]; - $object->data = [[1]]; - - $object2 = new stdClass(); - $object2->columns = [['name' => 'id']]; - $object2->data = [[999]]; + $object1 = (object) [ + 'columns' => [ + ['name' => 'id'], + ['name' => 'title'], + ], + 'data' => [ + [1, 'Go to school'] + ] + ]; + + $object2 = (object) [ + 'columns' => [ + ['name' => 'id'], + ['name' => 'title'], + ], + 'data' => [ + [2, 'Go to store'] + ] + ]; $collector = new AssocCollector(); - $collector->collect($object); + $collector->collect($object1); $collector->collect($object2); - $this->assertSame([['id' => 1], ['id' => 999]], $collector->get()->toArray()); + $expected = [ + ['id' => 1, 'title' => 'Go to school'], + ['id' => 2, 'title' => 'Go to store'] + ]; + $this->assertSame($expected, $collector->get()); } public function testCollectNothing() { $collector = new AssocCollector(); - $collector->collect((object)['apple']); + $collector->collect((object) ['apple']); - $this->assertEmpty($collector->get()->toArray()); + $this->assertEmpty($collector->get()); } } diff --git a/tests/Collector/BasicCollectorTest.php b/tests/Collector/BasicCollectorTest.php new file mode 100644 index 0000000..5dc539c --- /dev/null +++ b/tests/Collector/BasicCollectorTest.php @@ -0,0 +1,51 @@ +assertEmpty($collector->get()); + } + + public function testCollect() + { + $object1 = (object) [ + 'data' => [ + [1, 'Go to school'] + ] + ]; + + $object2 = (object) [ + 'data' => [ + [2, 'Go to store'] + ] + ]; + + $collector = new BasicCollector(); + $collector->collect($object1); + $collector->collect($object2); + + $expected = [ + [1, 'Go to school'], + [2, 'Go to store'], + ]; + $this->assertSame($expected, $collector->get()); + } + + public function testCollectNothing() + { + $collector = new BasicCollector(); + $collector->collect((object) ['banana']); + + $this->assertEmpty($collector->get()); + } +} diff --git a/tests/Collector/CollectorTest.php b/tests/Collector/CollectorTest.php deleted file mode 100644 index 0526875..0000000 --- a/tests/Collector/CollectorTest.php +++ /dev/null @@ -1,37 +0,0 @@ -assertInstanceOf(Collection::class, $collector->get()); - $this->assertEmpty($collector->get()->toArray()); - } - - public function testCollect() - { - $collector = new Collector(); - $collector->collect((object) ['data' => 1]); - $collector->collect((object) ['data' => 1]); - - $this->assertSame([1, 1], $collector->get()->toArray()); - } - - public function testCollectNothing() - { - $collector = new Collector(); - $collector->collect((object) ['banana']); - - $this->assertEmpty($collector->get()->toArray()); - } -} diff --git a/tests/ProcessorTest.php b/tests/ProcessorTest.php index d4775fd..243d2c1 100644 --- a/tests/ProcessorTest.php +++ b/tests/ProcessorTest.php @@ -5,15 +5,14 @@ namespace Clouding\Presto\Tests; use BlastCloud\Guzzler\UsesGuzzler; +use Clouding\Presto\Collectors\Collectorable; use Clouding\Presto\Connection\Connection; -use Clouding\Presto\Contracts\Collectorable; -use Clouding\Presto\Contracts\PrestoState; +use Clouding\Presto\PrestoState; use Clouding\Presto\Exceptions\PrestoException; use Clouding\Presto\Processor; use GuzzleHttp\Psr7\Response; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use PHPUnit\Framework\TestCase; -use Tightenco\Collect\Support\Collection; class ProcessorTest extends TestCase { @@ -41,7 +40,7 @@ public function testExecuteException() $this->expectException(PrestoException::class); - $this->expectExceptionMessage("{$response['error']['errorName']}: {$response['error']['message']}"); + $this->expectExceptionMessage("This is a error name: This is a error message"); $processor = new Processor($connection, $this->guzzler->getClient()); $processor->execute('Go to school', $collector); @@ -54,14 +53,12 @@ public function testExecute() $responses = [ [ 'nextUri' => 'http://example.com/1', - 'data' => [1, 2, 3], 'stats' => [ 'state' => 'xxx' ], ], [ 'nextUri' => 'http://example.com/2', - 'data' => [1, 2, 3], 'stats' => [ 'state' => 'xxx' ], @@ -82,17 +79,12 @@ public function testExecute() $collector->shouldReceive('collect') ->times(3); $collector->shouldReceive('get') - ->once() - ->andReturn(collect([1, 2, 3, 1, 2, 3])); + ->once(); $processor = new Processor($connection, $this->guzzler->getClient()); - $data = $processor->execute('Query String', $collector); - - $this->assertInstanceOf(Collection::class, $data); - $this->assertSame([1, 2, 3, 1, 2, 3], $data->toArray()); + $processor->execute('Query String', $collector); - // Test http request $this->guzzler->assertHistoryCount(3); $this->guzzler->expects($this->once()) ->post('http://presto.abc' . Processor::STATEMENT_URI) diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 4cbbf44..75696e3 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -5,12 +5,11 @@ namespace Clouding\Presto\Tests; use Clouding\Presto\Collectors\AssocCollector; -use Clouding\Presto\Collectors\Collector; +use Clouding\Presto\Collectors\BasicCollector; use Clouding\Presto\Processor; use Clouding\Presto\QueryBuilder; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use PHPUnit\Framework\TestCase; -use Tightenco\Collect\Support\Collection; class QueryBuilderTest extends TestCase { @@ -31,14 +30,13 @@ public function testGet() $processor = mock(Processor::class); $processor->shouldReceive('execute') ->once() - ->with('', Collector::class) - ->andReturn(collect([1, 2, 3])); + ->with('', BasicCollector::class) + ->andReturn([1, 2, 3]); $builder = new QueryBuilder($processor); $rows = $builder->get(); - $this->assertInstanceOf(Collection::class, $rows); - $this->assertEquals([1, 2, 3], $rows->toArray()); + $this->assertEquals([1, 2, 3], $rows); } public function testGetAssoc() @@ -47,12 +45,11 @@ public function testGetAssoc() $processor->shouldReceive('execute') ->once() ->with('', AssocCollector::class) - ->andReturn(collect([1, 2, 3])); + ->andReturn([1, 2, 3]); $builder = new QueryBuilder($processor); $rows = $builder->getAssoc(); - $this->assertInstanceOf(Collection::class, $rows); - $this->assertEquals([1, 2, 3], $rows->toArray()); + $this->assertEquals([1, 2, 3], $rows); } }