blib/lib/DBD/Mem.pm

filter: Statementstmt 85.9% 104 / 121 Branchbran 50.0% 10 / 20 Conditioncond 33.3% 6 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 88.4% 23 / 26 Podpod 0.0% 0 / 1 totaltotal 73.9% 145 / 196 CC 18 SCAR 17.5
CC
18
Cov
75%
get_mem_versions
30.0
seek
6.8
open_table
3.7
CRAP 5.8
« blib/lib/DBI/Gofer/Transport/stream.pm ↑ Summary blib/lib/DBI/Gofer/Serializer/DataDumper.pm »
1 #!/usr/bin/perl
2 # -*- perl -*-
3 #
4 # DBD::Mem - A DBI driver for in-memory tables
5 #
6 # This module is currently maintained by
7 #
8 # Jens Rehsack
9 #
10 # Copyright (C) 2016,2017 by Jens Rehsack
11 #
12 # All rights reserved.
13 #
14 # You may distribute this module under the terms of either the GNU
15 # General Public License or the Artistic License, as specified in
16 # the Perl README file.
17  
18 2 require 5.012;
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #################
22 package DBD::Mem;
23 #################
24 2 ▶use base qw( DBI::DBD::SqlEngine );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 our $drh;
26 2 our $VERSION = '0.001';
27 2 our $ATTRIBUTION = 'DBD::Mem by Jens Rehsack';
28  
29 # no need to have driver() unless you need private methods
30 #
31 sub driver ($;$)
32 2 ▶{
Subroutine driver: called CC=2 SCAR=7.0
Pod undocumented
33 2 my ( $class, $attr ) = @_;
34 2 ▶ return $drh if ($drh);
Branch: 1 branch
BranchTrueFalseTotal
if $DBD::Mem::drh 0 2 2
35  
36 # do the real work in DBI::DBD::SqlEngine
37 #
38 2 $attr->{Attribution} = 'DBD::Mem by Jens Rehsack';
39 2 $drh = $class->SUPER::driver($attr);
40  
41 2 return $drh;
42 }
43  
44 sub CLONE
45 0 ▶{
Subroutine CLONE: not called CC=1 SCAR=6.9
46 0 undef $drh;
47 }
48  
49 #####################
50 package DBD::Mem::dr;
51 #####################
52 2 our $imp_data_size = 0;
53 2 our @ISA = qw(DBI::DBD::SqlEngine::dr);
54  
55 # you could put some :dr private methods here
56  
57 # you may need to over-ride some DBI::DBD::SqlEngine::dr methods here
58 # but you can probably get away with just letting it do the work
59 # in most cases
60  
61 #####################
62 package DBD::Mem::db;
63 #####################
64 2 our $imp_data_size = 0;
65 2 our @ISA = qw(DBI::DBD::SqlEngine::db);
66  
67 2 ▶use Carp qw/carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 sub set_versions
70 2 ▶{
Subroutine set_versions: called CC=1 SCAR=0.0
71 2 my $this = $_[0];
72 2 $this->{mem_version} = $DBD::Mem::VERSION;
73 2 return $this->SUPER::set_versions();
74 }
75  
76 sub init_valid_attributes
77 2 ▶{
Subroutine init_valid_attributes: called CC=1 SCAR=0.0
78 2 my $dbh = shift;
79  
80 # define valid private attributes
81 #
82 # attempts to set non-valid attrs in connect() or
83 # with $dbh->{attr} will throw errors
84 #
85 # the attrs here *must* start with mem_ or foo_
86 #
87 # see the STORE methods below for how to check these attrs
88 #
89 $dbh->{mem_valid_attrs} = {
90 2 mem_version => 1, # verbose DBD::Mem version
91 mem_valid_attrs => 1, # DBD::Mem::db valid attrs
92 mem_readonly_attrs => 1, # DBD::Mem::db r/o attrs
93 mem_meta => 1, # DBD::Mem public access for f_meta
94 mem_tables => 1, # DBD::Mem public access for f_meta
95 };
96 $dbh->{mem_readonly_attrs} = {
97 2 mem_version => 1, # verbose DBD::Mem version
98 mem_valid_attrs => 1, # DBD::Mem::db valid attrs
99 mem_readonly_attrs => 1, # DBD::Mem::db r/o attrs
100 mem_meta => 1, # DBD::Mem public access for f_meta
101 };
102  
103 2 $dbh->{mem_meta} = "mem_tables";
104  
105 2 return $dbh->SUPER::init_valid_attributes();
106 }
107  
108 sub get_mem_versions
109 0 ▶{
Subroutine get_mem_versions: not called CC=5 SCAR=34.0
110 0 my ( $dbh, $table ) = @_;
111 0 ▶ $table ||= '';
Condition0%0/2
$table ||= ""
l!l
00
MC/DC0%0/1
$table ||= ""
$table
Truth table0%0/2
A
A= $table
Aresult
11
00
112  
113 0 my $meta;
114 0 my $class = $dbh->{ImplementorClass};
115 0 $class =~ s/::db$/::Table/;
116 0 ▶ $table and ( undef, $meta ) = $class->get_table_meta( $dbh, $table, 1 );
Branch: 1 branch
BranchTrueFalseTotal
$table and (undef, $meta) = $class->get_table_meta($dbh, $table, 1) 0 0 0
117 0 ▶ $meta or ( $meta = {} and $class->bootstrap_table_meta( $dbh, $meta, $table ) );
Branch: 1 branch
BranchTrueFalseTotal
$meta or $meta = {} and $class->bootstrap_table_meta($dbh, $meta, $table) 0 0 0
Condition0%0/6
$meta || ($meta = {}) && $class->bootstrap_table_meta($dbh, $meta, $table)
l!l&&r!l&&!r
000
$meta = {} and $class->bootstrap_table_meta($dbh, $meta, $table)
!ll&&!rl&&r
000
MC/DC0%0/3
$meta || ($meta = {}) && $class->bootstrap_table_meta($dbh, $meta, $table)
$meta $meta = {} $class->bootstrap_table_meta($dbh, $meta, $table)
Truth table0%0/4
A || B and C
A= $metaB= $meta = {}C= $class->bootstrap_table_meta($dbh, $meta, $table)
ABCresult
1XX1
0111
00X0
0100
118  
119 0 return sprintf( "%s using %s", $dbh->{mem_version}, $AnyData2::VERSION );
120 }
121  
122 package DBD::Mem::st;
123  
124 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 2 our $imp_data_size = 0;
128 2 our @ISA = qw(DBI::DBD::SqlEngine::st);
129  
130 ############################
131 package DBD::Mem::Statement;
132 ############################
133  
134 2 our @ISA = qw(DBI::DBD::SqlEngine::Statement);
135  
136  
137 sub open_table ($$$$$)
138 18 ▶{
Subroutine open_table: called CC=3 SCAR=13.1
139 18 my ( $self, $data, $table, $createMode, $lockMode ) = @_;
140  
141 18 my $class = ref $self;
142 18 $class =~ s/::Statement/::Table/;
143  
144 18 my $flags = {
145 createMode => $createMode,
146 lockMode => $lockMode,
147 };
148 18 ▶ if( defined( $data->{Database}->{mem_table_data}->{$table} ) && $data->{Database}->{mem_table_data}->{$table})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"Database"}{"mem_table_data"}{$table} and $data->{"Database"}{"mem_table_data"}{$table}) 0 18 18
Condition33%1/3
defined $data->{"Database"}{"mem_table_data"}{$table} and $data->{"Database"}{"mem_table_data"}{$table}
!ll&&!rl&&r
1800
MC/DC0%0/2
defined $data->{"Database"}{"mem_table_data"}{$table} and $data->{"Database"}{"mem_table_data"}{$table}
defined $data->{"Database"}{"mem_table_data"}{$table} $data->{"Database"}{"mem_table_data"}{$table}
Truth table33%1/3
A and B
A= defined $data->{"Database"}{"mem_table_data"}{$table}B= $data->{"Database"}{"mem_table_data"}{$table}
ABresult
0X0
100
111
149 {
150 0 my $t = $data->{Database}->{mem_tables}->{$table};
151 0 $t->seek( $data, 0, 0 );
152 0 return $t;
153 }
154  
155 18 return $self->SUPER::open_table($data, $table, $createMode, $lockMode);
156 }
157  
158 # ====== DataSource ============================================================
159  
160 package DBD::Mem::DataSource;
161  
162 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
164  
165 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 2 our @ISA = "DBI::DBD::SqlEngine::DataSource";
168  
169 sub complete_table_name ($$;$)
170 4 ▶{
Subroutine complete_table_name: called CC=1 SCAR=0.0
171 4 my ( $self, $meta, $table, $respect_case ) = @_;
172 4 $table;
173 }
174  
175 sub open_data ($)
176 18 ▶{
Subroutine open_data: called CC=2 SCAR=6.9
177 18 my ( $self, $meta, $attrs, $flags ) = @_;
178 18 ▶ $meta->{data_tbl} //= [];
Condition100%2/2
$meta->{"data_tbl"} //= []
l!l
144
MC/DC100%1/1
$meta->{"data_tbl"} //= []
$meta->{"data_tbl"}
Truth table100%2/2
A
A= $meta->{"data_tbl"}
Aresult
11
00
179 }
180  
181 ########################
182 package DBD::Mem::Table;
183 ########################
184  
185 # shamelessly stolen from SQL::Statement::RAM
186  
187 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
188  
189 2 our @ISA = qw(DBI::DBD::SqlEngine::Table);
190  
191 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
192  
193 sub new
194 18 ▶{
Subroutine new: called CC=1 SCAR=0.0
195 #my ( $class, $tname, $col_names, $data_tbl ) = @_;
196 18 my ( $class, $data, $attrs, $flags ) = @_;
197 18 my $self = $class->SUPER::new($data, $attrs, $flags);
198  
199 18 my $meta = $self->{meta};
200 18 $self->{records} = $meta->{data_tbl};
201 18 $self->{index} = 0;
202  
203 18 $self;
204 }
205  
206 sub bootstrap_table_meta
207 4 ▶{
Subroutine bootstrap_table_meta: called CC=2 SCAR=7.1
208 4 my ( $self, $dbh, $meta, $table ) = @_;
209  
210 4 ▶ $meta->{sql_data_source} //= "DBD::Mem::DataSource";
Condition50%1/2
$meta->{"sql_data_source"} //= "DBD::Mem::DataSource"
l!l
04
MC/DC0%0/1
$meta->{"sql_data_source"} //= "DBD::Mem::DataSource"
$meta->{"sql_data_source"}
Truth table50%1/2
A
A= $meta->{"sql_data_source"}
Aresult
11
00
211  
212 4 $meta;
213 }
214  
215 sub fetch_row
216 20 ▶{
Subroutine fetch_row: called CC=3 SCAR=11.0
217 20 my ( $self, $data ) = @_;
218  
219 return $self->{row} =
220 20 ( $self->{records} and ( $self->{index} < scalar( @{ $self->{records} } ) ) )
221 20 ▶ ? [ @{ $self->{records}->[ $self->{index}++ ] } ]
Branch: 1 branch
BranchTrueFalseTotal
$self->{'records'} && $self->{'index'} < scalar @{$self->{'records'};} ? : 12 8 20
Condition66%2/3
$self->{"records"} and $self->{"index"} < scalar @{$self->{"records"};}
!ll&&!rl&&r
0812
MC/DC50%1/2
$self->{"records"} and $self->{"index"} < scalar @{$self->{"records"};}
$self->{"records"} $self->{"index"} < scalar @{$self->{"records"};}
Truth table66%2/3
A and B
A= $self->{"records"}B= $self->{"index"} < scalar @{$self->{"records"};}
ABresult
0X0
100
111
222 : undef;
223 }
224  
225 sub push_row
226 12 ▶{
Subroutine push_row: called CC=1 SCAR=0.0
227 12 my ( $self, $data, $fields ) = @_;
228 12 my $currentRow = $self->{index};
229 12 $self->{index} = $currentRow + 1;
230 12 $self->{records}->[$currentRow] = $fields;
231 12 return 1;
232 }
233  
234 sub truncate
235 6 ▶{
Subroutine truncate: called CC=1 SCAR=0.0
236 6 my $self = shift;
237 6 return splice @{ $self->{records} }, $self->{index}, 1;
238 }
239  
240 sub push_names
241 4 ▶{
Subroutine push_names: called CC=2 SCAR=6.9
242 4 my ( $self, $data, $names ) = @_;
243 4 my $meta = $self->{meta};
244 4 $meta->{col_names} = $self->{col_names} = $names;
245 4 $self->{org_col_names} = [ @{$names} ];
246 4 $self->{col_nums} = {};
247 4 $self->{col_nums}{ $names->[$_] } = $_ for ( 0 .. scalar @$names - 1 );
248 }
249  
250 sub drop ($)
251 0 ▶{
Subroutine drop: not called CC=1 SCAR=6.9
252 0 my ($self, $data) = @_;
253 0 delete $data->{Database}{sql_meta}{$self->{table}};
254 0 return 1;
255 } # drop
256  
257 sub seek
258 12 ▶{
Subroutine seek: called CC=6 SCAR=19.2
259 12 my ( $self, $data, $pos, $whence ) = @_;
260 12 ▶ return unless defined $self->{records};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"records"} 0 12 12
261  
262 12 my ($currentRow) = $self->{index};
263 12 ▶ if ( $whence == 0 )
Branch: 3 branches
BranchTrueFalseTotal
if ($whence == 0) { } 6 6 12
elsif ($whence == 1) { } 0 6 6
elsif ($whence == 2) { } 6 0 6
264 {
265 6 $currentRow = $pos;
266 }
267 elsif ( $whence == 1 )
268 {
269 0 $currentRow += $pos;
270 }
271 elsif ( $whence == 2 )
272 {
273 6 $currentRow = @{ $self->{records} } + $pos;
274 }
275 else
276 {
277 0 croak $self . "->seek: Illegal whence argument ($whence)";
278 }
279  
280 12 ▶ $currentRow < 0 and
Branch: 1 branch
BranchTrueFalseTotal
$currentRow < 0 and croak("Illegal row number: $currentRow") 0 12 12
281 croak "Illegal row number: $currentRow";
282 12 $self->{index} = $currentRow;
283 }
284  
285 2 1;
286  
287 =head1 NAME
288  
289 DBD::Mem - a DBI driver for Mem & MLMem files
290  
291 =head1 SYNOPSIS
292  
293 use DBI;
294 $dbh = DBI->connect('dbi:Mem:', undef, undef, {});
295 $dbh = DBI->connect('dbi:Mem:', undef, undef, {RaiseError => 1});
296  
297 # or
298 $dbh = DBI->connect('dbi:Mem:');
299 $dbh = DBI->connect('DBI:Mem(RaiseError=1):');
300  
301 and other variations on connect() as shown in the L<DBI> docs and
302 <DBI::DBD::SqlEngine metadata|DBI::DBD::SqlEngine/Metadata>.
303  
304 Use standard DBI prepare, execute, fetch, placeholders, etc.
305  
306 =head1 DESCRIPTION
307  
308 DBD::Mem is a database management system that works right out of the box.
309 If you have a standard installation of Perl and DBI you can begin creating,
310 accessing, and modifying simple database tables without any further modules.
311 You can add other modules (e.g., SQL::Statement) for improved functionality.
312  
313 DBD::Mem doesn't store any data persistently - all data has the lifetime of
314 the instantiated C<$dbh>. The main reason to use DBD::Mem is to use extended
315 features of L<SQL::Statement> where temporary tables are required. One can
316 use DBD::Mem to simulate C<VIEWS> or sub-queries.
317  
318 Bundling C<DBD::Mem> with L<DBI> will allow us further compatibility checks
319 of L<DBI::DBD::SqlEngine> beyond the capabilities of L<DBD::File> and
320 L<DBD::DBM>. This will ensure DBI provided basis for drivers like
321 L<DBD::AnyData2> or L<DBD::Amazon> are better prepared and tested for
322 not-file based backends.
323  
324 =head2 Metadata
325  
326 There're no new meta data introduced by C<DBD::Mem>. See
327 L<DBI::DBD::SqlEngine/Metadata> for full description.
328  
329 =head1 GETTING HELP, MAKING SUGGESTIONS, AND REPORTING BUGS
330  
331 If you need help installing or using DBD::Mem, please write to the DBI
332 users mailing list at L<mailto:dbi-users@perl.org> or to the
333 comp.lang.perl.modules newsgroup on usenet. I cannot always answer
334 every question quickly but there are many on the mailing list or in
335 the newsgroup who can.
336  
337 DBD developers for DBD's which rely on DBI::DBD::SqlEngine or DBD::Mem or
338 use one of them as an example are suggested to join the DBI developers
339 mailing list at L<mailto:dbi-dev@perl.org> and strongly encouraged to join our
340 IRC channel at L<irc://irc.perl.org/dbi>.
341  
342 If you have suggestions, ideas for improvements, or bugs to report, please
343 report a bug as described in DBI. Do not mail any of the authors directly,
344 you might not get an answer.
345  
346 When reporting bugs, please send the output of C<< $dbh->mem_versions($table) >>
347 for a table that exhibits the bug and as small a sample as you can make of
348 the code that produces the bug. And of course, patches are welcome, too
349 :-).
350  
351 If you need enhancements quickly, you can get commercial support as
352 described at L<http://dbi.perl.org/support/> or you can contact Jens Rehsack
353 at rehsack@googlemail.com for commercial support.
354  
355 =head1 AUTHOR AND COPYRIGHT
356  
357 This module is written by Jens Rehsack < rehsack AT googlemail.com >.
358  
359 Copyright (c) 2016- by Jens Rehsack, all rights reserved.
360  
361 You may freely distribute and/or modify this module under the terms of
362 either the GNU General Public License (GPL) or the Artistic License, as
363 specified in the Perl README file.
364  
365 =head1 SEE ALSO
366  
367 L<DBI> for the Database interface of the Perl Programming Language.
368  
369 L<SQL::Statement> and L<DBI::SQL::Nano> for the available SQL engines.
370  
371 L<SQL::Statement::RAM> where the implementation is shamelessly stolen from
372 to allow DBI bundled Pure-Perl drivers increase the test coverage.
373  
374 L<DBD::SQLite> using C<dbname=:memory:> for an incredible fast in-memory database engine.
375  
376 =cut
« blib/lib/DBI/Gofer/Transport/stream.pm ↑ Summary blib/lib/DBI/Gofer/Serializer/DataDumper.pm »