Passed
Push — master ( f2cade...8ecc02 )
by Mauro
01:55
created

MySqlQueryBuilder::getQueriesBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the DbImporter package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace DbImporter\QueryBuilder;
12
13
class MySqlQueryBuilder extends AbstractQueryBuilder
14
{
15
    const MULTIPLE_QUERY_IMPORT_LIMIT = 4000;
16
17
    /**
18
     * @return string
19
     */
20
    private function getQueryHead()
21
    {
22
        $sql = 'INSERT ';
23
24
        if (true === $this->debug) {
25
            $sql .= 'IGNORE ';
26
        }
27
28
        $sql .= 'INTO `'.$this->table.'` (';
29
        $c = 1;
30
        $values = array_keys($this->mapping);
31
32
        foreach ($values as $value) {
33
            $sql .= '`'.$value.'`';
34
            $sql .= $this->appendComma($c, $values);
35
            $c++;
36
        }
37
38
        $sql .= ') VALUES ';
39
40
        return $sql;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    private function getQueryTail()
47
    {
48
        $sql = ' ON DUPLICATE KEY UPDATE ';
49
        $c = 1;
50
        $values = array_keys($this->mapping);
51
52
        foreach ($values as $value) {
53
            $sql .= '`'.$value.'`=VALUES('.$value.')';
54
            $sql .= $this->appendComma($c, $values);
55
            $c++;
56
        }
57
58
        return $sql;
59
    }
60
61
    /**
62
     * Returns the array of insert queries
63
     * @param string $mode
64
     *
65
     * @return array
66
     */
67
    public function getQueries($mode = 'multiple')
68
    {
69
        $sql = [];
70
71
        foreach ($this->getQueriesBody($mode) as $query) {
72
            $sql[] = $this->getQueryHead().$query.$this->getQueryTail();
73
        }
74
75
        return $sql;
76
    }
77
78
79
}
80