line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::App::PgREST; |
2
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
32
|
|
3
|
1
|
|
|
1
|
|
679
|
use methods; |
|
1
|
|
|
|
|
93925
|
|
|
1
|
|
|
|
|
12
|
|
4
|
1
|
|
|
1
|
|
2373
|
use 5.008_001; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
46
|
|
5
|
1
|
|
|
1
|
|
1785
|
use Plack::Builder; |
|
1
|
|
|
|
|
34607
|
|
|
1
|
|
|
|
|
118
|
|
6
|
1
|
|
|
1
|
|
1035
|
use Router::Resource; |
|
1
|
|
|
|
|
26412
|
|
|
1
|
|
|
|
|
10
|
|
7
|
1
|
|
|
1
|
|
1276
|
use parent qw(Plack::Component); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
8
|
1
|
|
|
1
|
|
74
|
use Plack::Util::Accessor qw( dsn dbh); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
9
|
1
|
|
|
1
|
|
1128
|
use Plack::Request; |
|
1
|
|
|
|
|
183799
|
|
|
1
|
|
|
|
|
55
|
|
10
|
1
|
|
|
1
|
|
2850
|
use JSON::PP qw(encode_json decode_json); |
|
1
|
|
|
|
|
35453
|
|
|
1
|
|
|
|
|
110
|
|
11
|
1
|
|
|
1
|
|
11
|
use parent qw(Exporter); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
14
|
|
|
|
|
|
|
our @EXPORT = qw(pgrest); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub pgrest { |
17
|
0
|
0
|
|
0
|
0
|
0
|
__PACKAGE__->new( @_ > 1 ? @_ : dsn => 'dbi:Pg:'.$_[0] )->to_app; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# maintain json object field order |
21
|
1
|
|
|
1
|
|
1601
|
use Tie::IxHash; |
|
1
|
|
|
|
|
7614
|
|
|
1
|
|
|
|
|
221
|
|
22
|
|
|
|
|
|
|
my $obj_parser_sub = \&JSON::PP::object; |
23
|
|
|
|
|
|
|
*JSON::PP::object = sub { |
24
|
11
|
|
|
11
|
|
7317
|
tie my %obj, 'Tie::IxHash'; |
25
|
11
|
|
|
|
|
187
|
$obj_parser_sub->(\%obj); |
26
|
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub n { |
29
|
0
|
|
|
0
|
0
|
|
my $n = shift; |
30
|
0
|
0
|
|
|
|
|
return $n unless $n; |
31
|
0
|
0
|
|
|
|
|
return (undef) if $n eq 'NaN'; |
32
|
0
|
|
|
|
|
|
return int($n); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub j { |
36
|
0
|
|
|
0
|
0
|
|
my $n = shift; |
37
|
0
|
0
|
|
|
|
|
return $n unless $n; |
38
|
0
|
|
|
|
|
|
return decode_json $n |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
1
|
|
826
|
method select($param, $args) { |
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
43
|
1
|
|
|
1
|
|
85
|
use Data::Dumper; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
401
|
|
44
|
0
|
|
|
|
|
|
my $req = encode_json({ |
45
|
|
|
|
|
|
|
collection => $args->{collection}, |
46
|
|
|
|
|
|
|
l => n($param->get('l')), |
47
|
|
|
|
|
|
|
sk => n($param->get('sk')), |
48
|
|
|
|
|
|
|
c => n($param->get('c')), |
49
|
|
|
|
|
|
|
s => j($param->get('s')), |
50
|
|
|
|
|
|
|
q => j($param->get('q')), |
51
|
|
|
|
|
|
|
}); |
52
|
0
|
|
|
|
|
|
my $ary_ref = $self->{dbh}->selectall_arrayref("select pgrest_select(?)", {}, $req); |
53
|
0
|
0
|
|
|
|
|
if (my $callback = $param->get('callback')) { |
54
|
0
|
|
|
|
|
|
$callback =~ s/[^\w\[\]\.]//g; |
55
|
0
|
|
|
|
|
|
return [200, ['Content-Type', 'application/javascript; charset=UTF-8'], |
56
|
|
|
|
|
|
|
[ |
57
|
|
|
|
|
|
|
";($callback)($ary_ref->[0][0]);" |
58
|
|
|
|
|
|
|
] |
59
|
|
|
|
|
|
|
]; |
60
|
|
|
|
|
|
|
} |
61
|
0
|
|
|
|
|
|
return [200, ['Content-Type', 'application/json; charset=UTF-8'], [$ary_ref->[0][0]]]; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
1
|
|
|
1
|
|
403
|
method bootstrap { |
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
$self->{dbh}->do("select pgrest_boot('{}')"); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
1
|
|
317
|
method to_app { |
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
|
unless ($self->{dbh}) { |
70
|
0
|
|
|
|
|
|
require DBIx::Connector; |
71
|
0
|
|
|
|
|
|
$self->{conn} = DBIx::Connector->new($self->{dsn}, '', '', { |
72
|
|
|
|
|
|
|
RaiseError => 1, |
73
|
|
|
|
|
|
|
AutoCommit => 1, |
74
|
|
|
|
|
|
|
}); |
75
|
0
|
|
|
|
|
|
$self->{dbh} = $self->{conn}->dbh; |
76
|
|
|
|
|
|
|
} |
77
|
0
|
0
|
|
|
|
|
die unless $self->{dbh}; |
78
|
0
|
|
|
|
|
|
$self->bootstrap; |
79
|
|
|
|
|
|
|
my $router = router { |
80
|
|
|
|
|
|
|
resource '/collections/{collection}' => sub { |
81
|
0
|
|
|
|
|
|
GET { $self->select(Plack::Request->new($_[0])->parameters, $_[1]) }; |
|
0
|
|
|
|
|
|
|
82
|
0
|
|
|
0
|
|
|
}; |
83
|
0
|
|
|
|
|
|
}; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
builder { |
86
|
0
|
|
|
0
|
|
|
sub { $router->dispatch(shift) }; |
|
0
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |
91
|
|
|
|
|
|
|
__END__ |