/[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

Contents of /sql/sql.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations)
Sat Oct 25 12:02:49 2008 UTC (15 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 724 byte(s)
convert SQL query using DBI to exhibit JSON

- example using Movable Type database
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 # sql.pl - covert SQL files to json
7 #
8 # 10/25/08 00:06:01 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
9
10 use DBI;
11 use JSON;
12
13 my $dsn = 'DBI:Pg:dbname=new';
14
15 my $comment;
16 my $sql;
17
18 while(<>) {
19 if (/^\\c\s*(.+)/ ) {
20 my $database = $1;
21 $dsn =~ s/=.*$/=$database/;
22 } elsif ( /^--(.+)/ ) {
23 $comment .= "$1\n";
24 } else {
25 $sql .= $_;
26 }
27 }
28
29 warn "# dsn = $dsn";
30 my $dbh = DBI->connect( $dsn, '', '', { RaiseError => 1 } ) || die $DBI::errstr;
31 $dbh->do( qq{ set client_encoding='utf-8' } );
32
33 warn "# sql $sql";
34 my $sth = $dbh->prepare( $sql );
35 $sth->execute();
36
37 my $items;
38
39 while ( my $row = $sth->fetchrow_hashref ) {
40 push @$items, $row;
41 }
42
43 print to_json( { items => $items } );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26