| 1 | ########################################################################### | ||
| 2 | # | ||
| 3 | # $Id: CSVDumper.pm,v 1.4 2006/09/17 13:43:52 pkaluski Exp $ | ||
| 4 | # $Name: Stable_0_16 $ | ||
| 5 | # | ||
| 6 | # This is a tool for dumping select results to csv files. | ||
| 7 | # | ||
| 8 | # $Log: CSVDumper.pm,v $ | ||
| 9 | # Revision 1.4 2006/09/17 13:43:52 pkaluski | ||
| 10 | # Refinement of POD. Added ability to read connection file givenin environment variable | ||
| 11 | # | ||
| 12 | # Revision 1.3 2006/09/10 18:16:12 pkaluski | ||
| 13 | # Added chunking. Selects are read from files or standard input (like in diamond operator. | ||
| 14 | # | ||
| 15 | # Revision 1.2 2006/02/10 22:32:16 pkaluski | ||
| 16 | # Major redesign in progress. Updated POD. Works. | ||
| 17 | # | ||
| 18 | # Revision 1.1 2006/01/01 13:13:49 pkaluski | ||
| 19 | # Initial revision. Works. | ||
| 20 | # | ||
| 21 | # | ||
| 22 | ########################################################################### | ||
| 23 | package LRpt::CSVDumper; | ||
| 24 | use Getopt::Long; | ||
| 25 | use DBI; | ||
| 26 | use LRpt::JarReader; | ||
| 27 | use LRpt::Config; | ||
| 28 | use LRpt::Collection; | ||
| 29 | use strict; | ||
| 30 | |||
| 31 | use vars qw( @EXPORT @ISA ); | ||
| 32 | @ISA = qw( Exporter ); | ||
| 33 | @EXPORT = qw( dump_selects ); | ||
| 34 | |||
| 35 | |||
| 36 | # | ||
| 37 | # I reference to an opened DBI connection | ||
| 38 | # | ||
| 39 | our $dbh_opened = ""; | ||
| 40 | |||
| 41 | # | ||
| 42 | # Initialization data for LRpt::JarReader | ||
| 43 | # | ||
| 44 | our @sel_rules = ( { 'name' => 'name' }, | ||
| 45 | { 'name' => 'select' } ); | ||
| 46 | |||
| 47 | =head1 NAME | ||
| 48 | |||
| 49 | LRpt::CSVDumper - LReport csv dumper. Dumps results of selects to csv files. | ||
| 50 | |||
| 51 | =head1 SYNOPSIS | ||
| 52 | |||
| 53 | lcsvdmp.pl selects.txt | ||
| 54 | |||
| 55 | lcsvdmp.pl --ext=dat --path=data --sep=";" --conn_file=connection.txt --chunk_size=num selects.txt | ||
| 56 | |||
| 57 | =head1 DESCRIPTION | ||
| 58 | |||
| 59 | This module is a part of L<C<LRpt>|LRpt> (B<LReport>) library. | ||
| 60 | You should not use C<LRpt::CSVDumper> module directly in your code. | ||
| 61 | Instead you should use B<lcsvdmp.pl> tool, which is a simple wrapper | ||
| 62 | around the module. B<lcsvdmp.pl> looks like this: | ||
| 63 | |||
| 64 | use strict; | ||
| 65 | use LRpt::CSVDumper; | ||
| 66 | |||
| 67 | dump_selects( @ARGV ); | ||
| 68 | |||
| 69 | |||
| 70 | B<lcsvdmp.pl> is a program for dumping results of a group of selects | ||
| 71 | to a group of csv files. Each select has its own file. Each file contains | ||
| 72 | a header row, which contains names of all columns returned | ||
| 73 | by a select and then all rows with the data. | ||
| 74 | It connects to a database via connection file, | ||
| 75 | which is expected to contain a perl code opening a connection to a database. | ||
| 76 | See section L</DATABASE CONNECTION FILE> for details. | ||
| 77 | |||
| 78 | Selects to be executed can be given either on standard input or in a file. | ||
| 79 | See L</SELECTS DEFINITION> for details. | ||
| 80 | |||
| 81 | =head1 COMMAND LINE OPTIONS | ||
| 82 | |||
| 83 | =over 4 | ||
| 84 | |||
| 85 | =item C<--ext> | ||
| 86 | |||
| 87 | Extension of csv files created. Default is "txt" | ||
| 88 | |||
| 89 | =item C<--path> | ||
| 90 | |||
| 91 | Path in which csv files are to be created. Default is a current directory | ||
| 92 | |||
| 93 | =item C<--sep> | ||
| 94 | |||
| 95 | Fields separator in csv files. Default is tab | ||
| 96 | |||
| 97 | =item C<--conn_file> | ||
| 98 | |||
| 99 | Path to a database connection file. Default is C<conn_file.txt>. | ||
| 100 | |||
| 101 | =item C<--help> | ||
| 102 | |||
| 103 | Prints help screen | ||
| 104 | |||
| 105 | =item C<--chunk_size> | ||
| 106 | |||
| 107 | Number of rows retrieved in one chunk. If not defined - default chunking size | ||
| 108 | is used. If equal to zero, than no chunking is used (all rows are loaded | ||
| 109 | to memory | ||
| 110 | |||
| 111 | =item C<selects.txt> | ||
| 112 | |||
| 113 | File with selects to be executed. | ||
| 114 | |||
| 115 | =back | ||
| 116 | |||
| 117 | =head1 METHODS | ||
| 118 | |||
| 119 | In this sections you will find a more or less complete listing of all | ||
| 120 | methods provided by the package. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | ######################################################################## | ||
| 125 | |||
| 126 | =head2 C<dump_selects> | ||
| 127 | |||
| 128 | dump_selects( @ARGV ); | ||
| 129 | |||
| 130 | Equivalent of a 'main' function. For meaning of parameters see | ||
| 131 | L<COMMAND LINE OPTIONS|"COMMAND LINE OPTIONS">. | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | ######################################################################## | ||
| 136 | sub dump_selects | ||
| 137 | { | ||
| 138 | local ( @ARGV ) = @_; | ||
| 139 | set_params(); | ||
| 140 | run_selects(); | ||
| 141 | } | ||
| 142 | |||
| 143 | ######################################################################## | ||
| 144 | |||
| 145 | =head2 C<set_params> | ||
| 146 | |||
| 147 | set_params(); | ||
| 148 | |||
| 149 | Parses command line parameters, checks environmental variables to gather | ||
| 150 | all parameters needed to process. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | ######################################################################## | ||
| 155 | sub set_params | ||
| 156 | { | ||
| 157 | my $ext = ""; | ||
| 158 | my $path = ""; | ||
| 159 | my $sep = ""; | ||
| 160 | my $conn_file = ""; | ||
| 161 | my $help = ""; | ||
| 162 | my $chunk_size = undef; | ||
| 163 | |||
| 164 | GetOptions( "ext=s" => \$ext, | ||
| 165 | "path=s" => \$path, | ||
| 166 | "sep=s" => \$sep, | ||
| 167 | "conn_file=s" => \$conn_file, | ||
| 168 | "chunk_size=s" => \$chunk_size, | ||
| 169 | "help" => \$help ); | ||
| 170 | |||
| 171 | if( $help ){ | ||
| 172 | print_usage(); | ||
| 173 | } | ||
| 174 | my $config = LRpt::Config->new( 'ext' => $ext, | ||
| 175 | 'path' => $path, | ||
| 176 | 'sep' => $sep, | ||
| 177 | 'conn_file' => $conn_file, | ||
| 178 | 'chunk_size' => $chunk_size ); | ||
| 179 | open_db_connection(); | ||
| 180 | } | ||
| 181 | |||
| 182 | ######################################################################## | ||
| 183 | |||
| 184 | =head2 C<open_db_connection> | ||
| 185 | |||
| 186 | open_db_connection() | ||
| 187 | |||
| 188 | Opens a database connection. Loads a connection file and evals it. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | ######################################################################## | ||
| 193 | sub open_db_connection | ||
| 194 | { | ||
| 195 | my $config = LRpt::Config->new(); | ||
| 196 | my $conn_file = "conn_file.txt"; | ||
| 197 | if( not -e $conn_file ){ | ||
| 198 | $conn_file = $config->get_value( 'conn_file' ); | ||
| 199 | } | ||
| 200 | my $dbh = ""; | ||
| 201 | open( CONFILE, "< " . $conn_file ) or | ||
| 202 | die "Cannot open $conn_file : $!"; | ||
| 203 | my @lines = <CONFILE>; | ||
| 204 | my $conn_str = join( "", @lines ); | ||
| 205 | eval $conn_str; | ||
| 206 | if( $@ ){ | ||
| 207 | die "$@"; | ||
| 208 | } | ||
| 209 | $dbh_opened = $dbh; | ||
| 210 | close( CONFILE ) or | ||
| 211 | die "Cannot close $conn_file : $!"; | ||
| 212 | } | ||
| 213 | |||
| 214 | ######################################################################## | ||
| 215 | |||
| 216 | =head2 C<run_selects> | ||
| 217 | |||
| 218 | run_selects(); | ||
| 219 | |||
| 220 | Run each select and dumps data to output file for each of them. | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | ######################################################################## | ||
| 225 | sub run_selects | ||
| 226 | { | ||
| 227 | my $path = shift; | ||
| 228 | |||
| 229 | my $config = LRpt::Config->new(); | ||
| 230 | my $chunk_size = $config->get_value( 'chunk_size' ); | ||
| 231 | |||
| 232 | if( !$path ){ | ||
| 233 | $path = $config->get_value( 'path' ); | ||
| 234 | } | ||
| 235 | my $ext = $config->get_value( 'ext' ); | ||
| 236 | my $sep = $config->get_value( 'sep' ); | ||
| 237 | |||
| 238 | unshift(@ARGV, '-') unless @ARGV; | ||
| 239 | while( my $file = shift( @ARGV ) ){ | ||
| 240 | open( SEL_FILE, "< $file" ) or die "Cannot open $file for reading: $!"; | ||
| 241 | my $select_jr = LRpt::JarReader->new( 'rules' => \@sel_rules, | ||
| 242 | 'filehandle' => *SEL_FILE ); | ||
| 243 | |||
| 244 | $select_jr->read_all(); | ||
| 245 | |||
| 246 | my @sel_names = $select_jr->get_all_values_of( 'name' ); | ||
| 247 | foreach my $sel ( @sel_names ){ | ||
| 248 | my $section = $select_jr->get_section_with( 'name' => $sel ); | ||
| 249 | my $select = $section->{ 'select' }; | ||
| 250 | my $sth = ""; | ||
| 251 | eval{ | ||
| 252 | $sth = $dbh_opened->prepare( $select ); | ||
| 253 | $sth->execute(); | ||
| 254 | }; | ||
| 255 | if( $@ ){ | ||
| 256 | die "Database error while executing query '$select'\n" . | ||
| 257 | "$@\n"; | ||
| 258 | } | ||
| 259 | open( OUTFILE, ">$path/$sel.$ext" ) or | ||
| 260 | die "Cannot open $path/$sel.$ext : $!"; | ||
| 261 | my $rows = []; | ||
| 262 | print OUTFILE "" . join( $sep, @{ $sth->{ 'NAME' } } ) . "\n"; | ||
| 263 | while( my $row = | ||
| 264 | ( | ||
| 265 | shift( @$rows ) || # get row from cache, or reload cache: | ||
| 266 | shift( @{ $rows = $sth->fetchall_arrayref(undef, $chunk_size) || | ||
| 267 | [] } | ||
| 268 | ) | ||
| 269 | ) | ||
| 270 | ) | ||
| 271 | { | ||
| 272 | print OUTFILE "" . join( $sep, @$row ) . "\n"; | ||
| 273 | } | ||
| 274 | close( OUTFILE ) or die "Cannot close $path/$sel.$ext : $!"; | ||
| 275 | print "name: $sel\n"; | ||
| 276 | print "select: $section->{ 'select' }\n"; | ||
| 277 | print "results: " . $config->get_value( 'path' ) . "/" . | ||
| 278 | $sel . "." . $config->get_value( 'ext' ) . "\n"; | ||
| 279 | print "%%\n"; | ||
| 280 | } | ||
| 281 | close( SEL_FILE ) or die "Cannot close $file : $!"; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | ######################################################################## | ||
| 286 | |||
| 287 | =head2 C<print_usage> | ||
| 288 | |||
| 289 | print_usage(); | ||
| 290 | |||
| 291 | Prints usage text | ||
| 292 | |||
| 293 | =cut | ||
| 294 | |||
| 295 | ########################################################################### | ||
| 296 | sub print_usage | ||
| 297 | { | ||
| 298 | print "Usage: $0 [--help] [--ext=<name>] [--path=<name>]"; | ||
| 299 | print " [--sep=<name>] [--conn_file=<name>] [--chunk_size=size]"; | ||
| 300 | print " filenames\n"; | ||
| 301 | print "\n"; | ||
| 302 | print " --conn_file - name of a database connection file.\n"; | ||
| 303 | print " Default is conn_file.txt\n"; | ||
| 304 | print " --ext - extension given to csv files\n"; | ||
| 305 | print " Default is txt\n"; | ||
| 306 | print " --help - prints this help screen\n"; | ||
| 307 | print " --path - directory in which csv files should be "; | ||
| 308 | print "created.\n"; | ||
| 309 | print " Default is a current directory\n"; | ||
| 310 | print " --sep - Field separator in generated csv files.\n"; | ||
| 311 | print " Default is tab\n"; | ||
| 312 | print " --chunk_size - Number of rows retrieved in one chunk. \n"; | ||
| 313 | print " If not defined - default chunking size is \n"; | ||
| 314 | print " used. If equal to zero, than no chunking is\n"; | ||
| 315 | print " used (all rows are loaded to memory)\n"; | ||
| 316 | print " filenames - Name of files with selects to be executed.\n"; | ||
| 317 | print " If non is given, standard input is used\n"; | ||
| 318 | exit( 0 ); | ||
| 319 | } | ||
| 320 | |||
| 321 | |||
| 322 | =head1 DATABASE CONNECTION FILE | ||
| 323 | |||
| 324 | A concept of a database connection file is supposed to provide tool's | ||
| 325 | openness for unlimited number of database drivers. Instead of trying | ||
| 326 | to predict several types of driver initialisation, it is expected, that | ||
| 327 | a user will provide a code which opens a database connection for a given | ||
| 328 | driver. This code will be then evaled by C<lcsvdmp.pl>. | ||
| 329 | |||
| 330 | The only thing, which is expected from this code snippet is that it assigns | ||
| 331 | a reference to an opened database connection to a variable named C<$dbh>. | ||
| 332 | Do not declare it with C<my>! | ||
| 333 | |||
| 334 | Example of the file: | ||
| 335 | |||
| 336 | $dbh = DBI->connect( "DBI:CSV:csv_sep_char=\t;f_dir=datafile/db", | ||
| 337 | { RaiseError => 1 }); | ||
| 338 | |||
| 339 | This is a simple case of connecting to database build from csv files. | ||
| 340 | |||
| 341 | But you can use more complex code. The example below, not only opens | ||
| 342 | a connection but also configures ODBC driver: | ||
| 343 | |||
| 344 | use Win32::ODBC; | ||
| 345 | my $DBName = "mydb"; | ||
| 346 | my $DBServer = "myserver"; | ||
| 347 | my $DBUser = "pkaluski"; | ||
| 348 | my $DBPassword = "password"; | ||
| 349 | |||
| 350 | no strict; | ||
| 351 | Win32::ODBC::ConfigDSN( ODBC_CONFIG_DSN, | ||
| 352 | "Sybase ASE ODBC Driver", | ||
| 353 | "DSN=mydriver", | ||
| 354 | "Database=$DBName", | ||
| 355 | "InterfacesFileServerName=$DBServer"); | ||
| 356 | use strict; | ||
| 357 | my $error = Win32::ODBC::Error(); | ||
| 358 | if( $error ){ | ||
| 359 | die $error; | ||
| 360 | } | ||
| 361 | $dbh = DBI->connect( "DBI:ODBC:BolekSybase", $DBUser, $DBPassword, | ||
| 362 | {RaiseError => 1, AutoCommit => 1}); | ||
| 363 | |||
| 364 | |||
| 365 | =head1 SELECTS DEFINITION | ||
| 366 | |||
| 367 | Selects to be executed can be given either on standard input or in a file | ||
| 368 | given in command line. | ||
| 369 | |||
| 370 | C<lcsvdmp.pl> expects the following format: | ||
| 371 | |||
| 372 | name: myselect1 | ||
| 373 | select: select * from CUSTOMERS where id = 123 | ||
| 374 | %% | ||
| 375 | name: myselect2 | ||
| 376 | select: select * from INVOICES where customer_id = 123 | ||
| 377 | %% | ||
| 378 | ... | ||
| 379 | ... | ||
| 380 | |||
| 381 | So it is basically a jar record format. For select C<myselect1> results will | ||
| 382 | be saved in C<myselect1.txt> file (unless default extension is not | ||
| 383 | overridden), C<myselect2> in C<myselect2.txt>. | ||
| 384 | |||
| 385 | If select's names are not given, C<lcsvdmp.pl> assigns them names following | ||
| 386 | the pattern C<selectN>, where C<N> is an integer, starting from 0. | ||
| 387 | |||
| 388 | =head1 ENVIRONMENT | ||
| 389 | |||
| 390 | On launch, C<lcsvdmp.pl> looks for the following environmental variables | ||
| 391 | in order to override defaults (in case according command line option | ||
| 392 | is not given): | ||
| 393 | |||
| 394 | =over 4 | ||
| 395 | |||
| 396 | =item C<LRPT_CSV_FILE_EXT> | ||
| 397 | |||
| 398 | Extension of created csv files | ||
| 399 | |||
| 400 | =item C<LRPT_CSV_FILE_PATH> | ||
| 401 | |||
| 402 | Path, in which csv files should be created | ||
| 403 | |||
| 404 | =item C<LRPT_CSV_FIELD_SEPARATOR> | ||
| 405 | |||
| 406 | Field separator in csv files | ||
| 407 | |||
| 408 | =item C<LRPT_CSV_CONNECTION_FILE> | ||
| 409 | |||
| 410 | Path to a database connection file. | ||
| 411 | |||
| 412 | =back | ||
| 413 | |||
| 414 | =head1 AUTHORS | ||
| 415 | |||
| 416 | Piotr Kaluski E<lt>pkaluski@piotrkaluski.comE<gt> | ||
| 417 | |||
| 418 | =head1 COPYRIGHT | ||
| 419 | |||
| 420 | Copyright (c) 2004-2006 Piotr Kaluski. Poland. All rights reserved. | ||
| 421 | |||
| 422 | You may distribute under the terms of either the GNU General Public License | ||
| 423 | or the Artistic License, as specified in the Perl README file. | ||
| 424 | |||
| 425 | =cut | ||
| 426 | |||
| 427 | |||
| 428 | |||
| 429 | 1; | ||
| 430 | |||
| 431 |