blib/lib/Data/Package/SQLite.pm

filter: Statementstmt 100% 8 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 11 / 11 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Data::Package::SQLite;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Data::Package::SQLite - A data package for a SQLite database
8  
9 =head1 SYNOPSIS
10  
11 ### Creating a SQLite data package
12
13 package My::Data;
14
15 use strict;
16 use base 'Data::Package::SQLite';
17
18 sub sqlite_locate { file => '/var/cache/mydata.sqlite' }
19
20 1;
21
22 ### Using your SQLite data package
23
24 use My::Data;
25
26 # Default data access
27 $dbh = My::Data->get;
28
29 # ... or if you want to be explicit
30 $dbh = My::Data->get('DBI::db');
31  
32 =head1 DESCRIPTION
33  
34 One of the best ways to distribute medium-sized (100k - 100meg) packages
35 containing datasets is using L<SQLite> databases.
36  
37 It allows you to have full SQL access to your data, and you can still
38 provide the data as a single file that does not need user/password access
39 and provides it's own security model (tied to filesystem access).
40  
41 C<Data::Package::SQLite> is a L<Data::Package> sub-class for providing
42 simplified read-only access to these databases in the form of a L<DBI>
43 connection handle, without the caller needing to know anything about
44 where the data is actually stored.
45  
46 =head1 METHODS
47  
48 Although the primary interface when using a C<Data::Package::SQLite>
49 module should be the same as for any other L<Data::Package> module,
50 some additional methods are defined for people creating their own
51 sub-classes of C<Data::Package::SQLite>.
52  
53 =cut
54  
55 2 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use base 'Data::Package';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 use Carp ();
59 use File::ShareDir ();
60 use Params::Util qw{_STRING _CLASS};
61 use DBI;
62  
63 use vars qw{$VERSION};
64 BEGIN {
65 $VERSION = '1.01';
66 }
67  
68 # Check the SQLite driver is available
69 unless ( grep { $_ eq 'SQLite' } DBI->available_drivers ) {
70 Carp::croak("SQLite DBI driver is not installed");
71 }
72  
73 # This class only provides a database handle
74 sub _provides { 'DBI::db' }
75  
76 # Load the database handle
77 sub __as_DBI_db {
78 my $self = shift;
79  
80 # Create the database handle
81 my $dbh = DBI->connect( $self->sqlite_dsn, "", "" );
82 unless ( $dbh ) {
83 Carp::croak("SQLite connection failed: $DBI::errstr");
84 }
85  
86 return $dbh;
87 }
88  
89 =pod
90  
91 =head2 sqlite_dsn
92  
93 The C<sqlite_dsn> method return a valid L<DBI> dsn for the creation
94 of the database handle. For any C<Data::Package::SQLite> package that
95 you C<get> a database handle from, the C<sqlite_dsn> method will
96 always return the location of the database that was loaded.
97  
98 When creating a sub-class, you should not return this directly, but
99 are encouraged to instead define your own C<sqlite_file> or even better
100 your own C<sqlite_location>.
101  
102 Returns a DSN string, or throws an exception on error.
103  
104 =cut
105  
106 sub sqlite_dsn {
107 my $self = shift;
108 my $dbfile = $self->sqlite_file;
109 unless ( _STRING($dbfile) ) {
110 Carp::croak("SQLite file name not provided, or not a string");
111 }
112 unless ( -f $dbfile ) {
113 Carp::croak("SQLite file '$dbfile' does not exist");
114 }
115 unless ( -r $dbfile ) {
116 Carp::croak("SQLite file '$dbfile' cannot be read");
117 }
118 return "dbi:SQLite:dbname=$dbfile";
119 }
120  
121 =pod
122  
123 =head2 sqlite_file
124  
125 sub sqlite_file { '/var/cache/my_class/data.sqlite' }
126  
127 The C<sqlite_file> method returns the location of the SQLite file to
128 be loaded.
129  
130 Please note that the fact a file name is returned by this method does
131 not necesarily mean it exists, because in some cases incorrect file
132 names can be generated, or a sub-class might defined this method
133 (incorrectly) directly.
134  
135 Returns a file path string, or throws an exception in some error
136 situations.
137  
138 =cut
139  
140 sub sqlite_file {
141 my $self = shift;
142 my @location = $self->sqlite_location;
143 my $type = _STRING(shift @location)
144 or Carp::croak('No or bad SQLite location type provided');
145  
146 if ( $type eq 'file' ) {
147 return shift @location;
148  
149 } elsif ( $type eq 'dist_file' ) {
150 my $dist = _STRING(shift @location)
151 or Carp::croak('No dist_file distribution provided');
152 my $file = _STRING(shift @location) || 'data.sqlite';
153 return File::ShareDir::dist_file( $dist, $file );
154  
155 } elsif ( $type eq 'module_file' ) {
156 my $module = _CLASS(shift @location)
157 or Carp::croak('Invalid or no module name provided');
158 my $file = _STRING(shift @location) || 'data.sqlite';
159 return File::ShareDir::module_file( $module, $file );
160  
161 } else {
162 Carp::croak("Unknown or unsupported location type '$type'");
163 }
164 }
165  
166 =pod
167  
168 =head2 sqlite_location
169  
170 # A general file location somewhere
171 sub sqlite_location { file => '/var/cache/my_class/data.sqlite' }
172
173 # The default data.sqlite for a distribution
174 sub sqlite_location { dist_file => 'My-Dist' }
175
176 # A specific file for a distribution
177 sub sqlite_location { dist_file => 'My-Dist', 'sqlite.db' }
178
179 # The default data.sqlite for a module
180 sub sqlite_location { module_file => 'My::Module' }
181
182 # A specific file for a module
183 sub sqlite_location { module_file => 'My::Module', 'sqlite.db' }
184  
185 The C<sqlite_location> method is the primary method for sub-classes to
186 specify the location of the SQLite database file.
187  
188 It should return a simple 2-3 element list, consisting of a location
189 type and 1 or more location values.
190  
191 The C<sqlite_location> method currently accepts 3 location types.
192  
193 =over 4
194  
195 =item file
196  
197 Mostly provides a direct pass-through to C<sqlite_file>.
198  
199 Takes a second param of the location of the file as a simple string.
200  
201 =item dist_file
202  
203 The C<dist_file> option provides access to the functionality provided
204 by the L<File::ShareDir> function C<dist_file>.
205  
206 It takes two additional values, the name of the distribution and the
207 name of the file within the dist dir.
208  
209 If the file name is not provided, a default value of F<data.sqlite>
210 is used.
211  
212 =item module_file
213  
214 The C<module_file> option provides access to the functionality provided
215 by the L<File::ShareDir> function C<module_file>.
216  
217 It takes two additional values, the name of the module and the
218 name of the file within the dist dir.
219  
220 If the file name is not provided, a default value of F<data.sqlite>
221 is used.
222  
223 =back
224  
225 If not provided, the default implementation of C<sqlite_location> will
226 return C<( module_file =E<gt> $class )>, where C<$class> is the name
227 of the L<Data::Package::SQLite> sub-class.
228  
229 =cut
230  
231 sub sqlite_location {
232 my $class = ref($_[0]) || "$_[0]";
233  
234 # We don't know for sure that the class actually has
235 # a data.sqlite file, but it is the best guess.
236 return ( 'module_file' => $class );
237 }
238  
239 1;
240  
241 =pod
242  
243 =head1 SUPPORT
244  
245 Bugs should be B<always> be reported via the CPAN bug tracker at:
246  
247 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Package-SQLite>
248  
249 For other issues, contact the author.
250  
251 =head1 AUTHORS
252  
253 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
254  
255 =head1 SEE ALSO
256  
257 L<Data::Package>, L<DBD::SQLite>, L<http://ali.as/>
258  
259 =head1 COPYRIGHT
260  
261 Copyright 2006 Adam Kennedy.
262  
263 This program is free software; you can redistribute
264 it and/or modify it under the same terms as Perl itself.
265  
266 The full text of the license can be found in the
267 LICENSE file included with this module.
268  
269 =cut
↑ Summary