Debian Codegraph @

scripts/dbs_printtab

raw
1 #! /usr/bin/env perl
2
3 # dbs_printtab - creates file set with SQL table data
4
5 # This script queries a SQL database and creates one file for each
6 # table in the database containing the data of the table.
7
8 # Copyright (C) 2000-2012 Stefan Hornburg
9
10 # Author: Stefan Hornburg (Racke) <[email protected]>
11 # Maintainer: Stefan Hornburg (Racke) <[email protected]>
12 # Version: 0.19
13
14 # This file is free software; you can redistribute it and/or modify it
15 # under the terms of the GNU General Public License as published by the
16 # Free Software Foundation; either version 2, or (at your option) any
17 # later version.
18
19 # This file is distributed in the hope that it will be
20 # useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
23
24 # You should have received a copy of the GNU General Public License
25 # along with this file; see the file COPYING. If not, write to the Free
26 # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27
28 use strict;
29 use warnings;
30
31 use DBIx::Easy;
32 use Getopt::Long;
33 use Term::ReadKey;
34
35 # process commandline parameters
36 my %opts;
37 my $whandler = $SIG{__WARN__};
38 $SIG{__WARN__} = sub {print STDERR "$0: @_";};
39 unless (GetOptions (\%opts, 'columns|c=s',
40 'sort=s')) {
41 exit 1;
42 }
43 $SIG{__WARN__} = $whandler;
44
45 my $USAGE = <<EOT;
46 Usage: $0 DRIVER DATABASE TABLE [USER]
47 EOT
48
49 # sanity check on commandline parameters
50 if (@ARGV < 3) {
51 warn ($USAGE);
52 exit 1;
53 }
54
55 my $dbif;
56 my $pwdused = 0;
57
58 my ($driver, $database, $table, $user) = @ARGV;
59 $dbif = new DBIx::Easy ($driver, $database, $user);
60
61 # handler for DBI error messages and missing password
62 $dbif -> install_handler (\&fatal);
63
64 # we need to explicitly establish the connection
65 # for the case that a password is needed
66 $dbif -> connect;
67
68 # query records in table
69 print $dbif -> view ($table, columns => $opts{'columns'}, order => $opts{'sort'});
70
71 # Destroy database object as written in manpage
72 undef $dbif;
73
74 # -----------------------------------
75 # FUNCTION: fatal
76 #
77 # Error handler called by DBIx::Easy.
78 # -----------------------------------
79
80 sub fatal {
81 my ($statement, $err, $msg) = @_;
82 my $pwd;
83
84 if ($dbif->is_auth_error ($err)) {
85 unless ($pwdused) {
86 print "We need a password.\n";
87 $pwd = querypwd();
88 $pwdused = 1;
89
90 # retry the connection
91 if (length ($pwd)) {
92 $dbif = new DBIx::Easy ($driver, $database, $user, $pwd);
93 $dbif -> install_handler (\&fatal);
94 $dbif -> connect ();
95 return;
96 } else {
97 die ("$statement.\n");
98 }
99 }
100 }
101 die ("$statement.\n");
102 }
103
104 # ----------------------------
105 # FUNCTION: querypwd
106 #
107 # Queries user for a password.
108 # ----------------------------
109
110 sub querypwd () {
111 my $pwd;
112
113 print "Password: ";
114 ReadMode ('noecho'); # turn echo off
115 $pwd = ReadLine (0);
116 ReadMode ('restore'); # restore terminal
117 print "\n";
118 chomp ($pwd);
119 $pwd;
120 }
121
122 =head1 NAME
123
124 dbs_printtab - Prints SQL table data on standard output
125
126 =head1 DESCRIPTION
127
128 dbs_printtab is an utility to print SQL table data on
129 standard output. dbs_printtab asks for a password if necessary.
130
131 =head1 COMMAND LINE PARAMETERS
132
133 Required command line parameters are the DBI driver
134 (C<Pg> for Postgres or C<mysql> for MySQL), the database name
135 and the table name. The fourth parameter is optionally
136 and specifies the database user and/or the host where the
137 database resides (C<racke>, C<[email protected]> or C<@linuxia.de>).
138
139 =head1 COMMAND LINE OPTIONS
140
141 =head2 B<-c>=COLUMN[,COLUMN], B<--columns>=COLUMN[,COLUMN]
142
143 The output is restricted to the given COLUMN arguments.
144
145 =head2 B<--sort>=KEY[,KEY]
146
147 The output is sorted by the KEY arguments.
148
149 =head1 BUGS
150
151 msql is not fully supported.
152
153 =head1 AUTHOR
154
155 Stefan Hornburg (Racke), [email protected]
156
157 =head1 SEE ALSO
158
159 perl(1), DBIx::Easy(3)
160
161 =cut