Commit 2520bd5d authored by Thomas Tsiakalakis's avatar Thomas Tsiakalakis

initial commit

parents
Pipeline #154 failed with stages
data/*
!data/.gitkeep
\ No newline at end of file
This diff is collapsed.
<?php
$importfile = ($argc === 2) ? $argv[1] : 'B1809244';
$importfiles = [$importfile, 'AUSPC', 'FD', 'FPC', 'INT', 'PACKSTAT'];
$importdir = 'data/';
foreach($importfiles as $file){
if(!file_exists($importdir . $file . '.DAT')){
echo 'import file ' . $file . '.DAT does not exist!' . PHP_EOL;
die();
}
}
$db = new \MySQLi('mdb2.local.zeyel.net', 'dhl', 'EVohsed4jea8Yovu', 'dhl');
include('_rules.php');
echo 'creating tables ';
foreach($rules as $table => $details){
echo $table . ' ';
$fieldsqls = [];
$primaries = [];
foreach($details['rules'] as $field => $rule){
switch($rule['type']){
case 's':
$type = "VARCHAR(${rule['length']})";
break;
case 'd':
$type = 'DATE';
break;
case 'i':
case 'n':
$type = "INT(${rule['length']})";
break;
default:
throw new Exception("unknown field type ${rule['type']} in $field in $table");
}
$nullable = '';
if(!array_key_exists('nullable', $rule) || $rule['nullable'] !== true || (array_key_exists('primary', $rule) && $rule['primary'] === true)){
$nullable = 'NOT ';
}
$fieldsqls[] = "`${field}` ${type} ${nullable}NULL";
if(array_key_exists('primary', $rule) && $rule['primary'] === true){
$primaries[] = "`${field}`";
}
}
$sql = "CREATE TABLE IF NOT EXISTS `" . $details['name'] . "` (" . implode(', ', $fieldsqls);
if(!empty($primaries)){
$sql .= ', PRIMARY KEY(' . implode(', ', $primaries) . ')';
}
$sql .= ') COMMENT="' . addslashes($details['description']) . '"';
$statement = $db->prepare($sql);
echo $db->error;
$statement->execute();
echo $statement->error;
$statement->close();
}
echo PHP_EOL;
$stats = [];
foreach($importfiles as $file){
echo PHP_EOL . 'importing file ' . $file . PHP_EOL;
$handle = fopen($importdir . $file . '.DAT', 'r');
$linenum = 0;
$rowcounter = 0;
$lastpercentage = 0;
$lasttype = '';
while (!feof($handle)) {
$linenum++;
$line = fgets($handle);
$type = substr($line, 0,2);
if(empty(trim($line))){
continue;
}
if(!array_key_exists($type, $rules)){
echo PHP_EOL . "unknown row type ${type} found in file ${file}.DAT in line ${linenum}" . PHP_EOL;
continue;
}
if(strlen(trim($line)) !== $rules[$type]['length']){
echo PHP_EOL . 'invalid row length ' . strlen(trim($line)) . ' (should be ' . $rules[$type]['length'] . ") in file ${file}.DAT in line ${linenum}" . PHP_EOL;
continue;
}
if($type !== $lasttype){
echo PHP_EOL . 'importing ' . $type . ' - ' . $rules[$type]['name']. PHP_EOL;
$lasttype = $type;
$rowcounter = 0;
}
$rowcounter++;
if(array_key_exists($type, $stats)){
$percentage = round(($rowcounter /$stats[$type]) * 100);
if($percentage !== $lastpercentage){
echo "\r " . $percentage . '% ';
$lastpercentage = $percentage;
}
}else{
if($linenum % 100 === 0){
echo "\r line " . $linenum . ' ';
}
}
$sql = 'REPLACE INTO `' . $rules[$type]['name'] . '` (`' . implode('`, `', array_keys($rules[$type]['rules'])) . '`) VALUES (' . str_repeat('?,', count($rules[$type]['rules']) - 1) . '?)';
$stmttypes = '';
$values = [];
$statsrow = [];
foreach($rules[$type]['rules'] as $field => $rule){
$nullable = false;
if(array_key_exists('nullable', $rule) && $rule['nullable'] === true && (!array_key_exists('primary', $rule) || $rule['primary'] !== true)){
$nullable = true;
}
$value = iconv('cp437','utf-8',substr($line, $rule['from'], $rule['length']));
if($type === 'XX' && in_array($field, ['DATEIKENNUNG', 'STATUS', 'SATZZAHL'])){
$statsrow[$field] = $value;
}
if($rule['type'] === 's' || $rule['type'] === 'd'){
if($nullable){
$values[] = NULL;
}else{
$values[] = trim($value);
}
}elseif($rule['type'] === 'i' || $rule['type'] === 'n'){
$values[] = intval(trim($value));
}
$stmttypes .= ($rule['type'] === 's' || $rule['type'] === 'd') ? 's' : 'd';
}
$statement = $db->prepare($sql);
echo $db->error;
$statement->bind_param($stmttypes, ...$values);
$statement->execute();
echo $statement->error;
$statement->close();
if(!empty($statsrow)){
if(array_key_exists($statsrow['DATEIKENNUNG'], $stats)){
$stats[$statsrow['DATEIKENNUNG']] += intval($statsrow['SATZZAHL']);
}else{
$stats[$statsrow['DATEIKENNUNG']] = intval($statsrow['SATZZAHL']);
}
}
}
}
echo PHP_EOL . 'done.' . PHP_EOL;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment