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 45     45   1404 use strict;
  45         94  
  45         1100  
3 45     45   220 use warnings;
  45         83  
  45         1123  
4 45     45   18453 use Bif::DB;
  45         144  
  45         381  
5 45     45   1642 use DBIx::ThinSQL qw//;
  45         88  
  45         915  
6 45     45   34535 use DBIx::ThinSQL::SQLite ':all';
  45         103793  
  45         320  
7 45     45   6094 use Log::Any '$log';
  45         137  
  45         233  
8              
9             our $VERSION = '0.1.5_7';
10             our @ISA = ('Bif::DB');
11              
12             create_methods(qw/nextval currval/);
13              
14             sub _connected {
15 132     132   291 my $dbh = shift;
16 132         233 my $debug = shift;
17              
18 132 50   0   537 $dbh->sqlite_trace( sub { $log->debug(@_) } ) if $debug;
  0         0  
19              
20 132         977 $dbh->do('PRAGMA foreign_keys = ON;');
21              
22             # $dbh->do('PRAGMA foreign_keys = OFF;');
23 132         16922 $dbh->do('PRAGMA temp_store = MEMORY;');
24 132         10952 $dbh->do('PRAGMA recursive_triggers = ON;');
25 132         10350 $dbh->do('PRAGMA synchronous = NORMAL;');
26 132 50       633262 $dbh->do('PRAGMA synchronous = OFF;') if $main::BIF_DB_NOSYNC;
27              
28 132         11841 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 132         192924 $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 132         12550 );
41              
42 132         821 return;
43             }
44              
45             sub connect {
46 132     132 1 22257 my $class = shift;
47 132         246 my $dsn = shift;
48 132         292 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 132     132   132556 connected => sub { _connected( shift, $debug ) }
58             },
59 132   50     2056 };
60              
61 132         1010 return $class->SUPER::connect( $dsn, $user, $password, $attrs );
62             }
63              
64             package Bif::DBW::db;
65             our @ISA = ('Bif::DB::db');
66              
67 45     45   20416 use DBIx::ThinSQL qw/case qv sq/;
  45         94  
  45         294  
68              
69             sub deploy {
70 85     85   246 my $db = shift;
71 85   33     398 my $version = shift || Carp::croak('deploy(VERSION)');
72              
73 85         32520 require DBIx::ThinSQL::Deploy;
74              
75             $db->txn(
76             sub {
77 85 50   85   28660 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 85         33330 require File::ShareDir;
89 85         262885 require Path::Tiny;
90              
91 85   33     1753 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 85         1483 'db-v' . $version
98             );
99              
100 85 50       4382 die "\nDirectory not found: " . $deploy_dir
101             unless -d $deploy_dir;
102              
103 85         3760 DBIx::ThinSQL::SQLite::create_sqlite_sequence($db);
104 85         37193 return $db->deploy_dir($deploy_dir);
105             }
106             }
107 85         124061 );
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_7 (2015-11-25)
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