|
| 1 | +<?php |
| 2 | + |
| 3 | +// credentials file |
| 4 | +require 'config.php'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @param array $config |
| 8 | + * |
| 9 | + * @return PDO |
| 10 | + */ |
| 11 | +function getPDO(array $config) |
| 12 | +{ |
| 13 | + $pdo = new PDO( |
| 14 | + sprintf( |
| 15 | + '%s:host=%s;port=%d;dbname=%s', |
| 16 | + $config['driver'], |
| 17 | + $config['hostname'], |
| 18 | + $config['port'], |
| 19 | + $config['database'] |
| 20 | + ), |
| 21 | + $config['user'], |
| 22 | + $config['password'] |
| 23 | + ); |
| 24 | + |
| 25 | + return $pdo; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Validate console input |
| 30 | + * |
| 31 | + * Die if input is not valid |
| 32 | + * |
| 33 | + * @param array $argv user inputs |
| 34 | + */ |
| 35 | +function validateInput($argv) |
| 36 | +{ |
| 37 | + if (count($argv) !== 3) { |
| 38 | + echo 'php get_by_sql_value.php <input> <output>' . PHP_EOL; |
| 39 | + die(1); |
| 40 | + } |
| 41 | + |
| 42 | + if (!file_exists($argv[1])) { |
| 43 | + echo "Error: " . $argv[1] . " is not a valid file" . PHP_EOL; |
| 44 | + die(1); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * @param int $bufferCount |
| 50 | + * |
| 51 | + * @return bool |
| 52 | + */ |
| 53 | +function bufferIsNotFull($bufferCount) |
| 54 | +{ |
| 55 | + return ($bufferCount < 1000); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @param string $select |
| 60 | + * @param string $table |
| 61 | + * @param string $where |
| 62 | + * @param array $buffer |
| 63 | + * @param PDO $pdo |
| 64 | + * @param resource $outputFile |
| 65 | + * |
| 66 | + * @return int |
| 67 | + */ |
| 68 | +function processBuffer($select, $table, $where, $buffer, $pdo, $outputFile) |
| 69 | +{ |
| 70 | + $values = implode("','", $buffer); |
| 71 | + $query = "SELECT $select from $table WHERE $where IN ('$values')"; |
| 72 | + |
| 73 | + $stmt1 = $pdo->query($query); |
| 74 | + $lost = 0; |
| 75 | + |
| 76 | + $result = array(); |
| 77 | + while ($line = $stmt1->fetch(\PDO::FETCH_NUM)) { |
| 78 | + if (!$stmt1) { |
| 79 | + throw new RuntimeException(join(' : ', $pdo->errorInfo())); |
| 80 | + } |
| 81 | + |
| 82 | + if (!is_array($line)) { |
| 83 | + throw new RuntimeException("Expects array as result, got $line"); |
| 84 | + } |
| 85 | + |
| 86 | + $result[] = current($line); |
| 87 | + } |
| 88 | + |
| 89 | + writeResultInOutputFile($result, $outputFile); |
| 90 | + |
| 91 | + if (count($result) !== count($buffer)) { |
| 92 | + return ($lost + count($buffer) - count($result)); |
| 93 | + } else { |
| 94 | + return $lost; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * @param array $values |
| 100 | + * @param resource $file |
| 101 | + */ |
| 102 | +function writeResultInOutputFile(array $values, $file) |
| 103 | +{ |
| 104 | + foreach ($values as $value) { |
| 105 | + fwrite($file, $value . "\n"); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +validateInput($argv); |
| 110 | + |
| 111 | +$filepath = $argv[1]; |
| 112 | + |
| 113 | +require 'Util/TextColorWriter.php'; |
| 114 | + |
| 115 | +$pdo = getPDO($config); |
| 116 | +$pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); |
| 117 | + |
| 118 | +$inputFilepath = $argv[1]; |
| 119 | +$outputFilepath = $argv[2]; |
| 120 | + |
| 121 | +$inputFile = fopen($inputFilepath, "r"); |
| 122 | +$outputFile = fopen($outputFilepath, 'w'); |
| 123 | + |
| 124 | +if (false === $inputFile) { |
| 125 | + throw new \Exception("Could not open file $inputFile"); |
| 126 | +} |
| 127 | +if (false === $outputFile) { |
| 128 | + throw new \Exception("Could not write file $outputFile"); |
| 129 | +} |
| 130 | + |
| 131 | +$buffer = array(); |
| 132 | + |
| 133 | +$i = 0; |
| 134 | +$lost = 0; |
| 135 | +$queriesRun = 0; |
| 136 | + |
| 137 | +while (($rawLine = fgets($inputFile)) !== false) { |
| 138 | + |
| 139 | + $cleanLine = str_replace('"', '', trim($rawLine)); |
| 140 | + |
| 141 | + if (bufferIsNotFull(count($buffer))) { |
| 142 | + $buffer[] = $cleanLine; |
| 143 | + $i++; |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + $buffer[] = $cleanLine; |
| 148 | + $queriesRun++; |
| 149 | + $lostLines = processBuffer($select, $table, $where, $buffer, $pdo, $outputFile); |
| 150 | + |
| 151 | + $lost += $lostLines; |
| 152 | + |
| 153 | + // reset buffer |
| 154 | + $bufferCount = 0; |
| 155 | + $buffer = array(); |
| 156 | + |
| 157 | + $i++; |
| 158 | +} |
| 159 | + |
| 160 | +// process what's left in buffer |
| 161 | +$queriesRun++; |
| 162 | +$lostLines = processBuffer($select, $table, $where, $buffer, $pdo, $outputFile); |
| 163 | +$lost += $lostLines; |
| 164 | + |
| 165 | + |
| 166 | +fclose($inputFile); |
| 167 | +fclose($outputFile); |
| 168 | + |
| 169 | +// echo result |
| 170 | +echo TextColorWriter::textColor('Done', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL; |
| 171 | +echo TextColorWriter::textColor("Processed : $i lines", TextColorWriter::BASH_PROMPT_CYAN) . PHP_EOL; |
| 172 | +echo TextColorWriter::textColor("Lost : $lost lines", TextColorWriter::BASH_PROMPT_CYAN) . PHP_EOL; |
| 173 | +echo TextColorWriter::textColor("SQL : $queriesRun queries", TextColorWriter::BASH_PROMPT_CYAN) . PHP_EOL; |
0 commit comments