File Coverage

blib/lib/DBX/Simple.pm
Criterion Covered Total %
statement 23 51 45.1
branch 0 4 0.0
condition 1 2 50.0
subroutine 8 14 57.1
pod 1 1 100.0
total 33 72 45.8


line stmt bran cond sub pod time code
1             package DBX::Simple;
2              
3 2     2   49019 use 5.006;
  2         8  
4 2     2   11 use strict;
  2         4  
  2         44  
5 2     2   10 use warnings;
  2         7  
  2         109  
6              
7             =head1 NAME
8              
9             DBX::Simple - Yet another DBI simplification wrapper.
10              
11             =head1 VERSION
12              
13             Version 0.01
14              
15             =cut
16              
17             our $VERSION = '0.01';
18              
19              
20             =head1 SYNOPSIS
21              
22             This is my DBI wrapper. There are many like it, but this one is mine.
23              
24             I have a horrible memory for syntactic detail. This is one reason I've been developing a semantically oriented programming
25             language for the past decade, but in the meantime, when I write Perl to manage my accounting or suck data from the Web,
26             I use SQLite through DBI and I just can't ever remember the syntax. I don't know why. I'm usually fine on SQL syntax, at
27             least for the basic things I do most of the time, but the actual DBI methods escape me again and again.
28              
29             This module is the syntax in my head, just so I don't have to keep looking everything up every single time. The class
30             subclasses DBI anyway, so anything you would do in DBI, you can do here - but with some simplifying alternative methods
31             as well.
32              
33             use DBX::Simple;
34            
35             my $dbh->DBX::Simple->connect(--DBI syntax--); # Just so I can support everything DBI supports, after looking it up.
36             my $dbh->DBX::Simple->open('sqlite file'); # 99% of my work. ->mysql and ->postgresql would also be reasonable.
37            
38             my $value = $dbh->get ('select value from table where id=?', $id); # Single value retrieval in one whack.
39             my @rows = $dbh->select ('select * from table'); # Rowset retrieval. Yes, I know about selectrow_array. I just can't remember it.
40             my $iter = $dbh->iterate ('select * from table'); # Returns an iterator that returns row arrayrefs.
41             my $sth = $dbh->prepare (--DBI syntax--);
42            
43             $dbh->do ("insert ..."); # Regular insertion, just like in DBI, except the hashref is skipped because I can never remember it.
44             my $record = $dbh->insert ("insert ..."); # Calls last_insert_id ('', '', '', ''), which will likely fail except with SQLite.
45            
46             Simple. Like the name says. And exposes DBI anyway for when simple won't cut it, or when DBI is already simple.
47              
48             One thing to notice: the class structure differs from DBI. DBX::Simple actually subclasses DBI::db - except for the C
49             method. So any class-level calls should still be done through DBI, not DBX::Simple. (Easy for me to forget, as I never actually
50             do class-level stuff, except for those very rare times when I do.)
51              
52             =cut
53              
54 2     2   4861 use DBI;
  2         49820  
  2         141  
55 2     2   18 use vars qw(@ISA);
  2         4  
  2         239  
56             @ISA = qw(DBI);
57              
58             =head1 SUBROUTINES/METHODS
59              
60             =head2 open
61              
62             The C method opens an SQLite database file. Uses 'db.sqlt' if no filename is provided.
63              
64             =cut
65              
66             sub open {
67 1     1 1 43 my $class = shift;
68 1   50     7 my $file = shift || 'db.sqlt';
69 1         13 $class->connect('dbi:SQLite:dbname=' . $file);
70             }
71              
72              
73             package DBX::Simple::db;
74 2     2   9 use vars qw(@ISA);
  2         4  
  2         680  
