/usr/bin/stag-db is in libdata-stag-perl 0.11-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | #!/usr/bin/perl -w
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell
# POD docs at bottom of file
use strict;
use Data::Stag qw(:all);
use Data::Stag::StagDB;
use Getopt::Long;
my $record_type;
my $unique_key;
my $dir;
my $fmt = '';
my $outfmt;
my $help;
my $top;
my $indexfile;
my $qf;
my @query = ();
my $keys;
my $reset;
GetOptions("record_type|r=s"=>\$record_type,
"unique_key|unique|u|k=s"=>\$unique_key,
"parser|format|p=s"=>\$fmt,
"handler|writer|w=s"=>\$outfmt,
"indexfile|index|i=s"=>\$indexfile,
"top=s"=>\$top,
"query|q=s@"=>\@query,
"qf=s"=>\$qf,
"help|h"=>\$help,
"keys"=>\$keys,
"reset"=>\$reset,
);
if ($help) {
system("perldoc $0");
exit 0;
}
my $sdb = Data::Stag::StagDB->new;
$sdb->record_type($record_type) if $record_type;
$sdb->unique_key($unique_key) if $unique_key;
$sdb->indexfile($indexfile) if $indexfile;
$sdb->reset() if $reset;
foreach my $file (@ARGV) {
my $p;
if ($file eq '-') {
$fmt ||= 'xml';
$p = Data::Stag->parser(-format=>$fmt, -fh=>\*STDIN);
$p->handler($sdb);
$p->parse(-fh=>\*STDIN);
}
else {
if (!-f $file) {
print "the file \"$file\" does not exist\n";
}
$p = Data::Stag->parser($file, $fmt);
$p->handler($sdb);
$p->parse($file);
}
}
if ($qf) {
open(F, $qf) || die "cannot open queryfile: $qf";
@query = map {chomp;$_} <F>;
close(F);
}
if ($keys) {
my $idx = $sdb->index_hash;
printf "$_\n", $_ foreach (keys %$idx);
}
if (@query) {
my $w;
if ($outfmt) {
$w = Data::Stag->getformathandler($outfmt);
}
else {
$w = Data::Stag->makehandler;
}
if ($top) {
$w->start_event($top);
}
my $idx = $sdb->index_hash;
my $n_found = 0;
foreach my $q (@query) {
my $nodes = $idx->{$q} || [];
if (!@$nodes) {
print STDERR "Could not find a record indexed by key: \"$q\"\n";
next;
}
foreach my $node (@$nodes) {
$n_found++;
if ($w) {
$node->sax($w);
}
else {
print $node->xml;
}
}
}
if ($top) {
$w->end_event($top);
}
if (!$n_found && !$top) {
print STDERR "NONE FOUND!\n";
}
else {
if (!$outfmt) {
print $w->stag->xml;
}
}
}
=head1 NAME
stag-db - persistent storage and retrieval for stag data (xml, sxpr, itext)
=head1 SYNOPSIS
stag-db -r person -k social_security_no -i ./person-idx myrecords.xml
stag-db -i ./person-idx -q 999-9999-9999 -q 888-8888-8888
=head1 DESCRIPTION
Builds a simple file-based database for persistent storage and
retrieval of nodes from a stag compatible document.
Imagine you have a very large file of data, in a stag compatible
format such as XML. You want to index all the elements of type
B<person>; each person can be uniquely identified by
B<social_security_no>, which is a direct subnode of B<person>
The first thing to do is to build an index file, which will be stored
in your current directory:
stag-db -r person -k social_security_no -i ./person-idx myrecords.xml
You can then use the index "person-idx" to retrieve B<person> nodes by
their social security number
stag-db -i ./person-idx -q 999-9999-9999 > some-person.xml
You can export using different stag formats
stag-db -i ./person-idx -q 999-9999-9999 -w sxpr > some-person.xml
You can retrieve multiple nodes (although these need to be rooted to
make a valid file)
stag-db -i ./person-idx -q 999-9999-9999 -q 888-8888-8888 -top personset
Or you can use a list of IDs from a file (newline delimited)
stag-db -i ./person-idx -qf my_ss_nmbrs.txt -top personset
=head2 ARGUMENTS
=head3 -i INDEXFILE
This file will be used as the persistent index for storage/retrieval
=head3 -r RELATION-NAME
This is the name of the stag node (XML element) that will be stored in
the index; for example, with the XML below you may want to use the
node name B<person> and the unique key B<id>
<person_set>
<person>
<id>...</id>
</person>
<person>
<id>...</id>
</person>
...
</person_set>
This flag should only be used when you want to store data
=head3 -k UNIQUE-KEY
This node will be used as the unique/primary key for the data
This node should be nested directly below the node that is being
stored in the index - if it is more that one below, specify a path
This flag should only be used when you want to store data
=head3 -u UNIQUE-KEY
Synonym for B<-k>
=head3 -p PARSER
This can be the name of a stag supported format (xml, sxpr, itext) -
XML is assumed by default
It can also be a module name - this module is used to parse the input
file into a stag stream; see L<Data::Stag::BaseGenerator> for details
on writing your own parsers/event generators
This flag should only be used when you want to store data
=head3 -q QUERY-ID
Fetches the relation/node with unique key value equal to query-id
Multiple arguments can be passed by specifying -q multple times
This flag should only be used when you want to query data
=head3 -top NODE-NAME
If this is specified in conjunction with B<-q> or B<-qf> then all the
query result nodes will be nested inside a node with this name (ie
this provides a root for the resulting document tree)
=head3 -qf QUERY-FILE
This is a file of newline-seperated IDs; this is useful for querying
the index in batch
=head3 -keys
This will write a list of all primary keys in the index
=head3 -w WRITER
This format will be used to write the data; can be any stag format
(xml, sxpr, itext) - default XML.
Can also be a module that catches the incoming stag event stream and
does something with it (for example, this could be a module you write
yourself that transforms the stag events into HTML)
=head1 SEE ALSO
L<Data::Stag>
For more complex stag to database mapping, see L<DBIx::DBStag> and the
scripts
L<stag-storenode>
L<selectall_xml>
=cut
|