1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2011 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2023 |
7
|
|
|
* @package Base |
8
|
|
|
* @subpackage Common |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\Base\Criteria\Expression\Sort; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* SQL implementation for sorting objects. |
17
|
|
|
* |
18
|
|
|
* @package Base |
19
|
|
|
* @subpackage Common |
20
|
|
|
*/ |
21
|
|
|
class SQL extends Base |
22
|
|
|
{ |
23
|
|
|
private static $operators = array( '+' => 'ASC', '-' => 'DESC' ); |
24
|
|
|
private \Aimeos\Base\DB\Connection\Iface $conn; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initializes the object. |
29
|
|
|
* |
30
|
|
|
* @param \Aimeos\Base\DB\Connection\Iface $conn Database connection object |
31
|
|
|
* @param string $operator Sorting operator ("+": ascending, "-": descending) |
32
|
|
|
* @param string $name Name of the variable or column to sort |
33
|
|
|
*/ |
34
|
|
|
public function __construct( \Aimeos\Base\DB\Connection\Iface $conn, string $operator, string $name ) |
35
|
|
|
{ |
36
|
|
|
if( !isset( self::$operators[$operator] ) ) { |
37
|
|
|
throw new \Aimeos\Base\Exception( sprintf( 'Invalid operator "%1$s"', $operator ) ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
parent::__construct( $operator, $name ); |
41
|
|
|
$this->conn = $conn; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns the available operators for the expression. |
47
|
|
|
* |
48
|
|
|
* @return array List of available operators |
49
|
|
|
*/ |
50
|
|
|
public static function getOperators() : array |
51
|
|
|
{ |
52
|
|
|
return array_keys( self::$operators ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Generates a string from the expression objects. |
58
|
|
|
* |
59
|
|
|
* @param array $types Associative list of variable or column names as keys and their corresponding types |
60
|
|
|
* @param array $translations Associative list of variable or column names that should be translated |
61
|
|
|
* @param \Aimeos\Base\Criteria\Plugin\Iface[] $plugins Associative list of item names as keys and plugin objects as values |
62
|
|
|
* @param array $funcs Associative list of item names and functions modifying the conditions |
63
|
|
|
* @return mixed Expression that evaluates to a boolean result |
64
|
|
|
*/ |
65
|
|
|
public function toSource( array $types, array $translations = [], array $plugins = [], array $funcs = [] ) |
66
|
|
|
{ |
67
|
|
|
$this->setPlugins( $plugins ); |
68
|
|
|
|
69
|
|
|
$name = $this->getName(); |
70
|
|
|
$transname = $this->translateName( $name, $translations, $funcs ); |
71
|
|
|
|
72
|
|
|
if( !$transname ) { |
73
|
|
|
throw new \Aimeos\Base\Exception( sprintf( 'Invalid sorting "%1$s"', $this->getName() ) ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if( !isset( $types[$name] ) ) { |
77
|
|
|
throw new \Aimeos\Base\Exception( sprintf( 'Invalid name "%1$s"', $name ) ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $transname . ' ' . self::$operators[$this->getOperator()]; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the connection object. |
86
|
|
|
* |
87
|
|
|
* return \Aimeos\Base\DB\Connection\Iface Connection object |
88
|
|
|
*/ |
89
|
|
|
public function getConnection() : \Aimeos\Base\DB\Connection\Iface |
90
|
|
|
{ |
91
|
|
|
return $this->conn; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the internal type of the function parameter. |
97
|
|
|
* |
98
|
|
|
* @param mixed &$item Reference to parameter value (will be updated if necessary) |
99
|
|
|
* @return string Internal parameter type |
100
|
|
|
* @throws \Aimeos\Base\Exception If an error occurs |
101
|
|
|
*/ |
102
|
|
|
protected function getParamType( &$item ) : string |
103
|
|
|
{ |
104
|
|
|
if( is_null( $item ) ) { |
105
|
|
|
return \Aimeos\Base\DB\Statement\Base::PARAM_NULL; |
106
|
|
|
} elseif( is_float( $item ) ) { |
107
|
|
|
return \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT; |
108
|
|
|
} elseif( is_int( $item ) ) { |
109
|
|
|
return \Aimeos\Base\DB\Statement\Base::PARAM_INT; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return \Aimeos\Base\DB\Statement\Base::PARAM_STR; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|