75             @ISA = qw(DBI::db);
76              
77              
78             =head2 get
79              
80             The C method retrieves a single value. I do this a I and it's a pain to bind variables
81             for one stinking value.
82              
83             =cut
84              
85             sub get {
86 0     0     my $self = shift;
87 0           my $query = shift;
88 0           my $sth = $self->prepare($query);
89 0           $sth->execute(@_);
90 0           my $row = $sth->fetchrow_arrayref;
91 0           $row->[0];
92             }
93              
94             =head2 select
95              
96             The C
97             In scalar mode, returns the arrayref from C.
98              
99             =cut
100              
101             sub select {
102 0     0     my $self = shift;
103 0           my $query = shift;
104 0 0         return unless defined wantarray;
105 0           my $sth = $self->prepare($query);
106 0           $sth->execute(@_);
107 0           my $ret = $sth->fetchall_arrayref;
108 0 0         return wantarray ? @$ret : $ret;
109             }
110              
111             =head2 iterate
112              
113             This could be done from the C end as well, but sometimes I already have a perfectly
114             good dbh and all I want is to iterate rows without the overhead of loading them all at once.
115              
116             =cut
117              
118             sub iterate {
119 0     0     my $self = shift;
120 0           my $query = shift;
121 0           my $sth = $self->prepare($query);
122 0           $sth->execute(@_);
123             sub {
124 0     0     $sth->fetchrow_arrayref;
125             }
126 0           }
127              
128             =head2 insert
129              
130             The C command calls C after the insertion. Just a little shorthand.
131              
132             =cut
133              
134             sub insert {
135 0     0     my $self = shift;
136 0           my $query = shift;
137 0           my $sth = $self->prepare($query);
138 0           $sth->execute(@_);
139 0           $self->last_insert_id('', '', '', '');
140             }
141              
142             =head2 do
143              
144             The C command works a little differently; DBI's version wants a hashref of attributes that I never use
145             and regularly screw up.
146              
147             =cut
148              
149             sub do {
150 0     0     my $self = shift;
151 0           my $query = shift;
152 0           my $sth = $self->prepare($query);
153 0           $sth->execute(@_);
154             }
155              
156             package DBX::Simple::st;
157 2     2   9 use vars qw(@ISA);
  2         3  
  2         137  
158             @ISA = qw(DBI::st);
159              
160             # We don't actually have anything to override in the statement, but it has to be defined or the DBI machinery won't work.
161              
162              
163             =head1 AUTHOR
164              
165             Michael Roberts, C<< >>
166              
167             =head1 BUGS
168              
169             Please report any bugs or feature requests to C, or through
170             the web interface at L. I will be notified, and then you'll
171             automatically be notified of progress on your bug as I make changes.
172              
173              
174              
175              
176             =head1 SUPPORT
177              
178             You can find documentation for this module with the perldoc command.
179              
180             perldoc DBX::Simple
181              
182              
183             You can also look for information at:
184              
185             =over 4
186              
187             =item * RT: CPAN's request tracker (report bugs here)
188              
189             L
190              
191             =item * AnnoCPAN: Annotated CPAN documentation
192              
193             L
194              
195             =item * CPAN Ratings
196              
197             L
198              
199             =item * Search CPAN
200              
201             L
202              
203             =back
204              
205              
206             =head1 ACKNOWLEDGEMENTS
207              
208              
209             =head1 LICENSE AND COPYRIGHT
210              
211             Copyright 2015 Michael Roberts.
212              
213             This program is free software; you can redistribute it and/or modify it
214             under the terms of the the Artistic License (2.0). You may obtain a
215             copy of the full license at:
216              
217             L
218              
219             Any use, modification, and distribution of the Standard or Modified
220             Versions is governed by this Artistic License. By using, modifying or
221             distributing the Package, you accept this license. Do not use, modify,
222             or distribute the Package, if you do not accept this license.
223              
224             If your Modified Version has been derived from a Modified Version made
225             by someone other than you, you are nevertheless required to ensure that
226             your Modified Version complies with the requirements of this license.
227              
228             This license does not grant you the right to use any trademark, service
229             mark, tradename, or logo of the Copyright Holder.
230              
231             This license includes the non-exclusive, worldwide, free-of-charge
232             patent license to make, have made, use, offer to sell, sell, import and
233             otherwise transfer the Package with respect to any patent claims
234             licensable by the Copyright Holder that are necessarily infringed by the
235             Package. If you institute patent litigation (including a cross-claim or
236             counterclaim) against any party alleging that the Package constitutes
237             direct or contributory patent infringement, then this Artistic License
238             to you shall terminate on the date that such litigation is filed.
239              
240             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
241             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
242             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
243             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
244             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
245             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
246             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
247             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
248              
249              
250             =cut
251              
252             1; # End of DBX::Simple