-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_by_sql_value.php
173 lines (139 loc) · 3.65 KB
/
get_by_sql_value.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
// credentials file
require 'config.php';
/**
* @param array $config
*
* @return PDO
*/
function getPDO(array $config)
{
$pdo = new PDO(
sprintf(
'%s:host=%s;port=%d;dbname=%s',
$config['driver'],
$config['hostname'],
$config['port'],
$config['database']
),
$config['user'],
$config['password']
);
return $pdo;
}
/**
* Validate console input
*
* Die if input is not valid
*
* @param array $argv user inputs
*/
function validateInput($argv)
{
if (count($argv) !== 3) {
echo 'php get_by_sql_value.php <input> <output>' . PHP_EOL;
die(1);
}
if (!file_exists($argv[1])) {
echo "Error: " . $argv[1] . " is not a valid file" . PHP_EOL;
die(1);
}
}
/**
* @param int $bufferCount
*
* @return bool
*/
function bufferIsNotFull($bufferCount)
{
return ($bufferCount < 1000);
}
/**
* @param string $select
* @param string $table
* @param string $where
* @param array $buffer
* @param PDO $pdo
* @param resource $outputFile
*
* @return int
*/
function processBuffer($select, $table, $where, $buffer, $pdo, $outputFile)
{
$values = implode("','", $buffer);
$query = "SELECT $select from $table WHERE $where IN ('$values')";
$stmt1 = $pdo->query($query);
$lost = 0;
$result = array();
while ($line = $stmt1->fetch(\PDO::FETCH_NUM)) {
if (!$stmt1) {
throw new RuntimeException(join(' : ', $pdo->errorInfo()));
}
if (!is_array($line)) {
throw new RuntimeException("Expects array as result, got $line");
}
$result[] = current($line);
}
writeResultInOutputFile($result, $outputFile);
if (count($result) !== count($buffer)) {
return ($lost + count($buffer) - count($result));
} else {
return $lost;
}
}
/**
* @param array $values
* @param resource $file
*/
function writeResultInOutputFile(array $values, $file)
{
foreach ($values as $value) {
fwrite($file, $value . "\n");
}
}
validateInput($argv);
$filepath = $argv[1];
require 'Util/TextColorWriter.php';
$pdo = getPDO($config);
$pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$inputFilepath = $argv[1];
$outputFilepath = $argv[2];
$inputFile = fopen($inputFilepath, "r");
$outputFile = fopen($outputFilepath, 'w');
if (false === $inputFile) {
throw new \Exception("Could not open file $inputFile");
}
if (false === $outputFile) {
throw new \Exception("Could not write file $outputFile");
}
$buffer = array();
$i = 0;
$lost = 0;
$queriesRun = 0;
while (($rawLine = fgets($inputFile)) !== false) {
$cleanLine = str_replace('"', '', trim($rawLine));
if (bufferIsNotFull(count($buffer))) {
$buffer[] = $cleanLine;
$i++;
continue;
}
$buffer[] = $cleanLine;
$queriesRun++;
$lostLines = processBuffer($select, $table, $where, $buffer, $pdo, $outputFile);
$lost += $lostLines;
// reset buffer
$bufferCount = 0;
$buffer = array();
$i++;
}
// process what's left in buffer
$queriesRun++;
$lostLines = processBuffer($select, $table, $where, $buffer, $pdo, $outputFile);
$lost += $lostLines;
fclose($inputFile);
fclose($outputFile);
// echo result
echo TextColorWriter::textColor('Done', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
echo TextColorWriter::textColor("Processed : $i lines", TextColorWriter::BASH_PROMPT_CYAN) . PHP_EOL;
echo TextColorWriter::textColor("Lost : $lost lines", TextColorWriter::BASH_PROMPT_CYAN) . PHP_EOL;
echo TextColorWriter::textColor("SQL : $queriesRun queries", TextColorWriter::BASH_PROMPT_CYAN) . PHP_EOL;