App/Chart/DBI.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 118 CC 6 SCAR 17.6
CC
6
Cov
0%
_new_instance
20.0
disconnect
6.0
read_single
6.0
CRAP 5.8
1 # Copyright 2007, 2008, 2009, 2010, 2011 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Chart. If not, see <http://www.gnu.org/licenses/>.
16  
17 package App::Chart::DBI;
18 use 5.006;
19 use strict;
20 use warnings;
21 use File::Spec;
22  
23 use App::Chart;
24  
25 # uncomment this to run the ### lines
26 #use Smart::Comments;
27  
28 # See Database/Create.pm for revisions
29 use constant DATABASE_SCHEMA_VERSION => 2;
30  
31  
32 # return the database filename ~/Chart/database.sqdb or the notes database
33 # filename ~/Chart/notes.sqdb, as absolute path in filesystem charset bytes
34 use constant::defer database_filename => sub {
35 return File::Spec->catfile (App::Chart::chart_directory(), 'database.sqdb');
36 };
37 use constant::defer notes_filename => sub {
38 return File::Spec->catfile(App::Chart::chart_directory(), 'notes.sqdb');
39 };
40  
41 use Class::Singleton 1.4;
42 use base 'Class::Singleton';
43 our $_instance;
44  
45 # singleton
46 sub _new_instance {
47 ### Chart-DBI _new_instance()
48 my $database_filename = database_filename();
49 ### $database_filename
50  
51 if (! -e $database_filename) {
52 require App::Chart::Database::Create;
53 App::Chart::Database::Create::initial_database ($database_filename);
54 }
55  
56 my $notes_filename = notes_filename();
57 ### $notes_filename
58 if (! -e $notes_filename) {
59 require App::Chart::Database::Create;
60 App::Chart::Database::Create::initial_notes ($notes_filename);
61 }
62  
63 require DBI;
64 my $dbh = DBI->connect ("dbi:SQLite:dbname=$database_filename",
65 '', '', {RaiseError=>1});
66 $dbh->func(90_000, 'busy_timeout'); # 90 seconds
67 $dbh->{'sqlite_unicode'} = 1;
68 $dbh->do ('ATTACH DATABASE ' . $dbh->quote($notes_filename)
69 . ' AS notesdb');
70  
71 my ($dbversion) = do {
72 local $dbh->{RaiseError} = undef;
73 local $dbh->{PrintError} = undef;
74 $dbh->selectrow_array
75 ("SELECT value FROM extra WHERE key='database-schema-version'")
76 };
77 $dbversion ||= 0;
78 if ($dbversion < DATABASE_SCHEMA_VERSION) {
79 require App::Chart::Database::Create;
80 $_instance = $dbh;
81 App::Chart::Database::Create::upgrade_database ($dbh, $dbversion);
82 }
83  
84 return $dbh;
85 }
86  
87 sub disconnect {
88 my ($class) = @_;
89 ### Chart-DBI _disconnect()
90 if (my $dbh = $class->has_instance) {
91  
92 # Empty cache to suppress warnings about statement handles.
93 # Is this supposed to be necessary?
94 $dbh->{'CachedKids'} = {};
95  
96 $dbh->disconnect;
97 no strict; # created on-demand by Singleton
98 $_instance = undef;
99 }
100 }
101  
102 sub read_single {
103 my ($dbh, $sql, @args) = @_;
104 if (! ref $dbh) { $dbh = $dbh->instance; }
105 { local $SIG{__DIE__} = sub { die "read_single('$sql')\n$@" };
106 my $sth = $dbh->prepare_cached ($sql);
107 my ($ret) = $dbh->selectrow_array ($sth, undef, @args);
108 $sth->finish;
109 return $ret;
110 }
111 }
112  
113 # sub transaction {
114 # my ($dbh, $subr) = @_;
115 # my $hold = App::Chart::chart_dirbroadcast()->hold;
116 #
117 # if ($dbh->{AutoCommit}) {
118 # $dbh->begin_work;
119 # local $SIG{__DIE__} = sub {
120 # ### Error during DBI transaction: "@_"
121 # $dbh->rollback;
122 # die @_;
123 # };
124 # $subr->();
125 # $dbh->commit;
126 # } else {
127 # $subr->();
128 # }
129 # }
130  
131 1;
132 __END__