1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Db\Dialect; |
12
|
|
|
|
13
|
|
|
use Phalcon\Db\Column; |
14
|
|
|
use Phalcon\Db\ColumnInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class MySQL |
18
|
|
|
* |
19
|
|
|
* Mysql class extends \Phalcon\Db\Dialect\Mysql to provide additional functionalities for MySQL database dialect. |
20
|
|
|
* - Regexp: " %s REGEXP $s" |
21
|
|
|
* - Distance: " ST_Distance_Sphere(%s, %s) " |
22
|
|
|
* - point: " point(%s, %s) " |
23
|
|
|
*/ |
24
|
|
|
class Mysql extends \Phalcon\Db\Dialect\Mysql |
25
|
|
|
{ |
26
|
2 |
|
public function __construct() |
27
|
|
|
{ |
28
|
2 |
|
$this->registerRegexpFunction(); |
29
|
2 |
|
$this->registerDistanceSphereFunction(); |
30
|
2 |
|
$this->registerPointFunction(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Register a custom REGEXP function for the database dialect. |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
2 |
|
public function registerRegexpFunction(): void |
39
|
|
|
{ |
40
|
2 |
|
$this->registerCustomFunction('regexp', function ($dialect, $expression) { |
41
|
|
|
$arguments = $expression['arguments']; |
42
|
|
|
return sprintf( |
43
|
|
|
" %s REGEXP %s", |
44
|
|
|
$dialect->getSqlExpression($arguments[0]), |
45
|
|
|
$dialect->getSqlExpression($arguments[1]) |
46
|
|
|
); |
47
|
2 |
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Register a custom distance sphere function to be used in SQL queries. |
52
|
|
|
* |
53
|
|
|
* This method registers the "ST_Distance_Sphere" function, which calculates the spherical distance between two points. |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
2 |
|
public function registerDistanceSphereFunction(): void |
58
|
|
|
{ |
59
|
2 |
|
$this->registerCustomFunction('ST_Distance_Sphere', function ($dialect, $expression) { |
60
|
|
|
$arguments = $expression['arguments']; |
61
|
|
|
return sprintf( |
62
|
|
|
" ST_Distance_Sphere(%s, %s)", |
63
|
|
|
$dialect->getSqlExpression($arguments[0]), |
64
|
|
|
$dialect->getSqlExpression($arguments[1]), |
65
|
|
|
); |
66
|
2 |
|
}); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Register a point function for SQL dialect. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
2 |
|
public function registerPointFunction(): void |
75
|
|
|
{ |
76
|
2 |
|
$this->registerCustomFunction('point', function ($dialect, $expression) { |
77
|
|
|
$arguments = $expression['arguments']; |
78
|
|
|
return sprintf( |
79
|
|
|
" point(%s, %s)", |
80
|
|
|
$dialect->getSqlExpression($arguments[0]), |
81
|
|
|
$dialect->getSqlExpression($arguments[1]), |
82
|
|
|
); |
83
|
2 |
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the SQL column definition for a given column. |
88
|
|
|
* |
89
|
|
|
* This is a temporary fix in regard to this github issue: |
90
|
|
|
* - https://github.com/phalcon/cphalcon/issues/16532 |
91
|
|
|
* |
92
|
|
|
* @param ColumnInterface $column The column to get the definition for. |
93
|
|
|
* @return string The SQL column definition. |
94
|
|
|
*/ |
95
|
|
|
public function getColumnDefinition(ColumnInterface $column): string |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
|
|
return parent::getColumnDefinition($column); |
99
|
|
|
} |
100
|
|
|
catch (\Phalcon\Db\Exception $e) { |
101
|
|
|
|
102
|
|
|
$columnSql = $this->checkColumnTypeSql($column); |
103
|
|
|
$columnType = $this->checkColumnType($column); |
104
|
|
|
|
105
|
|
|
switch ($columnType) { |
106
|
|
|
|
107
|
|
|
case Column::TYPE_BINARY: |
108
|
|
|
if (empty($columnSql)) { |
109
|
|
|
$columnSql .= 'BINARY'; |
110
|
|
|
} |
111
|
|
|
if ($column->getSize() > 0) { |
112
|
|
|
$columnSql .= $this->getColumnSize($column); |
113
|
|
|
} |
114
|
|
|
break; |
115
|
|
|
|
116
|
|
|
case Column::TYPE_VARBINARY: |
117
|
|
|
if (empty($columnSql)) { |
118
|
|
|
$columnSql .= 'VARBINARY'; |
119
|
|
|
} |
120
|
|
|
if ($column->getSize() > 0) { |
121
|
|
|
$columnSql .= $this->getColumnSize($column); |
122
|
|
|
} |
123
|
|
|
break; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $columnSql; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|