Skip to content

Commit

Permalink
Unify repositories to mimic Queries component more
Browse files Browse the repository at this point in the history
  • Loading branch information
iquito committed Apr 28, 2019
1 parent 9d006a8 commit 64d5354
Show file tree
Hide file tree
Showing 39 changed files with 3,146 additions and 656 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
root = true

[*]

end_of_line = lf
charset = utf-8
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = false

[*.php]

end_of_line = lf
charset = utf-8
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
9 changes: 6 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/tests export-ignore
/examples export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml export-ignore
/phpcs.xml export-ignore
/phpunit.xml.dist export-ignore
/phpcs.xml.dist export-ignore
/captainhook.json export-ignore
/.travis.yml export-ignore
/.travis.yml export-ignore
/ruleset.xml export-ignore
/.editorconfig export-ignore
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ before_script:

script:
- vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml
- vendor/bin/phpstan analyse src --level=7
- vendor/bin/phpcs --standard=psr2 --extensions=php src tests
- vendor/bin/phpstan analyse src bin --level=7
- vendor/bin/phpcs --standard=ruleset.xml --extensions=php src bin tests
- if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build -t clover --exit-code $TRAVIS_TEST_RESULT; fi

notifications:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Squirrel Entities Component
===========================

[![Build Status](https://img.shields.io/travis/com/squirrelphp/entities.svg)](https://travis-ci.com/squirrelphp/entities) [![Software License](https://img.shields.io/badge/license-MIT-success.svg?style=flat-round)](LICENSE) [![Test Coverage](https://api.codeclimate.com/v1/badges/36a9f5a3b4abbaf7901c/test_coverage)](https://codeclimate.com/github/squirrelphp/entities/test_coverage) [![Packagist Version](https://img.shields.io/packagist/v/squirrelphp/entities.svg?style=flat-round)](https://packagist.org/packages/squirrelphp/entities)
[![Build Status](https://img.shields.io/travis/com/squirrelphp/entities.svg)](https://travis-ci.com/squirrelphp/entities) [![Test Coverage](https://api.codeclimate.com/v1/badges/36a9f5a3b4abbaf7901c/test_coverage)](https://codeclimate.com/github/squirrelphp/entities/test_coverage) ![PHPStan](https://img.shields.io/badge/style-level%207-success.svg?style=flat-round&label=phpstan) [![Packagist Version](https://img.shields.io/packagist/v/squirrelphp/entities.svg?style=flat-round)](https://packagist.org/packages/squirrelphp/entities) [![PHP Version](https://img.shields.io/packagist/php-v/squirrelphp/entities.svg)](https://packagist.org/packages/squirrelphp/entities) [![Software License](https://img.shields.io/badge/license-MIT-success.svg?style=flat-round)](LICENSE)

Simple, safe and flexible implementation of handling SQL entities and repositories as well as multi-table SQL queries while staying lightweight and easy to understand and use.

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions captainhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"conditions": []
},
{
"action": "vendor/bin/phpstan analyse src --level=7",
"action": "vendor/bin/phpstan analyse src bin --level=7",
"options": [],
"conditions": []
},
{
"action": "vendor/bin/phpcs --standard=psr2 --extensions=php src tests",
"action": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache src bin tests",
"options": [],
"conditions": []
}
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
"symfony/console": "^4.0",
"symfony/finder": "^4.0",
"doctrine/annotations": "^1.4",
"squirrelphp/queries": "^0.5.4"
"squirrelphp/queries": "^0.6"
},
"require-dev": {
"mockery/mockery": "^1.0",
"phpstan/phpstan": "^0.11.5",
"phpunit/phpunit": "^8.0",
"squizlabs/php_codesniffer": "^3.0",
"captainhook/plugin-composer": "^4.0"
"captainhook/plugin-composer": "^4.0",
"slevomat/coding-standard": "^5.0"
},
"suggest": {
"squirrelphp/queries-bundle": "Symfony integration of squirrel/queries - automatic assembling of decorated connections",
Expand All @@ -41,7 +42,7 @@
"sort-packages": true
},
"bin": [
"squirrel_repositories_generate"
"bin/squirrel_repositories_generate"
],
"autoload": {
"psr-4": {
Expand All @@ -54,9 +55,9 @@
}
},
"scripts": {
"phpstan": "vendor/bin/phpstan analyse src --level=7",
"phpstan": "vendor/bin/phpstan analyse src bin --level=7",
"phpunit": "vendor/bin/phpunit --colors=always",
"phpcs": "vendor/bin/phpcs --standard=psr2 --extensions=php src tests",
"phpcs": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache src bin tests",
"codecoverage": "vendor/bin/phpunit --coverage-html tests/_reports"
}
}
File renamed without changes.
39 changes: 39 additions & 0 deletions ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<ruleset name="IQ">
<description>PSR2 with some additional useful sniffs</description>

<!-- Include the whole PSR-2 standard -->
<rule ref="PSR2"/>

<config name="installed_paths" value="../../slevomat/coding-standard"/>

<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowContinueWithoutIntegerOperandInSwitch"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEmpty"/>
<rule ref="SlevomatCodingStandard.Classes.UnusedPrivateElements"/>
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
<properties>
<property name="searchAnnotations" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Exceptions.DeadCatch"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses"/>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>
<rule ref="SlevomatCodingStandard.Namespaces.DisallowGroupUse"/>
<rule ref="SlevomatCodingStandard.Namespaces.MultipleUsesPerLine"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash"/>
<rule ref="SlevomatCodingStandard.Commenting.ForbiddenAnnotations">
<properties>
<property name="forbiddenAnnotations" type="array">
<element value="@author"/>
<element value="@created"/>
<element value="@copyright"/>
<element value="@license"/>
<element value="@package"/>
<element value="@version"/>
</property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Commenting.EmptyComment"/>
</ruleset>
14 changes: 5 additions & 9 deletions src/Action/MultiCountEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ public function __construct(MultiRepositoryReadOnlyInterface $queryHandler)
$this->queryHandler = $queryHandler;
}

