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

LoadDataInFile   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 51
c 0
b 0
f 0
ccs 14
cts 14
cp 1
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getDelimiter() 0 1 1
A setDelimiter() 0 1 1
A getEnclosure() 0 1 1
A setEnclosure() 0 1 1
A getEOL() 0 1 1
A setEOL() 0 1 1
A getEscape() 0 1 1
A setEscape() 0 1 1
A getTableName() 0 1 1
A setTableName() 0 1 1
A getIgnoreLines() 0 1 1
A setIgnoreLines() 0 1 1
A getCsvFile() 0 1 1
A setCsvFile() 0 4 2
A getQuery() 0 12 1
A escape() 0 3 1
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
}