Skip to content

Commit b1e8af5

Browse files
check poin
1 parent 578ae05 commit b1e8af5

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/Query/Grammar.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,40 @@
44

55
use Illuminate\Database\Query\Builder;
66
use Illuminate\Database\Query\Grammars\MySqlGrammar;
7+
use Illuminate\Support\Facades\Log;
8+
use Exception;
79

810
class Grammar extends MySqlGrammar
911
{
1012
public function compileOptions(array $options): string
1113
{
1214
$optionString = '';
1315
foreach ($options as $key => $value) {
14-
if (! empty($optionString)) {
16+
if (!empty($optionString)) {
1517
$optionString .= ',';
1618
}
17-
$optionString .= $key.'='.$value;
19+
$optionString .= $key . '=' . $value;
1820
}
1921

2022
return "OPTION ({$optionString})";
2123
}
2224

25+
public function compileDelete(Builder $query)
26+
{
27+
if (isset($query->orders)) {
28+
if ($query->connection->getConfig('ignore_order_by_in_deletes')) {
29+
if (env('APP_ENV') !== 'production') {
30+
Log::warning('SingleStore does not support "order by" in a delete statement. The "order by" clause will be ignored.');
31+
}
32+
$query->orders = [];
33+
} else {
34+
throw new Exception('SingleStore does not support "order by" in a delete statement. Use "ignore_order_by_in_deletes" configuration to ignore orderBy in delete operations.');
35+
}
36+
}
37+
38+
return parent::compileDelete($query);
39+
}
40+
2341
/**
2442
* Compile a "where fulltext" clause.
2543
*
@@ -81,7 +99,7 @@ protected function whereNull(Builder $query, $where)
8199
if ($this->isJsonSelector($where['column'])) {
82100
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
83101

84-
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) = \'NULL\')';
102+
return '(JSON_EXTRACT_JSON(' . $field . $path . ') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON(' . $field . $path . ')) = \'NULL\')';
85103
}
86104

87105
return parent::whereNull($query, $where);
@@ -92,7 +110,7 @@ protected function whereNotNull(Builder $query, $where)
92110
if ($this->isJsonSelector($where['column'])) {
93111
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
94112

95-
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) != \'NULL\')';
113+
return '(JSON_EXTRACT_JSON(' . $field . $path . ') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON(' . $field . $path . ')) != \'NULL\')';
96114
}
97115

98116
return parent::whereNotNull($query, $where);
@@ -103,7 +121,7 @@ protected function wrapJsonSelector($value)
103121
// Break apart the column name from the JSON keypath.
104122
[$field, $path] = $this->wrapJsonFieldAndPath($value);
105123

106-
if (! $path) {
124+
if (!$path) {
107125
return $field;
108126
}
109127

@@ -144,7 +162,7 @@ protected function wrapJsonFieldAndPath($column)
144162
return "'$part'";
145163
}, $parts);
146164

147-
$path = count($parts) ? ', '.implode(', ', $parts) : '';
165+
$path = count($parts) ? ', ' . implode(', ', $parts) : '';
148166

149167
return [$field, $path];
150168
}
@@ -157,7 +175,7 @@ protected function wrapJsonFieldAndPath($column)
157175
*/
158176
protected function wrapUnion($sql)
159177
{
160-
return 'SELECT * FROM ('.$sql.')';
178+
return 'SELECT * FROM (' . $sql . ')';
161179
}
162180

163181
/**
@@ -185,7 +203,7 @@ protected function compileUnion(array $union)
185203
{
186204
$conjunction = $union['all'] ? ' union all ' : ' union ';
187205

188-
return $conjunction.'('.$union['query']->toSql().')';
206+
return $conjunction . '(' . $union['query']->toSql() . ')';
189207
}
190208

191209
/**
@@ -203,19 +221,19 @@ public function compileSelect(Builder $query)
203221
return ltrim($sql);
204222
}
205223

206-
if (! empty($query->unionOrders) || isset($query->unionLimit) || isset($query->unionOffset)) {
207-
$sql = 'SELECT * FROM ('.$sql.') ';
224+
if (!empty($query->unionOrders) || isset($query->unionLimit) || isset($query->unionOffset)) {
225+
$sql = 'SELECT * FROM (' . $sql . ') ';
208226

209-
if (! empty($query->unionOrders)) {
210-
$sql .= ' '.$this->compileOrders($query, $query->unionOrders);
227+
if (!empty($query->unionOrders)) {
228+
$sql .= ' ' . $this->compileOrders($query, $query->unionOrders);
211229
}
212230

213231
if (isset($query->unionLimit)) {
214-
$sql .= ' '.$this->compileLimit($query, $query->unionLimit);
232+
$sql .= ' ' . $this->compileLimit($query, $query->unionLimit);
215233
}
216234

217235
if (isset($query->unionOffset)) {
218-
$sql .= ' '.$this->compileUnionOffset($query, $query->unionOffset);
236+
$sql .= ' ' . $this->compileUnionOffset($query, $query->unionOffset);
219237
}
220238
}
221239

@@ -247,11 +265,11 @@ private function compileOffsetWithLimit($offset, $limit): string
247265
{
248266
// OFFSET is not valid without LIMIT
249267
// Add a huge LIMIT clause
250-
if (! isset($limit)) {
268+
if (!isset($limit)) {
251269
// 9223372036854775807 - max 64-bit integer
252-
return ' LIMIT 9223372036854775807 OFFSET '.(int) $offset;
270+
return ' LIMIT 9223372036854775807 OFFSET ' . (int) $offset;
253271
}
254272

255-
return ' OFFSET '.(int) $offset;
273+
return ' OFFSET ' . (int) $offset;
256274
}
257275
}

tests/BaseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function getEnvironmentSetUp($app)
3333
// configuration possible, making for the ideal developer experience.
3434
$app['config']->set('database.default', 'mysql');
3535
$app['config']->set('database.connections.mysql.driver', 'singlestore');
36-
$app['config']->set('database.connections.mysql.options.'.PDO::ATTR_EMULATE_PREPARES, true);
36+
$app['config']->set('database.connections.mysql.options.' . PDO::ATTR_EMULATE_PREPARES, true);
37+
$app['config']->set('database.connections.mysql.ignore_order_by_in_deletes', true);
3738
}
3839
}

0 commit comments

Comments
 (0)