-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrocto_run_sql.pl
More file actions
51 lines (42 loc) · 1.04 KB
/
rocto_run_sql.pl
File metadata and controls
51 lines (42 loc) · 1.04 KB
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
#!/usr/bin/perl
#
# Read SQL query from disk and
# send SQL queries to Rocto
#
use DBI;
use strict;
my $user = $ARGV[0];
my $pass = $ARGV[1];
chomp(my $file = ($ARGV[2]));
print "SQL file is: " . $file . "\n";
die "Invalid SQL file\n" unless ($file =~/.*sql/);
die "File error: " unless -f $file;
print "SQL file is $file\n";
my $content;
my $sql;
open(my $fh, '<', $file) or die "cannot open file $file";
{
local $/;
chomp($sql = <$fh>);
}
close($fh);
print "SQL is $sql\n";
my $driver = "Pg";
my $database = "tvista";
my $dsn = "DBI:$driver:dbname = $database;host = 127.0.0.1;port = 1337";
my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully\n";
if($sql eq "") {
my $sql = qq(SELECT state_id,name,abbreviation FROM state;);
}
my $sth = $dbh->prepare( $sql );
my $rv = $sth->execute() or die $DBI::errstr;
if($rv < 0) {
print $DBI::errstr;
}
while(my @row = $sth->fetchrow_array()) {
print join("|", @row), "\n";
}
print "Operation done successfully\n";
$dbh->disconnect();