7 |
use DBI; |
use DBI; |
8 |
use zot; |
use zot; |
9 |
|
|
10 |
|
our %config = (); |
11 |
|
|
12 |
sub usage |
sub usage |
13 |
{ |
{ |
14 |
print STDERR "Usage: zot2sql \"database identifier string\" [ driver-specific options ] file\n"; |
print STDERR "Usage: zot2sql \"database identifier string\" [ driver-specific options ] file\n"; |
15 |
print STDERR " e.g.: zot2sql \"\$( pg_config --version )\" testdb file\n"; |
print STDERR " e.g.: zot2sql \"\$( pg_config --version )\" testdb file\n"; |
16 |
print STDERR @_; |
if (@_) |
17 |
|
{ |
18 |
|
print STDERR @_; |
19 |
|
} |
20 |
|
else |
21 |
|
{ |
22 |
|
print STDERR "Currently supported database identifiers are:\n"; |
23 |
|
print "\t$_\n" |
24 |
|
foreach (sort keys %config); |
25 |
|
} |
26 |
exit 1; |
exit 1; |
27 |
} |
} |
28 |
|
|
|
sub delay() { system('sync'); select(undef, undef, undef, 0.5) } |
|
|
|
|
|
|
|
|
our %config = (); |
|
|
|
|
29 |
$config{'^PostgreSQL 7\.1\.'} = |
$config{'^PostgreSQL 7\.1\.'} = |
30 |
{ |
{ |
31 |
'driver' => sub |
'driver' => sub |
40 |
{ |
{ |
41 |
'bigserial' => 'serial', |
'bigserial' => 'serial', |
42 |
}, |
}, |
43 |
|
'delay' => sub { system('sync'); select(undef, undef, undef, 0.5) }, |
44 |
}; |
}; |
45 |
|
|
46 |
$config{'^PostgreSQL 7\.2\.'} = |
$config{'^PostgreSQL 7\.2\.'} = |
47 |
{ |
{ |
48 |
'driver' => $config{'^PostgreSQL 7\.1\.'}{'driver'}, |
'driver' => $config{'^PostgreSQL 7\.1\.'}{'driver'}, |
49 |
'datatypes' => {}, |
'datatypes' => {}, |
50 |
|
'delay' => $config{'^PostgreSQL 7\.1\.'}{'delay'}, |
51 |
|
}; |
52 |
|
|
53 |
|
$config{'stdout'} = |
54 |
|
{ |
55 |
|
'driver' => sub { return "dbi:stdout:" }, |
56 |
|
'datatypes' => {}, |
57 |
|
'delay' => sub { }, |
58 |
}; |
}; |
59 |
|
|
60 |
usage() if (@ARGV < 3); |
usage() if (@ARGV < 2); |
61 |
|
|
62 |
our $file = pop @ARGV; |
our $file = pop @ARGV; |
63 |
our $db_conf = shift @ARGV; |
our $db_conf = shift @ARGV; |
136 |
|
|
137 |
print $sql; |
print $sql; |
138 |
$db->do( $sql ); |
$db->do( $sql ); |
139 |
delay; |
$config{$db_conf}{'delay'}->(); |
140 |
} |
} |
141 |
|
|
142 |
foreach my $table (keys %structure) |
foreach my $table (keys %structure) |
146 |
my $sql = "ALTER TABLE $table ADD FOREIGN KEY ($rel) REFERENCES $structure{$table}{REL}{$rel};\n"; |
my $sql = "ALTER TABLE $table ADD FOREIGN KEY ($rel) REFERENCES $structure{$table}{REL}{$rel};\n"; |
147 |
print $sql; |
print $sql; |
148 |
$db->do($sql); |
$db->do($sql); |
149 |
delay; |
$config{$db_conf}{'delay'}->(); |
150 |
} |
} |
151 |
} |
} |