1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Logikos\Csv\CsvToMysql; |
4
|
|
|
|
5
|
|
|
class LoadDataInFile { |
6
|
|
|
|
7
|
|
|
private $delimiter = ','; |
8
|
|
|
private $enclosure = '"'; |
9
|
|
|
private $eol = "\\n"; |
10
|
|
|
private $escape = '\\'; |
11
|
|
|
private $ignoreLines = 0; |
12
|
|
|
private $csvFile; |
13
|
|
|
private $tableName; |
14
|
|
|
|
15
|
|
|
public function getDelimiter() { return $this->delimiter; } |
16
|
|
|
public function setDelimiter($delimiter) { $this->delimiter = $delimiter; } |
17
|
|
|
|
18
|
|
|
public function getEnclosure() { return $this->enclosure; } |
19
|
|
|
public function setEnclosure($enclosure) { $this->enclosure = $enclosure; } |
20
|
|
|
|
21
|
|
|
public function getEOL() { return $this->eol; } |
22
|
|
|
public function setEOL($eol) { $this->eol = $eol; } |
23
|
|
|
|
24
|
|
|
public function getEscape() { return $this->escape; } |
25
|
|
|
public function setEscape($escape) { $this->escape = $escape; } |
26
|
|
|
|
27
|
|
|
public function getTableName() { return $this->tableName; } |
28
|
|
|
public function setTableName($name) { $this->tableName = $name; } |
29
|
|
|
|
30
|
|
|
public function getIgnoreLines() { return $this->ignoreLines; } |
31
|
|
|
public function setIgnoreLines($count) { $this->ignoreLines = $count; } |
32
|
|
|
|
33
|
|
|
public function getCsvFile() { return $this->csvFile; } |
34
|
3 |
|
public function setCsvFile($csvFile) { |
35
|
3 |
|
if (!is_writable($csvFile)) throw new Exception("Could not read file {$csvFile}"); |
36
|
2 |
|
$this->csvFile = $csvFile; |
37
|
2 |
|
} |
38
|
|
|
|
39
|
1 |
|
public function getQuery() { |
40
|
|
|
$sql = " |
41
|
1 |
|
LOAD DATA LOCAL INFILE {$this->getCsvFile()} |
42
|
1 |
|
INTO TABLE {$this->getTableName()} |
43
|
1 |
|
FIELDS TERMINATED BY '{$this->getDelimiter()}' |
44
|
1 |
|
OPTIONALLY ENCLOSED BY '{$this->getEnclosure()}' |
45
|
1 |
|
ESCAPED BY '{$this->escape($this->getEscape())}' |
46
|
1 |
|
LINES TERMINATED BY '{$this->getEOL()}' |
47
|
|
|
IGNORE 2 LINES |
48
|
|
|
"; |
49
|
1 |
|
return $sql; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
private function escape($string) { |
53
|
1 |
|
return addslashes($string); |
54
|
|
|
} |
55
|
|
|
} |