Skip to content

Commit 2834bce

Browse files
Added possibility to set query options
1 parent 6f628cd commit 2834bce

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/Query/Builder.php

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

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

src/Query/Grammar.php

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

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

tests/Hybrid/OptionTest.php

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

0 commit comments

Comments
 (0)