Skip to content

Commit

Permalink
Update Database and Adapter find methods to only use queries
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed Aug 9, 2022
1 parent 97754fb commit e48bdaa
Show file tree
Hide file tree
Showing 7 changed files with 505 additions and 179 deletions.
24 changes: 15 additions & 9 deletions bin/tasks/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* @var CLI
*/ global $cli;

use Faker\Factory;
use MongoDB\Client;
use Utopia\Cache\Cache;
Expand All @@ -17,19 +18,21 @@
use Utopia\Database\Validator\Authorization;
use Utopia\Validator\Numeric;
use Utopia\Validator\Text;

$cli
->task('query')
->desc('Query mock data')
->param('adapter', '', new Text(0), 'Database adapter', false)
->param('name', '', new Text(0), 'Name of created database.', false)
->param('limit', 25, new Numeric(), 'Limit on queried documents', true)
->action(function ($adapter, $name, $limit) {
->action(function (string $adapter, string $name, int $limit) {
$database = null;

switch ($adapter) {
case 'mongodb':
$options = ["typeMap" => ['root' => 'array', 'document' => 'array', 'array' => 'array']];
$client = new Client('mongodb://mongo/',
$client = new Client(
'mongodb://mongo/',
[
'username' => 'root',
'password' => 'example',
Expand Down Expand Up @@ -130,7 +133,8 @@
fclose($f);
});

function runQueries($database, $limit) {
function runQueries(Database $database, int $limit)
{
$results = [];
// Recent travel blogs
$query = ["created.greater(1262322000)", "genre.equal('travel')"];
Expand All @@ -151,21 +155,23 @@ function runQueries($database, $limit) {
return $results;
}

function addRoles($faker, $count) {
for ($i=0; $i < $count; $i++) {
function addRoles($faker, $count)
{
for ($i = 0; $i < $count; $i++) {
Authorization::setRole($faker->numerify('user####'));
}
return count(Authorization::getRoles());
}

function runQuery($query, $database, $limit) {
Console::log('Running query: ['.implode(', ', $query).']');
$query = array_map(function($q) {
function runQuery(array $query, Database $database, int $limit)
{
Console::log('Running query: [' . implode(', ', $query) . ']');
$query = array_map(function ($q) {
return Query::parse($q);
}, $query);

$start = microtime(true);
$documents = $database->find('articles', $query, $limit);
$database->find('articles', array_merge($query, [Query::limit($limit)]));
$time = microtime(true) - $start;
Console::success("{$time} s");
return $time;
Expand Down
12 changes: 3 additions & 9 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,10 @@ abstract public function deleteDocument(string $collection, string $id): bool;
*
* @param string $collection
* @param \Utopia\Database\Query[] $queries
* @param int $limit
* @param int $offset
* @param array $orderAttributes
* @param array $orderTypes
* @param array $cursor Array copy of document used for before/after pagination
* @param string $cursorDirection
*
* @return Document[]
*/
abstract public function find(string $collection, array $queries = [], int $limit = 25, int $offset = 0, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER): array;
abstract public function find(string $collection, array $queries = []): array;

/**
* Sum an attribute
Expand Down Expand Up @@ -459,11 +453,11 @@ abstract public function getSupportForCasting(): bool;
* @throws Exception
* @return string
*/
public function filter(string $value):string
public function filter(string $value): string
{
$value = preg_replace("/[^A-Za-z0-9\_\-]/", '', $value);

if(\is_null($value)) {
if (\is_null($value)) {
throw new Exception('Failed to filter key');
}

Expand Down
55 changes: 46 additions & 9 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,23 +770,60 @@ public function deleteDocument(string $collection, string $id): bool
*
* @param string $collection
* @param Query[] $queries
* @param int $limit
* @param int $offset
* @param array $orderAttributes
* @param array $orderTypes
* @param array $cursor
* @param string $cursorDirection
* @return Document[]
* @throws Exception
* @throws PDOException
*/
public function find(string $collection, array $queries = [], int $limit = 25, int $offset = 0, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER): array
public function find(string $collection, array $queries = []): array
{
$name = $this->filter($collection);
$roles = Authorization::getRoles();
$where = ['1=1'];
$orders = [];

$filters = [];
$limit = 25;
$offset = 0;
$orderAttributes = [];
$orderTypes = [];
$cursor = null;
$cursorDirection = Database::CURSOR_AFTER;
foreach ($queries as $query) {
if (!$query instanceof Query) continue;

$method = $query->getMethod();
$attribute = $query->getAttribute();
$values = $query->getValues();
switch ($method) {
case Query::TYPE_ORDERASC:
case Query::TYPE_ORDERDESC:
if (!empty($attribute)) {
$orderAttributes[] = $attribute;
}

$orderTypes[] = $method == Query::TYPE_ORDERASC ? Database::ORDER_ASC : Database::ORDER_DESC;
break;

case Query::TYPE_LIMIT:
$limit = $values[0];
break;

case Query::TYPE_OFFSET:
$offset = $values[0];
break;

case Query::TYPE_CURSORAFTER:
case Query::TYPE_CURSORBEFORE:
$cursor = $values[0];
$cursorDirection = $method == Query::TYPE_CURSORAFTER ? Database::CURSOR_AFTER : Database::CURSOR_BEFORE;
break;

default:
$filters[] = $query;
break;
}
}

$orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) {
'$id' => '_uid',
'$createdAt' => '_createdAt',
Expand Down Expand Up @@ -852,7 +889,7 @@ public function find(string $collection, array $queries = [], int $limit = 25, i
}
}

foreach ($queries as $i => $query) {
foreach ($filters as $i => $query) {
$query->setAttribute(match ($query->getAttribute()) {
'$id' => '_uid',
'$createdAt' => '_createdAt',
Expand Down Expand Up @@ -887,7 +924,7 @@ public function find(string $collection, array $queries = [], int $limit = 25, i
LIMIT :offset, :limit;
");

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

0 comments on commit e48bdaa

Please sign in to comment.