Passed
Push — master ( 16eb74...e2e5d8 )
by Emmanuel
02:58
created

SQLServer::getEnclosureForCSV()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
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
use Edyan\Neuralyzer\Exception\NeuralizerException;
20
21
/**
22
 * Various methods related to SQLServer
23
 */
24
class SQLServer extends AbstractDBHelper
25
{
26
    /**
27
     * Set the right enclosure
28
     * @return string
29
     */
30 1
    public function getEnclosureForCSV(): string
31
    {
32 1
        return chr(0);
33
    }
34
35
36
    /**
37
     * Load Data from a CSV
38
     * @param  string  $table
39
     * @param  string  $filename
40
     * @param  array   $fields
41
     * @param  string  $mode  Not in used here
42
     * @return string
43
     */
44 2
    public function loadData(
45
        string $table,
46
        string $filename,
47
        array $fields,
48
        string $mode
49
    ): string {
50 2
        if (substr(gethostbyname($this->conn->getHost()), 0, 3) !== '127') {
51 1
            throw new NeuralizerException('SQL Server must be on the same host than PHP');
52
        }
53
54 1
        $sql ="BULK INSERT {$table} FROM '{$filename}' WITH (
55 1
            FIELDTERMINATOR = '|', DATAFILETYPE = 'widechar', ROWTERMINATOR = '" . PHP_EOL . "'
56
        )";
57
58 1
        if ($this->pretend === false) {
59
            if ($mode === 'update') {
60
                $this->conn->query("TRUNCATE TABLE {$table}");
61
            }
62
63
            $this->conn->query($sql);
64
        }
65
66 1
        return $sql;
67
    }
68
}
69