3 # Copyright (C) 2007-2009 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Write Biopiece records to a MySQL database.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use Maasha::Biopieces;
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40 my ( $options, $user, $password, $dbh, $in, $out, $tmp_dir, $tmp_file, $fh_out, $record, $key, $type_hash, $i, @keys, $line );
42 $user = Maasha::Biopieces::biopiecesrc( "MYSQL_USER" );
43 $password = Maasha::Biopieces::biopiecesrc( "MYSQL_PASSWORD" );
45 $options = Maasha::Biopieces::parse_options(
47 { long => 'user', short => 'u', type => 'string', mandatory => 'no', default => $user, allowed => undef, disallowed => undef },
48 { long => 'password', short => 'p', type => 'string', mandatory => 'no', default => $password, allowed => undef, disallowed => undef },
49 { long => 'database', short => 'd', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
50 { long => 'table', short => 't', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
51 { long => 'index', short => 'i', type => 'list', mandatory => 'no' , default => undef, allowed => undef, disallowed => undef },
52 { long => 'database_replace', short => 'D', type => 'flag', mandatory => 'no' , default => undef, allowed => undef, disallowed => undef },
53 { long => 'table_replace', short => 'T', type => 'flag', mandatory => 'no' , default => undef, allowed => undef, disallowed => undef },
54 { long => 'table_append', short => 'A', type => 'flag', mandatory => 'no' , default => undef, allowed => undef, disallowed => undef },
55 { long => 'guess_type', short => 'g', type => 'uint', mandatory => 'no', default => 1000, allowed => undef, disallowed => 0 },
56 { long => 'sql', short => 's', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
57 { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
61 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
62 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
64 $options->{ 'guess_type' } = 0 if $options->{ 'table_append' } or $options->{ 'sql' }; # no point in analyzing data types.
66 $tmp_dir = Maasha::Biopieces::get_tmpdir();
67 $tmp_file = "$tmp_dir/mysql.tab";
69 $fh_out = Maasha::Filesys::file_write_open( $tmp_file );
75 while ( $record = Maasha::Biopieces::get_record( $in ) )
78 @keys = sort keys %{ $record } if not @keys;
80 guess_types( $type_hash, $record ) if $i < $options->{ 'guess_type' };
82 map { $line .= "$record->{ $_ }\t" } @keys;
85 print $fh_out "$line\n";
87 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
94 if ( Maasha::SQL::database_exists( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } ) )
96 if ( $options->{ 'database_replace' } )
98 Maasha::SQL::delete_database( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
99 Maasha::SQL::create_database( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
104 Maasha::SQL::create_database( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
107 $dbh = Maasha::SQL::connect( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
109 if ( Maasha::SQL::table_exists( $dbh, $options->{ 'table' } ) )
111 if ( not $options->{ 'table_replace' } and not $options->{ 'table_append' } ) {
112 Maasha::Common::error( qq(Table "$options->{ 'table' }" exists. Use --table_replace or --table_append) )
115 if ( $options->{ 'table_replace' } ) {
116 Maasha::SQL::delete_table( $dbh, $options->{ 'table' } ) if Maasha::SQL::table_exists( $dbh, $options->{ 'table' } );
119 if ( $options->{ 'sql' } ) {
120 Maasha::SQL::request( $dbh, $options->{ 'sql' } );
121 } elsif ( not $options->{ 'table_append' } ) {
122 create_table( $dbh, $options->{ 'table' }, $type_hash, $options->{ 'index' }, $options->{ 'verbose' } );
127 create_table( $dbh, $options->{ 'table' }, $type_hash, $options->{ 'index' }, $options->{ 'verbose' } );
130 Maasha::SQL::bulk_load_file( $dbh, $tmp_file, $options->{ 'table' }, "\t" ) if Maasha::SQL::table_exists( $dbh, $options->{ 'table' } );
132 Maasha::SQL::disconnect( $dbh );
136 Maasha::Biopieces::close_stream( $in );
137 Maasha::Biopieces::close_stream( $out );
140 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
145 # Martin A. Hansen, July 2009.
147 # Guess the MySQL table types from Biopiece records.
149 my ( $type_hash, # Hash with type info
150 $record, # Biopiece record
157 foreach $key ( keys %{ $record } )
159 if ( exists $type_hash->{ $key } )
161 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
163 if ( $record->{ $key } =~ tr/.// ) {
164 $type_hash->{ $key }->{ "TYPE" } = "FLOAT";
166 $type_hash->{ $key }->{ "TYPE" } = "INTEGER";
169 $type_hash->{ $key }->{ "MIN" } = Maasha::Calc::min( $record->{ $key }, $type_hash->{ $key }->{ "MIN" } );
170 $type_hash->{ $key }->{ "MAX" } = Maasha::Calc::max( $record->{ $key }, $type_hash->{ $key }->{ "MAX" } );
174 $type_hash->{ $key }->{ "TYPE" } = "ALPH";
175 $type_hash->{ $key }->{ "MIN" } = Maasha::Calc::min( length $record->{ $key }, $type_hash->{ $key }->{ "MIN" } );
176 $type_hash->{ $key }->{ "MAX" } = Maasha::Calc::max( length $record->{ $key }, $type_hash->{ $key }->{ "MAX" } );
181 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
183 $type_hash->{ $key }->{ "TYPE" } = "NUMBER";
184 $type_hash->{ $key }->{ "MIN" } = $record->{ $key };
185 $type_hash->{ $key }->{ "MAX" } = $record->{ $key };
189 $type_hash->{ $key }->{ "TYPE" } = "ALPH";
190 $type_hash->{ $key }->{ "MIN" } = length $record->{ $key };
191 $type_hash->{ $key }->{ "MAX" } = length $record->{ $key };
200 # Martin A. Hansen, July 2009
202 # Create a new MySQL table.
204 my ( $dbh, # Database handle
206 $type_hash, # Hashref with type info.
207 $fields_index, # Fields to index
208 $verbose, # Verbose switch
213 my ( %index_hash, $key, $field, @fields, $fields, $diff, $sql );
215 map { $index_hash{ $_ } = 1 } @{ $fields_index };
217 foreach $key ( sort keys %{ $type_hash } )
221 $diff = $type_hash->{ $key }->{ "MAX" } - $type_hash->{ $key }->{ "MIN" };
223 if ( $type_hash->{ $key }->{ "TYPE" } eq "INTEGER" )
227 if ( $type_hash->{ $key }->{ "MIN" } >= 0 ) {
228 # $field .= "UNSIGNED ";
231 if ( $diff <= 255 ) {
233 } elsif ( $diff <= 65535 ) {
234 $field .= "SMALLINT";
235 } elsif ( $diff <= 16777215 ) {
236 $field .= "MEDIUMINT";
237 } elsif ( $diff <= 4294967295 ) {
243 elsif ( $type_hash->{ $key }->{ "TYPE" } eq "FLOAT" )
245 $field .= "$key FLOAT"; # FLOAT(p) where p=precision from 0 to 23 results in a four-byte single-precision FLOAT column.
246 # A precision from 24 to 53 results in an eight-byte double-precision coulumn.
250 if ( $type_hash->{ $key }->{ "MAX" } > 1024 ) {
251 $field = "$key BLOB";
252 } if ( $diff == 0 ) {
253 $field = "$key CHAR($type_hash->{ $key }->{ 'MAX' })";
255 $field = "$key VARCHAR($type_hash->{ $key }->{ 'MAX' })";
259 $field .= ", INDEX $key" . "_index ($key)" if exists $index_hash{ $key };
261 push @fields, $field;
264 $fields = join( ", ", @fields );
266 $sql = "CREATE TABLE $table ($fields)"; #. " MAX_ROWS = 100000000";
268 print STDERR "$sql\n" if $verbose;
270 Maasha::SQL::request( $dbh, $sql );
274 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
279 Maasha::Biopieces::status_set();
285 Maasha::SQL::disconnect( $dbh ) if $dbh;
286 Maasha::Biopieces::status_log();
290 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<