Archive/SQL.pm

filter: Statementstmt 96.1% 126 / 131 Branchbran 50.0% 6 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.2% 33 / 35 Podpod 0.0% 0 / 5 totaltotal 90.1% 165 / 183 CC 7 SCAR 4.1
CC
7
Cov
92%
store
3.2
_extract_fact
3.1
initialize
3.0
CRAP 1.5
« Backend/SQL.pm ↑ Summary Index/PostgreSQL.pm »
1 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Metabase::Archive::SQL;
6 # ABSTRACT: Metabase archive backend role for common SQL actions
7  
8 3 our $VERSION = '1.001';
9  
10 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Compress::Zlib 2 qw(compress uncompress);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use DBI 1 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use DBIx::RunSQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use DBIx::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Data::Stream::Bulk::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Data::Stream::Bulk::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Data::Stream::Bulk::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use JSON 2 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use List::AllUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Metabase::Fact;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use SQL::Translator 0.11006 (); # required for deploy()
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use SQL::Translator::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use SQL::Translator::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use SQL::Translator::Utils qw/normalize_name/;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 with 'Metabase::Backend::SQL';
34 3 with 'Metabase::Archive' => { -version => 1.000 };
35  
36 3 has 'compressed' => (
37 is => 'rw',
38 isa => 'Bool',
39 default => 1,
40 );
41  
42 3 has _table_name => (
43 is => 'ro',
44 isa => 'Str',
45 default => 'metabase_archive',
46 );
47  
48 9 ▶sub initialize {
Subroutine initialize: called CC=3 SCAR=11.1
Pod undocumented
49 9 my ($self, @fact_classes) = @_;
50 9 my $schema = $self->schema;
51 9 my $table = SQL::Translator::Schema::Table->new( name => $self->_table_name );
52 $table->add_field(
53 name => 'guid',
54 is_nullable => 0,
55 9 ▶ %{$self->_guid_field_params}
Branch: 1 branch
BranchTrueFalseTotal
$table->add_field("name", "guid", "is_nullable", 0, %{$self->_guid_field_params;}) or die 9 0 9
56 ) or die;
57 $table->add_field(
58 name => 'fact',
59 is_nullable => 0,
60 9 ▶ %{$self->_blob_field_params}
Branch: 1 branch
BranchTrueFalseTotal
$table->add_field("name", "fact", "is_nullable", 0, %{$self->_blob_field_params;}) or die 9 0 9
61 ) or die;
62 9 $table->add_constraint(
63 name => $self->_table_name . "_pk",
64 fields => ['guid'],
65 type => PRIMARY_KEY,
66 );
67 9 $schema->add_table($table);
68 9 $self->_deploy_schema;
69 9 return;
70 }
71  
72 # given fact, store it and return guid; return
73 # XXX can we store a fact with a GUID already? Replaces? Or error?
74 # here assign only if no GUID already
75 10 ▶sub store {
Subroutine store: called CC=3 SCAR=11.5
Pod undocumented
76 10 my ( $self, $fact_struct ) = @_;
77 10 my $guid = lc $fact_struct->{metadata}{core}{guid};
78  
79 10 ▶ unless ($guid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($guid) 0 10 10
80 0 Carp::confess "Can't store: no GUID set for fact\n";
81 }
82  
83 # remove any metadata that can be regenerated
84 my $fact = {
85 content => $fact_struct->{content},
86 metadata => { core => $fact_struct->{metadata}{core} },
87 10 };
88  
89 10 my $json = eval { JSON->new->utf8->encode($fact) };
90 10 ▶ Carp::confess "Couldn't convert to JSON: $@"
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 10 10
91 unless $json;
92  
93 # if ( $self->compressed ) {
94 # $json = compress($json);
95 # }
96  
97  
98 10 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
99 10 $self->dbis->begin_work();
100 10 $self->dbis->insert($self->_table_name, {
101 guid => $self->_munge_guid($guid),
102 fact => $json,
103 });
104 10 $self->dbis->commit;
105 }
106 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 $self->dbis->rollback;
108 0 Carp::confess("Error inserting record: $_");
109 10 };
110  
111 10 return $guid;
112 }
113  
114 # given guid, retrieve it and return it
115 # type is directory path
116 # class isa Metabase::Fact::Subclass
117 2 ▶sub extract {
Subroutine extract: called CC=1 SCAR=0.0
Pod undocumented
118 2 my ( $self, $guid ) = @_;
119 2 my $rs = $self->dbis->select($self->_table_name, 'fact', {
120 guid => $self->_munge_guid($guid)
121 });
122 2 return $self->_extract_fact($rs->fetch->[0]);
123 }
124  
125 8 ▶sub _extract_fact {
Subroutine _extract_fact: called CC=3 SCAR=11.2
126 8 my ($self, $json) = @_;
127 8 ▶ return unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 8 8
128  
129 # if ( $self->compressed ) {
130 # $json = uncompress($json);
131 # }
132  
133 8 my $fact = eval { JSON->new->utf8->decode($json) };
134 8 ▶ Carp::confess "Couldn't convert from JSON: $@"
Branch: 1 branch
BranchTrueFalseTotal
unless $fact 0 8 8
135 unless $fact;
136  
137 8 return $fact;
138 }
139  
140 2 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.1
Pod undocumented
141 2 my ( $self, $guid ) = @_;
142  
143 2 my $rs;
144 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
145 2 $self->dbis->begin_work();
146 2 $rs = $self->dbis->delete($self->_table_name, {
147 guid => $self->_munge_guid($guid)
148 });
149 2 $self->dbis->commit;
150 }
151 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 0 $self->dbis->rollback;
153 0 Carp::confess("Error deleting record: $_");
154 2 };
155  
156 2 return $rs->rows;
157 }
158  
159 6 ▶sub iterator {
Subroutine iterator: called CC=1 SCAR=0.0
Pod undocumented
160 6 my ($self) = @_;
161 6 my $rs = $self->dbis->select($self->_table_name, 'fact'); # everything
162  
163 6 my $sth = $rs->{st}{sth}; # XXX encapsulation violation, oh, well
164  
165 # Not all DB's set 'Active' on the sth correctly
166 # so fall back to fetching all data if it can't
167 6 my $dbi_stream;
168 6 if ( 0 && $sth->FETCH('Active') ) {
169 $dbi_stream = Data::Stream::Bulk::DBI->new(
170 sth => $sth
171 );
172 }
173 else {
174 6 $dbi_stream = Data::Stream::Bulk::Array->new(
175 array => scalar $rs->arrays,
176 );
177 }
178  
179 return Data::Stream::Bulk::Filter->new(
180 stream => $dbi_stream,
181 6 ▶ filter => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
182 6 my $block = shift;
183 6 return [ map { $self->_extract_fact($_->[0]) } @$block ];
184 },
185 6 );
186 }
187  
188 3 1;
189  
190 __END__
« Backend/SQL.pm ↑ Summary Index/PostgreSQL.pm »