| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Footprintless::Plugin::Database::SqlShellAdapter; | ||
| 5 | $Footprintless::Plugin::Database::SqlShellAdapter::VERSION = '1.06'; | ||
| 6 | # ABSTRACT: An adaptor to SQL::Shell | ||
| 7 | # PODNAME: Footprintless::Plugin::Database::SqlShellAdapter | ||
| 8 | |||
| 9 | use Exporter qw(import); | ||
| 10 | use Footprintless::Util qw(slurp); | ||
| 11 | use SQL::Shell; | ||
| 12 | use Term::ReadLine; | ||
| 13 | |||
| 14 | our @EXPORT_OK = qw( | ||
| 15 | sql_shell | ||
| 16 | ); | ||
| 17 | |||
| 18 | use constant HISTORY_FILE => '~/.footprintless/.sqlsh_history'; | ||
| 19 | |||
| 20 | sub _help { | ||
| 21 | my ($self) = @_; | ||
| 22 | use Config; | ||
| 23 | require Pod::Select; | ||
| 24 | my $pager = $Config{pager}; | ||
| 25 | my $have_pager = ( $pager && -f $pager && -x _ ); | ||
| 26 | |||
| 27 | my $helptext = "SQL Shell\n"; | ||
| 28 | |||
| 29 | { | ||
| 30 | local ( *STDOUT, $^W ); | ||
| 31 | require Pod::Select; | ||
| 32 | require IO::Scalar; | ||
| 33 | tie( *STDOUT, 'IO::Scalar', \$helptext ); | ||
| 34 | Pod::Select::podselect( { -sections => ['COMMANDS'] }, $INC{'SQL/Shell.pm'} ); | ||
| 35 | Pod::Select::podselect( { -sections => ['COMMANDS ADDED BY SQLSH'] }, $0 ); | ||
| 36 | untie(*STDOUT); | ||
| 37 | $helptext =~ s/=head1.*?\n//sg; | ||
| 38 | $helptext =~ s/\n\n/\n/g; | ||
| 39 | } | ||
| 40 | |||
| 41 | if ($have_pager) { | ||
| 42 | local ( *STDOUT, $^W ); | ||
| 43 | open( my $pager, '|', $pager ); | ||
| 44 | print( $pager $helptext ); | ||
| 45 | close($pager); | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | print $helptext; | ||
| 49 | } | ||
| 50 | |||
| 51 | return 1; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub _parse_script { | ||
| 55 | my ($script) = @_; | ||
| 56 | |||
| 57 | # should do a better job, but... | ||
| 58 | return map { | ||
| 59 | my $line = $_; | ||
| 60 | $line =~ s/^\s+//; | ||
| 61 | $line =~ s/\s+$//; | ||
| 62 | $line | ||
| 63 | } split( /;/, $script ); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub sql_shell { | ||
| 67 | my ( $connection_string, $username, $password, @args ) = @_; | ||
| 68 | |||
| 69 | my $sqlsh = new SQL::Shell( { Verbose => 1 } ); | ||
| 70 | $sqlsh->connect( $connection_string, $username, $password ); | ||
| 71 | |||
| 72 | if ( -t STDIN ) { ## no critic | ||
| 73 | $sqlsh->set( 'Interactive', 1 ); | ||
| 74 | ( $ENV{EDITOR} ) = | ||
| 75 | $ENV{PERL_READLINE_MODE} ? $ENV{PERL_READLINE_MODE} | ||
| 76 | : $ENV{SHELLOPTS} ? $ENV{SHELLOPTS} =~ /\b(emacs|vi)\b/ | ||
| 77 | : $ENV{EDITOR}; | ||
| 78 | |||
| 79 | my $term = Term::ReadLine->new("SQL Shell (fpl)"); | ||
| 80 | my $autohistory = $term->Features()->{autohistory}; | ||
| 81 | my $ornaments = $term->ornaments(); | ||
| 82 | if ( defined($ornaments) && $ornaments ne ',,,' ) { | ||
| 83 | $sqlsh->set( 'NULL', "\x1B[1mNULL\x1B[0m" ); | ||
| 84 | } | ||
| 85 | |||
| 86 | $sqlsh->set( | ||
| 87 | 'GetHistory', | ||
| 88 | sub { | ||
| 89 | return [ $term->GetHistory() ]; | ||
| 90 | } | ||
| 91 | ); | ||
| 92 | $sqlsh->set( | ||
| 93 | 'SetHistory', | ||
| 94 | sub { | ||
| 95 | my $history = shift; | ||
| 96 | $term->SetHistory(@$history); | ||
| 97 | } | ||
| 98 | ); | ||
| 99 | $sqlsh->set( | ||
| 100 | 'AddHistory', | ||
| 101 | sub { | ||
| 102 | my $cmd = shift; | ||
| 103 | $term->addhistory($cmd) unless $autohistory; | ||
| 104 | } | ||
| 105 | ); | ||
| 106 | |||
| 107 | my $quit = 0; | ||
| 108 | $sqlsh->install_cmds( | ||
| 109 | { qr/^help|\?$/ => \&_help, | ||
| 110 | qr/^reload$/ => sub { | ||
| 111 | my ($self) = @_; | ||
| 112 | my $settings = $self->{settings}; | ||
| 113 | if ( $settings->{Interactive} ) { | ||
| 114 | exec( $^X, $0, @args ); | ||
| 115 | exit 0; | ||
| 116 | } | ||
| 117 | return 1; | ||
| 118 | }, | ||
| 119 | qr/^(cat|more|less) (.+)/ => sub { | ||
| 120 | my ( $self, $pager, $file ) = @_; | ||
| 121 | return system( $pager, $file ) == 0; | ||
| 122 | }, | ||
| 123 | qr/^(?:exit|quit|bye|\w+\s+off)$/i => sub { | ||
| 124 | my ($sqlsh) = @_; | ||
| 125 | $sqlsh->disconnect(); | ||
| 126 | $quit = 1; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | ); | ||
| 130 | |||
| 131 | $sqlsh->load_history(HISTORY_FILE) if ( -f HISTORY_FILE ); | ||
| 132 | |||
| 133 | local $_; | ||
| 134 | my $prompt = "SQL> "; | ||
| 135 | while ( defined( $_ = $term->readline($prompt) ) ) { | ||
| 136 | eval { $sqlsh->execute_cmd($_); }; | ||
| 137 | print("Error: $@") if ($@); | ||
| 138 | last if ($quit); | ||
| 139 | } | ||
| 140 | |||
| 141 | $sqlsh->save_history(HISTORY_FILE); | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | my $script = slurp(); | ||
| 145 | $sqlsh->execute_cmd($_) foreach _parse_script($script); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | __END__ |