Skip to content

Commit 8f83fbd

Browse files
committed
getAttributeProjection
1 parent 9e13d50 commit 8f83fbd

3 files changed

Lines changed: 57 additions & 116 deletions

File tree

src/Database/Adapter/MariaDB.php

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,15 +1724,6 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
17241724

17251725
$queries = array_map(fn ($query) => clone $query, $queries);
17261726

1727-
// $orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) {
1728-
// '$id' => '_uid',
1729-
// '$internalId' => '_id',
1730-
// '$tenant' => '_tenant',
1731-
// '$createdAt' => '_createdAt',
1732-
// '$updatedAt' => '_updatedAt',
1733-
// default => $orderAttribute
1734-
// }, $orderAttributes);
1735-
17361727
$hasIdAttribute = false;
17371728
foreach ($orderAttributes as $i => $attribute) {
17381729
$originalAttribute = $attribute;
@@ -1763,11 +1754,11 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
17631754
$binds[':cursor'] = $cursor[$originalAttribute];
17641755

17651756
$where[] = "(
1766-
{$this->quote($defaultAlias)}.{$this->quote($attribute)} {$this->getSQLOperator($orderMethod)} :cursor
1757+
{$defaultAlias}.{$this->quote($attribute)} {$this->getSQLOperator($orderMethod)} :cursor
17671758
OR (
1768-
{$this->quote($defaultAlias)}.{$this->quote($attribute)} = :cursor
1759+
{$defaultAlias}.{$this->quote($attribute)} = :cursor
17691760
AND
1770-
{$this->quote($defaultAlias)}._id {$this->getSQLOperator($orderMethodInternalId)} {$cursor['$internalId']}
1761+
{$defaultAlias}._id {$this->getSQLOperator($orderMethodInternalId)} {$cursor['$internalId']}
17711762
)
17721763
)";
17731764
} elseif ($cursorDirection === Database::CURSOR_BEFORE) {
@@ -1791,7 +1782,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
17911782
: Query::TYPE_LESSER;
17921783
}
17931784

1794-
$where[] = "({$this->quote($defaultAlias)}.{$this->quote('_id')} {$this->getSQLOperator($orderMethod)} {$cursor['$internalId']})";
1785+
$where[] = "({$defaultAlias}._id {$this->getSQLOperator($orderMethod)} {$cursor['$internalId']})";
17951786
}
17961787

17971788
// Allow order type without any order attribute, fallback to the natural order (_id)
@@ -1802,9 +1793,9 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
18021793
$order = $order === Database::ORDER_ASC ? Database::ORDER_DESC : Database::ORDER_ASC;
18031794
}
18041795

1805-
$orders[] = "{$this->quote($defaultAlias)}.{$this->quote('_id')} ".$this->filter($order);
1796+
$orders[] = "{$defaultAlias}._id ".$this->filter($order);
18061797
} else {
1807-
$orders[] = "{$this->quote($defaultAlias)}.{$this->quote('_id')} " . ($cursorDirection === Database::CURSOR_AFTER ? Database::ORDER_ASC : Database::ORDER_DESC); // Enforce last ORDER by '_id'
1798+
$orders[] = "$defaultAlias._id " . ($cursorDirection === Database::CURSOR_AFTER ? Database::ORDER_ASC : Database::ORDER_DESC); // Enforce last ORDER by '_id'
18081799
}
18091800
}
18101801

