Skip to content

Commit a432f8a

Browse files
authored
Merge pull request #2 from ellgreen/charset
Added charset to builder
2 parents 2334fa3 + 776e980 commit a432f8a

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

src/Builder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Builder
1414
public ?string $file = null;
1515
public ?string $table = null;
1616
public bool $local = false;
17+
public ?string $charset = null;
1718

1819
public ?string $fieldsTerminatedBy = null;
1920
public ?string $fieldsEnclosedBy = null;
@@ -74,6 +75,12 @@ public function local(bool $local): self
7475
return $this;
7576
}
7677

78+
public function charset(?string $charset): self
79+
{
80+
$this->charset = $charset;
81+
return $this;
82+
}
83+
7784
public function fieldsTerminatedBy(string $terminatedBy): self
7885
{
7986
$this->fieldsTerminatedBy = $terminatedBy;

src/Grammar.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public function compileLoadFile(Builder $query): array
1616
'into table ' . $this->wrapTable($query->table),
1717
);
1818

19+
if (isset($query->charset)) {
20+
$querySegments->push('character set ' . $this->quoteString($query->charset));
21+
}
22+
1923
$querySegments->push($this->compileFields(
2024
$query->fieldsTerminatedBy,
2125
$query->fieldsEnclosedBy,

tests/Feature/LoadFileTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public function testSimpleLoad()
1212
LoadFile::connection('mysql')
1313
->file(realpath(__DIR__ . '/../data/people-simple.csv'), true)
1414
->into('people')
15+
->charset('utf8mb4')
1516
->columns(['name', 'dob', 'greeting'])
1617
->fields(',', '"', '\\\\', true)
1718
->lines('', '\\n')

tests/Unit/BuilderTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public function testSetLocal()
5353
$this->assertTrue($this->builder->local);
5454
}
5555

56+
public function testSetCharset()
57+
{
58+
$this->builder->charset($charset = 'utf8mb4');
59+
60+
$this->assertSame($charset, $this->builder->charset);
61+
}
62+
5663
public function testSetFieldsTerminatedBy()
5764
{
5865
$this->builder->fieldsTerminatedBy(',');

0 commit comments

Comments
 (0)