#!/usr/bin/perl
use warnings;
use strict;

use DBI;
use Data::Dump qw(dump);

my $dbh = DBI->connect ("dbi:CSV:", "", "", {
	f_dir => "./db3",
	f_ext => ".csv",
});

sub lookup {
	my $sql = shift;
	my $data;

	my $sth = $dbh->prepare( $sql );
	$sth->execute;

	while( my $row = $sth->fetchrow_arrayref ) {
		my @r = @$row;
		my $id = shift @r;
		$data->{ $row->[0] } = $#$row == 1 ? $row->[1] : join('', @r);
	}

	return $data;
}

my $names = lookup( qq{
select Id, texteEN from "SymbolNames"
});

#warn "# names = ",dump( $names );

my $hints = lookup( qq{
select Id, texteEN from "SymbolHints"
});

my $types = lookup( qq{
select TypeId, TypeName from "DataTyps"
});

my $units = lookup( qq{
select Id, TexteEN from "UnitNames"
});

my $diags = lookup( qq{
select Id,x0136,x0142,x0144,x0147,x0151,x0152,x0153,x0158,x0160,x0161 from "DiagnoseDaten"
where x0147 = 1
});
#warn "# diags = ",dump($diags);

my $sth = $dbh->prepare( qq{
select * from "ComuInfos"
order by adresse
});

$sth->execute;

while( my $row = $sth->fetchrow_hashref ) {
	my $hint = $hints->{ $row->{texteid} };
	$hint =~ s/[\r\n]+/ | /gs;
	printf "%-3d %02x %-27s %-15s |%s| %s %s [ %s ]\n",
		$row->{adresse},
		$row->{adresse},
		$types->{ $row->{typid} },
		$units->{ $row->{id} },
		$diags->{ $row->{id} } || '          ',
		$row->{symbolname},
		$names->{ $row->{texteid} } || $row->{symbolname},
		$hint,
	;
}
