Skip to content

Commit 06a0cb1

Browse files
authored
Merge pull request #3 from ellgreen/replace-and-ignore
Added replace and ignore support
2 parents b634d15 + aea9e44 commit 06a0cb1

File tree

6 files changed

+123
-9
lines changed

6 files changed

+123
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ LoadFile::file('/path/to/employees.csv', $local = true)
4141
LoadFile::file('/path/to/employees.csv', $local = true)
4242
->into('employees')
4343
->columns(['forename', 'surname', 'employee_id'])
44-
->ignore(1)
44+
->ignoreLines(1)
4545
->load();
4646
```
4747

src/Builder.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class Builder
1414
public ?string $file = null;
1515
public ?string $table = null;
1616
public bool $local = false;
17+
18+
public bool $replace = false;
19+
public bool $ignore = false;
20+
1721
public ?string $charset = null;
1822

1923
public ?string $fieldsTerminatedBy = null;
@@ -75,6 +79,28 @@ public function local(bool $local): self
7579
return $this;
7680
}
7781

82+
public function replace(bool $replace = true): self
83+
{
84+
$this->replace = $replace;
85+
86+
if ($replace) {
87+
$this->ignore = false;
88+
}
89+
90+
return $this;
91+
}
92+
93+
public function ignore(bool $ignore = true): self
94+
{
95+
$this->ignore = $ignore;
96+
97+
if ($ignore) {
98+
$this->replace = false;
99+
}
100+
101+
return $this;
102+
}
103+
78104
public function charset(?string $charset): self
79105
{
80106
$this->charset = $charset;
@@ -143,9 +169,9 @@ public function lines(string $startingBy, string $terminatedBy): self
143169
return $this;
144170
}
145171

146-
public function ignore(int $lines): self
172+
public function ignoreLines(int $count): self
147173
{
148-
$this->ignoreLines = $lines;
174+
$this->ignoreLines = $count;
149175
return $this;
150176
}
151177

src/Grammar.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@ public function compileLoadFile(Builder $query): array
1212
$querySegments = collect();
1313

1414
$querySegments->push(
15-
'load data ' . ($query->local ? 'local' : '') . ' infile ' . $this->quoteString($query->file),
16-
'into table ' . $this->wrapTable($query->table),
15+
'load data' . ($query->local ? ' local' : ''),
16+
'infile ' . $this->quoteString($query->file),
1717
);
1818

19+
if ($query->replace) {
20+
$querySegments->push('replace');
21+
}
22+
23+
if ($query->ignore) {
24+
$querySegments->push('ignore');
25+
}
26+
27+
$querySegments->push('into table ' . $this->wrapTable($query->table));
28+
1929
if (isset($query->charset)) {
2030
$querySegments->push('character set ' . $this->quoteString($query->charset));
2131
}

tests/Feature/LoadFileTest.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testIgnoreRow()
6161
{
6262
LoadFile::file(realpath(__DIR__ . '/../data/people.csv'), true)
6363
->into('people')
64-
->ignore(1)
64+
->ignoreLines(1)
6565
->columns([DB::raw('@forename'), DB::raw('@surname'), 'dob'])
6666
->set([
6767
'greeting' => 'Hello',
@@ -83,4 +83,48 @@ public function testIgnoreRow()
8383
'greeting' => 'Hello',
8484
]);
8585
}
86+
87+
public function testReplace()
88+
{
89+
LoadFile::file(realpath(__DIR__ . '/../data/people-simple.csv'), true)
90+
->replace()
91+
->into('people')
92+
->columns(['name', 'dob', 'greeting'])
93+
->fields(',', '"', '\\\\', true)
94+
->load();
95+
96+
$this->assertDatabaseHas('people', [
97+
'name' => 'John Doe',
98+
'dob' => '1980-01-01',
99+
'greeting' => 'Bonjour',
100+
]);
101+
102+
$this->assertDatabaseHas('people', [
103+
'name' => 'Jane Doe',
104+
'dob' => '1975-06-30',
105+
'greeting' => 'Hello',
106+
]);
107+
}
108+
109+
public function testIgnore()
110+
{
111+
LoadFile::file(realpath(__DIR__ . '/../data/people-simple.csv'), true)
112+
->ignore()
113+
->into('people')
114+
->columns(['name', 'dob', 'greeting'])
115+
->fields(',', '"', '\\\\', true)
116+
->load();
117+
118+
$this->assertDatabaseHas('people', [
119+
'name' => 'John Doe',
120+
'dob' => '1980-01-01',
121+
'greeting' => 'Bonjour',
122+
]);
123+
124+
$this->assertDatabaseHas('people', [
125+
'name' => 'Jane Doe',
126+
'dob' => '1975-06-30',
127+
'greeting' => 'Hello',
128+
]);
129+
}
86130
}

tests/Unit/BuilderTest.php

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

56+
public function testSetReplace()
57+
{
58+
$this->builder->replace();
59+
60+
$this->assertTrue($this->builder->replace);
61+
}
62+
63+
public function testSetIgnore()
64+
{
65+
$this->builder->ignore();
66+
67+
$this->assertTrue($this->builder->ignore);
68+
}
69+
5670
public function testSetCharset()
5771
{
5872
$this->builder->charset($charset = 'utf8mb4');
@@ -114,9 +128,9 @@ public function testSetLines()
114128
$this->assertSame('\\n', $this->builder->linesTerminatedBy);
115129
}
116130

117-
public function testSetIgnore()
131+
public function testSetIgnoreLines()
118132
{
119-
$this->builder->ignore(1);
133+
$this->builder->ignoreLines(1);
120134

121135
$this->assertSame(1, $this->builder->ignoreLines);
122136
}

tests/Unit/GrammarTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,29 @@ public function testSimpleCompile()
3737
SQL);
3838
}
3939

40+
public function testReplace()
41+
{
42+
$this->builder->replace();
43+
44+
$this->assertSqlAndBindings(<<<'SQL'
45+
load data local infile '/path/to/employees.csv' replace
46+
into table `employees` (`forename`, `surname`, `employee_id`)
47+
SQL);
48+
}
49+
50+
public function testIgnore()
51+
{
52+
$this->builder->ignore();
53+
54+
$this->assertSqlAndBindings(<<<'SQL'
55+
load data local infile '/path/to/employees.csv' ignore
56+
into table `employees` (`forename`, `surname`, `employee_id`)
57+
SQL);
58+
}
59+
4060
public function testIgnoreLines()
4161
{
42-
$this->builder->ignore(1);
62+
$this->builder->ignoreLines(1);
4363

4464
$this->assertSqlAndBindings(<<<'SQL'
4565
load data local infile '/path/to/employees.csv'

0 commit comments

Comments
 (0)