| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | DBIx::SQLEngine::Driver::NullP - Extends SQLEngine for Simple Testing | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | B<DBI Wrapper>: Adds methods to a DBI database handle. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | my $sqldb = DBIx::SQLEngine->new( 'dbi:NullP:' ); | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | B<Testing Subclass:> To allow basic framework testing. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | $sqldb->next_result( | ||||||||||||||||||||||||||||
| 14 | rowcount => 1, | ||||||||||||||||||||||||||||
| 15 | hashref => [ { id => 201, name => "Dave Jones" } ], | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my $rows = $sqldb->fetch_select( | ||||||||||||||||||||||||||||
| 19 | table => 'students' | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ok( $sqldb->last_query, 'select * from students' ); | ||||||||||||||||||||||||||||
| 23 | ok( scalar @$rows == $sqldb->last_result->{rowcount} ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | This package provides a subclass of DBIx::SQLEngine which works with the DBI's DBD::NullP to provide a simple testing capability. See the "t/null.t" test script for a usage example. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Queries using the NullP driver and subclass keep track of the SQL statements that are executed against them, allowing a simple way of checking whether the SQL generation code is working as expected. You can also provide raw results to be returned by the next query, in order to confirm that other aspects of the result processing are operational. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | ######################################################################## | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | package DBIx::SQLEngine::Driver::NullP; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | ######################################################################## | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 TESTING INTERFACE | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | To simulate normal driver operation, before executing your query, load the results you would expect to receive using next_result, and afterwards confirm that the SQL statement and parameters used matched what you expected. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =over 4 | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =item next_result() | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | $sqldb->next_result( | ||||||||||||||||||||||||||||
| 52 | rowcount => $number, | ||||||||||||||||||||||||||||
| 53 | hashref => $array_of_hashes, | ||||||||||||||||||||||||||||
| 54 | arrayref => $array_of_arrays, | ||||||||||||||||||||||||||||
| 55 | columns => $array_of_hashes, | ||||||||||||||||||||||||||||
| 56 | ) | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Sets up the values that will be returned by the next query. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =item last_query() | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | $sqldb->last_query() : $statement_and_params | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Returns the most recent query and parameters captured by prepare_execute(). Parameters are joined by "/" characters. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =item last_result() | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | $sqldb->last_result() : $result_hash_ref | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Returns the values set with next_result() and used by the most recent query. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =back | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 9 | ▶ | sub next_result { | ||||||||||||||||||||||||||
|
Subroutine
next_result: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 9 | my ( $self, %results ) = @_; | |||||||||||||||||||||||||||
| 78 | 2 | ▶ | $results{rowcount} ||= ( $results{hashref} ? scalar @{ $results{hashref} } : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $results{arrayref} ? scalar @{ $results{arrayref} } : | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$results{"rowcount"} ||= $results{"hashref"} ? scalar @{$results{"hashref"};} : ($results{"arrayref"} ? scalar @{$results{"arrayref"};} : 0)
MC/DC100%2/2
$results{"rowcount"} ||= $results{"hashref"} ? scalar @{$results{"hashref"};} : ($results{"arrayref"} ? scalar @{$results{"arrayref"};} : 0)
$results{"rowcount"}
$results{"hashref"} ? scalar @{$results{"hashref"};} : ($results{"arrayref"} ? scalar @{$results{"arrayref"};} : 0)
Truth table100%3/3
A ||= B
A= $results{"rowcount"}B= $results{"hashref"} ? scalar @{$results{"hashref"};} : ($results{"arrayref"} ? scalar @{$results{"arrayref"};} : 0)
| |||||||||||||||||||||||||||||
| 80 | 0 ); | ||||||||||||||||||||||||||||
| 81 | 9 | $self->{_next_result} = \%results; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub last_result { | ||||||||||||||||||||||||||
|
Subroutine
last_result: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 86 | 0 | $self->{_last_result}; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 89 | ▶ | sub last_query { | ||||||||||||||||||||||||||
|
Subroutine
last_query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 89 | my $self = shift; | |||||||||||||||||||||||||||
| 91 | 89 | join('/', $self->{_last_sth_statement}, @{ $self->{_last_sth_params} } ) | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | ######################################################################## | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 INTERNAL STATEMENT METHODS (DBI STH) | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head2 Statement Handle Lifecycle | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =over 4 | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item prepare_execute() | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | $sqldb->prepare_execute ($sql, @params) : $sth | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Captures the statement and parameters that would otherwise have been sent to the Null driver to be used for later reporting by last_query(). | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item done_with_query() | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | $sqldb->done_with_query ($sth) : () | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Clears the values stored by next_result. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =back | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # $sth = $self->prepare_execute($sql); | ||||||||||||||||||||||||||||
| 119 | # $sth = $self->prepare_execute($sql, @params); | ||||||||||||||||||||||||||||
| 120 | 86 | ▶ | sub prepare_execute { | ||||||||||||||||||||||||||
|
Subroutine
prepare_execute: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 86 | my ($self, $sql, @params) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 86 | my $sth; | |||||||||||||||||||||||||||
| 124 | 86 | $sth = $self->prepare_cached($sql); | |||||||||||||||||||||||||||
| 125 | 86 | $self->{_last_sth_params} = []; | |||||||||||||||||||||||||||
| 126 | 86 | for my $param_no ( 0 .. $#params ) { | |||||||||||||||||||||||||||
| 127 | 79 | my $param_v = $params[$param_no]; | |||||||||||||||||||||||||||
| 128 | 79 | ▶ | my @param_v = ( ref($param_v) eq 'ARRAY' ) ? @$param_v : $param_v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | # $sth->bind_param( $param_no+1, @param_v ); | ||||||||||||||||||||||||||||
| 130 | 79 | $self->{_last_sth_params}[ $param_no ] = $param_v; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 86 | $self->{_last_sth_execute} = $sth->execute(); | |||||||||||||||||||||||||||
| 133 | 86 | $self->{_last_sth_statement} = $sth->{Statement}; | |||||||||||||||||||||||||||
| 134 | 86 | $self->{_last_result} = $self->{_next_result}; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 86 | return $sth; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # $self->done_with_query( $sth ); | ||||||||||||||||||||||||||||
| 140 | 86 | ▶ | sub done_with_query { | ||||||||||||||||||||||||||
|
Subroutine
done_with_query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 86 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 86 | $self->{_next_result} = undef; | |||||||||||||||||||||||||||
| 143 | 86 | $self->SUPER::done_with_query( @_ ); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | ######################################################################## | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 Retrieving Rows from a Statement | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =over 4 | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =item get_execute_rowcount() | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | $sqldb->get_execute_rowcount ($sth) : $row_count | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Returns the value stored by next_result() using the key "rowcount". | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =item fetchall_hashref() | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | $sqldb->fetchall_hashref ($sth) : $array_of_hashes | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Returns the value stored by next_result() using the key "hashref". | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item fetchall_hashref_columns() | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | $sqldb->fetchall_hashref ($sth) : $array_of_hashes | ||||||||||||||||||||||||||||
| 167 | $sqldb->fetchall_hashref ($sth) : ( $array_of_hashes, $column_info ) | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Returns the value stored by next_result() using the key "hashref", and if called in a list context, also returns the value for the key "columns". | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =item fetchall_arrayref() | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | $sqldb->fetchall_arrayref ($sth) : $array_of_arrays | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Returns the value stored by next_result() using the key "arrayref". | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item fetchall_arrayref_columns() | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | $sqldb->fetchall_hashref ($sth) : $array_of_arrays | ||||||||||||||||||||||||||||
| 180 | $sqldb->fetchall_hashref ($sth) : ( $array_of_arrays, $column_info ) | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Returns the value stored by next_result() using the key "arrayref", and if called in a list context, also returns the value for the key "columns". | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =item retrieve_columns() | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | $sqldb->retrieve_columns ($sth) : $column_info | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Retrieves the value stored by next_result() using the key "columns". | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =item visitall_hashref() | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | $sqldb->visitall_hashref ($sth, $coderef) : () | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Uses the value stored by next_result() using the key "hashref". Calls the coderef on each row with values as a hashref, and returns a list of their results. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =item visitall_array() | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | $sqldb->visitall_array ($sth, $coderef) : () | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Uses the value stored by next_result() using the key "arrayref". Calls the coderef on each row with values as a list, and returns a list of their results. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =back | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =cut | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 45 | ▶ | sub get_execute_rowcount { (shift)->{_next_result}{rowcount} } | ||||||||||||||||||||||||||
|
Subroutine
get_execute_rowcount: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | sub fetchall_arrayref { (shift)->{_next_result}{arrayref} } | ||||||||||||||||||||||||||
|
Subroutine
fetchall_arrayref: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | 0 | ▶ | sub fetchall_hashref { (shift)->{_next_result}{hashref} } | ||||||||||||||||||||||||||
|
Subroutine
fetchall_hashref: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub retrieve_columns { (shift)->{_next_result}{columns} } | ||||||||||||||||||||||||||
|
Subroutine
retrieve_columns: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 0 | ▶ | sub fetchall_arrayref_columns { | ||||||||||||||||||||||||||
|
Subroutine
fetchall_arrayref_columns: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 212 | 0 | my ($self, $sth) = @_; | |||||||||||||||||||||||||||
| 213 | 0 | ▶ | my $cols = wantarray() ? $self->{_next_result}{columns} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 0 | my $rows = $self->{_next_result}{arrayref}; | |||||||||||||||||||||||||||
| 215 | 0 | ▶ | wantarray ? ( $rows, $cols ) : $rows; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 41 | ▶ | sub fetchall_hashref_columns { | ||||||||||||||||||||||||||
|
Subroutine
fetchall_hashref_columns: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 219 | 41 | my ($self, $sth) = @_; | |||||||||||||||||||||||||||
| 220 | 41 | ▶ | my $cols = wantarray() ? $self->{_next_result}{columns} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 41 | my $rows = $self->{_next_result}{hashref}; | |||||||||||||||||||||||||||
| 222 | 41 | ▶ | wantarray ? ( $rows, $cols ) : $rows; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # $self->visitall_hashref( $sth, $coderef ); | ||||||||||||||||||||||||||||
| 226 | # Calls a codref for each row returned by the statement handle | ||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub visitall_hashref { | ||||||||||||||||||||||||||
|
Subroutine
visitall_hashref: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | 0 | my ($self, $sth, $coderef) = @_; | |||||||||||||||||||||||||||
| 229 | 0 | ▶ | my $rows = $self->{_next_result}{hashref} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 0 | map &$coderef( $_ ), @$rows | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # $self->visitall_array( $sth, $coderef ); | ||||||||||||||||||||||||||||
| 234 | # Calls a codref for each row returned by the statement handle | ||||||||||||||||||||||||||||
| 235 | 0 | ▶ | sub visitall_array { | ||||||||||||||||||||||||||
|
Subroutine
visitall_array: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 236 | 0 | my ($self, $sth, $coderef) = @_; | |||||||||||||||||||||||||||
| 237 | 0 | ▶ | my $rows = $self->{_next_result}{arrayref} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 0 | map &$coderef( @$_ ), @$rows | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | ######################################################################## | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 10 | 1; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | __END__ | ||||||||||||||||||||||||||||