| 1 | =head1 NAME | ||
| 2 | |||
| 3 | GnaData - Routines for converting formats | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | This class does GnaData. | ||
| 8 | |||
| 9 | =cut | ||
| 10 | |||
| 11 | package GnaData; | ||
| 12 | use strict; | ||
| 13 | use DBI; | ||
| 14 | use IO::Handle; | ||
| 15 | use IO::File; | ||
| 16 | |||
| 17 | $GnaData::VERSION = '0.01'; | ||
| 18 | |||
| 19 | sub dump_rdb { | ||
| 20 | my ($connect, $table, $fh) = @_; | ||
| 21 | my ($field); | ||
| 22 | my ($i); | ||
| 23 | my ($dbh) = DBI->connect($connect); | ||
| 24 | my ($sth) = $dbh->prepare("select * from " . $table); | ||
| 25 | my (@date_fields) = (); | ||
| 26 | $sth->execute(); | ||
| 27 | |||
| 28 | my ($line) = join("\t", @{$sth->{'NAME_lc'}}) ."\n"; | ||
| 29 | $fh->print($line); | ||
| 30 | $line =~ s/[a-z0-9\_\-]/\-/gi; | ||
| 31 | $fh->print($line); | ||
| 32 | |||
| 33 | my($i) = 0; | ||
| 34 | foreach $field (@{$sth->{'NAME_lc'}}) { | ||
| 35 | if (&is_date_field($field)) { | ||
| 36 | push(@date_fields, $i); | ||
| 37 | } | ||
| 38 | $i++; | ||
| 39 | } | ||
| 40 | |||
| 41 | my (@array); | ||
| 42 | |||
| 43 | while (@array = $sth->fetchrow_array) { | ||
| 44 | foreach $i (@date_fields) { | ||
| 45 | $array[$i] =~ s/0000\-00\-00//gi; | ||
| 46 | $array[$i] =~ s/^([0-9]{4})\-([0-9]{2})\-([0-9]{2})$/$1$2$3/gi; | ||
| 47 | } | ||
| 48 | $line = join("\t", @array) . "\n"; | ||
| 49 | $fh->print($line); | ||
| 50 | } | ||
| 51 | $sth->finish(); | ||
| 52 | $dbh->disconnect; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub load_rdb { | ||
| 56 | my ($connect, $table, $inh) = @_; | ||
| 57 | my($outh) = IO::File->new("/tmp/gnadata.load.$$", "w"); | ||
| 58 | my($attributes) = {}; | ||
| 59 | |||
| 60 | if (defined $outh) { | ||
| 61 | my($dbh) = DBI->connect($connect); | ||
| 62 | my($sth) = $dbh->prepare("listfields $table"); | ||
| 63 | $sth->execute(); | ||
| 64 | my($translate_fields) = $sth->{'NAME_lc'}; | ||
| 65 | my($field); | ||
| 66 | my($i) = 0; | ||
| 67 | foreach $field (@{$translate_fields}) { | ||
| 68 | $attributes->{$field} = {}; | ||
| 69 | $attributes->{$field}->{'is_date'} = | ||
| 70 | &is_date_field($field); | ||
| 71 | $attributes->{$field}->{'NULLABLE'} = | ||
| 72 | $sth->{'NULLABLE'}->[$i]; | ||
| 73 | $i++; | ||
| 74 | } | ||
| 75 | |||
| 76 | $sth->finish(); | ||
| 77 | &rdb_to_sqlload($inh, $outh, $translate_fields, | ||
| 78 | $attributes); | ||
| 79 | $outh->close(); | ||
| 80 | if (-e "/tmp/gnadata.load.$$") { | ||
| 81 | |||
| 82 | $dbh->do("delete from $table"); | ||
| 83 | $dbh->do('load data local infile ' . "\"/tmp/gnadata.load.$$\"" | ||
| 84 | . ' into table ' . $table); | ||
| 85 | |||
| 86 | } | ||
| 87 | $dbh->disconnect; | ||
| 88 | unlink "/tmp/gnadata.load.$$"; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub rdb_to_sqlload { | ||
| 93 | my ($inh, $outh, | ||
| 94 | $translate_fields, $attr) = @_; | ||
| 95 | my ($line); | ||
| 96 | my ($first_line) = 1; | ||
| 97 | my ($second_line) = 1; | ||
| 98 | my ($field); | ||
| 99 | my (@infields); | ||
| 100 | my (@outfields); | ||
| 101 | my (%f); | ||
| 102 | my ($i); | ||
| 103 | |||
| 104 | while ($line = $inh->getline()) { | ||
| 105 | if ($first_line) { | ||
| 106 | chop $line; | ||
| 107 | |||
| 108 | @infields = split(/\t/, $line); | ||
| 109 | if ($translate_fields eq undef) { | ||
| 110 | @outfields = @infields; | ||
| 111 | } else { | ||
| 112 | @outfields = @{$translate_fields}; | ||
| 113 | } | ||
| 114 | |||
| 115 | $first_line = 0; | ||
| 116 | $second_line = 1; | ||
| 117 | } elsif ($second_line) { | ||
| 118 | $first_line = 0; | ||
| 119 | $second_line = 0; | ||
| 120 | } else { | ||
| 121 | chop $line; | ||
| 122 | %f = (); | ||
| 123 | @f{@infields} = split(/\t/, $line); | ||
| 124 | |||
| 125 | foreach $field (@outfields) { | ||
| 126 | if ($attr->{$field}->{'is_date'} == 1) { | ||
| 127 | $f{$field} =~ | ||
| 128 | s/^([0-9]{4})([0-9]{2})([0-9]{2})$/$1-$2-$3/; | ||
| 129 | } elsif ($attr->{$field}->{'NULLABLE'} == 1) { | ||
| 130 | $f{$field} = '\N'; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | $outh->print(join("\t", @f{@outfields}), "\n"); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | sub is_date_field { | ||
| 139 | my($field) = @_; | ||
| 140 | return ($field =~ /^date/i || $field =~ /date$/i); | ||
| 141 | } | ||
| 142 | |||
| 143 | 1; | ||
| 144 |