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