function DB2TempTable($baseFile, $expectedHash, $columns, $signedCols = []) { global $dirnm, $db; $filePath = "{$dirnm}/{$baseFile}.db2"; if (!file_exists($filePath)) { $filePath = "{$dirnm}/{$baseFile}.dbc"; } if (!file_exists($filePath)) { LogLine("Could not find {$filePath}\n "); exit(1); } LogLine("ttbl{$baseFile}"); $reader = new Reader($filePath); if ($expectedHash) { AssertDB2LayoutHash($reader, $expectedHash, $baseFile); } $reader->setFieldNames($columns); $reader->setFieldsSigned($signedCols); $fieldTypes = $reader->getFieldTypes(); $fieldCounts = []; foreach ($reader->generateRecords() as $id => $rec) { foreach ($columns as $colName) { $fieldCounts[$colName] = is_array($rec[$colName]) ? count($rec[$colName]) : 1; } break; } $maxLengths = []; foreach ($columns as $colName) { if ($fieldTypes[$colName] == Reader::FIELD_TYPE_STRING) { $maxLengths[$colName] = 1; } } if (count($maxLengths)) { $x = 0; $recordCount = count($reader->getIds()); foreach ($reader->generateRecords() as $id => $rec) { EchoProgress(++$x / $recordCount); foreach ($maxLengths as $colName => &$maxLength) { if (is_array($rec[$colName])) { foreach ($rec[$colName] as $recVal) { $maxLength = max($maxLength, strlen($recVal)); } } else { $maxLength = max($maxLength, strlen($rec[$colName])); } } unset($maxLength); } EchoProgress(false); } $sql = 'create temporary table `ttbl' . $baseFile . '` (`id` int,'; $y = 0; $tableCols = []; $indexFields = ['id']; $paramTypes = 'i'; foreach ($columns as $colName) { if ($y++ > 0) { $sql .= ','; } for ($x = 1; $x <= $fieldCounts[$colName]; $x++) { $tableColName = $colName . ($fieldCounts[$colName] > 1 ? $x : ''); $sql .= ($x > 1 ? ',' : '') . "`{$tableColName}` "; $tableCols[] = $tableColName; if (strtolower(substr($colName, -2)) == 'id') { $indexFields[] = $tableColName; } switch ($fieldTypes[$colName]) { case Reader::FIELD_TYPE_INT: $sql .= 'int'; $paramTypes .= 'i'; break; case Reader::FIELD_TYPE_FLOAT: $sql .= 'float'; $paramTypes .= 'd'; break; case Reader::FIELD_TYPE_STRING: $sql .= 'varchar(' . $maxLengths[$colName] . ')'; $paramTypes .= 's'; break; default: $sql .= 'int'; $paramTypes .= 'i'; break; } } } foreach ($indexFields as $idx) { $sql .= ", index using hash (`{$idx}`)"; } $sql .= ') engine=memory;'; RunAndLogError($sql); $sql = sprintf('insert into `%s` (`id`,`%s`) values (?,%s)', "ttbl{$baseFile}", implode('`,`', $tableCols), substr(str_repeat('?,', count($tableCols)), 0, -1)); $stmt = $db->prepare($sql); $row = []; $idCol = 0; $params = [$paramTypes, &$idCol]; foreach ($columns as $colName) { for ($x = 1; $x <= $fieldCounts[$colName]; $x++) { $tableColName = $colName . ($fieldCounts[$colName] > 1 ? $x : ''); $row[$tableColName] = null; $params[] =& $row[$tableColName]; } } call_user_func_array([$stmt, 'bind_param'], $params); $x = 0; $recordCount = count($reader->getIds()); foreach ($reader->generateRecords() as $id => $rec) { EchoProgress(++$x / $recordCount); $idCol = $id; foreach ($columns as $colName) { if ($fieldCounts[$colName] > 1) { for ($z = 1; $z <= $fieldCounts[$colName]; $z++) { $row[$colName . $z] = $rec[$colName][$z - 1]; } } else { $row[$colName] = $rec[$colName]; } } RunAndLogError($stmt->execute()); } EchoProgress(false); unset($reader); }
$species = $name = null; $stmt->bind_param('is', $species, $name); $x = 0; $recordCount = count($battlePetReader->getIds()); foreach ($battlePetReader->generateRecords() as $id => $rec) { EchoProgress(++$x / $recordCount); $species = $id; $creature = $creatureReader->getRecord($rec['npc']); if (is_null($creature)) { continue; } $name = $creature['name']; $stmt->execute(); } $stmt->close(); EchoProgress(false); unset($creatureReader); unset($battlePetReader); /* */ } /* */ LogLine("Done.\n"); function LogLine($msg) { if ($msg == '') { return; } if (substr($msg, -1, 1) == "\n") { $msg = substr($msg, 0, -1); } echo "\n" . date('H:i:s') . ' ' . $msg;