-
Notifications
You must be signed in to change notification settings - Fork 55
Feat 5376 improve the select query #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
0777736
1ca3476
fedafbf
0278adf
cde877c
b93544f
3a3b51f
e39efe7
60bae57
c4c6e2a
f901305
e61eb13
0313f8c
5fd6373
27364ea
85afec2
4d20bc3
a2d25c4
e480ad8
56dedb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1335,6 +1335,26 @@ protected function getAttributeProjection(array $selections, string $prefix = '' | |
| $projection['_createdAt'] = 1; | ||
| $projection['_updatedAt'] = 1; | ||
|
Comment on lines
1335
to
1336
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to make sure these are not set by default the same as maraidb
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Except _collection and _id rest have to be set because.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make it so that they are not required at this point? I think we should be able to if we can do it for MariaDB
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For all adapters _uid and _permissions are a must. MariaDB/MySQL/SQLite: https://github.com/faisalill/database/blob/feat-5376-improve-the-select-query/src/Database/Adapter/MariaDB.php#L1226 Because of this:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For Postgres I am querying all the internal attributes and if any internal attribute is in the query I am removing it from selection then before the document is returned I check which internal attributes were in the query and only keep those internal attributes in the document. |
||
| $projection['_permissions'] = 1; | ||
| $projection['_collection'] = 1; | ||
|
|
||
| if (isset($projection['$id'])) { | ||
| unset($projection['$id']); | ||
| } | ||
| if (isset($projection['$internalId'])) { | ||
| unset($projection['$internalId']); | ||
| } | ||
| if (isset($projection['$permissions'])) { | ||
| unset($projection['$permissions']); | ||
| } | ||
| if (isset($projection['$createdAt'])) { | ||
| unset($projection['$createdAt']); | ||
| } | ||
| if (isset($projection['$updatedAt'])) { | ||
| unset($projection['$updatedAt']); | ||
| } | ||
| if (isset($projection['$collection'])) { | ||
| unset($projection['$collection']); | ||
| } | ||
|
|
||
| return $projection; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,6 +121,16 @@ class Database | |
| public const EVENT_INDEX_CREATE = 'index_create'; | ||
| public const EVENT_INDEX_DELETE = 'index_delete'; | ||
|
|
||
| // Internal Parameters | ||
| public const INTERNAL_PARAMETERS = [ | ||
|
faisalill marked this conversation as resolved.
Outdated
|
||
| '$id', | ||
| '$internalId', | ||
| '$createdAt', | ||
| '$updatedAt', | ||
| '$permissions', | ||
| '$collection' | ||
| ]; | ||
|
|
||
| protected Adapter $adapter; | ||
|
|
||
| protected Cache $cache; | ||
|
|
@@ -2286,6 +2296,26 @@ public function getDocument(string $collection, string $id, array $queries = []) | |
|
|
||
| $this->trigger(self::EVENT_DOCUMENT_READ, $document); | ||
|
|
||
| foreach ($queries as $query) { | ||
| if ($query->getMethod() == Query::TYPE_SELECT) { | ||
| $queriedValues = $query->getValues(); | ||
| $defaultKeys = Database::INTERNAL_PARAMETERS; | ||
|
|
||
| foreach ($queriedValues as $queriedValue) { | ||
| if (in_array($queriedValue, $defaultKeys)) { | ||
| $index = array_search($queriedValue, $defaultKeys); | ||
| unset($defaultKeys[$index]); | ||
| } | ||
| } | ||
|
|
||
| foreach ($defaultKeys as $defaultKey) { | ||
| if ($document->isSet($defaultKey)) { | ||
| $document->removeAttribute($defaultKey); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
faisalill marked this conversation as resolved.
|
||
| return $document; | ||
| } | ||
|
|
||
|
|
@@ -4001,6 +4031,18 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu | |
|
|
||
| $this->trigger(self::EVENT_DOCUMENT_FIND, $results); | ||
|
|
||
| // remove default keys $id and $permissions from the results | ||
|
|
||
| foreach ($queries as $query) { | ||
| if ($query->getMethod() === Query::TYPE_SELECT) { | ||
| foreach ($results as $result) { | ||
|
faisalill marked this conversation as resolved.
|
||
| foreach (Database::INTERNAL_PARAMETERS as $parameter) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will remove internal attributes even if they were selected, we need to make sure they are only removed when not selected Let's add a test to cover this case as well
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests here check whether only the queried internal attribute is returned. |
||
| $result->removeAttribute($parameter); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
faisalill marked this conversation as resolved.
|
||
| return $results; | ||
| } | ||
|
|
||
|
|
@@ -4335,8 +4377,12 @@ public function decode(Document $collection, Document $document, array $selectio | |
| } | ||
| } | ||
|
|
||
| if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) { | ||
| $document->setAttribute($key, ($array) ? $value : $value[0]); | ||
| if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections) || \in_array($key, ['$createdAt', '$updatedAt'])) { | ||
|
faisalill marked this conversation as resolved.
Outdated
|
||
| if (($key === '$createdAt' || $key === '$updatedAt') && $value[0] === null) { | ||
|
faisalill marked this conversation as resolved.
Outdated
|
||
| continue; | ||
| } else { | ||
| $document->setAttribute($key, ($array) ? $value : $value[0]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -4494,6 +4540,8 @@ private function validateSelections(Document $collection, array $queries): array | |
| } | ||
| } | ||
|
|
||
| $keys = \array_merge($keys, Database::INTERNAL_PARAMETERS); | ||
|
|
||
| $invalid = \array_diff($selections, $keys); | ||
| if (!empty($invalid) && !\in_array('*', $invalid)) { | ||
| throw new DatabaseException('Cannot select attributes: ' . \implode(', ', $invalid)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1016,7 +1016,9 @@ public function testGetDocument(Document $document): Document | |
| */ | ||
| public function testGetDocumentSelect(Document $document): Document | ||
| { | ||
| $document = static::getDatabase()->getDocument('documents', $document->getId(), [ | ||
| $documentId = $document->getId(); | ||
|
|
||
| $document = static::getDatabase()->getDocument('documents', $documentId, [ | ||
| Query::select(['string', 'integer']), | ||
| ]); | ||
|
|
||
|
|
@@ -1029,6 +1031,23 @@ public function testGetDocumentSelect(Document $document): Document | |
| $this->assertArrayNotHasKey('boolean', $document->getAttributes()); | ||
| $this->assertArrayNotHasKey('colors', $document->getAttributes()); | ||
| $this->assertArrayNotHasKey('with-dash', $document->getAttributes()); | ||
| $this->assertArrayNotHasKey('$id', $document); | ||
| $this->assertArrayNotHasKey('$internalId', $document); | ||
| $this->assertArrayNotHasKey('$collection', $document); | ||
| $this->assertArrayNotHasKey('$permissions', $document); | ||
| $this->assertArrayNotHasKey('$createdAt', $document); | ||
| $this->assertArrayNotHasKey('$updatedAt', $document); | ||
|
|
||
| $document = static::getDatabase()->getDocument('documents', $documentId, [ | ||
| Query::select(['string', 'integer', '$id', '$internalId', '$permissions', '$createdAt', '$updatedAt', '$collection']), | ||
| ]); | ||
|
|
||
| $this->assertArrayHasKey('$id', $document); | ||
| $this->assertArrayHasKey('$internalId', $document); | ||
| $this->assertArrayHasKey('$permissions', $document); | ||
| $this->assertArrayHasKey('$createdAt', $document); | ||
| $this->assertArrayHasKey('$updatedAt', $document); | ||
| $this->assertArrayHasKey('$collection', $document); | ||
|
|
||
| return $document; | ||
| } | ||
|
|
@@ -2539,18 +2558,19 @@ public function testFindSelect(): void | |
| $documents = static::getDatabase()->find('movies', [ | ||
| Query::select(['name', 'year']) | ||
| ]); | ||
|
|
||
| foreach ($documents as $document) { | ||
| $this->assertArrayHasKey('$id', $document); | ||
| $this->assertArrayHasKey('$internalId', $document); | ||
| $this->assertArrayHasKey('$collection', $document); | ||
| $this->assertArrayHasKey('$createdAt', $document); | ||
| $this->assertArrayHasKey('$updatedAt', $document); | ||
| $this->assertArrayHasKey('$permissions', $document); | ||
| $this->assertArrayHasKey('name', $document); | ||
| $this->assertArrayHasKey('year', $document); | ||
| $this->assertArrayNotHasKey('director', $document); | ||
| $this->assertArrayNotHasKey('price', $document); | ||
| $this->assertArrayNotHasKey('active', $document); | ||
| $this->assertArrayNotHasKey('director', $document); | ||
| $this->assertArrayNotHasKey('$id', $document); | ||
| $this->assertArrayNotHasKey('$internalId', $document); | ||
| $this->assertArrayNotHasKey('$collection', $document); | ||
| $this->assertArrayNotHasKey('$createdAt', $document); | ||
| $this->assertArrayNotHasKey('$updatedAt', $document); | ||
| $this->assertArrayNotHasKey('$permissions', $document); | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a test for the |
||
|
|
||
|
|
@@ -4156,14 +4176,13 @@ public function testOneToOneOneWayRelationship(): void | |
| $this->assertArrayNotHasKey('area', $person->getAttribute('library')); | ||
|
|
||
| $person = static::getDatabase()->getDocument('person', 'person1', [ | ||
| Query::select(['*', 'library.name']) | ||
| Query::select(['*', 'library.name', '$id']) | ||
| ]); | ||
|
|
||
| $this->assertArrayHasKey('$id', $person); | ||
| $this->assertEquals('Library 1', $person->getAttribute('library')->getAttribute('name')); | ||
|
faisalill marked this conversation as resolved.
|
||
| $this->assertArrayNotHasKey('area', $person->getAttribute('library')); | ||
|
|
||
|
|
||
|
|
||
| $document = static::getDatabase()->getDocument('person', $person->getId(), [ | ||
| Query::select(['name']), | ||
| ]); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.