4 |
use Data::Dumper; $Data::Dumper::Deepcopy = 1; |
use Data::Dumper; $Data::Dumper::Deepcopy = 1; |
5 |
use strict; |
use strict; |
6 |
use Carp; |
use Carp; |
|
use DBI; |
|
7 |
use zot; |
use zot; |
8 |
|
|
9 |
our %config = (); |
our %config = (); |
39 |
return( ( sprintf("dbi:Pg:dbname=%s;host=%s", $db_name, $db_host), |
return( ( sprintf("dbi:Pg:dbname=%s;host=%s", $db_name, $db_host), |
40 |
$user, $password, { 'RaiseError' => 1 } ) ); |
$user, $password, { 'RaiseError' => 1 } ) ); |
41 |
}, |
}, |
42 |
'delay' => sub { system('sync'); select(undef, undef, undef, 0.5) }, |
# 'delay' => sub { system('sync'); select(undef, undef, undef, 0.005) }, |
|
'delay' => sub { }, |
|
43 |
'datatypes' => {}, |
'datatypes' => {}, |
44 |
'relational' => sub { |
'relational' => sub { |
45 |
my $table = shift; |
my $table = shift; |
60 |
{ |
{ |
61 |
'bigserial' => 'serial', |
'bigserial' => 'serial', |
62 |
}, |
}, |
|
'delay' => $config{'^PostgreSQL 7\.2\.'}{'delay'}, |
|
63 |
'relational' => $config{'^PostgreSQL 7\.2\.'}{'relational'}, |
'relational' => $config{'^PostgreSQL 7\.2\.'}{'relational'}, |
64 |
}; |
}; |
65 |
|
|
66 |
$config{'stdout'} = |
$config{'raw'} = |
67 |
{ |
{ |
68 |
'driver' => sub { return "dbi:stdout:" }, |
'driver' => sub { return "dbi:stdout:" }, |
69 |
'datatypes' => {}, |
'datatypes' => {}, |
70 |
'delay' => sub { }, |
'delay' => sub {}, |
71 |
'relational' => $config{'^PostgreSQL 7\.1\.'}{'relational'}, |
'relational' => $config{'^PostgreSQL 7\.1\.'}{'relational'}, |
72 |
}; |
}; |
73 |
|
|
90 |
exit 1; |
exit 1; |
91 |
} |
} |
92 |
|
|
|
# try to connect |
|
|
our $db = DBI->connect( $config{$db_conf}{'driver'}->(@ARGV) ); |
|
|
|
|
93 |
# check the file exists |
# check the file exists |
94 |
open F, $file or die "Unable to open $file: $!"; |
open F, $file or die "Unable to open $file: $!"; |
95 |
our %structure = zot::parse(<F>); |
our %structure = zot::parse(<F>); |
144 |
} |
} |
145 |
else |
else |
146 |
{ |
{ |
147 |
|
$r = ""; |
148 |
if ($r =~ /^_start_t\s/) |
if ($r =~ /^_start_t\s/) |
149 |
{ |
{ |
150 |
|
$r = 'NOT NULL'; |
151 |
|
} |
152 |
|
|
153 |
|
if (substr($field, -1) eq '*') |
154 |
|
{ |
155 |
|
substr($field, -1) = ''; |
156 |
|
$r .= ' UNIQUE'; |
157 |
|
} |
158 |
|
|
159 |
|
if (substr($field, -1) eq '+') |
160 |
|
{ |
161 |
|
substr($field, -1) = ''; |
162 |
$r .= ' NOT NULL'; |
$r .= ' NOT NULL'; |
163 |
} |
} |
164 |
push @r, $r; |
|
165 |
|
push @r, sprintf("%s %s %s", $field, $type, $r); |
166 |
} |
} |
167 |
} |
} |
168 |
|
|
186 |
my $sql = "CREATE TABLE $table (\n" . join(",\n", map(" $_", @r)) . "\n);\n"; |
my $sql = "CREATE TABLE $table (\n" . join(",\n", map(" $_", @r)) . "\n);\n"; |
187 |
|
|
188 |
$v and print STDERR $sql; |
$v and print STDERR $sql; |
189 |
$db->do( $sql ); |
print $sql; |
|
$config{$db_conf}{'delay'}->(); |
|
190 |
} |
} |
191 |
|
|
192 |
if ($config{$db_conf}{'relational'}) |
if ($config{$db_conf}{'relational'}) |
206 |
$sql = $config{$db_conf}{'relational'}->($table, $rel, $structure{$table}{REL}{$rel}); |
$sql = $config{$db_conf}{'relational'}->($table, $rel, $structure{$table}{REL}{$rel}); |
207 |
} |
} |
208 |
$v and print STDERR $sql; |
$v and print STDERR $sql; |
209 |
$db->do($sql); |
print $sql; |
|
$config{$db_conf}{'delay'}->(); |
|
210 |
} |
} |
211 |
} |
} |
212 |
} |
} |