-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
117 lines (108 loc) · 3.32 KB
/
index.php
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
<?php
$names = [
'Bill',
'John',
'Gary',
'Michael',
'Johan',
'Alan',
'Geoff',
'William',
'Archie',
'Bismarck',
'Henry',
'Lancelot',
'Sebastian',
'Cale',
'Jacques',
'Luke',
'Paul',
'Moses',
'Ziba',
'Thando'
];
$surnames = [
'Johnson',
'Smith',
'Williams',
'Brown',
'Taylor',
'Miller',
'Wilson',
'Moore',
'Anderson',
'Thomas',
'Jackson',
'White',
'Harris',
'Martin',
'Thompson',
'Garcia',
'Martinez',
'Robinson',
'Clark',
'Rodriguez'
];
$batch_size = 10000;
function create_csv($num_records, $batch_size) {
global $names, $surnames;
$data = [];
$file = fopen('./output/output.csv', 'w');
fputcsv($file, ['Id', 'Name', 'Surname', 'Initials', 'Age', 'DateOfBirth']);
$unique_records = [];
for ($i = 0; $i < $num_records; $i++) {
do {
$name = $names[array_rand($names)];
$surname = $surnames[array_rand($surnames)];
$age = rand(1, 99);
$dob = date('d/m/Y', strtotime("-$age years"));
$record = [$i+1, $name, $surname, $name[0], $age, $dob];
$record_string = implode(',', $record);
} while (isset($unique_records[$record_string]));
$unique_records[$record_string] = true;
$data[] = $record;
if (($i+1) % $batch_size == 0 || $i+1 == $num_records) {
foreach ($data as $row) {
fputcsv($file, $row);
}
$data = [];
}
}
fclose($file);
}
if (isset($_POST['submit'])) {
create_csv($_POST['num_records'], $batch_size);
$num_records = '';
}
if (file_exists("./output/output.csv")) {
$db = new SQLite3('./test.db');
$db->exec('CREATE TABLE IF NOT EXISTS csv_import (Id INTEGER, Name TEXT, Surname TEXT, Initials TEXT, Age INTEGER, DateOfBirth TEXT)');
$file = fopen('./output/output.csv', 'r');
fgetcsv($file);
$data = [];
while (($row = fgetcsv($file)) !== FALSE) {
$data[] = "('$row[0]', '$row[1]', '$row[2]', '$row[3]', '$row[4]', '$row[5]')";
if (count($data) == $batch_size) {
$db->exec("INSERT INTO csv_import (Id, Name, Surname, Initials, Age, DateOfBirth) VALUES " . implode(',', $data));
$data = [];
}
}
if (!empty($data)) {
$db->exec("INSERT INTO csv_import (Id, Name, Surname, Initials, Age, DateOfBirth) VALUES " . implode(',', $data));
}
fclose($file);
$db->exec('CREATE TEMPORARY TABLE csv_import_temp AS SELECT MIN(Id) as Id, Name, Surname, Initials, Age, DateOfBirth FROM csv_import GROUP BY Name, Surname, Initials, Age, DateOfBirth');
$db->exec('DELETE FROM csv_import');
$db->exec('INSERT INTO csv_import SELECT * FROM csv_import_temp');
$db->exec('DROP TABLE csv_import_temp');
$result = $db->query('SELECT COUNT(*) AS count FROM csv_import');
$row = $result->fetchArray();
echo "Imported {$row['count']} unique records.";
}
?>
<form method="post">
<label for="num_records">Number of records:</label>
<input type="number" id="num_records" name="num_records" min="1" required>
<br>
<input name="submit" type="submit" value="Submit">
</form>