-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfile.php
More file actions
134 lines (113 loc) · 3.24 KB
/
Infile.php
File metadata and controls
134 lines (113 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Helpers Connection Component
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Helpers\Connection;
use FloatPHP\Classes\Filesystem\Arrayify;
use FloatPHP\Classes\Server\System;
use \PDOException;
use \PDO;
System::setTimeLimit(0);
System::setMemoryLimit('-1');
/**
* MySQL infile class.
*/
class Infile
{
/**
* @access private
* @var array $config
*/
private $config = [];
/**
* Set config.
*
* @param array $config
*/
public function __construct($config = [])
{
$this->config = Arrayify::merge([
'host' => 'localhost',
'port' => 3306,
'name' => '',
'user' => 'root',
'pswd' => '',
'charset' => false,
'debug' => false,
'truncate' => false,
'table' => '',
'file' => '',
'action' => 'REPLACE', // IGNORE
'delimiter' => ';',
'enclosed' => '\"',
'line' => '\n',
'ignore' => false
], $config);
}
/**
* Execute.
*
* @access public
* @return void
*/
public function execute() : void
{
try {
$start = microtime(true);
// Set PDO params
$dsn = "mysql:dbname={$this->config['name']};";
$dsn .= "host={$this->config['host']};";
$dsn .= "port={$this->config['port']}";
// Init PDO INFILE
$pdo = new PDO($dsn, $this->config['user'], $this->config['pswd'], [
PDO::MYSQL_ATTR_LOCAL_INFILE => true
]);
// Truncate table
if ( $this->config['truncate'] ) {
$pdo->exec("TRUNCATE `{$this->config['table']}`;");
}
// Execute query
$pdo->exec($this->buildQuery());
if ( $this->config['debug'] ) {
$end = microtime(true);
$time = $end - $start;
echo "Executed in : {$time} seconds\n";
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* Build query.
*
* @access private
* @return string
*/
private function buildQuery() : string
{
$query = "LOAD DATA INFILE '{$this->config['file']}' ";
if ( $this->config['action'] ) {
$query .= "{$this->config['action']} ";
}
$query .= "INTO TABLE `{$this->config['table']}` ";
if ( $this->config['charset'] ) {
$query .= "CHARACTER SET {$this->config['charset']} ";
}
$query .= "FIELDS TERMINATED BY '{$this->config['delimiter']}' ";
$query .= "OPTIONALLY ENCLOSED BY '{$this->config['enclosed']}' ";
$query .= "LINES TERMINATED BY '{$this->config['line']}'";
if ( $this->config['ignore'] ) {
$query .= " IGNORE {$this->config['ignore']} LINES";
}
return $query;
}
}