| 1 | #ABSTRACT: some function on mysql, base DBI | ||
| 2 | package SimpleDBI::mysql; | ||
| 3 | use base qw/SimpleDBI/; | ||
| 4 | use DBD::mysql; | ||
| 5 | use DBI; | ||
| 6 | use File::Temp qw/tempfile/; | ||
| 7 | use SimpleR::Reshape; | ||
| 8 | |||
| 9 | #sub new { | ||
| 10 | # my ($self, %opt) = @_; | ||
| 11 | # | ||
| 12 | # my $dbh = connect_db(%opt); | ||
| 13 | # | ||
| 14 | # bless { %opt, dbh => $dbh }, __PACKAGE__; | ||
| 15 | #} | ||
| 16 | |||
| 17 | sub connect_db { | ||
| 18 | my ($self, %opt) = @_; | ||
| 19 | |||
| 20 | my %default_opt = ( | ||
| 21 | mysql_local_infile => 1, | ||
| 22 | mysql_connect_timeout => 14400, | ||
| 23 | port => $opt{port} || 3306, | ||
| 24 | host => $opt{host}, | ||
| 25 | database => $opt{db}, | ||
| 26 | ); | ||
| 27 | my $conn_str = | ||
| 28 | join( ";", map { "$_=$default_opt{$_}" } keys(%default_opt) ); | ||
| 29 | |||
| 30 | my $dbh = DBI->connect( "DBI:mysql:$conn_str", $opt{usr}, $opt{passwd}, | ||
| 31 | { 'RaiseError' => 0, PrintError => 1, mysql_enable_utf8=> $opt{enable_utf8} , } ), | ||
| 32 | or die $DBI::errstr; | ||
| 33 | |||
| 34 | return $dbh; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub query_db { | ||
| 38 | my ( $self, $sql, %opt ) = @_; | ||
| 39 | $opt{attr} ||= undef, | ||
| 40 | $opt{bind_values} ||= []; | ||
| 41 | $opt{result_type} ||= | ||
| 42 | $opt{hash_key} ? 'hashref' | ||
| 43 | : $opt{file} ? 'file' | ||
| 44 | : 'arrayref'; | ||
| 45 | |||
| 46 | my $sth = $self->{dbh}->prepare( $sql, $opt{attr} ); | ||
| 47 | $sth->execute( @{ $opt{bind_values} } ); | ||
| 48 | |||
| 49 | my $header = $sth->{NAME}; | ||
| 50 | |||
| 51 | if ( $opt{result_type} eq 'arrayref' ){ | ||
| 52 | my $data = $sth->fetchall_arrayref ; | ||
| 53 | unshift @$data, $header if($opt{write_head}); | ||
| 54 | return $data; | ||
| 55 | } | ||
| 56 | |||
| 57 | if ( $opt{result_type} eq 'hashref' ) { | ||
| 58 | $opt{hash_key} ||= []; | ||
| 59 | return $sth->fetchall_hashref( @{ $opt{hash_key} } ); | ||
| 60 | } | ||
| 61 | |||
| 62 | if ( $opt{result_type} eq 'file' ) { | ||
| 63 | open my $fh, ">:utf8", $opt{file}; | ||
| 64 | if($opt{write_head}){ | ||
| 65 | print $fh join( $opt{sep}, @$header ), "\n"; | ||
| 66 | } | ||
| 67 | while ( my @row = $sth->fetchrow_array ) { | ||
| 68 | print $fh join( $opt{sep}, @row ), "\n"; | ||
| 69 | } | ||
| 70 | close $fh; | ||
| 71 | return $opt{file}; | ||
| 72 | } | ||
| 73 | |||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub load_table { | ||
| 78 | my ( $self, $data, %opt ) = @_; | ||
| 79 | $opt{sep} ||= $DEFAULT_SEP; | ||
| 80 | $opt{db} ||= $self->{db}; | ||
| 81 | $opt{charset} ||= $self->{charset}; | ||
| 82 | |||
| 83 | my ( $file, $temp_fh ) = ( $data, undef ); | ||
| 84 | if ( !-f $data ) { | ||
| 85 | ( $temp_fh, $file ) = tempfile( 'tmpXXXXXXXXXXXXX', TMPDIR => 1 ); | ||
| 86 | write_table( | ||
| 87 | $data, | ||
| 88 | sep => $opt{sep}, | ||
| 89 | file => $file, | ||
| 90 | charset => $opt{charset}, | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | |||
| 94 | my $replace_flag = $opt{replace} ? 'REPLACE' : ''; | ||
| 95 | my $set_charset = | ||
| 96 | ( $opt{charset} eq 'utf8' ) ? 'character set UTF8' : ''; | ||
| 97 | |||
| 98 | if(!$opt{field}){ | ||
| 99 | open my $fh, "<:$opt{charset}", $file; | ||
| 100 | my $s =<$fh>; | ||
| 101 | close $fh; | ||
| 102 | $s=~s/^\s+|\s+$//sg; | ||
| 103 | $opt{field} = [ split $opt{sep}, $s ]; | ||
| 104 | $opt{skip_head} = 1; | ||
| 105 | } | ||
| 106 | my $field_str = join( ", ", @{ $opt{field} } ); | ||
| 107 | my $ignore_str = $opt{skip_head} ? 'IGNORE 1 LINES' : ''; | ||
| 108 | |||
| 109 | my $file_s = quotemeta($file); | ||
| 110 | my $load_sql = qq[load data local infile '$file_s' | ||
| 111 | $replace_flag into table $opt{db}.$opt{table} | ||
| 112 | $set_charset | ||
| 113 | fields terminated by '$opt{sep}' | ||
| 114 | lines terminated by '\\n' | ||
| 115 | $ignore_str | ||
| 116 | ($field_str) | ||
| 117 | ;]; | ||
| 118 | |||
| 119 | $self->{dbh}->do($load_sql); | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; |