Skip to content

Commit

Permalink
Update Query class
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed Aug 9, 2022
1 parent 34e492b commit 97754fb
Show file tree
Hide file tree
Showing 11 changed files with 475 additions and 357 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static::getDatabase()->createDocument('movies', new Document([

```php
$documents = static::getDatabase()->find('movies', [
new Query(Query::TYPE_EQUAL, ['year', 2019]),
Query::equal('year', [2019]),
]);
```

Expand Down
52 changes: 25 additions & 27 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function createCollection(string $name, array $attributes = [], array $in
$indexAttributes = $index->getAttribute('attributes');
foreach ($indexAttributes as $nested => $attribute) {
$indexLength = $index->getAttribute('lengths')[$key] ?? '';
$indexLength = (empty($indexLength)) ? '' : '('.(int)$indexLength.')';
$indexLength = (empty($indexLength)) ? '' : '(' . (int)$indexLength . ')';
$indexOrder = $index->getAttribute('orders')[$key] ?? '';
$indexAttribute = $this->filter($attribute);

Expand Down Expand Up @@ -795,8 +795,8 @@ public function find(string $collection, array $queries = [], int $limit = 25, i
}, $orderAttributes);

$hasIdAttribute = false;
foreach($orderAttributes as $i => $attribute) {
if($attribute === '_uid') {
foreach ($orderAttributes as $i => $attribute) {
if ($attribute === '_uid') {
$hasIdAttribute = true;
}

Expand Down Expand Up @@ -839,30 +839,30 @@ public function find(string $collection, array $queries = [], int $limit = 25, i
}

// Allow order type without any order attribute, fallback to the natural order (_id)
if(!$hasIdAttribute) {
if (!$hasIdAttribute) {
if (empty($orderAttributes) && !empty($orderTypes)) {
$order = $orderTypes[0] ?? Database::ORDER_ASC;
if ($cursorDirection === Database::CURSOR_BEFORE) {
$order = $order === Database::ORDER_ASC ? Database::ORDER_DESC : Database::ORDER_ASC;
}

$orders[] = 'table_main._id ' . $this->filter($order);
} else {
$orders[] = 'table_main._id ' . ($cursorDirection === Database::CURSOR_AFTER ? Database::ORDER_ASC : Database::ORDER_DESC); // Enforce last ORDER by '_id'
}
}

foreach ($queries as $i => $query) {
$query->setFirstParam(match ($query->getFirstParam()) {
$query->setAttribute(match ($query->getAttribute()) {
'$id' => '_uid',
'$createdAt' => '_createdAt',
'$updatedAt' => '_updatedAt',
default => $query->getFirstParam()
default => $query->getAttribute()
});

$conditions = [];
foreach ($query->getArrayParam(1) as $key => $value) {
$conditions[] = $this->getSQLCondition('table_main.' . $query->getFirstParam(), $query->getMethod(), ':attribute_' . $i . '_' . $key . '_' . $query->getFirstParam(), $value);
foreach ($query->getValues() as $key => $value) {
$conditions[] = $this->getSQLCondition('table_main.' . $query->getAttribute(), $query->getMethod(), ':attribute_' . $i . '_' . $key . '_' . $query->getAttribute(), $value);
}
$condition = implode(' OR ', $conditions);
$where[] = empty($condition) ? '' : '(' . $condition . ')';
Expand All @@ -889,8 +889,8 @@ public function find(string $collection, array $queries = [], int $limit = 25, i

foreach ($queries as $i => $query) {
if ($query->getMethod() === Query::TYPE_SEARCH) continue;
foreach ($query->getArrayParam(1) as $key => $value) {
$stmt->bindValue(':attribute_' . $i . '_' . $key . '_' . $query->getFirstParam(), $value, $this->getPDOType($value));
foreach ($query->getValues() as $key => $value) {
$stmt->bindValue(':attribute_' . $i . '_' . $key . '_' . $query->getAttribute(), $value, $this->getPDOType($value));
}
}

Expand Down Expand Up @@ -959,16 +959,16 @@ public function count(string $collection, array $queries = [], int $max = 0): in
$limit = ($max === 0) ? '' : 'LIMIT :max';

foreach ($queries as $i => $query) {
$query->setFirstParam(match ($query->getFirstParam()) {
$query->setAttribute(match ($query->getAttribute()) {
'$id' => '_uid',
'$createdAt' => '_createdAt',
'$updatedAt' => '_updatedAt',
default => $query->getFirstParam()
default => $query->getAttribute()
});

$conditions = [];
foreach ($query->getArrayParam(1) as $key => $value) {
$conditions[] = $this->getSQLCondition('table_main.' . $query->getFirstParam(), $query->getMethod(), ':attribute_' . $i . '_' . $key . '_' . $query->getFirstParam(), $value);
foreach ($query->getValues() as $key => $value) {
$conditions[] = $this->getSQLCondition('table_main.' . $query->getAttribute(), $query->getMethod(), ':attribute_' . $i . '_' . $key . '_' . $query->getAttribute(), $value);
}

$condition = implode(' OR ', $conditions);
Expand All @@ -992,8 +992,8 @@ public function count(string $collection, array $queries = [], int $max = 0): in

foreach ($queries as $i => $query) {
if ($query->getMethod() === Query::TYPE_SEARCH) continue;
foreach ($query->getArrayParam(1) as $key => $value) {
$stmt->bindValue(':attribute_' . $i . '_' . $key . '_' . $query->getFirstParam(), $value, $this->getPDOType($value));
foreach ($query->getValues() as $key => $value) {
$stmt->bindValue(':attribute_' . $i . '_' . $key . '_' . $query->getAttribute(), $value, $this->getPDOType($value));
}
}

Expand Down Expand Up @@ -1028,16 +1028,16 @@ public function sum(string $collection, string $attribute, array $queries = [],
$limit = ($max === 0) ? '' : 'LIMIT :max';

foreach ($queries as $i => $query) {
$query->setFirstParam(match ($query->getFirstParam()) {
$query->setAttribute(match ($query->getAttribute()) {
'$id' => '_uid',
'$createdAt' => '_createdAt',
'$updatedAt' => '_updatedAt',
default => $query->getFirstParam()
default => $query->getAttribute()
});

$conditions = [];
foreach ($query->getArrayParam(1) as $key => $value) {
$conditions[] = $this->getSQLCondition('table_main.' . $query->getFirstParam(), $query->getMethod(), ':attribute_' . $i . '_' . $key . '_' . $query->getFirstParam(), $value);
foreach ($query->getValues() as $key => $value) {
$conditions[] = $this->getSQLCondition('table_main.' . $query->getAttribute(), $query->getMethod(), ':attribute_' . $i . '_' . $key . '_' . $query->getAttribute(), $value);
}

$where[] = implode(' OR ', $conditions);
Expand All @@ -1059,8 +1059,8 @@ public function sum(string $collection, string $attribute, array $queries = [],

foreach ($queries as $i => $query) {
if ($query->getMethod() === Query::TYPE_SEARCH) continue;
foreach ($query->getArrayParam(1) as $key => $value) {
$stmt->bindValue(':attribute_' . $i . '_' . $key . '_' . $query->getFirstParam(), $value, $this->getPDOType($value));
foreach ($query->getValues() as $key => $value) {
$stmt->bindValue(':attribute_' . $i . '_' . $key . '_' . $query->getAttribute(), $value, $this->getPDOType($value));
}
}

Expand Down Expand Up @@ -1488,7 +1488,7 @@ protected function getSQLIndex(string $collection, string $id, string $type, ar
break;
}

return "CREATE {$type} `{$id}` ON `{$this->getDefaultDatabase()}`.`{$this->getNamespace()}_{$collection}` ( ". implode(', ', $attributes) ." )";
return "CREATE {$type} `{$id}` ON `{$this->getDefaultDatabase()}`.`{$this->getNamespace()}_{$collection}` ( " . implode(', ', $attributes) . " )";
}

/**
Expand Down Expand Up @@ -1584,7 +1584,7 @@ protected function getPDO()
/**
* Returns default PDO configuration
*/
public static function getPdoAttributes():array
public static function getPdoAttributes(): array
{
return [
PDO::ATTR_TIMEOUT => 3, // Specifies the timeout duration in seconds. Takes a value of type int.
Expand All @@ -1595,6 +1595,4 @@ public static function getPdoAttributes():array
PDO::ATTR_STRINGIFY_FETCHES => true // Returns all fetched data as Strings
];
}


}
10 changes: 5 additions & 5 deletions src/Database/Adapter/Mongo/MongoDBAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -738,18 +738,18 @@ protected function buildFilters($queries): array
$filters = [];

foreach ($queries as $i => $query) {
if ($query->getFirstParam() === '$id') {
$query->setFirstParam('_uid');
if ($query->getAttribute() === '$id') {
$query->setAttribute('_uid');
}
$attribute = $query->getFirstParam();
$attribute = $query->getAttribute();
$operator = $this->getQueryOperator($query->getMethod());
$value = (count($query->getArrayParam(1)) > 1) ? $query->getArrayParam(1) : $query->getArrayParam(1)[0];
$value = (count($query->getValues()) > 1) ? $query->getValues() : $query->getValues()[0];

// TODO@kodumbeats Mongo recommends different methods depending on operator - implement the rest
if (is_array($value) && $operator === '$eq') {
$filters[$attribute]['$in'] = $value;
} elseif ($operator === '$in') {
$filters[$attribute]['$in'] = $query->getArrayParam(1);
$filters[$attribute]['$in'] = $query->getValues();
} elseif ($operator === '$search') {
// only one fulltext index per mongo collection, so attribute not necessary
$filters['$text'][$operator] = $value;
Expand Down
Loading

0 comments on commit 97754fb

Please sign in to comment.