/[simile]/sql/sql.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sql/sql.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 33 by dpavlin, Sat Oct 25 12:02:49 2008 UTC revision 34 by dpavlin, Sat Oct 25 12:47:24 2008 UTC
# Line 9  use strict; Line 9  use strict;
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;
# Line 34  warn "# sql $sql"; Line 36  warn "# sql $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;

Legend:
Removed from v.33  
changed lines
  Added in v.34

  ViewVC Help
Powered by ViewVC 1.1.26