my $mapname = $map->{'name'};
$self->debug( sprintf( "Erstelle Map-Datei '%s' für Mapping '%s' ...", $map->{'mapfile'}, $mapname ) );
-
- # Ermitteln der Maximallänge der ersten Spalte
- my $first_col = $map->{'columns'}[0];
my $resultset = $map->{'resultset'};
- my $o_opts = {
- 'select' => [ { MAX => { CHAR_LENGTH => $first_col } } ],
- 'as' => [ 'max_col_length' ],
- };
- my $max_col_length = 0;
- $self->debug( sprintf( "Ermittle Maximallänge in Spalte '%s' von Mapping '%s' ...", $first_col, $mapname ) ) if $self->verbose >= 3;
- eval {
- for my $rs ( $self->schema->resultset($resultset)->search( $map->{'where'}, $o_opts )->first() ) {
- $max_col_length = $rs->get_column('max_col_length');
- }
- };
- if ( $@ ) {
- $self->error( sprintf( "Konnte Maximallänge in Spalte '%s' von Mapping '%s' nicht ermitteln: %s.", $first_col, $mapname, $@ ) );
- return undef;
- }
- $self->debug( sprintf( "Maximallänge in Spalte '%s' von Mapping '%s': %s", $first_col, $mapname, ( $max_col_length // 'undef' ) ) ) if $self->verbose >= 3;
# Sonstige Informationen zur Resultsource beschaffen
my $table_name = $self->schema->source( $map->{'resultset'} )->from();
if ( $dsn =~ /host=([a-z0-9\-_\.]+)/i ) {
$host = $1;
}
- if ( $db =~ /database=([a-z0-9\-_\.]+)/i ) {
+ if ( $dsn =~ /database=([a-z0-9\-_\.]+)/i ) {
$db = $1;
}
- if ( $db =~ /port=(\d+)/i ) {
+ if ( $dsn =~ /port=(\d+)/i ) {
$db = $1;
}
}
$port //= 3306;
$host .= " (Port " . $port . ")" if $port != 3306;
+ # Ermittlung der Ergebnisse aus der Datenbank
+ my %Map = ();
+ my $max_col_length = 0;
+ my $cols = [];
+
+ my $two_identic_cols = 0;
+ $two_identic_cols = 1 if $map->{'columns'}[0] eq $map->{'columns'}[1];
+
+ push @$cols, $map->{'columns'}[0];
+ push @$cols, $map->{'columns'}[1] unless $two_identic_cols;
+
+ my $o_opts = {
+ 'select' => $cols,
+ 'as' => $cols,
+ };
+ $o_opts->{'order_by'} = $map->{'order_by'} if $map->{'order_by'};
+ $self->debug( sprintf( "Hole Zeilen von Mapping '%s' ...", $mapname ) );
+
+ eval {
+ for my $rs ( $self->schema->resultset($resultset)->search( $map->{'where'}, $o_opts )->all() ) {
+ my $field1 = lc( decode_utf8( $rs->get_column( $cols->[0] ) ) );
+ my $field2 = $two_identic_cols ? $field1 : decode_utf8( $rs->get_column( $cols->[1] ) );
+ next unless defined( $field1 ) and defined( $field2 );
+ $max_col_length = length( $field1 ) if length( $field1 ) > $max_col_length;
+ $Map{$field1} = [] unless $Map{$field1};
+ push @{ $Map{$field1} }, $field2;
+ }
+ };
+ if ( $@ ) {
+ $self->error( sprintf( "Konnte Mapping '%s' nicht ermitteln: %s.", $mapname, $@ ) );
+ return undef;
+ }
+ $self->debug( "Gelesenes Mapping: ", \%Map ) if $self->verbose >= 3;
+
# Erstellen der Arbeitsdatei
$self->debug( sprintf( "Öffne Arbeitsdatei '%s' ...", $map->{'workfile'} ) ) if $self->verbose >= 2;
unless ( open( FILE, ">", $map->{'workfile'}->stringify ) ) {
print FILE encode_utf8( sprintf "# Zum Ändern bitte MySQL-Tabelle '%s' in der Datenbank '%s' auf %s editieren.\n", $table_name, $db, $host );
print FILE "#\n\n";
- # Nachgucken, ob es überhaupt Zeilen zu Schreiben gibt:
- $self->debug( sprintf( "Ermittle Anzahl der Zeilen von Mapping '%s' ...", $mapname ) ) if $self->verbose >= 3;
- my $zahl = 0;
- eval {
- $zahl = $self->schema->resultset($resultset)->count( $map->{'where'} );
- };
- if ( $@ ) {
- $self->error( sprintf( "Konnte Anzahl der Zeilen Mapping '%s' nicht ermitteln: %s.", $mapname, $@ ) );
- close FILE;
- return undef;
- }
- $self->debug( sprintf( "Anzahl der Zeilen Mapping '%s': %s", $mapname, ( $zahl // 'undef' ) ) );
+ $self->debug( sprintf( "Anzahl der Zeilen Mapping '%s': %d", $mapname, scalar( keys %Map ) ) ) if $self->verbose >= 3;
- if ( $zahl ) {
+ # Schreiben der eigentlichen Map-Inhaltes
+ if ( keys %Map ) {
$max_col_length += 1 if $map->{'text_type'} eq 'aliases';
- # Das eigentliche Herausholen aus der Datenbank
-
- my $cols = [];
- my $two_identic_cols = 0;
- $two_identic_cols = 1 if $map->{'columns'}[0] eq $map->{'columns'}[1];
- push @$cols, $map->{'columns'}[0];
- push @$cols, $map->{'columns'}[1] unless $two_identic_cols;
-
- $o_opts = {
- 'select' => $cols,
- 'as' => $cols,
- };
- $o_opts->{'order_by'} = $map->{'order_by'} if $map->{'order_by'};
- $self->debug( sprintf( "Hole Zeilen von Mapping '%s' ...", $mapname ) );
- eval {
- for my $rs ( $self->schema->resultset($resultset)->search( $map->{'where'}, $o_opts )->all() ) {
-
- my $field1 = encode_utf8( lc( decode_utf8( $rs->get_column( $cols->[0] ) ) ) );
- my $field2 = $two_identic_cols ? $field1 : $rs->get_column( $cols->[1] );
- $field1 .= ':' if $map->{'text_type'} eq 'aliases';
- printf FILE "%-*s %s\n", $max_col_length, $field1, $field2;
-
- }
- };
- if ( $@ ) {
- $self->error( sprintf( "Konnte Mapping '%s' nicht ermitteln: %s.", $mapname, $@ ) );
- close FILE;
- return undef;
+ for my $key ( sort keys %Map ) {
+ my $f1 = encode_utf8($key);
+ my $f2 = encode_utf8( join( ', ', @{ $Map{$key} } ) );
+ $f1 .= ':' if $map->{'text_type'} eq 'aliases';
+ printf FILE "%-*s %s\n", $max_col_length, $f1, $f2;
}
print FILE "\n";
close FILE;
+ # Nachgucken, welche Mappings sich verändert haben
my $changed = 0;
my $binary = $self->postfix_binary;
$self->debug( sprintf( "Mapping '%s' hat sich %s.", $mapname, ( $changed ? 'GEÄNDERT' : 'NICHT VERÄNDERT' ) ) );
}
+ # postmap bzw. postalias auf geänderte Mappings anwenden
if ( $changed ) {
$self->info( sprintf( "Aktualisiere Mapping '%s' in Datei '%s' ...", $mapname, $map->{'mapfile'} ) );