Skip to content

Instantly share code, notes, and snippets.

@craigiansmith
Created November 23, 2016 07:27
Show Gist options
  • Save craigiansmith/74d487bf0f50b8abe97854f07850c7a5 to your computer and use it in GitHub Desktop.
Save craigiansmith/74d487bf0f50b8abe97854f07850c7a5 to your computer and use it in GitHub Desktop.
Change JasperSoft report file to mysql script
#!/usr/bin/perl
# Open file
$file = $ARGV[0];
open(FH, '<', $file);
@raw_data = <FH>;
close(FH);
# Create array to store all jasper vars found
@vars = ();
# Search for Jasper vars like $P{word} or $P!{word}
foreach $line (@raw_data) {
if($line =~ /\$P\!?\{([^}]*)\}/) {
push (@vars, lc $1);
}
}
# Open file for writing to
open(FH, '>', "$file.new");
# Make sure array of variables only has distinct entries
my %hash = map { $_, 1} @vars;
my @unique = keys %hash;
# Prepend set statements to beginning of file
# such as "SET @word = '';" for each word
foreach $var (@unique) {
print FH "SET \@$var = '';\n";
}
# Substitute mysql vars in place of the Jasper vars i.e. @word
foreach $line (@raw_data) {
$line =~ s/\$P\!?\{([^}]*)\}/\@\L$1/g;
print FH $line;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment