From 776e9809f0786f869a99592e996ded6fd54367c9 Mon Sep 17 00:00:00 2001 From: Ellis Green Date: Mon, 3 May 2021 19:24:13 +0100 Subject: [PATCH] Added charset to builder --- src/Builder.php | 7 +++++++ src/Grammar.php | 4 ++++ tests/Feature/LoadFileTest.php | 1 + tests/Unit/BuilderTest.php | 7 +++++++ 4 files changed, 19 insertions(+) diff --git a/src/Builder.php b/src/Builder.php index ba625fc..cede84e 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -14,6 +14,7 @@ class Builder public ?string $file = null; public ?string $table = null; public bool $local = false; + public ?string $charset = null; public ?string $fieldsTerminatedBy = null; public ?string $fieldsEnclosedBy = null; @@ -74,6 +75,12 @@ public function local(bool $local): self return $this; } + public function charset(?string $charset): self + { + $this->charset = $charset; + return $this; + } + public function fieldsTerminatedBy(string $terminatedBy): self { $this->fieldsTerminatedBy = $terminatedBy; diff --git a/src/Grammar.php b/src/Grammar.php index 0658b3d..990d069 100644 --- a/src/Grammar.php +++ b/src/Grammar.php @@ -16,6 +16,10 @@ public function compileLoadFile(Builder $query): array 'into table ' . $this->wrapTable($query->table), ); + if (isset($query->charset)) { + $querySegments->push('character set ' . $this->quoteString($query->charset)); + } + $querySegments->push($this->compileFields( $query->fieldsTerminatedBy, $query->fieldsEnclosedBy, diff --git a/tests/Feature/LoadFileTest.php b/tests/Feature/LoadFileTest.php index 36eeb63..3cde6ea 100644 --- a/tests/Feature/LoadFileTest.php +++ b/tests/Feature/LoadFileTest.php @@ -12,6 +12,7 @@ public function testSimpleLoad() LoadFile::connection('mysql') ->file(realpath(__DIR__ . '/../data/people-simple.csv'), true) ->into('people') + ->charset('utf8mb4') ->columns(['name', 'dob', 'greeting']) ->fields(',', '"', '\\\\', true) ->lines('', '\\n') diff --git a/tests/Unit/BuilderTest.php b/tests/Unit/BuilderTest.php index fe489fa..ade4047 100644 --- a/tests/Unit/BuilderTest.php +++ b/tests/Unit/BuilderTest.php @@ -53,6 +53,13 @@ public function testSetLocal() $this->assertTrue($this->builder->local); } + public function testSetCharset() + { + $this->builder->charset($charset = 'utf8mb4'); + + $this->assertSame($charset, $this->builder->charset); + } + public function testSetFieldsTerminatedBy() { $this->builder->fieldsTerminatedBy(',');