Issues (50)

src/Criteria/SQL.php (1 issue)

Labels
Severity
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-2025
7
 * @package Base
8
 * @subpackage Common
9
 */
10
11
12
namespace Aimeos\Base\Criteria;
13
14
15
/**
16
 * SQL search class
17
 *
18
 * @package Base
19
 * @subpackage Common
20
 */
21
class SQL extends \Aimeos\Base\Criteria\Base
22
{
23
	private ?\Aimeos\Base\Criteria\Expression\Iface $conditions = null;
24
	private \Aimeos\Base\DB\Connection\Iface $conn;
25
	private array $sortations = [];
26
	private int $sliceStart = 0;
27
	private int $sliceSize = 100;
28
29
30
	/**
31
	 * Initializes the SQL search object
32
	 *
33
	 * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection object
34
	 */
35
	public function __construct( \Aimeos\Base\DB\Connection\Iface $conn )
36
	{
37
		$this->conn = $conn;
38
	}
39
40
41
	/**
42
	 * Creates a new combine expression.
43
	 *
44
	 * Available composition operators are:
45
	 * "&&": term1 AND term2
46
	 * "||": term1 OR term2
47
	 * "!": NOT term
48
	 *
49
	 * @param string $operator One of the known operators
50
	 * @param \Aimeos\Base\Criteria\Expression\Compare\Iface[] $list List of expression objects
51
	 * @return \Aimeos\Base\Criteria\Expression\Combine\Iface Combine expression object
52
	 */
53
	public function combine( string $operator, array $list ) : \Aimeos\Base\Criteria\Expression\Combine\Iface
54
	{
55
		return new \Aimeos\Base\Criteria\Expression\Combine\SQL( $operator, $list );
56
	}
57
58
59
	/**
60
	 * Creates a new compare expression.
61
	 *
62
	 * Available comparision operators are:
63
	 * "==": item EQUAL value
64
	 * "!=": item NOT EQUAL value
65
	 * "~=": item LIKE value
66
	 * "=~": item STARTS WITH value
67
	 * ">=": item GREATER OR EQUAL value
68
	 * "<=": item SMALLER OR EQUAL value
69
	 * ">": item GREATER value
70
	 * "<": item SMALLER value
71
	 *
72
	 * @param string $operator One of the known operators
73
	 * @param string $name Name of the variable or column that should be used for comparison
74
	 * @param mixed $value Value the variable or column should be compared to
75
	 * @return \Aimeos\Base\Criteria\Expression\Compare\Iface Compare expression object
76
	 */
77
	public function compare( string $operator, string $name, $value ) : \Aimeos\Base\Criteria\Expression\Compare\Iface
78
	{
79
		return new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, $operator, $name, $value );
80
	}
81
82
83
	/**
84
	 * Creates a new sort expression.
85
	 *
86
	 * Available sorting operators are:
87
	 * "+": sort ascending
88
	 * "-": sort descending
89
	 *
90
	 * @param string $operator One of the known operators
91
	 * @param string $name Name of the variable or column that should be used for sorting
92
	 * @return \Aimeos\Base\Criteria\Expression\Sort\Iface Sort expression object
93
	 */
94
	public function sort( string $operator, string $name ) : \Aimeos\Base\Criteria\Expression\Sort\Iface
95
	{
96
		return new \Aimeos\Base\Criteria\Expression\Sort\SQL( $this->conn, $operator, $name );
97
	}
98
99
100
	/**
101
	 * Returns the available compare, combine and sort operators.
102
	 *
103
	 * @return array Associative list of lists (compare, combine, sort) containing the available operators
104
	 */
105
	public function getOperators() : array
106
	{
107
		return array(
108
			'combine' => \Aimeos\Base\Criteria\Expression\Combine\SQL::getOperators(),
109
			'compare' => \Aimeos\Base\Criteria\Expression\Compare\SQL::getOperators(),
110
			'sort' => \Aimeos\Base\Criteria\Expression\Sort\SQL::getOperators(),
111
		);
112
	}
113
114
115
	/**
116
	 * Returns the expression string.
117
	 *
118
	 * @param array $types Associative list of item names and their types
119
	 * @param array $translations Associative list of item names that should be translated
120
	 * @param \Aimeos\Base\Criteria\Plugin\Iface[] $plugins Associative list of item names as keys and plugin objects as values
121
	 * @param array $funcs Associative list of item names and functions modifying the conditions
122
	 * @return mixed Data for searching
123
	 */
124
	public function getConditionSource( array $types, array $translations = [], array $plugins = [], array $funcs = [] )
