diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 3415ed0..dd4efcb 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -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; + } } diff --git a/src/Query/Grammar.php b/src/Query/Grammar.php index ee209c0..118fbbf 100644 --- a/src/Query/Grammar.php +++ b/src/Query/Grammar.php @@ -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. * diff --git a/tests/Hybrid/OptionTest.php b/tests/Hybrid/OptionTest.php new file mode 100644 index 0000000..c655d4a --- /dev/null +++ b/tests/Hybrid/OptionTest.php @@ -0,0 +1,59 @@ +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(); + } + } +}