3 #######################################################################
4 # Copyright (C) 2010 - Medical Information Integration, LLC
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This loads the FDA Orange Book data for drugs into the "drugs"
12 # table of OpenEMR. If a row already exists with the same drug name
13 # it is ignored, otherwise a new row is created with the drug
14 # trade name provided from the Orange Book data.
18 # Converts openemr log ouput containing deleted records and
19 # produces the necessary "insert into <table> values ... on duplicate key
22 # the log table's comments contains all of the deleted records in
23 # "tablename: field1='value1' field2='value2'" form.
25 # there are embedded newlines from the original web forms. They are
26 # encoded to allow the split operation to work correctly, then restored.
28 # there is one mysql reserved word used as a column name in use that I
29 # have discovered. the word 'interval' must be enclosed in backticks in
30 # order to be accepted by mysql.
33 # This procedure will restore all records deleted on a given date/time) that are in the log table
35 # First, you must look into the admin/logs screen and determine the date that the delete occured on.
37 # Adjust the time accordingly
39 # use the mysql command or similar tool to produce the restore file
40 # ========================================
44 # select distinct comments from log where event like 'delete' and date >='<date> 00:00:00' and date <= '<date> 23:59:59';
45 # ========================================
48 # Verify data is correct
50 # Run: perl convert_logcomments.pl < outfile > restore.sql
52 # verify insert statements
56 # mysql <database> < restore.sql
61 # %deleted is a hash that holds the read in values from the file on the
65 # %tablefields is simply a hash that holds the tablenames and all of the
66 # fields found for each table. it makes it easier to construct the
86 # engage slurp mode aka pull the entire file into the $input variable
93 # encode embedded newlines (they are of the form "0x0d 0x0a"
94 # without spaces) these have been entered in at the web browser and must
97 $input =~s/\r\n/jasonnewline/g;
99 # Double backslashes need to be relegated to single backslashes
100 # leaning toothpick syndrome
104 # fix reserved word issues
105 $input=~s/(interval)/`$1`/g;
107 @records = split /\n/, $input;
113 # these next lines are skipping over the mysql monitor formatting
114 next if ( m/^mysql/i );
116 next if ( m/\|\s+comments\s+\|\s*$/ );
117 next if ( m/^\s*$/ );
118 next if ( m/^\d+\s+rows in/ );
122 # split on the FIRST colon in the line.
123 ($table,$entry) = split /:\s*/,$_,2;
125 $i=$#{ $deleted{$table}}+1;
127 # nuke extra white space between quotes
128 $entry =~ s/'(\s+)'/''/g;
132 print "tablename: $table\n";
133 print "$table entry #$i\n";
134 print "\t\tentry: $entry\n\n";
137 { # for a moment, override input record separator and chomp:
138 # lopping off the last single quote, if present.
139 # otherwise, this last quote screws up the output
144 #split the line on single quotes followed by a space
145 @temp = split /' /, $entry;
148 # $field is the table's field name
149 # $value is the value.
150 ($field,$value) = split /=/;
152 #un-encode embedded newlines
153 $value =~ s/jasonnewline/\r\n/g;
157 print "\t$field = $value\n";
159 $deleted{$table}[$i]{$field}=$value;
160 $tablefields{$table}{$field} = 1;
167 foreach $table (keys %deleted)
169 #get the fields that should be present
170 @fields=keys %{ $tablefields{$table} };
171 for $i (0 .. $#{ $deleted{$table} } )
173 foreach $entry (@fields)
175 if (!exists ( $deleted{$table}[$i]{$entry} ) )
177 $deleted{$table}[$i]{$entry} = "";
184 # now to display properly:
186 foreach $table (keys %deleted)
188 # This is the beginning of the statement
189 $sql = "insert into $table ( ";
190 @fields=keys %{ $tablefields{$table} };
191 $sql .= join ",", @fields;
193 $sql .= ") values \n";
195 # and this is the middle
197 for $i (0 .. $#{$deleted{$table}} )
202 $x=$deleted{$table}[$i]{$_};
205 $values[$i] .= qq/'',/;
209 $values[$i] .= $deleted{$table}[$i]{$_} . ",";
213 $values[$i] .= "),\n";
217 #this is the end of the statement
218 # don't forget the "on duplicate key replace" part
219 $sqlpost ="on duplicate key update ";
220 # this composes the rest of $sqlpost
224 $sqlpost .= "$_=values($_), ";
230 #now put them together: beginning, middle, end
233 for $i (0 .. $#values)