@@ -2018,8 +2009,8 @@ public function sum(string $collection, string $attribute, array $queries = [],
20182009
: '';
20192010

20202011
$sql = "
2021-
SELECT SUM({$attribute}) as sum FROM (
2022-
SELECT {$attribute}
2012+
SELECT SUM({$this->quote($attribute)}) as sum FROM (
2013+
SELECT {$this->quote($attribute)}
20232014
FROM {$this->getSQLTable($name)} AS {$defaultAlias}
20242015
{$sqlWhere}
20252016
{$limit}
@@ -2045,55 +2036,6 @@ public function sum(string $collection, string $attribute, array $queries = [],
20452036
return $result['sum'] ?? 0;
20462037
}
20472038

2048-
/**
2049-
* Get the SQL projection given the selected attributes
2050-
*
2051-
* @param array<string> $selections
2052-
* @param string $prefix
2053-
* @return mixed
2054-
* @throws Exception
2055-
*/
2056-
protected function getAttributeProjection(array $selections, string $prefix = ''): mixed
2057-
{
2058-
if (empty($selections) || \in_array('*', $selections)) {
2059-
if (!empty($prefix)) {
2060-
return "`{$prefix}`.*";
2061-
}
2062-
return '*';
2063-
}
2064-
2065-
// Remove $id, $permissions and $collection if present since it is always selected by default
2066-
$selections = \array_diff($selections, ['$id', '$permissions', '$collection']);
2067-
2068-
$selections[] = '_uid';
2069-
$selections[] = '_permissions';
2070-
2071-
if (\in_array('$internalId', $selections)) {
2072-
$selections[] = '_id';
2073-
$selections = \array_diff($selections, ['$internalId']);
2074-
}
2075-
if (\in_array('$createdAt', $selections)) {
2076-
$selections[] = '_createdAt';
2077-
$selections = \array_diff($selections, ['$createdAt']);
2078-
}
2079-
if (\in_array('$updatedAt', $selections)) {
2080-
$selections[] = '_updatedAt';
2081-
$selections = \array_diff($selections, ['$updatedAt']);
2082-
}
2083-
2084-
if (!empty($prefix)) {
2085-
foreach ($selections as &$selection) {
2086-
$selection = "`{$prefix}`.`{$this->filter($selection)}`";
2087-
}
2088-
} else {
2089-
foreach ($selections as &$selection) {
2090-
$selection = "`{$this->filter($selection)}`";
2091-
}
2092-
}
2093-
2094-
return \implode(', ', $selections);
2095-
}
2096-
20972039
/**
20982040
* Get SQL Condition
20992041
*

src/Database/Adapter/Postgres.php

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,56 +1843,6 @@ public function sum(string $collection, string $attribute, array $queries = [],
18431843
return $result['sum'] ?? 0;
18441844
}
18451845

1846-
/**
1847-
* Get the SQL projection given the selected attributes
1848-
*
1849-
* @param string[] $selections
1850-
* @param string $prefix
1851-
* @return string
1852-
* @throws Exception
1853-
*/
1854-
protected function getAttributeProjection(array $selections, string $prefix = ''): string
1855-
{
1856-
if (empty($selections) || \in_array('*', $selections)) {
1857-
if (!empty($prefix)) {
1858-
return "\"{$prefix}\".*";
1859-
}
1860-
return '*';
1861-
}
1862-
1863-
// Remove $id ,$permissions and $collection from selections if present since they are always selected
1864-
$selections = \array_diff($selections, ['$id', '$permissions', '$collection']);
1865-
1866-
$selections[] = '_uid';
1867-
$selections[] = '_permissions';
1868-
1869-
if (\in_array('$internalId', $selections)) {
1870-
$selections[] = '_id';
1871-
$selections = \array_diff($selections, ['$internalId']);
1872-
}
1873-
if (\in_array('$createdAt', $selections)) {
1874-
$selections[] = '_createdAt';
1875-
$selections = \array_diff($selections, ['$createdAt']);
1876-
}
1877-
if (\in_array('$updatedAt', $selections)) {
1878-
$selections[] = '_updatedAt';
1879-
$selections = \array_diff($selections, ['$updatedAt']);
1880-
}
1881-
1882-
if (!empty($prefix)) {
1883-
foreach ($selections as &$selection) {
1884-
$selection = "\"{$prefix}\".\"{$this->filter($selection)}\"";
1885-
}
1886-
} else {
1887-
foreach ($selections as &$selection) {
1888-
$selection = "\"{$this->filter($selection)}\"";
1889-
}
1890-
}
1891-
1892-
return \implode(', ', $selections);
1893-
}
1894-
1895-
18961846
/**
18971847
* Get SQL Condition
18981848
*

src/Database/Adapter/SQL.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,55 @@ public function updateDocuments(string $collection, Document $updates, array $do
14841484
*/
14851485
abstract protected function quote(string $string): string;
14861486

1487+
/**
1488+
* Get the SQL projection given the selected attributes
1489+
*
1490+
* @param array<string> $selections
1491+
* @param string $prefix
1492+
* @return mixed
1493+
* @throws Exception
1494+
*/
1495+
protected function getAttributeProjection(array $selections, string $prefix = ''): mixed
1496+
{
1497+
if (empty($selections) || \in_array('*', $selections)) {
1498+
if (!empty($prefix)) {
1499+
return "{$this->quote($prefix)}.*";
1500+
}
1501+
return '*';
1502+
}
1503+
1504+
// Remove $id, $permissions and $collection if present since it is always selected by default
1505+
$selections = \array_diff($selections, ['$id', '$permissions', '$collection']);
1506+
1507+
$selections[] = '_uid';
1508+
$selections[] = '_permissions';
1509+
1510+
if (\in_array('$internalId', $selections)) {
1511+
$selections[] = '_id';
1512+
$selections = \array_diff($selections, ['$internalId']);
1513+
}
1514+
if (\in_array('$createdAt', $selections)) {
1515+
$selections[] = '_createdAt';
1516+
$selections = \array_diff($selections, ['$createdAt']);
1517+
}
1518+
if (\in_array('$updatedAt', $selections)) {
1519+
$selections[] = '_updatedAt';
1520+
$selections = \array_diff($selections, ['$updatedAt']);
1521+
}
1522+
1523+
if (!empty($prefix)) {
1524+
foreach ($selections as &$selection) {
1525+
$selection = "{$this->quote($prefix)}.{$this->quote($this->filter($selection))}";
1526+
}
1527+
} else {
1528+
foreach ($selections as &$selection) {
1529+
$selection = "{$this->quote($this->filter($selection))}";
1530+
}
1531+
}
1532+
1533+
return \implode(', ', $selections);
1534+
}
1535+
14871536
protected function getInternalKeyForAttribute(string $attribute): string
14881537
{
14891538
return match ($attribute) {

0 commit comments

Comments
 (0)