public function inRepositories(array $repositories)
public function inRepositories(array $repositories): self
{
$this->repositories = $repositories;
return $this;
}

public function connectedBy(array $repositoryConnections)
public function joinTables(array $repositoryConnections): self
{
$this->connections = $repositoryConnections;
return $this;
}

public function where(array $whereClauses): self
Expand All @@ -67,17 +69,11 @@ public function blocking(bool $active = true): self
*/
public function getNumber(): int
{
$results = $this->queryHandler->select([
'fields' => [
'num' => 'COUNT(*)',
],
return $this->queryHandler->count([
'repositories' => $this->repositories,
'tables' => $this->connections,
'where' => $this->where,
'flattenFields' => true,
'lock' => $this->blocking,
]);

return $results[0] ?? 0;
}
}
27 changes: 20 additions & 7 deletions src/Action/MultiSelectEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public function blocking(bool $active = true): self
/**
* Execute SELECT query and return a list of entries as arrays that matched it
*/
public function getEntries(): array
public function getAllEntries(): array
{
return $this->queryHandler->select([
return $this->queryHandler->fetchAll([
'fields' => $this->fields,
'repositories' => $this->repositories,
'tables' => $this->connections,
Expand All @@ -166,19 +166,16 @@ public function getEntries(): array
*/
public function getOneEntry(): ?array
{
$results = $this->queryHandler->select([
return $this->queryHandler->fetchOne([
'fields' => $this->fields,
'repositories' => $this->repositories,
'tables' => $this->connections,
'where' => $this->where,
'order' => $this->orderBy,
'group' => $this->groupBy,
'limit' => 1,
'offset' => $this->startAt,
'lock' => $this->blocking,
]);

return \array_pop($results);
}

/**
Expand All @@ -188,7 +185,23 @@ public function getOneEntry(): ?array
*/
public function getFlattenedFields(): array
{
return $this->queryHandler->selectFlattenedFields([
return $this->queryHandler->fetchAll([
'fields' => $this->fields,
'repositories' => $this->repositories,
'tables' => $this->connections,
'where' => $this->where,
'order' => $this->orderBy,
'group' => $this->groupBy,
'limit' => $this->limitTo,
'offset' => $this->startAt,
'lock' => $this->blocking,
'flattenFields' => true,
]);
}

public function getIterator(): MultiSelectIterator
{
return new MultiSelectIterator($this->queryHandler, [
'fields' => $this->fields,
'repositories' => $this->repositories,
'tables' => $this->connections,
Expand Down
66 changes: 48 additions & 18 deletions src/Action/MultiSelectEntriesFreeform.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public function fields(array $getTheseFields): self
return $this;
}

public function inRepositories(array $repositories)
public function inRepositories(array $repositories): self
{
$this->repositories = $repositories;
return $this;
}

public function queryAfterFROM(string $query): self
Expand All @@ -76,7 +77,7 @@ public function withParameters(array $queryParameters = []): self
return $this;
}

public function freeformQueriesAreBadPractice(string $confirmWithOK): self
public function confirmFreeformQueriesAreBadPractice(string $confirmWithOK): self
{
if ($confirmWithOK === 'OK') {
$this->confirmBadPractice = true;
Expand All @@ -87,17 +88,28 @@ public function freeformQueriesAreBadPractice(string $confirmWithOK): self
/**
* Execute SELECT query and return a list of entries as arrays that matched it
*/
public function getEntries(): array
public function getAllEntries(): array
{
if ($this->confirmBadPractice !== true) {
throw DBDebug::createException(
DBInvalidOptionException::class,
[ActionInterface::class],
'No confirmation that freeform queries are bad practice'
);
}
$this->makeSureBadPracticeWasConfirmed();

return $this->queryHandler->fetchAll([
'fields' => $this->fields,
'repositories' => $this->repositories,
'query' => $this->query,
'parameters' => $this->parameters,
]);
}

return $this->queryHandler->select([
/**
* Execute SELECT query and return exactly one entry, if one was found at all
*
* @return array|null
*/
public function getOneEntry(): ?array
{
$this->makeSureBadPracticeWasConfirmed();

return $this->queryHandler->fetchOne([
'fields' => $this->fields,
'repositories' => $this->repositories,
'query' => $this->query,
Expand All @@ -111,6 +123,31 @@ public function getEntries(): array
* @return string[]|int[]|float[]|bool[]|null[]
*/
public function getFlattenedFields(): array
{
$this->makeSureBadPracticeWasConfirmed();

return $this->queryHandler->fetchAll([
'fields' => $this->fields,
'repositories' => $this->repositories,
'query' => $this->query,
'parameters' => $this->parameters,
'flattenFields' => true,
]);
}

public function getIterator(): MultiSelectIterator
{
$this->makeSureBadPracticeWasConfirmed();

return new MultiSelectIterator($this->queryHandler, [
'fields' => $this->fields,
'repositories' => $this->repositories,
'query' => $this->query,
'parameters' => $this->parameters,
]);
}

private function makeSureBadPracticeWasConfirmed()
{
if ($this->confirmBadPractice !== true) {
throw DBDebug::createException(
Expand All @@ -119,12 +156,5 @@ public function getFlattenedFields(): array
'No confirmation that freeform queries are bad practice'
);
}

return $this->queryHandler->selectFlattenedFields([
'fields' => $this->fields,
'repositories' => $this->repositories,
'query' => $this->query,
'parameters' => $this->parameters,
]);
}
}
Loading

0 comments on commit 64d5354

Please sign in to comment.