Skip to content

Commit

Permalink
Merge pull request #66 from singlestore-labs/ISSUE-61
Browse files Browse the repository at this point in the history
Added possibility to set query options
  • Loading branch information
AdalbertMemSQL authored Jul 31, 2023
2 parents b47dff3 + 98b5840 commit bee2c30
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,23 @@

class Builder extends BaseBuilder
{
public $options;

public function options(array $options)
{
$this->options = $options;

return $this;
}

public function toSql()
{
$sql = parent::toSql();

if (! empty($this->options)) {
$sql .= ' '.$this->grammar->compileOptions($this->options);
}

return $sql;
}
}
13 changes: 13 additions & 0 deletions src/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

class Grammar extends MySqlGrammar
{
public function compileOptions(array $options): string
{
$optionString = '';
foreach ($options as $key => $value) {
if (! empty($optionString)) {
$optionString .= ',';
}
$optionString .= $key.'='.$value;
}

return "OPTION ({$optionString})";
}

/**
* Compile a "where fulltext" clause.
*
Expand Down
59 changes: 59 additions & 0 deletions tests/Hybrid/OptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace SingleStore\Laravel\Tests\Hybrid;

use Illuminate\Support\Facades\DB;
use SingleStore\Laravel\Schema\Blueprint;
use SingleStore\Laravel\Tests\BaseTest;

class OptionTest extends BaseTest
{
use HybridTestHelpers;

protected function setUp(): void
{
parent::setUp();

if ($this->runHybridIntegrations()) {
$this->createTable(function (Blueprint $table) {
$table->id();
});
}
}

/** @test */
public function singleOption()
{
$query = DB::table('test')->options(['interpreter_mode' => 'compile']);
echo get_class($query);
$this->assertEquals('select * from `test` OPTION (interpreter_mode=compile)', $query->toSql());

if ($this->runHybridIntegrations()) {
$query->get();
}
}

/** @test */
public function emptyOption()
{
$query = DB::table('test')->options([]);
echo get_class($query);
$this->assertEquals('select * from `test`', $query->toSql());

if ($this->runHybridIntegrations()) {
$query->get();
}
}

/** @test */
public function multiOption()
{
$query = DB::table('test')->options(['interpreter_mode' => 'compile', 'resource_pool' => 'default_pool']);
echo get_class($query);
$this->assertEquals('select * from `test` OPTION (interpreter_mode=compile,resource_pool=default_pool)', $query->toSql());

if ($this->runHybridIntegrations()) {
$query->get();
}
}
}

0 comments on commit bee2c30

Please sign in to comment.