2
by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1. |
1 |
# This program is copyright 2009-2011 Percona Inc.
|
2 |
# Feedback and improvements are welcome.
|
|
3 |
#
|
|
4 |
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
|
|
5 |
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
|
|
6 |
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
7 |
#
|
|
8 |
# This program is free software; you can redistribute it and/or modify it under
|
|
9 |
# the terms of the GNU General Public License as published by the Free Software
|
|
10 |
# Foundation, version 2; OR the Perl Artistic License. On UNIX and similar
|
|
11 |
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
|
|
12 |
# licenses.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU General Public License along with
|
|
15 |
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
16 |
# Place, Suite 330, Boston, MA 02111-1307 USA.
|
|
17 |
# ###########################################################################
|
|
18
by Daniel Nichter
Remove $Revision$ and finish re-branding modules. Rename MaatkitTest.pm to PerconaTest.pm. Put copyrights on one line. |
18 |
# Outfile package
|
2
by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1. |
19 |
# ###########################################################################
|
9
by Daniel Nichter
Move module docu to work for NaturalDocs. |
20 |
{
|
4
by Daniel Nichter
Enclose all packages blocks and add Package devel docu. |
21 |
# Package: Outfile
|
22 |
# Outfile writes rows to a file in SELECT INTO OUTFILE format.
|
|
2
by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1. |
23 |
package Outfile; |
24 |
||
25 |
use strict; |
|
26 |
use warnings FATAL => 'all'; |
|
27 |
use English qw(-no_match_vars); |
|
134
by Daniel Nichter
Replace MKDEBUG with PTDEBUG in modules. |
28 |
use constant PTDEBUG => $ENV{PTDEBUG} || 0; |
2
by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1. |
29 |
|
30 |
sub new { |
|
31 |
my ( $class, %args ) = @_; |
|
32 |
my $self = {}; |
|
33 |
return bless $self, $class; |
|
34 |
}
|
|
35 |
||
36 |
# Print out in SELECT INTO OUTFILE format.
|
|
37 |
# $rows is an arrayref from DBI::selectall_arrayref().
|
|
38 |
sub write { |
|
39 |
my ( $self, $fh, $rows ) = @_; |
|
40 |
foreach my $row ( @$rows ) { |
|
41 |
print $fh escape($row), "\n" |
|
42 |
or die "Cannot write to outfile: $OS_ERROR\n"; |
|
43 |
}
|
|
44 |
return; |
|
45 |
}
|
|
46 |
||
47 |
# Formats a row the same way SELECT INTO OUTFILE does by default. This is
|
|
48 |
# described in the LOAD DATA INFILE section of the MySQL manual,
|
|
49 |
# http://dev.mysql.com/doc/refman/5.0/en/load-data.html
|
|
50 |
sub escape { |
|
51 |
my ( $row ) = @_; |
|
52 |
return join("\t", map { |
|
53 |
s/([\t\n\\])/\\$1/g if defined $_; # Escape tabs etc |
|
54 |
defined $_ ? $_ : '\N'; # NULL = \N |
|
55 |
} @$row); |
|
56 |
}
|
|
57 |
||
58 |
sub _d { |
|
59 |
my ($package, undef, $line) = caller 0; |
|
60 |
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } |
|
61 |
map { defined $_ ? $_ : 'undef' } |
|
62 |
@_; |
|
63 |
print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; |
|
64 |
}
|
|
65 |
||
66 |
1; |
|
4
by Daniel Nichter
Enclose all packages blocks and add Package devel docu. |
67 |
}
|
2
by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1. |
68 |
# ###########################################################################
|
69 |
# End Outfile package
|
|
70 |
# ###########################################################################
|