Completed
Push — master ( fb988b...6625be )
by Emmanuel
07:10 queued 05:31
created

SQLServer::loadData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

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