-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunsql
executable file
·94 lines (78 loc) · 1.84 KB
/
runsql
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/perl
#
# runsql - utility to run SQL code from files given as parameters
#
# Copyright (c) Timo Kokkonen <[email protected]> 2001-2003.
#
require 5;
use Getopt::Long;
use Sauron::DB;
use Sauron::Util;
use Sauron::Sauron;
load_config();
##############################################
GetOptions("help|h","verbose|v","notransaction|n");
$count=@ARGV;
if ($opt_help || $count < 1) {
print "syntax: $0 [--help] [--separate] <filename> [<filename> ...]\n\n";
print " Parameter descriptions: \n",
" --notransaction do not use transactions\n",
" --verbose display SQL code\n\n";
print "" if ($opt_help);
exit(0);
}
db_connect();
db_debug(0);
$opt_verbose = ($opt_verbose ? 1 : 0);
db_begin() unless ($opt_notransaction);
while ($filename=$ARGV[0]) {
shift;
unless (open(FILE,$filename)) {
error("cannot open file: $filename");
next;
}
print "Processing file: $filename\n" if ($opt_verbose);
$comment=0;
$cmd='';
while(<FILE>) {
fileloop:
s/\s*$//;
s/\/\*.*\*\///;
if (/(\/\*.*$)/) {
#print "comment start: '$1'\n";
$comment=1;
s/\/\*.*$//;
} elsif (/(^.*\*\/)/) {
#print "comment end: '$1'\n";
$comment=0;
s/^.*\*\///;
} elsif ($comment) {
next;
}
next if (/^\s*$/);
if (/(^.*?;)/) {
$last=$1;
$cmd .= $last;
$cmd =~ s/^\s+//;
unless ($cmd =~ /^\s*$/) {
print "command: '$cmd'\n" if ($opt_verbose);
$res = db_exec($cmd);
if ($res < 0) {
error("SQL command failed at $filename($.) : " . db_errormsg());
exit(1) unless($opt_notransaction);
}
}
$cmd='';
s/(^.*?;)//;
goto fileloop;
}
#print " $_\n" if ($opt_verbose);
s/\s+/ /g;
$cmd .= $_;
}
close(FILE);
}
unless ($opt_notransaction) {
fatal("cannot changes to database!") if (db_commit() < 0);
}
exit;