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 data set |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
1 |
|
public static function getDriverOptions(): array |
30
|
|
|
{ |
31
|
1 |
|
if (!defined('\PDO::MYSQL_ATTR_LOCAL_INFILE')) { |
32
|
|
|
return []; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return [ |
36
|
1 |
|
\PDO::MYSQL_ATTR_LOCAL_INFILE => true |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add a custom enum type |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
* @throws \Doctrine\DBAL\DBALException |
46
|
|
|
*/ |
47
|
|
|
public function registerCustomTypes(): void |
48
|
|
|
{ |
49
|
|
|
// already registered |
50
|
|
|
if (\Doctrine\DBAL\Types\Type::hasType('neuralyzer_enum')) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Else register |
55
|
|
|
// Manage specific types such as enum |
56
|
|
|
\Doctrine\DBAL\Types\Type::addType( |
57
|
|
|
'neuralyzer_enum', |
58
|
|
|
'Edyan\Neuralyzer\Doctrine\Type\Enum' |
59
|
|
|
); |
60
|
|
|
$platform = $this->conn->getDatabasePlatform(); |
61
|
|
|
$platform->registerDoctrineTypeMapping('enum', 'neuralyzer_enum'); |
62
|
|
|
$platform->registerDoctrineTypeMapping('bit', 'boolean'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
1 |
|
public function loadData(string $table, string $fname, array $fields, string $mode): string |
70
|
|
|
{ |
71
|
1 |
|
$sql ="LOAD DATA LOCAL INFILE '{$fname}' |
72
|
1 |
|
REPLACE INTO TABLE {$table} |
73
|
1 |
|
FIELDS TERMINATED BY '|' ENCLOSED BY '\"' LINES TERMINATED BY '" . PHP_EOL . "' |
74
|
1 |
|
(`" . implode("`, `", $fields) . "`)"; |
75
|
|
|
// Run the query if asked |
76
|
1 |
|
if ($this->pretend === false) { |
77
|
|
|
$this->conn->query($sql); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return $sql; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|