Debian Codegraph @

eg/pisql

raw
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use DBD::FirebirdEmbedded;
7
8 my $dbh;
9
10 while (1) {
11 print "> ";
12 my $in = <>;
13 last unless defined($in);
14
15 next if $in =~ /^\s*--/;
16
17 if ( $in =~ /^\s*create\s*database\s*'([^']+)'\s*$/ ) {
18 my $db_path = $1;
19 DBD::FirebirdEmbedded->create_database({db_path => $1});
20 $dbh = DBI->connect( "dbi:FirebirdEmbedded:db=$1", undef, undef,
21 { AutoCommit => 0 } );
22 }
23 elsif ( $in =~ /^\s*connect '([^']+)'\s*$/ ) {
24 $dbh = DBI->connect( "dbi:FirebirdEmbedded:db=$1", undef, undef,
25 { AutoCommit => 0 } );
26 }
27 elsif ( $in =~ /^\s*exit\s*/i ) {
28 $dbh->commit if $dbh;
29 last;
30 }
31 elsif ( $in =~ /^\s*quit\s*/i ) {
32 $dbh->rollback if $dbh;
33 last;
34 }
35 else {
36 if ($dbh) {
37 my $sth = $dbh->prepare_cached($in);
38 $sth->execute();
39 if ( $sth->{NUM_OF_FIELDS} > 0 ) {
40 print join( "\t", @{ $sth->{NAME} } ), "\n";
41 while ( my $row = $sth->fetchrow_arrayref ) {
42 print
43 join( "\t", map( defined($_) ? $_ : 'NULL', @$row ) ),
44 "\n";
45 }
46 $sth->finish;
47 }
48 }
49 else {
50 warn "E: Not connected to a database.\n";
51 }
52 }
53 }
54
55 if ($dbh) {
56 $dbh->rollback;
57 $dbh->disconnect;
58 }