diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index ec634948e..7f8301f30 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -1005,9 +1005,8 @@ class DataMapperAbstract implements DataMapperInterface { $query = new Builder(self::$db); $query->prefix(self::$db->getPrefix()) - ->select(static::$primaryKey) - ->from(static::$table) - ->random(); + ->random(static::$primaryKey) + ->from(static::$table); return self::get(self::$db->con->prepare($query->toSql())->execute(), $relations); } diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index 3d7363b1f..c47b0383f 100644 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -160,6 +160,8 @@ class Builder extends BuilderAbstract protected $unionOrders = []; + public $random = false; + /** * Comparison operators. * @@ -239,6 +241,27 @@ class Builder extends BuilderAbstract return $this; } + /** + * Select. + * + * @param array $columns Columns + * + * @return Builder + * + * @todo Closure is not working this way, needs to be evaluated befor assigning + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function random(...$columns) : Builder + { + $this->select(...$columns); + + $this->type = QueryType::RANDOM; + + return $this; + } + /** * Bind parameter. * @@ -784,6 +807,13 @@ class Builder extends BuilderAbstract return $this; } + public function random() + { + $this->random = true; + + return $this; + } + public function on() { diff --git a/DataStorage/Database/Query/Grammar/Grammar.php b/DataStorage/Database/Query/Grammar/Grammar.php index f709e0850..7a0fcfa1e 100644 --- a/DataStorage/Database/Query/Grammar/Grammar.php +++ b/DataStorage/Database/Query/Grammar/Grammar.php @@ -80,6 +80,16 @@ class Grammar extends GrammarAbstract 'wheres', ]; + /** + * Random components. + * + * @var string[] + * @since 1.0.0 + */ + protected $randomComponents = [ + 'random' + ]; + /** * Compile components. * @@ -109,6 +119,9 @@ class Grammar extends GrammarAbstract case QueryType::DELETE: $components = []; break; + case QueryType::RANDOM: + $components = $this->selectComponents; + break; default: throw new \InvalidArgumentException('Unknown query type.'); } diff --git a/DataStorage/Database/Query/Grammar/MicrosoftGrammar.php b/DataStorage/Database/Query/Grammar/MicrosoftGrammar.php new file mode 100644 index 000000000..10e2f7fff --- /dev/null +++ b/DataStorage/Database/Query/Grammar/MicrosoftGrammar.php @@ -0,0 +1,53 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Query\Grammar; + +/** + * Grammar class. + * + * @category Framework + * @package phpOMS\DataStorage\Database\Query\Grammar + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class MicrosoftGrammar extends Grammar +{ + /** + * Compile random. + * + * @param Builder $query Builder + * @param array $columns Columns + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileRandom(Builder $query, array $columns) : string + { + $expression = $this->expressionizeTableColumn($columns, $query->getPrefix()); + + if ($expression === '') { + $expression = '*'; + } + + return 'SELECT TOP 1 ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY IDX FETCH FIRST 1 ROWS ONLY'; + } +} diff --git a/DataStorage/Database/Query/Grammar/MysqlGrammar.php b/DataStorage/Database/Query/Grammar/MysqlGrammar.php index d87c869d0..5bf7d89d9 100644 --- a/DataStorage/Database/Query/Grammar/MysqlGrammar.php +++ b/DataStorage/Database/Query/Grammar/MysqlGrammar.php @@ -37,4 +37,26 @@ class MysqlGrammar extends Grammar * @since 1.0.0 */ protected $systemIdentifier = '`'; + + /** + * Compile random. + * + * @param Builder $query Builder + * @param array $columns Columns + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileRandom(Builder $query, array $columns) : string + { + $expression = $this->expressionizeTableColumn($columns, $query->getPrefix()); + + if ($expression === '') { + $expression = '*'; + } + + return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RAND() LIMIT 1'; + } } diff --git a/DataStorage/Database/Query/Grammar/OracleGrammar.php b/DataStorage/Database/Query/Grammar/OracleGrammar.php new file mode 100644 index 000000000..a424ae033 --- /dev/null +++ b/DataStorage/Database/Query/Grammar/OracleGrammar.php @@ -0,0 +1,53 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Query\Grammar; + +/** + * Grammar class. + * + * @category Framework + * @package phpOMS\DataStorage\Database\Query\Grammar + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class OracleGrammar extends Grammar +{ + /** + * Compile random. + * + * @param Builder $query Builder + * @param array $columns Columns + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileRandom(Builder $query, array $columns) : string + { + $expression = $this->expressionizeTableColumn($columns, $query->getPrefix()); + + if ($expression === '') { + $expression = '*'; + } + + return 'SELECT ' . $expression . ' FROM (SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY dbms_random.value) WHERE rownum = 1'; + } +} diff --git a/DataStorage/Database/Query/Grammar/PostgresGrammar.php b/DataStorage/Database/Query/Grammar/PostgresGrammar.php index e69de29bb..670441c64 100644 --- a/DataStorage/Database/Query/Grammar/PostgresGrammar.php +++ b/DataStorage/Database/Query/Grammar/PostgresGrammar.php @@ -0,0 +1,53 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Query\Grammar; + +/** + * Grammar class. + * + * @category Framework + * @package phpOMS\DataStorage\Database\Query\Grammar + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class PostgresGrammar extends Grammar +{ + /** + * Compile random. + * + * @param Builder $query Builder + * @param array $columns Columns + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileRandom(Builder $query, array $columns) : string + { + $expression = $this->expressionizeTableColumn($columns, $query->getPrefix()); + + if ($expression === '') { + $expression = '*'; + } + + return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() LIMIT 1'; + } +} diff --git a/DataStorage/Database/Query/Grammar/SQLiteGrammar.php b/DataStorage/Database/Query/Grammar/SQLiteGrammar.php index 9bc62563d..8e534caa7 100644 --- a/DataStorage/Database/Query/Grammar/SQLiteGrammar.php +++ b/DataStorage/Database/Query/Grammar/SQLiteGrammar.php @@ -37,4 +37,26 @@ class SqliteGrammar extends Grammar * @since 1.0.0 */ public $systemIdentifier = '`'; + + /** + * Compile random. + * + * @param Builder $query Builder + * @param array $columns Columns + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileRandom(Builder $query, array $columns) : string + { + $expression = $this->expressionizeTableColumn($columns, $query->getPrefix()); + + if ($expression === '') { + $expression = '*'; + } + + return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() LIMIT 1'; + } } diff --git a/DataStorage/Database/Query/Grammar/SqlServerGrammar.php b/DataStorage/Database/Query/Grammar/SqlServerGrammar.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/DataStorage/Database/Query/QueryType.php b/DataStorage/Database/Query/QueryType.php index 89ea4a162..db5e364f0 100644 --- a/DataStorage/Database/Query/QueryType.php +++ b/DataStorage/Database/Query/QueryType.php @@ -34,4 +34,5 @@ abstract class QueryType extends Enum const INSERT = 1; const UPDATE = 2; const DELETE = 3; + const RANDOM = 4; }