Advertisement
hakonhagland

extract-data

Jul 27th, 2021
1,685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.65 KB | None | 0 0
  1. use feature qw(say);
  2. use strict;
  3. use warnings;
  4.  
  5. {
  6.     my @tests = (
  7.         [1, 'Empty table error - data load failed.'],
  8.  
  9.         [1, {'message' => 'Prepare failed: [Teradata][ODBC Teradata Driver][Teradata Database](-5628)Column DWH_TENANT_ID not found in Q. (SQL-HY000) '}],
  10.  
  11.         [1, {'message' =>
  12.            {'message' => 'Prepare failed: [Teradata][ODBC Teradata Driver][Teradata Database](-5628)Column not found . (SQL-HY000) '}}],
  13.  
  14.         [1, {'message' =>
  15.           {'message' =>
  16.            {'message' => 'Prepare failed: [Teradata][ODBC Teradata Driver][Teradata Database](-5628)Column not found . (SQL-HY000) ' }}}],
  17.         [2, {'message' =>
  18.           {'message' =>
  19.            {'sql_stmt' => ' select * from db.table',
  20.             'message' => 'Execute failed: [Teradata][ODBC Teradata Driver][Teradata Database](-2631)Transaction ABORTed due to Deadlock. (SQL-40001)' }}}],
  21.     );
  22.  
  23.     for my $i (0..$#tests) {
  24.         my $test = $tests[$i];
  25.         my $type = $test->[0];
  26.         my $hash = { "_errlist_" => [$test->[1]] };
  27.         if ( $type == 1 ) {
  28.             my $errmsg = extract_err( $hash->{_errlist_}[0], $type );
  29.             say "Test #$i: $errmsg";
  30.         }
  31.         elsif ( $type == 2 ) {
  32.             my $sql_statement = $hash->{_errlist_}[0]{message}{message}{sql_stmt};
  33.             say "Test #$i: (sql) : $sql_statement";
  34.         }
  35.     }
  36. }
  37.  
  38. sub extract_err {
  39.     my ( $hash ) = @_;
  40.  
  41.     if (ref $hash eq "HASH") {
  42.         return extract_err( $hash->{message} );
  43.     }
  44.     elsif (!(ref $hash)) {
  45.         return $hash;
  46.     }
  47.     else {
  48.         die "Unexpected reference type: ", ref $hash, "\n";
  49.     }
  50. }
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement