File: stag-autoddl.pl

package info (click to toggle)
libdbix-dbstag-perl 0.12-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,420 kB
  • sloc: perl: 6,152; sql: 588; xml: 221; lisp: 59; makefile: 20
file content (122 lines) | stat: -rwxr-xr-x 2,378 bytes parent folder | download | duplicates (3)
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
#!/usr/local/bin/perl -w

=head1 NAME 

stag-autoddl.pl - wrapper for the Data::Stag modules

=head1 SYNOPSIS

  stag-autoddl.pl -parser XMLAutoddl -handler ITextWriter file1.txt file2.txt

  stag-autoddl.pl -parser MyMod::MyParser -handler MyMod::MyWriter file.txt

=head1 DESCRIPTION

script wrapper for the Data::Stag modules

=over

=item -help|h

shows this document

=item -p|parser FORMAT

FORMAT is one of xml, sxpr or itext, or the name of a perl module

xml assumed as default

=item -w|writer FORMAT

FORMAT is one of xml, sxpr or itext, or the name of a perl module

This is only used if transforms are required on the data to turn it
into relational form; the transformed xml (or other stag format) is
stored in the file specified by -t

=item -link|l NODE_NAME

this node name will be used as a linking table

multiple linking nodes can be set:

 -l foo2bar -l x2y -l bim2bum


=item -t TRANSFORMED_FILE_NAME

the transformed input file is written here

=back

=cut



use strict;

use Carp;
use Data::Stag qw(:all);
use DBIx::DBStag;
use FileHandle;
use Getopt::Long;

my $parser = "";
my $handler = "";
my $mapf;
my $tosql;
my $toxml;
my $toperl;
my $debug;
my $help;
my @link = ();
my $ofn;
GetOptions(
           "help|h"=>\$help,
           "parser|format|p=s" => \$parser,
           "handler|writer|w=s" => \$handler,
           "xml"=>\$toxml,
           "perl"=>\$toperl,
           "debug"=>\$debug,
           "link|l=s@"=>\@link,
	   "transform|t=s"=>\$ofn,
          );
if ($help) {
    system("perldoc $0");
    exit 0;
}

my $db = DBIx::DBStag->new;

my $fn = shift @ARGV;
die "max 1 file" if @ARGV;
autoddl($fn);

sub autoddl {
    my $fn = shift;
    
    my $tree = 
      Data::Stag->parse($fn, 
                        $parser);
    my $ddl = $db->autoddl($tree, \@link);
    my $transforms = $db->source_transforms;
    if (@$transforms) {
	foreach (@$transforms) {
	    print STDERR "-- SOURCE REQUIRES TRANSFORM: $_->[0] => $_->[1]\n";
	}
	if (!$ofn) {
	    print STDERR "-- $fn requires transforms; consider running with -transform\n";
	}
	else {
	    $tree->transform(@$transforms);
	    my $W = $tree->getformathandler($handler || 'xml');
	    my $ofh = FileHandle->new(">$ofn") || die("cannot write transformed file $ofn");
	    $W->fh($ofh);
#	    $W->fh(\*STDOUT);
	    $tree->events($W);
	    $ofh->close;
	}
    }
    print $ddl;
}