125
	{
126
		$types['1'] = \Aimeos\Base\DB\Statement\Base::PARAM_INT;
127
128
		if( $this->conditions && ( $string = $this->conditions->toSource( $types, $translations, $plugins, $funcs ) ) !== '' ) {
129
			return $string;
130
		}
131
132
		return '1 = 1';
133
	}
134
135
136
	/**
137
	 * Returns the original condition expression objects.
138
	 *
139
	 * @return \Aimeos\Base\Criteria\Expression\Iface|null Original expression objects
140
	 */
141
	public function getConditions() : ?\Aimeos\Base\Criteria\Expression\Iface
142
	{
143
		return $this->conditions;
144
	}
145
146
147
	/**
148
	 * Sets the expression objects.
149
	 *
150
	 * @param \Aimeos\Base\Criteria\Expression\Iface $conditions Expression object
151
	 * @return \Aimeos\Base\Criteria\Iface Object instance for fluent interface
152
	 */
153
	public function setConditions( \Aimeos\Base\Criteria\Expression\Iface $conditions ) : Iface
154
	{
155
		if( $conditions instanceof \Aimeos\Base\Criteria\Expression\Sort\Iface ) {
156
			throw new \Aimeos\Base\Exception( 'Sortation objects are not allowed' );
157
		}
158
159
		$this->conditions = $conditions;
160
		return $this;
161
	}
162
163
164
	/**
165
	 * Returns the string for sorting the result
166
	 *
167
	 * @param array $types Associative list of variable or column names as keys and their corresponding types
168
	 * @param array $translations Associative list of item names that should be translated
169
	 * @param array $funcs Associative list of item names and functions modifying the conditions
170
	 * @return mixed Data for sorting the items
171
	 */
172
	public function getSortationSource( array $types, array $translations = [], array $funcs = [] )
173
	{
174
		if( empty( $this->sortations ) )
175
		{
176
			reset( $types );
177
178
			if( ( $name = key( $types ) ) === false ) {
179
				throw new \Aimeos\Base\Exception( 'No sortation types available' );
180
			}
181
182
			return $this->sort( '+', $name )->toSource( $types, $translations, [], $funcs );
183
		}
184
185
186
		$sortation = [];
187
188
		foreach( $this->sortations as $sortitem )
189
		{
190
			if( ( $string = $sortitem->toSource( $types, $translations, [], $funcs ) ) !== '' ) {
191
				$sortation[] = $string;
192
			}
193
		}
194
195
		return implode( ', ', $sortation );
196
	}
197
198
199
	/**
200
	 * Returns the original sorting array for ordering the results.
201
	 *
202
	 * @return array Original sortation list (array of objects)
203
	 */
204
	public function getSortations() : array
205
	{
206
		return $this->sortations;
207
	}
208
209
210
	/**
211
	 * Stores the sortation objects for sorting the result.
212
	 *
213
	 * @param \Aimeos\Base\Criteria\Expression\Sort\SQL[] $sortations List of objects implementing \Aimeos\Base\Criteria\Expression\Sort\Iface
214
	 * @return \Aimeos\Base\Criteria\Iface Object instance for fluent interface
215
	 */
216
	public function setSortations( array $sortations ) : Iface
217
	{
218
		self::implements( \Aimeos\Base\Criteria\Expression\Sort\Iface::class, $sortations );
219
220
		$this->sortations = $sortations;
221
		return $this;
222
	}
223
224
225
	/**
226
	 * Returns the number of requested items.
227
	 *
228
	 * @return int Number of items
229
	 */
230
	public function getLimit() : int
231
	{
232
		return $this->sliceSize;
233
	}
234
235
236
	/**
237
	 * Returns the start number of requested items.
238
	 *
239
	 * @return int Start number of the items
240
	 */
241
	public function getOffset() : int
242
	{
243
		return $this->sliceStart;
244
	}
245
246
247
	/**
248
	 * Sets the start number and the size of the requested data slice.
249
	 *
250
	 * @param int $start Start number of the items
251
	 * @param int $size Number of items
252
	 * @return \Aimeos\Base\Criteria\PHP Object instance for fluent interface
0 ignored issues
show
The type Aimeos\Base\Criteria\PHP was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
253
	 */
254
	public function slice( int $offset, int $limit = 100 ) : \Aimeos\Base\Criteria\Iface
255
	{
256
		$this->sliceStart = $offset;
257
		$this->sliceSize = $limit;
258
259
		return $this;
260
	}
261
262
263
	/**
264
	 * Returns the connection object.
265
	 *
266
	 * return \Aimeos\Base\DB\Connection\Iface Connection object
267
	 */
268
	public function getConnection() : \Aimeos\Base\DB\Connection\Iface
269
	{
270
		return $this->conn;
271
	}
272
}
273