9 |
|
|
10 |
use DBI; |
use DBI; |
11 |
use JSON; |
use JSON; |
12 |
|
use Data::Dump qw/dump/; |
13 |
|
|
14 |
my $dsn = 'DBI:Pg:dbname=new'; |
my $dsn = 'DBI:Pg:dbname=new'; |
15 |
|
my $debug = 0; |
16 |
|
|
17 |
my $comment; |
my $comment; |
18 |
my $sql; |
my $sql; |
36 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
37 |
$sth->execute(); |
$sth->execute(); |
38 |
|
|
39 |
my $items; |
my $json; |
40 |
|
# I would love to use $sth->{TYPE} to get types from database, but it's just |
41 |
|
# not verbose enough to be useful for us, so we are using _n suffix |
42 |
|
|
43 |
|
my $rename_cols; |
44 |
|
|
45 |
|
foreach my $col ( @{ $sth->{NAME} } ) { |
46 |
|
warn "## check type of $col" if $debug; |
47 |
|
my $old_name = $col; |
48 |
|
if ( $col =~ s/_valueType_(\w+)//i ) { |
49 |
|
$json->{properties}->{$col} = { valueType => $1 }; |
50 |
|
$rename_cols->{ $old_name } = $col; |
51 |
|
} |
52 |
|
} |
53 |
|
|
54 |
|
warn "## rename_cols ", dump( $rename_cols ); |
55 |
|
|
56 |
while ( my $row = $sth->fetchrow_hashref ) { |
while ( my $row = $sth->fetchrow_hashref ) { |
57 |
push @$items, $row; |
$row->{ $rename_cols->{ $_ } } = delete $row->{ $_ } foreach keys %$rename_cols; |
58 |
|
push @{ $json->{items} }, $row; |
59 |
} |
} |
60 |
|
|
61 |
print to_json( { items => $items } ); |
print to_json( $json ); |
62 |
|
# we have to call dump *AFTER* to_json because it screws types |
63 |
|
warn "# json = ",dump( $json ) if $debug; |