1 |
dpavlin |
246 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
|
6 |
|
|
use DBI; |
7 |
|
|
use Data::Dump qw/dump/; |
8 |
|
|
use JSON::XS; |
9 |
|
|
|
10 |
|
|
my $debug = 0; |
11 |
|
|
my $dsn = 'DBI:Pg:dbname=a3c'; |
12 |
|
|
|
13 |
|
|
my $dbh = DBI->connect($dsn,"","", { 'RaiseError' => '1' }) || die $DBI::errstr; |
14 |
|
|
$dbh->do( qq{ set client_encoding='utf-8' } ); |
15 |
|
|
|
16 |
|
|
my $json = JSON::XS->new; |
17 |
|
|
$json->utf8( 1 ); |
18 |
|
|
$json->pretty( 1 ); |
19 |
|
|
|
20 |
|
|
my $sth = $dbh->prepare( 'select * from stats.instance_organization' ); |
21 |
|
|
$sth->execute(); |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
my @items; |
25 |
|
|
|
26 |
|
|
while (my $row = $sth->fetchrow_hashref ) { |
27 |
|
|
warn "# row = ",dump( $row ) if $debug; |
28 |
|
|
|
29 |
|
|
foreach my $col ( keys %$row ) { |
30 |
|
|
delete $row->{$col} unless defined $row->{$col}; |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
push @items, $row; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
print $json->encode( |
37 |
|
|
{ |
38 |
|
|
items => [ @items ], |
39 |
|
|
} |
40 |
|
|
); |