Completed
Push — master ( 433807...d10db1 )
by Todd
03:45
created

LoadDataInFile::hasColumnMap()   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
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
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
  private $columnMap;
15
  private $columnBinds = [];
16
17
  public function getDelimiter()           { return $this->delimiter;  }
18
  public function setDelimiter($delimiter) { $this->delimiter = $delimiter;  }
19
20
  public function getEnclosure()           { return $this->enclosure;  }
21
  public function setEnclosure($enclosure) { $this->enclosure = $enclosure;  }
22
23
  public function getEOL()                 { return $this->eol; }
24
  public function setEOL($eol)             { $this->eol = $eol; }
25
26
  public function getEscape()              { return $this->escape;  }
27
  public function setEscape($escape)       { $this->escape = $escape;  }
28
29
  public function getTableName()           { return $this->tableName;  }
30
  public function setTableName($name)      { $this->tableName = $name;  }
31
32
  public function getIgnoreLines()         { return $this->ignoreLines; }
33
  public function setIgnoreLines($count)   { $this->ignoreLines = $count; }
34
35
  public function getColMap()              { return $this->columnMap; }
36
  public function setColMap($columnMap)    { $this->columnMap = $columnMap; }
37
38
  public function getColumnBinds()         { return $this->columnBinds; }
39
  public function SetColumnBinds($binds)   { $this->columnBinds = $binds; }
40
41
  public function getCsvFile()             { return $this->csvFile; }
42
  public function setCsvFile($csvFile)     { $this->csvFile = $csvFile; }
43
44 1
  public function getQuery() {
45
    $sql  = "
46 1
      LOAD DATA LOCAL INFILE '{$this->getCsvFile()}'
47 1
      INTO TABLE {$this->getTableName()}
48 1
      FIELDS TERMINATED BY '{$this->getDelimiter()}'
49 1
      OPTIONALLY ENCLOSED BY '{$this->getEnclosure()}'
50 1
      ESCAPED BY '{$this->escape($this->getEscape())}'
51 1
      LINES TERMINATED BY '{$this->getEOL()}'
52 1
      IGNORE {$this->getIgnoreLines()} LINES
53 1
      ( {$this->getColVarsExpression()} )
54 1
      SET {$this->getSetExpression()}
55
    ";
56 1
    return $sql;
57
  }
58
59 1
  public function getBinds() {
60 1
    $binds = [];
61 1
    foreach ($this->columnBinds as $k=>$v) $binds[":{$k}"] = $v;
62 1
    return $binds;
63
  }
64
65 2
  public function getColVars() {
66 2
    $vars = [];
67 2
    for ($i=1, $count=count($this->columnMap); $i<=$count; $i++)
68 2
      array_push($vars, "@col{$i}");
69 2
    return $vars;
70
  }
71
72 4
  public function getSetExpression() {
73 4
    $expressions = [];
74 4
    if ($this->hasColumnMap())   array_push($expressions, $this->getColMapExpression());
75 4
    if ($this->hasColumnBinds()) array_push($expressions, $this->getBoundSetExpression());
76 4
    return $this->arrayToStringList($expressions);
77
  }
78
79
  protected function hasColumnMap()   { return !empty($this->columnMap); }
80
  protected function hasColumnBinds() { return !empty($this->columnBinds); }
81
82 3
  protected function getColMapExpression() {
83 3
    $pieces = [];
84 3
    $colNum = 1;
85 3
    foreach ($this->columnMap as $colName) {
86 3
      if (!is_null($colName))
87 3
        array_push($pieces, "{$colName} = @col{$colNum}");
88 3
      $colNum++;
89
    }
90 3
    return implode(",\n", $pieces);
91
  }
92
93 1
  protected function getColVarsExpression() {
94 1
    return $this->arrayToStringList($this->getColVars());
95
  }
96
97 3
  protected function getBoundSetExpression() {
98 3
    $binds = [];
99 3
    foreach ($this->columnBinds as $k=>$v) {
100 3
      array_push($binds, "{$k} = :{$k}");
101
    }
102 3
    return $this->arrayToStringList($binds);
103
  }
104
105 4
  protected function arrayToStringList(array $items) {
106 4
    return implode(",\n", $items);
107
  }
108
109
  private function escape($string)      { return addslashes($string); }
110
}
111