diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 7f8301f30..006145e3a 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -994,21 +994,26 @@ class DataMapperAbstract implements DataMapperInterface /** * Get random object * + * @param int $amount Amount of random models * @param int $relations Relations type * - * @return array + * @return mixed * * @since 1.0.0 * @author Dennis Eichhorn */ - public static function getRandom(int $relations = RelationType::ALL) + public static function getRandom(int $amount = 1, int $relations = RelationType::ALL) { $query = new Builder(self::$db); $query->prefix(self::$db->getPrefix()) ->random(static::$primaryKey) - ->from(static::$table); + ->from(static::$table) + ->limit($amount); - return self::get(self::$db->con->prepare($query->toSql())->execute(), $relations); + $sth = self::$db->con->prepare($query->toSql()); + $sth->execute(); + + return self::get($sth->fetchAll(), $relations); } /** diff --git a/DataStorage/Database/Query/Grammar/MicrosoftGrammar.php b/DataStorage/Database/Query/Grammar/MicrosoftGrammar.php index 10e2f7fff..1e7ecc829 100644 --- a/DataStorage/Database/Query/Grammar/MicrosoftGrammar.php +++ b/DataStorage/Database/Query/Grammar/MicrosoftGrammar.php @@ -48,6 +48,8 @@ class MicrosoftGrammar extends Grammar $expression = '*'; } - return 'SELECT TOP 1 ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY IDX FETCH FIRST 1 ROWS ONLY'; + $query->limit = $query->limit ?? 1; + + return 'SELECT TOP 1 ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY IDX FETCH FIRST ' . $query->limit . ' ROWS ONLY'; } } diff --git a/DataStorage/Database/Query/Grammar/MysqlGrammar.php b/DataStorage/Database/Query/Grammar/MysqlGrammar.php index 5bf7d89d9..30f7622cc 100644 --- a/DataStorage/Database/Query/Grammar/MysqlGrammar.php +++ b/DataStorage/Database/Query/Grammar/MysqlGrammar.php @@ -57,6 +57,6 @@ class MysqlGrammar extends Grammar $expression = '*'; } - return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RAND() LIMIT 1'; + return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RAND() ' . $this->compileLimit($query, $query->limit); } } diff --git a/DataStorage/Database/Query/Grammar/OracleGrammar.php b/DataStorage/Database/Query/Grammar/OracleGrammar.php index a424ae033..58d8e0b8a 100644 --- a/DataStorage/Database/Query/Grammar/OracleGrammar.php +++ b/DataStorage/Database/Query/Grammar/OracleGrammar.php @@ -48,6 +48,8 @@ class OracleGrammar extends Grammar $expression = '*'; } - return 'SELECT ' . $expression . ' FROM (SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY dbms_random.value) WHERE rownum = 1'; + $query->limit = $query->limit ?? 1; + + return 'SELECT ' . $expression . ' FROM (SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY dbms_random.value) WHERE rownum >= 1 AND rownum <= ' . $query->limit; } } diff --git a/DataStorage/Database/Query/Grammar/PostgresGrammar.php b/DataStorage/Database/Query/Grammar/PostgresGrammar.php index 670441c64..fac4df664 100644 --- a/DataStorage/Database/Query/Grammar/PostgresGrammar.php +++ b/DataStorage/Database/Query/Grammar/PostgresGrammar.php @@ -48,6 +48,6 @@ class PostgresGrammar extends Grammar $expression = '*'; } - return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() LIMIT 1'; + return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() ' . $this->compileLimit($query, $query->limit); } } diff --git a/DataStorage/Database/Query/Grammar/SQLiteGrammar.php b/DataStorage/Database/Query/Grammar/SQLiteGrammar.php index 8e534caa7..f2bc8765c 100644 --- a/DataStorage/Database/Query/Grammar/SQLiteGrammar.php +++ b/DataStorage/Database/Query/Grammar/SQLiteGrammar.php @@ -57,6 +57,6 @@ class SqliteGrammar extends Grammar $expression = '*'; } - return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() LIMIT 1'; + return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() ' . $this->compileLimit($query, $query->limit); } }