Passed
Push — master ( e18e45...a1f222 )
by Emmanuel
03:09
created

MySQL::loadData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 16
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 4
crap 2
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 MySQL
21
 */
22
class MySQL extends AbstractDBHelper
23
{
24
    /**
25
     * Send options to be able to load dataset
26
     * @return array
27
     */
28 47
    public static function getDriverOptions(): array
29
    {
30
        return [
31 47
            \PDO::MYSQL_ATTR_LOCAL_INFILE => true
32
        ];
33
    }
34
35
36
    /**
37
     * Add a custom enum type
38
     * @return void
39
     */
40 46
    public function registerCustomTypes(): void
41
    {
42
        // already registered
43 46
        if (\Doctrine\DBAL\Types\Type::hasType('neuralyzer_enum')) {
44 45
            return;
45
        }
46
47
        // Else register
48
        // Manage specific types such as enum
49 1
        \Doctrine\DBAL\Types\Type::addType(
50 1
            'neuralyzer_enum',
51 1
            'Edyan\Neuralyzer\Doctrine\Type\Enum'
52
        );
53 1
        $platform = $this->conn->getDatabasePlatform();
54 1
        $platform->registerDoctrineTypeMapping('enum', 'neuralyzer_enum');
55 1
        $platform->registerDoctrineTypeMapping('bit', 'boolean');
56 1
    }
57
58
59
    /**
60
     * Load Data from a CSV
61
     * @param  string  $table
62
     * @param  string  $filename
63
     * @param  array   $fields
64
     * @param  string  $mode  Not in used here
65
     * @return string
66
     */
67 7
    public function loadData(
68
        string $table,
69
        string $filename,
70
        array $fields,
71
        string $mode
72
    ): string {
73 7
        $sql ="LOAD DATA LOCAL INFILE '{$filename}'
74 7
     REPLACE INTO TABLE {$table}
75 7
     FIELDS TERMINATED BY '|' ENCLOSED BY '\"' LINES TERMINATED BY '" . PHP_EOL . "'
76 7
     (`" . implode("`, `", $fields) . "`)";
77
        // Run the query if asked
78 7
        if ($this->pretend === false) {
79 6
            $this->conn->query($sql);
80
        }
81
82 6
        return $sql;
83
    }
84
}
85