2 # Generates a csv with the table generated by the execution of an arbitrary sql
3 # The sql can be an argument of the program, if no argument is supplied then it
4 # reads its sql from stdin.
16 my $logger = The00z::Logger->new('/dev/stderr');
17 my $helper = The00z::Helper->new
19 '<header>' => "$0: [SQL]\r This program use an arbitrary sql to execute on"
20 . "the db, fetch a table and display it as a csv (comma separate value),"
21 . "yea I know that the query `EXPORT' can do this job but I never"
22 . "managed to get it work.\r if the sql if not provided as an argument"
23 . "then it is readed from stdin",
24 '-h -help' =>'Shows help and exit',
25 '-u --db_user' => "Specify the db_user, if this option is not "
26 . "specified then the environment variable DB_NAME is read for fetch "
27 . 'its value, if it does not exist then the program exit with a db error.',
28 '-p --db_passwd' =>"Specify the db_passwd, if this option is not "
29 . "specified then the environment variable DB_PASSWD is read for fetch "
30 . 'its value, if it does not exist then the program exit with a db error.',
31 '-n --db_name' => "Specify the db_name, if this option is not "
32 . "specified then the environment variable DB_NAME is read for fetch "
33 . 'its value, if it does not exist then the program exit with a db error.',
34 '-e --db_engine' => "Selects the DB engine to use: MySQL or Sybase. Default: mysql",
35 '-d --db_debug' => "Shows debug information, each time you use this "
36 . "option the verbosity level increase by one showing more information.",
39 my %db_data = ( 'db_user' => undef, 'db_passwd' => undef, 'db_name' => undef
40 , 'db_engine' => undef, 'db_server' => undef);
44 my $server = "localhost";
46 GetOptions ('help|h' => sub { $helper->show;exit 1; }
47 , 'db_name|n=s' => \$db_data{'db_name'}
48 , 'db_user|u=s' => \$db_data{'db_user'}
49 , 'db_passwd|p=s' => \$db_data{'db_passwd'}
50 , 'db_engine|e=s' => \$db_data{'db_engine'}
51 , 'db_server|s=s' => \$db_data{'db_server'}
52 , 'debug|d+' => \$loglevel
55 $logger->level($loglevel);
56 $logger->write(1, "Entering debugging mode (". $logger->level(). ")\n");
58 @db_data{sort keys %db_data}
59 = map { ( ! defined $db_data{$_} and $ENV{uc()} ) ? $ENV{uc()} : undef}
62 croak "ERROR: I need a db_name" unless $db_data{db_name};
63 croak "ERROR: I need a db_user!!" unless $db_data{db_user};
64 croak "ERROR: I need a db_passwd!!" unless $db_data{db_passwd};
68 $db_data{'db_engine'} = 'mysql' unless $db_data{'db_engine'};
69 $db_data{'db_server'} = 'localhost' unless $db_data{'db_server'};
71 if ($db_data{'db_engine'} =~ /mysql/i)
73 $dsn.="mysql:host=$db_data{db_server}";
74 } elsif ($db_data{'db_engine'} =~ /sybase/i) {
75 $dsn.="Sybase:server=$db_data{db_server}";
77 croak "Engine unknown!: $db_data{db_engine}\n";
81 # if there are no args then read sql from stdin
86 $_ =~ s/^[[:space:]]*/ /g;
93 croak 'Error no sql' unless $sql;
95 $logger->write(1, join(' : ' ,
96 map { "$_ => " . ($db_data{$_} ? $db_data{$_} : undef) }
97 keys %db_data), "\n");
100 $dsn.=";database=$db_data{db_name}";
102 my $dbh = DBI->connect($dsn, "$db_data{db_user}", "$db_data{db_passwd}"
103 , {PrintError => 0, RaiseError => 1}) or croak "ERROR: $!";
105 $logger->write(1, "Executing `$sql'\n");
107 my $sth = $dbh->prepare($sql);
111 print join (',', map { $_ ? $_ : ''} @$row), "\n"
112 while ($row = $sth->fetchrow_arrayref());