Skip to content

Commit bee2c30

Browse files
Merge pull request #66 from singlestore-labs/ISSUE-61
Added possibility to set query options
2 parents b47dff3 + 98b5840 commit bee2c30

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

src/Query/Builder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,23 @@
66

77
class Builder extends BaseBuilder
88
{
9+
public $options;
10+
11+
public function options(array $options)
12+
{
13+
$this->options = $options;
14+
15+
return $this;
16+
}
17+
18+
public function toSql()
19+
{
20+
$sql = parent::toSql();
21+
22+
if (! empty($this->options)) {
23+
$sql .= ' '.$this->grammar->compileOptions($this->options);
24+
}
25+
26+
return $sql;
27+
}
928
}

src/Query/Grammar.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77

88
class Grammar extends MySqlGrammar
99
{
10+
public function compileOptions(array $options): string
11+
{
12+
$optionString = '';
13+
foreach ($options as $key => $value) {
14+
if (! empty($optionString)) {
15+
$optionString .= ',';
16+
}
17+
$optionString .= $key.'='.$value;
18+
}
19+
20+
return "OPTION ({$optionString})";
21+
}
22+
1023
/**
1124
* Compile a "where fulltext" clause.
1225
*

tests/Hybrid/OptionTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace SingleStore\Laravel\Tests\Hybrid;
4+
5+
use Illuminate\Support\Facades\DB;
6+
use SingleStore\Laravel\Schema\Blueprint;
7+
use SingleStore\Laravel\Tests\BaseTest;
8+
9+
class OptionTest extends BaseTest
10+
{
11+
use HybridTestHelpers;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
if ($this->runHybridIntegrations()) {
18+
$this->createTable(function (Blueprint $table) {
19+
$table->id();
20+
});
21+
}
22+
}
23+
24+
/** @test */
25+
public function singleOption()
26+
{
27+
$query = DB::table('test')->options(['interpreter_mode' => 'compile']);
28+
echo get_class($query);
29+
$this->assertEquals('select * from `test` OPTION (interpreter_mode=compile)', $query->toSql());
30+
31+
if ($this->runHybridIntegrations()) {
32+
$query->get();
33+
}
34+
}
35+
36+
/** @test */
37+
public function emptyOption()
38+
{
39+
$query = DB::table('test')->options([]);
40+
echo get_class($query);
41+
$this->assertEquals('select * from `test`', $query->toSql());
42+
43+
if ($this->runHybridIntegrations()) {
44+
$query->get();
45+
}
46+
}
47+
48+
/** @test */
49+
public function multiOption()
50+
{
51+
$query = DB::table('test')->options(['interpreter_mode' => 'compile', 'resource_pool' => 'default_pool']);
52+
echo get_class($query);
53+
$this->assertEquals('select * from `test` OPTION (interpreter_mode=compile,resource_pool=default_pool)', $query->toSql());
54+
55+
if ($this->runHybridIntegrations()) {
56+
$query->get();
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)