15 |
=cut |
=cut |
16 |
|
|
17 |
my $c = { |
my $c = { |
|
name => 'imenik', |
|
|
dbi => 'Pg:dbname=vip', |
|
|
sql => qq{ |
|
|
select ime,tel from imenik |
|
|
}, |
|
|
pk => 'tel', |
|
18 |
debug => 1, |
debug => 1, |
19 |
}; |
}; |
20 |
|
|
66 |
|
|
67 |
=cut |
=cut |
68 |
|
|
69 |
|
my $usage = "$0 database_name (--create|--drop) name [--sql='select id,foo,bar from table'] [--pk=id]\n"; |
70 |
|
|
71 |
GetOptions($c, qw/create=s drop=s node_url=s sql=s pk=s user=s passwd=s debug+/); |
GetOptions($c, qw/create=s drop=s node_url=s sql=s pk=s user=s passwd=s debug+/); |
72 |
|
|
73 |
|
my $dbname = shift @ARGV || die $usage; |
74 |
|
|
75 |
|
$c->{dbi} = 'Pg:dbname=' . $dbname; |
76 |
|
|
77 |
warn "# c: ", Dumper($c) if ($c->{debug}); |
warn "# c: ", Dumper($c) if ($c->{debug}); |
78 |
|
|
79 |
my $table = $c->{create} || $c->{drop} || die "$0 (--create|--drop) name [--sql='select id,foo,bar from table']\n"; |
my $table = $c->{create} || $c->{drop} || die $usage; |
80 |
|
|
81 |
$c->{node_url} = 'http://localhost:1978/node/' . $table; |
$c->{node_url} = 'http://localhost:1978/node/' . $table; |
82 |
|
|
141 |
|
|
142 |
drop_triggers( $table ); |
drop_triggers( $table ); |
143 |
|
|
144 |
|
if (! $c->{pk}) { |
145 |
|
|
146 |
|
warn "# finding primary key for $table\n" if ($c->{debug}); |
147 |
|
|
148 |
|
$c->{pk} = $dbh->selectrow_array(qq{ |
149 |
|
SELECT |
150 |
|
a.attname, t.typname |
151 |
|
FROM pg_type t, pg_attribute a |
152 |
|
WHERE t.oid = a.atttypid AND attrelid = ( |
153 |
|
SELECT indexrelid |
154 |
|
FROM pg_class c, pg_index i |
155 |
|
WHERE c.relname = '$table' |
156 |
|
AND c.oid = i.indrelid |
157 |
|
AND indisprimary |
158 |
|
AND indnatts = 1 |
159 |
|
) |
160 |
|
}); |
161 |
|
|
162 |
|
} |
163 |
|
|
164 |
|
die "$0: can't find single column primary key for table ${table}. Please specify column with --pk\n" unless ($c->{pk}); |
165 |
|
|
166 |
|
warn "using primary key $c->{pk}\n"; |
167 |
|
|
168 |
$dbh->begin_work; |
$dbh->begin_work; |
169 |
|
|
170 |
|
$c->{sql} ||= "select * from $table"; |
171 |
|
|
172 |
my $sth = $dbh->prepare($c->{sql}) || die $dbh->errstr(); |
my $sth = $dbh->prepare($c->{sql}) || die $dbh->errstr(); |
173 |
$sth->execute() || die $sth->errstr(); |
$sth->execute() || die $sth->errstr(); |
174 |
|
|
175 |
my @cols = @{ $sth->{NAME} }; |
my @cols = @{ $sth->{NAME} }; |
176 |
|
|
177 |
|
die "SQL '$c->{sql}' didn't include primary key $c->{pk}\n" unless grep(/^\Q$c->{pk}\E$/, @cols); |
178 |
|
|
179 |
warn "# columns: ",join(",", @cols),"\n" if ($c->{debug}); |
warn "# columns: ",join(",", @cols),"\n" if ($c->{debug}); |
180 |
|
|
181 |
my $total = $sth->rows; |
my $total = $sth->rows; |