-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate_query_with_codes.pl
69 lines (56 loc) · 1.61 KB
/
populate_query_with_codes.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use strict;
use warnings;
my @statements;
my @standard;
sub readFile{
my (%args) = @_;
@statements=();
@standard=();
print "Reading file...\n";
open my $fh, '<', $args{'filename'} or die "Cannot open $args{'filename'}: $!";
my $header = <$fh>;
while (<$fh> ) {
chomp;
my $val = (split( ",", $_ ))[1];
if ($val =~ /%/ ) {
push(@statements, "readcode like '$val'");
} else {
push(@standard, "'$val'");
}
}
close($fh);
print "Done.\n\n";
}
sub writeQuery{
my (%args) = @_;
if(@standard) {
push(@statements, "readcode IN (" . join(',',@standard) . ")");
}
my $sql = "WHERE " . join(' OR ',@statements);
open my $fh, '<', $args{'filename'} or die "Cannot open $args{'filename'}: $!";
local $/;
my $document = <$fh>;
close ($fh);
$document =~s/\{\{query\}\}/$sql/gi;
open $fh, '>', 'queries/create_view_for_extract' . $args{'suffix'} . '.sql' or die "Cannot open queries/create_view_for_extract' . $args{'suffix'} . '.sql: $!";
print $fh $document;
close ($fh);
print "Done!\n";
}
#create output directories if not exists
if( !-d "./queries/"){
mkdir "queries" or die "Failed to create the queries directory.";
}
if( !-d "./out/"){
mkdir "out" or die "Failed to create the out directory.";
}
my %args = ('filename' => "codes/code-list-drugs.csv", 'suffix' => "-drugs.sql");
readFile(%args);
$args{'filename'} = "templates/query-drugs.sql.tmpl";
writeQuery(%args);
$args{'filename'} = "templates/query-drugs.mysql.tmpl";
$args{'suffix'} = "-drugs.mysql";
writeQuery(%args);
$args{'filename'} = "templates/query-drugs-out.mysql.tmpl";
$args{'suffix'} = "-drugs-out.mysql";
writeQuery(%args);