| 1 | package Sash::Command; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Sash::CommandHash; | ||
| 6 | use Sash::Buffer; | ||
| 7 | use Sash::Table; | ||
| 8 | use Sash::Timer; | ||
| 9 | use Sash::ResultHistory; | ||
| 10 | use Sash::Properties; | ||
| 11 | |||
| 12 | use Carp; | ||
| 13 | |||
| 14 | use Data::Dumper; | ||
| 15 | |||
| 16 | # Singleton implementation of the Mediator Pattern | ||
| 17 | |||
| 18 | my $_valid = 0; | ||
| 19 | my $_use_timer = 0; | ||
| 20 | my $_writing_query; | ||
| 21 | my $_large_query_size = 5000; | ||
| 22 | |||
| 23 | tie my $_buffer, 'Sash::Buffer'; | ||
| 24 | |||
| 25 | sub writing_query { | ||
| 26 | my $class = shift; | ||
| 27 | return $_writing_query = ( shift || $_writing_query ); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub large_query_size { | ||
| 31 | my $class = shift; | ||
| 32 | return $_large_query_size = ( shift || $_large_query_size ); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub begin { | ||
| 36 | my $class = shift; | ||
| 37 | my $args = shift; #hash ref | ||
| 38 | |||
| 39 | # Reset whether to use the timer for the command in indicate we have been | ||
| 40 | # properly invoked | ||
| 41 | $_use_timer = 1; | ||
| 42 | $_valid = 1; | ||
| 43 | |||
| 44 | if ( defined $args->{no_timer} ) { | ||
| 45 | $_use_timer = 0; | ||
| 46 | } else { | ||
| 47 | Sash::Timer->start; | ||
| 48 | } | ||
| 49 | |||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub end { | ||
| 54 | my $class = shift; | ||
| 55 | my $args = shift; #hash ref | ||
| 56 | |||
| 57 | croak 'Improper class usage! Must call begin method.' unless $_valid; | ||
| 58 | |||
| 59 | Sash::Timer->stop if $_use_timer; | ||
| 60 | |||
| 61 | # Reset ourselves for the next invocation | ||
| 62 | $_valid = 0; | ||
| 63 | |||
| 64 | return $args->{result} if defined $args->{result} && Sash::Properties->output eq Sash::Properties->perlval; | ||
| 65 | |||
| 66 | # Plan for other independent operations | ||
| 67 | if ( defined $args->{result} ) { | ||
| 68 | croak 'Result not of type Sash::Table' unless ref $args->{result} eq 'Sash::Table'; | ||
| 69 | |||
| 70 | my $table = $args->{result}; | ||
| 71 | |||
| 72 | # We are responsible first for adding the result to the history... | ||
| 73 | Sash::ResultHistory->add( $table ); | ||
| 74 | |||
| 75 | # and then displaying the result back to the user. | ||
| 76 | $table->display( ( $_use_timer ) ? Sash::Timer->elapsed : undef ); | ||
| 77 | } | ||
| 78 | |||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub get_command_hash { | ||
| 83 | my $class = shift; | ||
| 84 | |||
| 85 | my $command_hash = Sash::CommandHash->new( 'Sash::Command' ); | ||
| 86 | |||
| 87 | return { | ||
| 88 | 'clear' => $command_hash->build( { use => 'clear' } ), | ||
| 89 | #'connect' => $command_hash->build( { use => 'connect' } ), | ||
| 90 | 'edit' => $command_hash->build( { use => 'edit_buffer', proc => 'meth' } ), | ||
| 91 | 'help' => $command_hash->build( { use => 'help', proc => 'meth' } ), | ||
| 92 | 'history' => $command_hash->build( { use => 'history', proc => 'meth' } ), | ||
| 93 | 'list' => $command_hash->build( { use => 'list' } ), | ||
| 94 | 'quit' => $command_hash->build( { use => 'quit', proc => 'meth' } ), | ||
| 95 | 'reconnect' => $command_hash->build( { use => 'reconnect' } ), | ||
| 96 | 'result' => { | ||
| 97 | 'cmds' => { | ||
| 98 | # 'head' => get_command_hash( { use => 'head' } ), | ||
| 99 | # 'tail' => get_command_hash( { use => 'tail' } ), | ||
| 100 | 'all' => $command_hash->build( { use => 'all' } ), | ||
| 101 | # Add args to this so you can say: result limit -s 100 100 | ||
| 102 | 'limit' => $command_hash->build( { use => 'limit' } ), | ||
| 103 | 'revert' => $command_hash->build( { use => 'revert' } ), | ||
| 104 | #'grep' => get_command_hash( { use => 'grep', proc => 'meth' } ), | ||
| 105 | 'search' => $command_hash->build( { use => 'search' } ), | ||
| 106 | 'sort' => $command_hash->build( { use => 'sort', proc => 'meth' } ), | ||
| 107 | 'undo' => $command_hash->build( { use => 'undo' } ), | ||
| 108 | 'redo' => $command_hash->build( { use => 'redo' } ), | ||
| 109 | }, | ||
| 110 | }, | ||
| 111 | 'set' => { | ||
| 112 | 'cmds' => { | ||
| 113 | 'output' => $command_hash->build( { use => 'set_output' } ), | ||
| 114 | } | ||
| 115 | }, | ||
| 116 | 'x' => $command_hash->build( { use => 'x' } ), | ||
| 117 | 'q' => { syn => "quit" }, | ||
| 118 | 'c' => { syn => 'clear' }, | ||
| 119 | 'e' => { syn => 'edit' }, | ||
| 120 | 'l' => { syn => 'list' }, | ||
| 121 | 'exit' => { syn => "quit" }, | ||
| 122 | }; | ||
| 123 | } | ||
| 124 | |||
| 125 | # Adapter for commands the need the elapsed time after they call end; | ||
| 126 | sub elapsed { | ||
| 127 | my $class = shift; | ||
| 128 | |||
| 129 | return Sash::Timer->elapsed; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub AUTOLOAD { | ||
| 133 | my $command_class = Sash::Plugin::Factory->get_plugin_command_class; | ||
| 134 | my @plugin_methods = Sash::Plugin::Factory->get_plugin_command_hash; | ||
| 135 | |||
| 136 | our $AUTOLOAD; | ||
| 137 | |||
| 138 | if ( $AUTOLOAD =~ /::(\w+)$/ && grep { $1 eq $_ } @plugin_methods ) { | ||
| 139 | my $method_to_invoke = "${1}_meth"; | ||
| 140 | $method_to_invoke = "${1}_proc" unless $command_class->can( $method_to_invoke ); | ||
| 141 | $command_class->$method_to_invoke( shift ); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | sub default_command { | ||
| 146 | my $class = shift; | ||
| 147 | my $term = shift; | ||
| 148 | my $args = shift; | ||
| 149 | |||
| 150 | if ( $_writing_query ) { | ||
| 151 | ( my $query = $args->{rawline} ) =~ s/;$//g; | ||
| 152 | |||
| 153 | # Remember this is tied :) | ||
| 154 | $_buffer .= $query; | ||
| 155 | |||
| 156 | if ( $args->{rawline} =~ /;$/ ) { | ||
| 157 | Sash::Command->execute_query( $term, $_buffer, $args->{cursor_class} ); | ||
| 158 | $_writing_query = 0; | ||
| 159 | $term->set_standard_prompt; | ||
| 160 | } | ||
| 161 | } else { | ||
| 162 | no strict; | ||
| 163 | |||
| 164 | # The following is done so that user's of sash can write scripts at the prompt | ||
| 165 | # and then copy them to their real destination | ||
| 166 | ( my $command = $args->{rawline} ) =~ s/\$client->//ig; | ||
| 167 | eval $command; | ||
| 168 | croak $@ if $@; | ||
| 169 | } | ||
| 170 | |||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub _getCallerArgs { | ||
| 175 | my $self = shift; | ||
| 176 | my $invoked_by = shift; | ||
| 177 | my $method_args = shift; | ||
| 178 | my $rawline_replace = shift; | ||
| 179 | |||
| 180 | my $caller_args; | ||
| 181 | |||
| 182 | # We get a different type of argument depending on who invoked us. There | ||
| 183 | # are 2 valid classes that can invoke us: | ||
| 184 | # | ||
| 185 | # 1) Sash::Terminal->call_cmd | ||
| 186 | # 2) Sash::Command->AUTOLOAD which makes it look like we got invoked by | ||
| 187 | # Sash::Plugin::VerticalResponse::Command->getXXX_meth | ||
| 188 | if ( $invoked_by->isa( 'Sash::Terminal' ) ) { | ||
| 189 | ( $caller_args = $method_args->{rawline} ) =~ s/$rawline_replace\s*(.*);?$/$1/; | ||
| 190 | } elsif ( $invoked_by->isa( 'Sash::Command' ) ) { | ||
| 191 | $caller_args = $method_args; | ||
| 192 | } else { | ||
| 193 | croak "Invalid Invocation"; | ||
| 194 | } | ||
| 195 | |||
| 196 | return $caller_args; | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | sub _bringVarIntoScope { | ||
| 201 | my $self = shift; | ||
| 202 | my $user_defined_var = shift; | ||
| 203 | |||
| 204 | return undef unless defined $user_defined_var; | ||
| 205 | |||
| 206 | my $var; | ||
| 207 | |||
| 208 | # We don't play nice with arrays and hashes, but do with their references. I | ||
| 209 | # don't think dollar amounts are going to turn out quite right either: | ||
| 210 | # | ||
| 211 | # push @nvpair = { name => 'purchase_amount', value => '$230.34' } | ||
| 212 | # | ||
| 213 | # That might produce an undesired result | ||
| 214 | eval { | ||
| 215 | if ( $user_defined_var =~ /\$\w*/ || $user_defined_var =~ /{.*}/ || $user_defined_var =~ /\[.*\]/ ) { | ||
| 216 | $user_defined_var =~ s/\$/\$Sash::Command::/g; | ||
| 217 | eval "\$var = $user_defined_var;"; | ||
| 218 | } else { | ||
| 219 | $var = $user_defined_var; | ||
| 220 | } | ||
| 221 | }; if ( $@ ) { | ||
| 222 | # No reason to die here because undef might be a valid value to the caller. | ||
| 223 | return undef; | ||
| 224 | } | ||
| 225 | |||
| 226 | return $var; | ||
| 227 | } | ||
| 228 | |||
| 229 | # ALL Command Methods | ||
| 230 | |||
| 231 | sub all_desc { return <<EOF } | ||
| 232 | Use this method to get all of the current results in the buffer. | ||
| 233 | EOF | ||
| 234 | |||
| 235 | sub all_doc { return <<EOF } | ||
| 236 | More to come! | ||
| 237 | EOF | ||
| 238 | |||
| 239 | sub all_proc { | ||
| 240 | my $i = 1; | ||
| 241 | foreach ( Sash::ResultHistory->all ) { | ||
| 242 | print $i++ . ")\n"; | ||
| 243 | $_->display; | ||
| 244 | } | ||
| 245 | |||
| 246 | return; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub x_desc { return <<EOF } | ||
| 250 | EOF | ||
| 251 | |||
| 252 | sub x_doc { return <<EOF } | ||
| 253 | EOF | ||
| 254 | |||
| 255 | sub x_proc { | ||
| 256 | my $args = shift; | ||
| 257 | |||
| 258 | my $var = __PACKAGE__->_bringVarIntoScope( $args ); | ||
| 259 | $var = [ $var ] unless ref $var eq 'ARRAY'; | ||
| 260 | |||
| 261 | $Data::Dumper::Indent = 1; | ||
| 262 | $Data::Dumper::Useqq = 1; | ||
| 263 | $Data::Dumper::Pair = ' => '; | ||
| 264 | |||
| 265 | eval "print Data::Dumper->Dump( \$var, [ qw( $args ) ] );"; | ||
| 266 | croak $@ if $@; | ||
| 267 | } | ||
| 268 | |||
| 269 | # CLEAR Command Methods. | ||
| 270 | |||
| 271 | sub clear_desc { return <<EOF } | ||
| 272 | EOF | ||
| 273 | |||
| 274 | sub clear_doc { return <<EOF } | ||
| 275 | More to come! | ||
| 276 | EOF | ||
| 277 | |||
| 278 | sub clear_proc { | ||
| 279 | # Remember this is a tied variable so explicitly set the contents to null. | ||
| 280 | # Undef would not be good here. | ||
| 281 | $_buffer = ''; | ||
| 282 | return; | ||
| 283 | } | ||
| 284 | |||
| 285 | # EDIT BUFFER Command Methods. | ||
| 286 | |||
| 287 | sub edit_buffer_desc { return <<EOF } | ||
| 288 | Use this command to edit the current query buffer. | ||
| 289 | EOF | ||
| 290 | |||
| 291 | sub edit_buffer_doc { return <<EOF } | ||
| 292 | More to come! | ||
| 293 | EOF | ||
| 294 | |||
| 295 | sub edit_buffer_meth { | ||
| 296 | my $term = shift; | ||
| 297 | |||
| 298 | Sash::Command->writing_query( 1 ); | ||
| 299 | |||
| 300 | # Thank You Tied Variables! | ||
| 301 | system( '$EDITOR ' . Sash::Buffer->filename ); | ||
| 302 | $_buffer =~ s/\s+$//; | ||
| 303 | print $_buffer; | ||
| 304 | |||
| 305 | $term->set_continue_prompt; | ||
| 306 | |||
| 307 | return; | ||
| 308 | } | ||
| 309 | |||
| 310 | # GREP Command Methods | ||
| 311 | |||
| 312 | sub grep_meth { | ||
| 313 | my $term = shift; | ||
| 314 | ( my $options, my $regex ) = ( my $args = shift->{rawline} ) =~ /grep *-([iv]*) +(.*);$/; | ||
| 315 | |||
| 316 | # Move into Sash::Table like i did with match_string and sort? | ||
| 317 | my $data = Sash::ResultHistory->current( )->rowRefs(); | ||
| 318 | my $grep_result = Sash::Table->new( $data, Sash::ResultHistory->current( )->{header} ); | ||
| 319 | $grep_result->match_string( $regex ); | ||
| 320 | |||
| 321 | Sash::Command->end( { result => $grep_result } ); | ||
| 322 | |||
| 323 | return; | ||
| 324 | } | ||
| 325 | |||
| 326 | # HELP Command Methods | ||
| 327 | |||
| 328 | sub help_desc { return <<EOF } | ||
| 329 | Sash Rules! | ||
| 330 | EOF | ||
| 331 | |||
| 332 | sub help_doc { return <<EOF } | ||
| 333 | More help to come! | ||
| 334 | EOF | ||
| 335 | |||
| 336 | sub help_args { | ||
| 337 | shift->help_args( undef, @_ ); | ||
| 338 | } | ||
| 339 | |||
| 340 | sub help_meth { | ||
| 341 | shift->help_call( undef, @_ ); | ||
| 342 | } | ||
| 343 | |||
| 344 | # HISTORY Command Methods | ||
| 345 | |||
| 346 | sub history_desc { return <<EOF } | ||
| 347 | Prints the command history" | ||
| 348 | EOF | ||
| 349 | |||
| 350 | sub history_doc { return <<EOF } | ||
| 351 | usage: history [cd] N | ||
| 352 | |||
| 353 | Specify a number to list the last N lines of history. Pass -c to clear | ||
| 354 | the command history, -d num to delete a single item. | ||
| 355 | EOF | ||
| 356 | |||
| 357 | sub history_args { | ||
| 358 | return "[-c] [-d] [number]"; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub history_meth { | ||
| 362 | shift->history_call( @_ ); | ||
| 363 | } | ||
| 364 | |||
| 365 | # LIMIT Command Methods | ||
| 366 | |||
| 367 | sub limit_desc { return <<EOF } | ||
| 368 | Use this command to reduce the viewable size of the current result set | ||
| 369 | EOF | ||
| 370 | |||
| 371 | sub limit_doc { return <<EOF } | ||
| 372 | usage: limit n | ||
| 373 | |||
| 374 | Reduce the viewable size of the current result to n rows. Use the revert command | ||
| 375 | to undo this operation on the result set. | ||
| 376 | EOF | ||
| 377 | |||
| 378 | sub limit_proc { | ||
| 379 | ( my $limit = shift ) =~ s/;$//; | ||
| 380 | |||
| 381 | Sash::Command->begin; | ||
| 382 | |||
| 383 | my $data = [ @{Sash::ResultHistory->current( )->rowRefs()}[0 .. $limit - 1] ]; | ||
| 384 | Sash::Command->end( { result => Sash::Table->new( $data, Sash::ResultHistory->current( )->{header} ) } ); | ||
| 385 | |||
| 386 | return; | ||
| 387 | } | ||
| 388 | |||
| 389 | # LIST Command Methods | ||
| 390 | |||
| 391 | sub list_desc { return <<EOF } | ||
| 392 | EOF | ||
| 393 | |||
| 394 | sub list_doc { return <<EOF } | ||
| 395 | More to come! | ||
| 396 | EOF | ||
| 397 | |||
| 398 | sub list_proc { | ||
| 399 | print $_buffer if $_buffer; | ||
| 400 | return; | ||
| 401 | } | ||
| 402 | |||
| 403 | # QUIT Command Methods | ||
| 404 | |||
| 405 | sub quit_desc { return <<EOF } | ||
| 406 | Quit the application | ||
| 407 | EOF | ||
| 408 | |||
| 409 | sub quit_doc { return <<EOF } | ||
| 410 | Did you really mean to type help on the quit command? Really? | ||
| 411 | EOF | ||
| 412 | |||
| 413 | sub quit_meth { | ||
| 414 | shift->exit_requested( 1 ); | ||
| 415 | } | ||
| 416 | |||
| 417 | # RECONNECT Command Methods | ||
| 418 | |||
| 419 | sub reconnect_desc { return <<EOF } | ||
| 420 | Use this method to reconnect to the datasource | ||
| 421 | EOF | ||
| 422 | |||
| 423 | sub reconnect_doc { return <<EOF } | ||
| 424 | More to come! | ||
| 425 | EOF | ||
| 426 | |||
| 427 | sub reconnect_proc { | ||
| 428 | my $plugin = Sash::Plugin::Factory->get_plugin; | ||
| 429 | |||
| 430 | $plugin->connect( { | ||
| 431 | username => $plugin->username, | ||
| 432 | password => $plugin->password, | ||
| 433 | endpoint => $plugin->endpoint, | ||
| 434 | } ); | ||
| 435 | |||
| 436 | return; | ||
| 437 | } | ||
| 438 | |||
| 439 | # REDO Command Methods | ||
| 440 | |||
| 441 | sub redo_desc { return <<EOF } | ||
| 442 | Use this method to redo the last result command | ||
| 443 | EOF | ||
| 444 | |||
| 445 | sub redo_doc { return <<EOF } | ||
| 446 | More to come! | ||
| 447 | EOF | ||
| 448 | |||
| 449 | sub redo_proc { | ||
| 450 | # Be explicit for clarity | ||
| 451 | my $table = Sash::ResultHistory->redo; | ||
| 452 | $table->display; | ||
| 453 | |||
| 454 | return; | ||
| 455 | } | ||
| 456 | |||
| 457 | # REVERT Command Methods | ||
| 458 | |||
| 459 | sub revert_desc { return <<EOF } | ||
| 460 | Use this method to reset the current result set to original size | ||
| 461 | EOF | ||
| 462 | |||
| 463 | sub revert_doc { return <<EOF } | ||
| 464 | More to come! | ||
| 465 | EOF | ||
| 466 | |||
| 467 | sub revert_proc { | ||
| 468 | # Be explicit for clarity | ||
| 469 | my $table = Sash::ResultHistory->revert; | ||
| 470 | $table->display; | ||
| 471 | |||
| 472 | return; | ||
| 473 | } | ||
| 474 | |||
| 475 | # SEARCH Command Methods | ||
| 476 | |||
| 477 | sub search_desc { return <<EOF } | ||
| 478 | Sash Rules! | ||
| 479 | EOF | ||
| 480 | |||
| 481 | sub search_doc { return <<EOF } | ||
| 482 | More help to come! | ||
| 483 | EOF | ||
| 484 | |||
| 485 | sub search_proc { | ||
| 486 | my $pattern; | ||
| 487 | my $args; | ||
| 488 | |||
| 489 | die "usage: result grep [iv] regex\n" if scalar @_ > 2; | ||
| 490 | |||
| 491 | Sash::Command->begin; | ||
| 492 | |||
| 493 | if ( scalar @_ == 1 ) { | ||
| 494 | $pattern = shift; | ||
| 495 | } else { | ||
| 496 | $args = shift; | ||
| 497 | $pattern = shift; | ||
| 498 | } | ||
| 499 | |||
| 500 | my $ignore_case = 1 if defined $args && $args =~ /i/; | ||
| 501 | |||
| 502 | my $pattern_result = Sash::ResultHistory->current( )->match_string( $pattern, $ignore_case ); | ||
| 503 | |||
| 504 | if ( scalar @{$pattern_result->data} > 0 ) { | ||
| 505 | if ( defined $args && $args =~ /v/ ) { | ||
| 506 | # This is lossy but should be effective for what we need to do. | ||
| 507 | my %match_rows = map { ( join '^^^^', @$_ ) => $_ } @{$pattern_result->rowRefs}; | ||
| 508 | my $data = [ map { if ( $match_rows{ join '^^^^', @$_ } ) { } else { $_ } } @{Sash::ResultHistory->current( )->rowRefs} ]; | ||
| 509 | |||
| 510 | Sash::Command->end( { result => Sash::Table->new( $data, Sash::ResultHistory->current( )->{header} ) } ); | ||
| 511 | } else { | ||
| 512 | Sash::Command->end( { result => $pattern_result } ); | ||
| 513 | } | ||
| 514 | } else { | ||
| 515 | print "No matches found.\n"; | ||
| 516 | } | ||
| 517 | |||
| 518 | return; | ||
| 519 | } | ||
| 520 | |||
| 521 | # SET_OUTPUT Command Methods | ||
| 522 | |||
| 523 | sub set_output_desc { return <<EOF } | ||
| 524 | EOF | ||
| 525 | |||
| 526 | sub set_output_doc { return <<EOF } | ||
| 527 | EOF | ||
| 528 | |||
| 529 | # Just a wrapper. | ||
| 530 | sub set_output_proc { | ||
| 531 | my $output = lc( shift ); | ||
| 532 | |||
| 533 | Sash::Properties->output( $output ); | ||
| 534 | } | ||
| 535 | |||
| 536 | # SELECT Command Methods | ||
| 537 | |||
| 538 | sub select_desc { return <<EOF } | ||
| 539 | Use this command to select data from an available object | ||
| 540 | EOF | ||
| 541 | |||
| 542 | sub select_doc { return <<EOF } | ||
| 543 | More to come | ||
| 544 | Can you dig? | ||
| 545 | EOF | ||
| 546 | |||
| 547 | # This is really just a wrapper to execute_query which is the | ||
| 548 | # true Sash::Command that gets executed so no reason to add those | ||
| 549 | # begin/end invocations here. | ||
| 550 | |||
| 551 | sub select_meth { | ||
| 552 | my $class = shift; | ||
| 553 | my $term = shift; | ||
| 554 | my $args = shift; | ||
| 555 | |||
| 556 | $_writing_query = 1; | ||
| 557 | |||
| 558 | # Resetting the buffer won't be valid if query adds union/intersect | ||
| 559 | # to the language | ||
| 560 | ( $_buffer = $args->{rawline} ) =~ s/;$//; | ||
| 561 | |||
| 562 | if ( $args->{rawline} =~ /;$/ ) { | ||
| 563 | $_writing_query = 0; | ||
| 564 | Sash::Command->execute_query( $term, $_buffer, $args->{cursor_class} ); | ||
| 565 | } else { | ||
| 566 | $term->set_continue_prompt; | ||
| 567 | } | ||
| 568 | } | ||
| 569 | |||
| 570 | sub execute_query { | ||
| 571 | my $class = shift; | ||
| 572 | my $term = shift; | ||
| 573 | my $query = shift; | ||
| 574 | my $cursor_class = shift; | ||
| 575 | |||
| 576 | $term->set_standard_prompt; | ||
| 577 | |||
| 578 | Sash::Command->begin; | ||
| 579 | |||
| 580 | $_writing_query = 0; | ||
| 581 | |||
| 582 | my $table; | ||
| 583 | my $data = [ ]; | ||
| 584 | my $header; | ||
| 585 | my $cursor; | ||
| 586 | |||
| 587 | # We will assume that we do not want to get all of the records for | ||
| 588 | # really large query result sets. | ||
| 589 | my $retrieve_all_records = 0; | ||
| 590 | |||
| 591 | eval { | ||
| 592 | $cursor = $cursor_class->open( { | ||
| 593 | query => $query, | ||
| 594 | caller => 'execute_query' | ||
| 595 | } ); | ||
| 596 | }; if ( $@ ) { | ||
| 597 | die $@; | ||
| 598 | } | ||
| 599 | |||
| 600 | while ( my $table = $cursor->fetch ) { | ||
| 601 | $data = [ @$data, @{$table->rowRefs} ]; | ||
| 602 | $header = $table->{header} unless defined $header; | ||
| 603 | |||
| 604 | $retrieve_all_records = 1 if $cursor->size <= Sash::Command->large_query_size; | ||
| 605 | |||
| 606 | $retrieve_all_records = $term->prompt_for( 'retrieve all ' . $cursor->size . ' records? (y/n)' ) | ||
| 607 | if $cursor->size > Sash::Command->large_query_size && ! $retrieve_all_records; | ||
| 608 | |||
| 609 | $retrieve_all_records = 0 if $retrieve_all_records =~ /n/i; | ||
| 610 | |||
| 611 | last unless $retrieve_all_records; | ||
| 612 | } | ||
| 613 | |||
| 614 | $cursor->close; | ||
| 615 | |||
| 616 | $table = Sash::Table->new( $data, $header ); | ||
| 617 | |||
| 618 | # Special case that no records came back from the query. | ||
| 619 | if ( scalar @{$table->rowRefs} ) { | ||
| 620 | Sash::Command->end( { result => $table } ); | ||
| 621 | } else { | ||
| 622 | Sash::Command->end; | ||
| 623 | print 'Empty set ' . Sash::Command->elapsed . "\n"; | ||
| 624 | } | ||
| 625 | |||
| 626 | return; | ||
| 627 | } | ||
| 628 | |||
| 629 | # SORT Command Methods | ||
| 630 | |||
| 631 | sub sort_desc { return <<EOF } | ||
| 632 | Use this command to sort the data in the currect result set | ||
| 633 | EOF | ||
| 634 | |||
| 635 | sub sort_doc { return <<EOF } | ||
| 636 | More to come! | ||
| 637 | EOF | ||
| 638 | |||
| 639 | sub sort_meth { | ||
| 640 | my $term = shift; | ||
| 641 | ( my $args = shift->{rawline} ) =~ s/sort *(.*?);?/$1/g; | ||
| 642 | |||
| 643 | die "You must select a record set first\n" unless Sash::ResultHistory->size > 0; | ||
| 644 | |||
| 645 | Sash::Command->begin; | ||
| 646 | |||
| 647 | my @tokens = split /,/, $args; | ||
| 648 | my @sort; | ||
| 649 | |||
| 650 | foreach ( @tokens ) { | ||
| 651 | my $column; | ||
| 652 | my $order; | ||
| 653 | my $type; | ||
| 654 | my @group; | ||
| 655 | |||
| 656 | foreach ( split / / ) { | ||
| 657 | if ( /^alpha|num|asc|desc|0|1/ ) { | ||
| 658 | $type = 1 if ( $_ eq 'alpha' ); | ||
| 659 | $type = 0 if ( $_ eq 'num' ); | ||
| 660 | $order = 1 if ( $_ eq 'desc' ); | ||
| 661 | $order = 0 if ( $_ eq 'asc' ); | ||
| 662 | } else { | ||
| 663 | $column = $_; | ||
| 664 | } | ||
| 665 | } | ||
| 666 | |||
| 667 | # Examine if we need to provide defaults | ||
| 668 | $type = 1 unless defined $type; | ||
| 669 | $order = 1 unless defined $order; | ||
| 670 | |||
| 671 | push @sort, $column, $type, $order; | ||
| 672 | } | ||
| 673 | |||
| 674 | Sash::Command->end( { result => Sash::ResultHistory->current( )->sort( @sort ) } ); | ||
| 675 | |||
| 676 | return; | ||
| 677 | } | ||
| 678 | |||
| 679 | # UNDO Command Methods | ||
| 680 | |||
| 681 | sub undo_desc { return <<EOF } | ||
| 682 | Use this method to undo the last result command | ||
| 683 | EOF | ||
| 684 | |||
| 685 | sub undo_doc { return <<EOF } | ||
| 686 | More to come! | ||
| 687 | EOF | ||
| 688 | |||
| 689 | sub undo_proc { | ||
| 690 | # Be explicit for clarity. ResultHistory gives back an instance of | ||
| 691 | # Sash::Table hence the second line. | ||
| 692 | my $table = Sash::ResultHistory->undo; | ||
| 693 | $table->display; | ||
| 694 | |||
| 695 | return; | ||
| 696 | } | ||
| 697 | |||
| 698 | |||
| 699 | 1; |