-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDongle.php
180 lines (154 loc) · 4.11 KB
/
Dongle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php namespace October\Rain\Database;
use Exception;
/**
* Database driver dongle
*
* This class uses regex to convert MySQL to various other drivers.
*/
class Dongle
{
/**
* @var DB Database helper object
*/
protected $db;
/**
* @var string Driver to convert to: mysql, sqlite, pgsql, sqlsrv, postgis.
*/
protected $driver;
/**
* Constructor.
*/
public function __construct($driver = 'mysql', $db = null)
{
$this->db = $db;
$this->driver = $driver;
}
/**
* Helper method, softly checks if a database is present.
* @return boolean
*/
public function hasDatabase()
{
try {
$this->db->connection()->getDatabaseName();
}
catch (Exception $ex) {
return false;
}
return true;
}
/**
* Transforms and executes a raw SQL statement
* @param string $sql
* @return mixed
*/
public function raw($sql)
{
return $this->db->raw($this->parse($sql));
}
/**
* Transforms an SQL statement to match the active driver.
* @param string $sql
* @return string
*/
public function parse($sql)
{
$sql = $this->parseGroupConcat($sql);
$sql = $this->parseConcat($sql);
$sql = $this->parseIfNull($sql);
return $sql;
}
/**
* Transforms GROUP_CONCAT statement.
* @param string $sql
* @return string
*/
public function parseGroupConcat($sql)
{
$result = preg_replace_callback('/group_concat\(([^)]+)\)/i', function($matches){
if (!isset($matches[1]))
return $matches[0];
switch ($this->driver) {
default:
case 'mysql':
return $matches[0];
case 'pgsql':
case 'postgis':
case 'sqlite':
return str_ireplace(' separator ', ', ', $matches[0]);
}
}, $sql);
if ($this->driver == 'pgsql' || $this->driver == 'postgis') {
$result = preg_replace("/\\(([]a-zA-Z\\-\\_]+)\\,/i", "(CAST($1 AS VARCHAR),", $result);
$result = str_ireplace('group_concat(', 'string_agg(', $result);
}
return $result;
}
/**
* Transforms CONCAT statement.
* @param string $sql
* @return string
*/
public function parseConcat($sql)
{
return preg_replace_callback('/(?:group_)?concat\(([^)]+)\)(?R)/i', function($matches){
if (!isset($matches[1])) {
return $matches[0];
}
// This is a group_concat() so ignore it
if (strpos($matches[0], 'group_') === 0) {
return $matches[0];
}
$concatFields = array_map('trim', explode(',', $matches[1]));
switch ($this->driver) {
default:
case 'mysql':
return $matches[0];
case 'pgsql':
case 'postgis':
case 'sqlite':
return implode(' || ', $concatFields);
}
}, $sql);
}
/**
* Transforms IFNULL statement.
* @param string $sql
* @return string
*/
public function parseIfNull($sql)
{
if ($this->driver != 'pgsql' && $this->driver != 'postgis') {
return $sql;
}
return str_ireplace('ifnull(', 'coalesce(', $sql);
}
/**
* Some drivers require same-type comparisons.
* @param string $sql
* @return string
*/
public function cast($sql, $asType = 'INTEGER')
{
if ($this->driver != 'pgsql' && $this->driver != 'postgis') {
return $sql;
}
return 'CAST('.$sql.' AS '.$asType.')';
}
/**
* Returns the driver name as a string, eg: pgsql
* @return string
*/
public function getDriver()
{
return $this->driver;
}
/**
* Get the table prefix.
* @return string
*/
public function getTablePrefix()
{
return $this->db->getTablePrefix();
}
}