Advertisement
Guest User

Untitled

a guest
Mar 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. package db_row2col;
  3. use strict;
  4. use DBI;
  5. use Data::Dumper;
  6.  
  7. use constant DB => "DBNAME";
  8. use constant USER => "USERNAME";
  9. use constant PASSWORD => "PASSWORD";
  10. use constant TAB => "TABNAME";
  11.  
  12.  
  13. #___DEF VARS_______________
  14. my ( @data, @restr_data, $ser_out );
  15.  
  16. #____I UNTANGLE PERL AND NON-PERL-CODE IN THE MOST CASES, SO I TRY TO PUT SQL-QUERIES INTO VARIABLES__
  17. my ( $sel_all, $tab_name, $query ) = ( "SELECT * FROM", TAB );
  18. my $query = $sel_all." ". TAB. ";";
  19.  
  20. #___SUBROUTINES AND CONSTRUCTOR____
  21.  
  22. sub new {
  23. my $type = shift;
  24. my $self = {};
  25. bless $self, $type;
  26. $self;
  27. }
  28.  
  29. sub pull_data {
  30. my ( $ref, $query, $data ) = ( shift, shift, shift );
  31. my $dbh = DBI->connect("DBI:mysql:database=" . DB, USER, PASSWORD);
  32.  
  33. my $call_data = $dbh->prepare($query);
  34. $call_data->execute() or die $call_data->err_str;
  35.  
  36. while( my $hshRef = $call_data -> fetchrow_hashref() ){
  37. my @tmp;
  38. map {
  39. push @tmp, $hshRef->{ $_ }
  40. } @{$call_data->{ NAME }};
  41.  
  42. push @{$data}, [ @tmp ];
  43.  
  44. }
  45. $data;
  46. }
  47.  
  48. #___INIT SCRIPT___
  49. my $inst = db_row2col -> new();
  50. #my $res_set = $inst -> pull_data( $query, [ @data ] );
  51.  
  52. # it is not really another order of elements, just a manipulated data-output
  53. for my $y (0..$#{$inst -> pull_data( $query, [ @data ] )->[0]} ){
  54. printf '%-25s' x @{$inst -> pull_data( $query, [ @data ] )}."n", map $_->[$y], @{$inst -> pull_data( $query, [ @data ] )};
  55. }
  56.  
  57. print "n______________________nn";
  58.  
  59. #real re-ordering of columns in row in a new array of reference to arrays
  60. for my $i ( 0..$#{ $inst -> pull_data( $query, [ @data ] ) } ) {
  61. my $extern_loop = 0;
  62. for my $j ( 0..$#{ $inst -> pull_data( $query, [ @data ] )->[$i] } ) {
  63.  
  64.  
  65. #normal, not manipulated output
  66. printf "%-30s", $inst -> pull_data( $query, [ @data ] ) -> [$i]->[$j];
  67. # make restructured new array of reference on arrays, rows to columns, col. to rows
  68. $restr_data[$j][$i] = $inst -> pull_data( $query, [ @data ] ) -> [$i]->[$j];
  69. }
  70. print "n";
  71.  
  72. }
  73.  
  74. print "n";
  75. #output data in simple json format
  76.  
  77.  
  78. $ser_out .= " [ n";
  79. for my $cnt (0.. $#restr_data){
  80. $ser_out .= " t{ "obj".$cnt."" : [ ";
  81. map {
  82. $ser_out .= sprintf "%-20s", $_.",";
  83. } @{ $restr_data[$cnt] };
  84.  
  85. #remove comma after last value and close object
  86. $ser_out .= $cnt == $#restr_data ? " ] }":" ] },n";
  87.  
  88. #remove comma after the last arrayref
  89. $ser_out =~ m/,.*?]/i ? $ser_out =~ s/,(s*?]+)/$1 /gi : $ser_out;
  90. }
  91. #close json
  92. $ser_out .= "n ] n";
  93.  
  94. print "BEGIN_________nn".$ser_out;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement