File: pisql

package info (click to toggle)
libdbd-firebird-perl 1.39-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 648 kB
  • sloc: perl: 4,748; ansic: 2,425; makefile: 16
file content (58 lines) | stat: -rwxr-xr-x 1,426 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl

use warnings;
use strict;

use DBD::FirebirdEmbedded;

my $dbh;

while (1) {
    print "> ";
    my $in = <>;
    last unless defined($in);

    next if $in =~ /^\s*--/;

    if ( $in =~ /^\s*create\s*database\s*'([^']+)'\s*$/ ) {
        my $db_path = $1;
        DBD::FirebirdEmbedded->create_database({db_path => $1});
        $dbh = DBI->connect( "dbi:FirebirdEmbedded:db=$1", undef, undef,
            { AutoCommit => 0 } );
    }
    elsif ( $in =~ /^\s*connect '([^']+)'\s*$/ ) {
        $dbh = DBI->connect( "dbi:FirebirdEmbedded:db=$1", undef, undef,
            { AutoCommit => 0 } );
    }
    elsif ( $in =~ /^\s*exit\s*/i ) {
        $dbh->commit if $dbh;
        last;
    }
    elsif ( $in =~ /^\s*quit\s*/i ) {
        $dbh->rollback if $dbh;
        last;
    }
    else {
        if ($dbh) {
            my $sth = $dbh->prepare_cached($in);
            $sth->execute();
            if ( $sth->{NUM_OF_FIELDS} > 0 ) {
                print join( "\t", @{ $sth->{NAME} } ), "\n";
                while ( my $row = $sth->fetchrow_arrayref ) {
                    print
                        join( "\t", map( defined($_) ? $_ : 'NULL', @$row ) ),
                        "\n";
                }
                $sth->finish;
            }
        }
        else {
            warn "E: Not connected to a database.\n";
        }
    }
}

if ($dbh) {
    $dbh->rollback;
    $dbh->disconnect;
}