line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cookieville::Read; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Cookieville::Read - Controller for getting data from the schema |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
7712
|
use Mojo::Base 'Mojolicious::Controller'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
13
|
|
10
|
2
|
|
|
2
|
|
357
|
use Mojo::JSON 'decode_json'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1416
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 METHODS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head2 search |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Get data from a given source. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub search { |
21
|
14
|
|
|
14
|
1
|
28962
|
my $self = shift; |
22
|
14
|
|
|
|
|
26
|
my $rs = eval { $self->db->resultset($self->stash('source')) }; |
|
14
|
|
|
|
|
420
|
|
23
|
14
|
|
100
|
|
|
21347
|
my $q = decode_json($self->param('q') || 'null'); |
24
|
14
|
|
|
|
|
7825
|
my %extra; |
25
|
|
|
|
|
|
|
|
26
|
14
|
100
|
|
|
|
62
|
unless ($rs) { |
27
|
|
|
|
|
|
|
return $self->respond_to( |
28
|
1
|
|
|
1
|
|
475
|
csv => sub { shift->render(text => 'No source by that name.', status => 404); }, |
29
|
0
|
|
|
0
|
|
0
|
txt => sub { shift->render(text => 'No source by that name.', status => 404); }, |
30
|
3
|
|
|
3
|
|
1412
|
any => sub { shift->render(json => {message => 'No source by that name.'}, status => 404); }, |
31
|
4
|
|
|
|
|
43
|
); |
32
|
|
|
|
|
|
|
} |
33
|
10
|
100
|
|
|
|
35
|
unless (ref $q eq 'HASH') { |
34
|
|
|
|
|
|
|
return $self->respond_to( |
35
|
1
|
|
|
1
|
|
447
|
csv => sub { shift->render(text => 'Invalid (q) query param.', status => 400); }, |
36
|
1
|
|
|
1
|
|
438
|
any => sub { shift->render(json => {message => 'Invalid (q) query param.'}, status => 400); }, |
37
|
2
|
|
|
|
|
19
|
); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
8
|
100
|
|
|
|
30
|
$extra{order_by} = decode_json $self->param('order_by') if $self->param('order_by'); |
41
|
8
|
100
|
|
|
|
1158
|
$extra{page} = $self->param('page') if $self->param('page'); |
42
|
8
|
100
|
|
|
|
818
|
$extra{rows} = $self->param('limit') if $self->param('limit'); |
43
|
8
|
100
|
|
|
|
842
|
$extra{columns} = decode_json $self->param('columns') if $self->param('columns'); |
44
|
|
|
|
|
|
|
|
45
|
8
|
|
|
|
|
1065
|
$self->stash(extra => \%extra, rs => $rs->search_rs($q, \%extra)); |
46
|
8
|
|
|
|
|
1927
|
$self->respond_to(csv => \&_search_as_csv, txt => \&_search_as_csv, any => \&_search_as_json,); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _search_as_csv { |
50
|
2
|
|
|
2
|
|
892
|
my $self = shift; |
51
|
2
|
|
|
|
|
5
|
my $rs = $self->stash('rs'); |
52
|
2
|
|
|
|
|
14
|
my @columns; |
53
|
|
|
|
|
|
|
my @csv; |
54
|
|
|
|
|
|
|
|
55
|
2
|
100
|
|
|
|
6
|
if (my $columns = $self->stash('extra')->{columns}) { |
56
|
1
|
|
|
|
|
9
|
@columns = @$columns; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
1
|
|
|
|
|
13
|
@columns = $rs->result_source->columns; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
2
|
|
|
|
|
13
|
push @csv, join ',', @columns; |
63
|
|
|
|
|
|
|
|
64
|
2
|
|
|
|
|
9
|
while (my $row = $rs->next) { |
65
|
4
|
|
|
|
|
42
|
push @csv, join ',', map { |
66
|
2
|
|
|
|
|
5835
|
my $v = $row->get_column($_); |
67
|
4
|
|
|
|
|
29
|
$v =~ s!"!""!g; |
68
|
4
|
100
|
|
|
|
26
|
$v =~ /[\s,]/ ? qq("$v") : $v; |
69
|
|
|
|
|
|
|
} @columns; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
2
|
|
|
|
|
96
|
$self->render(text => join '', map {"$_\n"} @csv); |
|
4
|
|
|
|
|
15
|
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _search_as_json { |
76
|
6
|
|
|
6
|
|
2785
|
my $self = shift; |
77
|
6
|
|
|
|
|
17
|
my $rs = $self->stash('rs'); |
78
|
6
|
|
|
|
|
47
|
my @data; |
79
|
|
|
|
|
|
|
|
80
|
6
|
|
|
|
|
29
|
while (my $row = $rs->next) { |
81
|
5
|
|
|
|
|
25763
|
push @data, {$row->get_columns}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
6
|
|
|
|
|
2805
|
$self->render(json => {data => \@data}); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 AUTHOR |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Jan Henning Thorsen - C |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |