| 1 | # | ||||||||||
| 2 | # Copyright (c) 2015 Christian Jaeger, copying@christianjaeger.ch | ||||||||||
| 3 | # | ||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||
| 7 | # bundled with this file. | ||||||||||
| 8 | # | ||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | FP::DBI - DBI with results as lazy lists | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | use FP::DBI; | ||||||||||
| 17 | |||||||||||
| 18 | $dbh = FP::DBI->connect($data_source, $username, $auth, \%attr); | ||||||||||
| 19 | |||||||||||
| 20 | # same as `DBI`: | ||||||||||
| 21 | .. | ||||||||||
| 22 | $sth = $dbh->prepare($statement); | ||||||||||
| 23 | .. | ||||||||||
| 24 | $rv = $sth->execute; | ||||||||||
| 25 | |||||||||||
| 26 | # then: | ||||||||||
| 27 | my $s = $sth->row_stream; # purearrays blessed to FP::DBI::Row | ||||||||||
| 28 | # or | ||||||||||
| 29 | #my $s = $sth->array_stream; # arrays | ||||||||||
| 30 | # or | ||||||||||
| 31 | #my $s = $sth->hash_stream; # hashes | ||||||||||
| 32 | |||||||||||
| 33 | use PXML::XHTML; | ||||||||||
| 34 | TABLE | ||||||||||
| 35 | (TH($s->first->map (\&TD)), | ||||||||||
| 36 | $s->rest->take (10)->map (sub {TR($_[0]->map (\&TD))})) | ||||||||||
| 37 | |||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||
| 39 | |||||||||||
| 40 | Get rows as items in a lazy linked list (functional stream). | ||||||||||
| 41 | |||||||||||
| 42 | NOTE: `DBI` is designed so that when running another `execute` on the | ||||||||||
| 43 | same statement handle, fetching returns rows for the new execute; this | ||||||||||
| 44 | means, a new execute makes it impossible to retrieve further results | ||||||||||
| 45 | from the previous one. Thus if a result stream isn't fully used before | ||||||||||
| 46 | a new `execute` or a different result request is being made, then the | ||||||||||
| 47 | original stream can't be further evaluated anymore; to prevent this | ||||||||||
| 48 | from happening, an interlock mechanism is built in that throws an | ||||||||||
| 49 | error in this case. | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | =head1 SEE ALSO | ||||||||||
| 53 | |||||||||||
| 54 | L<DBI>, L<FP::Stream> | ||||||||||
| 55 | |||||||||||
| 56 | =head1 NOTE | ||||||||||
| 57 | |||||||||||
| 58 | This is alpha software! Read the status section in the package README | ||||||||||
| 59 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 60 | |||||||||||
| 61 | =cut | ||||||||||
| 62 | |||||||||||
| 63 | package FP::DBI; | ||||||||||
| 64 | |||||||||||
| 65 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 66 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 2 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 68 | |||||||||||
| 69 | 2 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 70 | 2 | ▶ | use Chj::TEST; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | |||||||||||
| 72 | 2 | ▶ | use Chj::NamespaceCleanAbove; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 73 | |||||||||||
| 74 | { | ||||||||||
| 75 | |||||||||||
| 76 | package FP::DBI::db; | ||||||||||
| 77 | 2 | our @ISA = 'DBI::db'; | |||||||||
| 78 | |||||||||||
| 79 | 1 | ▶ | sub prepare { | ||||||||
|
Subroutine
prepare: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 1 | my $s = shift; | |||||||||
| 81 | 1 | my $st = $s->SUPER::prepare(@_); | |||||||||
| 82 | 1 | bless $st, "FP::DBI::st" | |||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | { | ||||||||||
| 87 | |||||||||||
| 88 | 2 | package FP::DBI::Row; | |||||||||
| 89 | 2 | ▶ | use FP::PureArray(); # XX oh, ugly, to load the below namespace! | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 90 | 2 | ▶ | use base 'FP::_::PureArray'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | { | ||||||||||
| 94 | |||||||||||
| 95 | 2 | package FP::DBI::st; | |||||||||
| 96 | 2 | our @ISA = 'DBI::st'; | |||||||||
| 97 | 2 | ▶ | use FP::Lazy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 98 | 2 | ▶ | use FP::Weak; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 99 | 2 | ▶ | use FP::List; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 100 | |||||||||||
| 101 | 6 | ▶ | sub make_X_stream { | ||||||||
|
Subroutine
make_X_stream: called CC=1 SCAR=0.1
| |||||||||||
| 102 | 6 | my ($method, $maybe_mapfn) = @_; | |||||||||
| 103 | 6 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||
| 104 | 6 | my $s = shift; | |||||||||
| 105 | 6 | my $id = ++$$s{private_fp__dbi__id}; | |||||||||
| 106 | 6 | my $lp; | |||||||||
| 107 | 6 | ▶ | $lp = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.3
| |||||||||||
| 108 | 6 | my $lp = $lp; #keep strong reference | |||||||||
| 109 | 5 | ▶ | lazy { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=16.1
| |||||||||||
| 110 | 5 | ▶ | $$s{private_fp__dbi__id} == $id | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | or die("stream was interrupted by another execute" | ||||||||||
| 112 | . " or stream request"); | ||||||||||
| 113 | 3 | ▶ | if (my $v = $s->$method) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | ▶ | cons($maybe_mapfn ? &$maybe_mapfn($v) : $v, &$lp); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | } else { | ||||||||||
| 116 | 3 | null | |||||||||
| 117 | } | ||||||||||
| 118 | } | ||||||||||
| 119 | 6 | }; | |||||||||
| 120 | 6 | Weakened($lp)->() | |||||||||
| 121 | } | ||||||||||
| 122 | 6 | } | |||||||||
| 123 | |||||||||||
| 124 | 2 | ▶ | use Chj::NamespaceCleanAbove; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 125 | |||||||||||
| 126 | 6 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: called CC=1 SCAR=0.0
| |||||||||||
| 127 | 6 | my $s = shift; | |||||||||
| 128 | 6 | $$s{private_fp__dbi__id}++; | |||||||||
| 129 | 6 | $s->SUPER::execute(@_) | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | *row_stream = make_X_stream( | ||||||||||
| 133 | "fetchrow_arrayref", | ||||||||||
| 134 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 135 | 0 | bless([@{ $_[0] }], "FP::DBI::Row") | |||||||||
| 136 | } | ||||||||||
| 137 | 2 | ); | |||||||||
| 138 | 2 | *array_stream = make_X_stream("fetchrow_arrayref"); | |||||||||
| 139 | 2 | *hash_stream = make_X_stream("fetchrow_hashref"); | |||||||||
| 140 | |||||||||||
| 141 | 2 | _END_ | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | 2 | ▶ | use base 'DBI'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 145 | |||||||||||
| 146 | 1 | ▶ | sub connect { | ||||||||
|
Subroutine
connect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 147 | 1 | my $cl = shift; | |||||||||
| 148 | 1 | my $v = $cl->SUPER::connect(@_); | |||||||||
| 149 | 1 | bless $v, "FP::DBI::db" | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 2 | _END_ # namespace cleaning | |||||||||