mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-21 05:48:41 +00:00
Implementing random
This commit is contained in:
parent
8ca2f07108
commit
2b2808ef4e
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 <d.eichhorn@oms.com>
|
||||
*/
|
||||
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()
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -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.');
|
||||
}
|
||||
|
|
|
|||
53
DataStorage/Database/Query/Grammar/MicrosoftGrammar.php
Normal file
53
DataStorage/Database/Query/Grammar/MicrosoftGrammar.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.0
|
||||
*
|
||||
* @category TBD
|
||||
* @package TBD
|
||||
* @author OMS Development Team <dev@oms.com>
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
* @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 <dev@oms.com>
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
* @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 <d.eichhorn@oms.com>
|
||||
*/
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
|
@ -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 <d.eichhorn@oms.com>
|
||||
*/
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
53
DataStorage/Database/Query/Grammar/OracleGrammar.php
Normal file
53
DataStorage/Database/Query/Grammar/OracleGrammar.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.0
|
||||
*
|
||||
* @category TBD
|
||||
* @package TBD
|
||||
* @author OMS Development Team <dev@oms.com>
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
* @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 <dev@oms.com>
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
* @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 <d.eichhorn@oms.com>
|
||||
*/
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.0
|
||||
*
|
||||
* @category TBD
|
||||
* @package TBD
|
||||
* @author OMS Development Team <dev@oms.com>
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
* @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 <dev@oms.com>
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
* @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 <d.eichhorn@oms.com>
|
||||
*/
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
|
@ -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 <d.eichhorn@oms.com>
|
||||
*/
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,4 +34,5 @@ abstract class QueryType extends Enum
|
|||
const INSERT = 1;
|
||||
const UPDATE = 2;
|
||||
const DELETE = 3;
|
||||
const RANDOM = 4;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user