File Coverage

blib/lib/Bif/DBW.pm
Criterion Covered Total %
statement 51 57 89.4
branch 4 10 40.0
condition 3 8 37.5
subroutine 12 14 85.7
pod 1 1 100.0
total 71 90 78.8


line stmt bran cond sub pod time code
1             package Bif::DBW;
2 54     54   1366 use strict;
  54         81  
  54         1662  
3 54     54   230 use warnings;
  54         76  
  54         1470  
4 54     54   17536 use Bif::DB;
  54         152  
  54         391  
5 54     54   2080 use DBIx::ThinSQL qw//;
  54         228  
  54         1095  
6 54     54   32340 use DBIx::ThinSQL::SQLite ':all';
  54         106624  
  54         394  
7 54     54   6559 use Log::Any '$log';
  54         137  
  54         288  
8              
9             our $VERSION = '0.1.5_5';
10             our @ISA = ('Bif::DB');
11              
12             create_methods(qw/nextval currval/);
13              
14             sub _connected {
15 159     159   326 my $dbh = shift;
16 159         307 my $debug = shift;
17              
18 159 50   0   513 $dbh->sqlite_trace( sub { $log->debug(@_) } ) if $debug;
  0         0  
19              
20 159         1297 $dbh->do('PRAGMA foreign_keys = ON;');
21              
22             # $dbh->do('PRAGMA foreign_keys = OFF;');
23 159         21026 $dbh->do('PRAGMA temp_store = MEMORY;');
24 159         12731 $dbh->do('PRAGMA recursive_triggers = ON;');
25 159         11451 $dbh->do('PRAGMA synchronous = NORMAL;');
26 159 50       746852 $dbh->do('PRAGMA synchronous = OFF;') if $main::BIF_DB_NOSYNC;
27              
28 159         14426 create_functions( $dbh,
29             qw/debug warn create_sequence nextval currval sha1_hex agg_sha1_hex/ );
30              
31             # TODO remove the following two statements before the first
32             # production release.
33 159         355897 $dbh->do('PRAGMA reverse_unordered_selects = ON;');
34             $dbh->sqlite_create_function(
35             'check_fks',
36             -1,
37             sub {
38 0     0   0 $dbh->check_fks;
39             }
40 159         15676 );
41              
42 159         1006 return;
43             }
44              
45             sub connect {
46 159     159 1 18194 my $class = shift;
47 159         276 my $dsn = shift;
48 159         311 my ( $user, $password, $attrs, $debug ) = @_;
49              
50             $attrs ||= {
51             RaiseError => 1,
52             PrintError => 0,
53             ShowErrorStatement => 1,
54             sqlite_see_if_its_a_number => 1,
55             sqlite_unicode => 1,
56             Callbacks => {
57 159     159   347263 connected => sub { _connected( shift, $debug ) }
58             },
59 159   50     2444 };
60              
61 159         1178 return $class->SUPER::connect( $dsn, $user, $password, $attrs );
62             }
63              
64             package Bif::DBW::db;
65             our @ISA = ('Bif::DB::db');
66              
67 54     54   21289 use DBIx::ThinSQL qw/case qv sq/;
  54         100  
  54         344  
68              
69             sub deploy {
70 103     103   231 my $db = shift;
71 103   33     454 my $version = shift || Carp::croak('deploy(VERSION)');
72              
73 103         28303 require DBIx::ThinSQL::Deploy;
74              
75             $db->txn(
76             sub {
77 103 50   103   101709 if ( defined &static::find ) {
78             my $src =
79 0         0 'auto/share/dist/App-bif/' . $db->{Driver}->{Name} . '.sql';
80              
81             my $sql = static::find($src)
82 0 0       0 or die 'unsupported database type: ' . $db->{Driver}->{Name};
83              
84 0         0 DBIx::ThinSQL::SQLite::create_sqlite_sequence($db);
85 0         0 return $db->deploy_sql($sql);
86             }
87             else {
88 103         31798 require File::ShareDir;
89 103         349386 require Path::Tiny;
90              
91 103   33     1714 my $share_dir = $main::BIF_SHARE_DIR
92             || File::ShareDir::dist_dir('App-bif');
93              
94             my $deploy_dir = Path::Tiny::path(
95             $share_dir,
96             $db->{Driver}->{Name},
97 103         2232 'db-v' . $version
98             );
99              
100 103 50       5298 die "\nDirectory not found: " . $deploy_dir
101             unless -d $deploy_dir;
102              
103 103         4131 DBIx::ThinSQL::SQLite::create_sqlite_sequence($db);
104 103         46187 return $db->deploy_dir($deploy_dir);
105             }
106             }
107 103         133264 );
108             }
109              
110             package Bif::DBW::st;
111             our @ISA = ('Bif::DB::st');
112              
113             1;
114              
115             =head1 NAME
116              
117             =for bif-doc #perl
118              
119             Bif::DBW - read-write helper methods for a bif database
120              
121             =head1 VERSION
122              
123             0.1.5_5 (2015-08-13)
124              
125             =head1 SYNOPSIS
126              
127             use strict;
128             use warnings;
129             use Bif::DBW;
130              
131             # Bif::DBW inherits Bif::DB -> DBIx::ThinSQL -> DBI.
132             my $dbw = Bif::DBW->connect( $dsn );
133              
134             # Read and write operations on a bif database:
135              
136             $dbw->txn(sub {
137             my ($old,$new) = $dbw->deploy;
138              
139             $dbw->xdo(
140             insert_into => 'changes',
141             values => $hashref,
142             );
143             });
144              
145             =head1 DESCRIPTION
146              
147             B is a L derivative that provides various read-write
148             methods for retrieving information from a L repository. For a
149             read-only equivalent see L. The read-only and read-write parts
150             are separated for security and performance reasons.
151              
152             =head1 DBH METHODS
153              
154             =over
155              
156             =item nextval( $name ) -> Int
157              
158             Advance the sequence C<$name> to its next value and return that value.
159              
160             =item currval( $name ) -> Int
161              
162             Return the current value of the sequence <$name>.
163              
164             =item deploy -> (Int, Int)
165              
166             Deploys the current Bif distribution schema to the database, returning
167             the previous (possibly 0) and newly deployed versions.
168              
169             =back
170              
171             =head1 SQLITE FUNCTIONS
172              
173             The following SQL functions created using the user-defined-function
174             feature of SQLite.
175              
176             =over
177              
178             =item create_sequence()
179              
180             TODO
181              
182             =item nextval( $name ) -> Int
183              
184             Advance the sequence C<$name> to its next value and return that value.
185              
186             =item currval( $name ) -> Int
187              
188             Return the current value of the sequence <$name>.
189              
190             =item debug()
191              
192             TODO
193              
194             =item sha1_hex()
195              
196             TODO
197              
198             =item agg_sha1_hex()
199              
200             TODO
201              
202             =back
203              
204             =head1 SEE ALSO
205              
206             L, L
207              
208             =head1 AUTHOR
209              
210             Mark Lawrence Enomad@null.netE
211              
212             =head1 COPYRIGHT AND LICENSE
213              
214             Copyright 2013-2015 Mark Lawrence
215              
216             This program is free software; you can redistribute it and/or modify it
217             under the terms of the GNU General Public License as published by the
218             Free Software Foundation; either version 3 of the License, or (at your
219             option) any later version.
220