Completed
Pull Request — master (#9)
by Sander
04:40
created

PostgreSQL::loadData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 4
crap 3
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 PostgreSQL
21
 */
22
class PostgreSQL extends AbstractDBHelper
23
{
24
    /**
25
     * Set the right enclosure
26
     * @return string
27
     */
28 10
    public function getEnclosureForCSV(): string
29
    {
30 10
        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 7
    public function loadData(
43
        string $table,
44
        string $filename,
45
        array $fields,
46
        string $mode
47
    ): string {
48 7
        $fields = implode(', ', $fields);
49
50 7
        if ($this->pretend === false) {
51 6
            if ($mode === 'update') {
52 4
                $this->conn->query("TRUNCATE {$table}");
53
            }
54 6
            $pdo = $this->conn->getWrappedConnection();
55 6
            $pdo->pgsqlCopyFromFile($table, $filename, '|', '\\\\N', $fields);
56
        }
57
58 6
        $sql = "COPY {$table} ($fields) FROM '{$filename}' ";
59 6
        $sql.= '... Managed by pgsqlCopyFromFile';
60
61 6
        return $sql;
62
    }
63
}
64