Completed
Push — master ( d86ad0...519833 )
by Todd
04:01
created

LoadDataInFile::getIgnoreLines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
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
}