Script sqlextract.pl for extracting SQLs from existing copy.in -
09-30-2009
, 08:33 AM
Hi,
see also
http://community.ingres.com/wiki/Ext...g_copy.in_file
Typical problem: we have to restore only some specific tables /
views / etc. from a given database unload / copy.in file.
Normal way: open copy.in file in Windows notepad (or whatever editor),
create another blank text file - for example - restore.sql and
manually copy-and-paste the necessary SQLs from copy.in into
restore.sql. This can be quite time-consuming...
Better way: usage of attached Perl script sqlextract.pl
---begin sqlextract.pl---
#!/usr/bin/perl
#
# take first parameter as file name to be analysed
$sourcefile = $ARGV[0];
print "source file: $sourcefile\n";
$pattern = $ARGV[1];
print "search pattern: $pattern\n";
$outputfile = $ARGV[2];
print "output file: $outputfile\n\n";
# read file
open(IN, "<$sourcefile") or die "file $sourcefile does not exist,
exiting...\n";;
@sourcefilelines= <IN>;
close IN;
# all lines into one big string
$sourcefilestring = join('',@sourcefilelines);
# replace \p\g with DELIM
$sourcefilestring =~ s/\\p\\g/DELIM/g;
# replace lines "create table mytable("
# with "create table mytable ("
$sourcefilestring =~ s/\(\n/\ (\n/g;
# split big string into single elements, separator is \p\g
@sqlcommands = split('DELIM', $sourcefilestring);
# debug - print everything on the screen
#$i = 0;
# while ($i <= $#sqlcommands)
# {
# print $i, "\n";
# print $sqlcommands[$i++], "\n";
# }
# create / open output file
open(FILE, ">$outputfile") or die "cannot create / open file
$outputfile\n";
# some stuff that should always be in the output file
print FILE "set autocommit on\n\\p\\g\n";
$i = 0;
while ($i <= $#sqlcommands)
{
if ($sqlcommands[$i] =~ m/$pattern/)
{
print FILE $sqlcommands[$i], "\n\\p\\g\n";
}
$i++;
}
---end sqlextract.pl---
Usage examples
sqlextract.pl copy.in "mytable" extractedcommands.sql
extracts all SQLs containing mytable, but will - of course - also
catch mytable2, mytable3 etc.
sqlextract.pl copy.in " mytable " extractedcommands.sql
extracts only SQLs containing mytable as a separate word
sqlextract.pl copy.in " mytable1 | mytable2 | mytable3 "
extractedcommands.sql
extracts SQLs containing mytable1 or mytable2 or mytable3 as a
separate word
sqlextract.pl copy.in "modify |create index " extractedcommands.sql
extracts all modify... and create index... commands
Regards
Gerhard |