Passed
Push — master ( bbf0da...682cd1 )
by Emmanuel
02:36
created

SQLServer::getEnclosureForCSV()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author Emmanuel Dyan
8
 * @copyright 2018 Emmanuel Dyan
9
 *
10
 * @package edyan/neuralyzer
11
 *
12
 * @license GNU General Public License v2.0
13
 *
14
 * @link https://github.com/edyan/neuralyzer
15
 */
16
17
namespace Edyan\Neuralyzer\Helper\DB;
18
19
/**
20
 * Various methods related to SQLServer
21
 */
22
class SQLServer extends AbstractDBHelper
23
{
24
    /**
25
     * Set the right enclosure
26
     * @return string
27
     */
28
    public function getEnclosureForCSV(): string
29
    {
30
        return chr(0);
31
    }
32
33
34
    /**
35
     * Load Data from a CSV
36
     * @param  string  $table
37
     * @param  string  $filename
38
     * @param  array   $fields
39
     * @param  string  $mode  Not in used here
40
     * @return string
41
     */
42
    public function loadData(
43
        string $table,
44
        string $filename,
45
        array $fields,
46
        string $mode
47
    ): string {
48
        if (substr(gethostbyname($this->conn->getHost()), 0, 3) !== '127') {
49
            throw new NeuralizerException('SQL Server must be on the same host than PHP');
50
        }
51
52
        $sql ="BULK INSERT {$table} FROM '{$filename}' WITH (
53
            FIELDTERMINATOR = '|', DATAFILETYPE = 'widechar', ROWTERMINATOR = '" . PHP_EOL . "'
54
        )";
55
56
        if ($this->pretend === false) {
57
            if ($mode === 'update') {
58
                $this->conn->query("TRUNCATE TABLE {$table}");
59
            }
60
61
            $this->conn->query($sql);
62
        }
63
64
        return $sql;
65
    }
66
}
67