1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\Database\Drivers\MySQL; |
10
|
|
|
|
11
|
|
|
use Spiral\Database\Entities\QueryCompiler as AbstractCompiler; |
12
|
|
|
use Spiral\Database\Injections\ParameterInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* MySQL syntax specific compiler. |
16
|
|
|
*/ |
17
|
|
|
class MySQLCompiler extends AbstractCompiler |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function orderParameters( |
23
|
|
|
string $queryType, |
24
|
|
|
array $whereParameters = [], |
25
|
|
|
array $onParameters = [], |
26
|
|
|
array $havingParameters = [], |
27
|
|
|
array $columnIdentifiers = [] |
28
|
|
|
): array { |
29
|
|
|
if ($queryType == self::UPDATE_QUERY) { |
30
|
|
|
//Where statement has pretty specific order |
31
|
|
|
return array_merge($onParameters, $columnIdentifiers, $whereParameters); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return parent::orderParameters( |
35
|
|
|
$queryType, |
36
|
|
|
$whereParameters, |
37
|
|
|
$onParameters, |
38
|
|
|
$havingParameters, |
39
|
|
|
$columnIdentifiers |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
* |
46
|
|
|
* @link http://dev.mysql.com/doc/refman/5.0/en/select.html#id4651990 |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
protected function compileLimit(int $limit, int $offset): string |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
if (empty($limit) && empty($offset)) { |
51
|
|
|
return ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$statement = ''; |
55
|
|
|
if (!empty($limit) || !empty($offset)) { |
56
|
|
|
//When limit is not provided but offset does we can replace limit value with PHP_INT_MAX |
57
|
|
|
$statement = 'LIMIT ' . ($limit ?: '18446744073709551615') . ' '; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!empty($offset)) { |
61
|
|
|
$statement .= "OFFSET {$offset}"; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return trim($statement); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Resolve operator value based on value value. ;). |
69
|
|
|
* |
70
|
|
|
* @param mixed $parameter |
71
|
|
|
* @param string $operator |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function prepareOperator($parameter, string $operator): string |
76
|
|
|
{ |
77
|
|
|
if (!$parameter instanceof ParameterInterface) { |
78
|
|
|
//Probably fragment |
79
|
|
|
return $operator; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($parameter->getType() == \PDO::PARAM_NULL) { |
83
|
|
|
switch ($operator) { |
84
|
|
|
case '=': |
85
|
|
|
return 'IS'; |
86
|
|
|
case '!=': |
87
|
|
|
return 'IS NOT'; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return parent::prepareOperator($parameter, $operator); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.