Advertisement
Guest User

Inserting Into DB

a guest
Jan 2nd, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.28 KB | None | 0 0
  1. #!/usr/local/bin/perl
  2. use DatabaseConnection;
  3.  
  4.  
  5. use strict;
  6.  
  7. use Data::Dumper;
  8.  
  9. package main;
  10.  
  11.  
  12. # MySQL database configuration
  13. my $dsn = "DBI:mysql:perl_training";
  14. my $username = "mamagdy";
  15. my $password = "";
  16.  
  17. # DatabaseConnection object
  18. my $dbc = DatabaseConnection->connect($dsn, $username, $password);
  19.  
  20. my $logfile = 'database-insertion-log.txt';
  21. open(my $logfileHandle, '>', $logfile) or die "Could not open file '$logfile' $!";
  22.  
  23. my $fileCount = 0;
  24. foreach my $fp (glob("logfiles/*.txt")) {
  25.    
  26.     open (my $fh, "<", $fp) or die "Cannot read/open $fp";
  27.     my $mid;
  28.     my $bid;
  29.     while (my $row = <$fh>) {
  30.         # Removing the newline at the end of each line
  31.         chomp($row);
  32.         $row =~ s/true/1/g;
  33.         $row =~ s/false/0/g;
  34.  
  35.         my %parameters = split /[,=]/, $row;    # Splitting into hash
  36.         #print Dumper(\%parameters);
  37.        
  38.         # Builds: id, cd, ct, tt, mid, p, i, s, g, r, w, e, a ,q, c, o
  39.         # Machines: id, m, np, ms
  40.        
  41.         # check if the machine does not exits
  42.         # if so, add it to the table of Machines
  43.         my @result = $dbc->retrieve('Machines', "m = '$parameters{'m'}'");
  44.        
  45.         if (!@result) {
  46.             $dbc->insert('Machines', "" , $parameters{'m'}, $parameters{'np'}, $parameters{'ms'});
  47.             @result = $dbc->retrieve('Machines', "m = '$parameters{'m'}'");
  48.             $mid += 1;
  49.         }
  50.        
  51.         $mid = $result[0][0];
  52.        
  53.         # Considering only builds from the first n files and discarding the rest (for testing purposes)
  54.         if ($fileCount < 10000) {
  55.             # Checking if all the necessary fields are there
  56.             if (!$parameters{'cd'} || !$parameters{'ct'} ) {
  57.                 # If the current date or the current time does not exist
  58.                 print $logfileHandle "Warning: Missing cd and/or ct fields.";          
  59.             }
  60.  
  61.             if (!$mid) {
  62.                 # If no machine is assigned to the current build
  63.                 print $logfileHandle "Warning: Missing m field.";  
  64.             }
  65.             # Inserting into the Builds table
  66.             $dbc->insert('Builds', "", $parameters{'cd'}, $parameters{'ct'}, $parameters{'tt'},
  67.                     $mid, $parameters{'p'}, $parameters{'i'},,$parameters{'s'},
  68.                     $parameters{'g'}, $parameters{'r'}, $parameters{'w'},
  69.                     $parameters{'e'}, $parameters{'a'}, $parameters{'q'},
  70.                     $parameters{'c'}, $parameters{'o'});
  71.  
  72.         }
  73.  
  74.     }  
  75.  
  76.     close $fh or die "Cannot close $fp";
  77.     $fileCount += 1;
  78. }  
  79.  
  80. close $logfileHandle;
  81. # Disconnecting ..
  82. $dbc->disconnect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement