| 1 | package DBIx::NamedQuery; | ||||||||||||||
| 2 | # $Id: NamedQuery.pm 23 2006-06-14 13:21:39Z BoleslavB $ | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 1 | ▶ | use 5.006; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Exporter (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use FileHandle (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | ################################################################################ | ||||||||||||||
| 14 | |||||||||||||||
| 15 | |||||||||||||||
| 16 | # History | ||||||||||||||
| 17 | # 0.10 (2006-06-14) - Initial internal release | ||||||||||||||
| 18 | # 0.11 (2006-06-26) - Fixes in code and in documentation | ||||||||||||||
| 19 | |||||||||||||||
| 20 | |||||||||||||||
| 21 | ################################################################################ | ||||||||||||||
| 22 | # Global variables | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 1 | our @EXPORT_OK = qw(load_named_queries load_named_queries_from_file | |||||||||||||
| 25 | get_named_query set_named_query prepare_named_query | ||||||||||||||
| 26 | execute_named_query select_row_from_named_query | ||||||||||||||
| 27 | ); | ||||||||||||||
| 28 | |||||||||||||||
| 29 | 1 | our $VERSION = '0.11'; | |||||||||||||
| 30 | |||||||||||||||
| 31 | 1 | our %NAMED_QUERY = (); | |||||||||||||
| 32 | |||||||||||||||
| 33 | |||||||||||||||
| 34 | ################################################################################ | ||||||||||||||
| 35 | # Named query manipulation | ||||||||||||||
| 36 | |||||||||||||||
| 37 | sub load_named_queries | ||||||||||||||
| 38 | 2 | ▶ | { | ||||||||||||
|
Subroutine
load_named_queries: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||
| 39 | 2 | my ($stream_handle) = (@_); | |||||||||||||
| 40 | 2 | ▶ | if (not defined $stream_handle) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 41 | # By default use DATA stream from main program | ||||||||||||||
| 42 | 1 | $stream_handle = \*main::DATA; | |||||||||||||
| 43 | } elsif (eof($stream_handle)) { | ||||||||||||||
| 44 | 0 | Carp::carp("Not an open filehandle: <$stream_handle>"); | |||||||||||||
| 45 | 0 | return undef; | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | # Load complete contents of the stream and preprocess it | ||||||||||||||
| 48 | 2 | my $stream_contents; | |||||||||||||
| 49 | 2 | eval { | |||||||||||||
| 50 | 1 | ▶ | no warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 51 | 2 | local $/ = undef; | |||||||||||||
| 52 | 2 | $stream_contents = <$stream_handle>; | |||||||||||||
| 53 | 2 | close($stream_handle); | |||||||||||||
| 54 | }; | ||||||||||||||
| 55 | 2 | ▶ | if (not defined $stream_contents) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 0 | return undef; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | # Preprocess the loaded text | ||||||||||||||
| 59 | 2 | $stream_contents =~ s/^\s*#.*?\n//gm; | |||||||||||||
| 60 | 2 | $stream_contents =~ s/\s+\n/\n/gm; | |||||||||||||
| 61 | # Divide stream into headers and bodies | ||||||||||||||
| 62 | 2 | my @parts = split(/^(--\[.*?\])\s*$/m, $stream_contents); | |||||||||||||
| 63 | 2 | undef($stream_contents); | |||||||||||||
| 64 | 2 | my $actual_label = undef; | |||||||||||||
| 65 | 2 | foreach my $part (@parts) { | |||||||||||||
| 66 | 12 | ▶ | if ($part =~ /^--\[\s*(.*?)\s*\]\s*$/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | # Header part found, begin a new named query | ||||||||||||||
| 68 | 5 | $actual_label = $1; | |||||||||||||
| 69 | 5 | next; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | 7 | ▶ | next unless defined $actual_label; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | # Process body part following the header | ||||||||||||||
| 73 | 5 | $part =~ s/(?:\A\s*)|(?:\s*\z)//g; | |||||||||||||
| 74 | 5 | $NAMED_QUERY{$actual_label} = $part; | |||||||||||||
| 75 | 5 | undef($actual_label); | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | 2 | my $query_count = scalar keys %NAMED_QUERY; | |||||||||||||
| 78 | 2 | return $query_count; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | |||||||||||||||
| 82 | sub load_named_queries_from_file | ||||||||||||||
| 83 | 1 | ▶ | { | ||||||||||||
|
Subroutine
load_named_queries_from_file: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||
| 84 | 1 | my ($filename) = @_; | |||||||||||||
| 85 | 1 | my $stream = FileHandle->new($filename, '<'); | |||||||||||||
| 86 | 1 | ▶ | if (not defined $stream) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 0 | return undef; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | 1 | return load_named_queries($stream); | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | |||||||||||||||
| 93 | sub get_named_query | ||||||||||||||
| 94 | 0 | ▶ | { | ||||||||||||
|
Subroutine
get_named_query: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 95 | 0 | my ($query_name) = @_; | |||||||||||||
| 96 | 0 | ▶ | if (not exists $NAMED_QUERY{$query_name}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | 0 | return undef; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | 0 | return $NAMED_QUERY{$query_name}; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | |||||||||||||||
| 103 | sub set_named_query | ||||||||||||||
| 104 | 0 | ▶ | { | ||||||||||||
|
Subroutine
set_named_query: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 105 | 0 | ▶ | while (my @pair = splice(@_, 0, 2)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 0 | ▶ | last if 2 != scalar @pair; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | 0 | my ($query_name, $query_text) = @pair; | |||||||||||||
| 108 | 0 | $NAMED_QUERY{$query_name} = $query_text; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | |||||||||||||||
| 113 | ################################################################################ | ||||||||||||||
| 114 | # Initialization operations | ||||||||||||||
| 115 | |||||||||||||||
| 116 | sub import | ||||||||||||||
| 117 | 1 | ▶ | { | ||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.2
| |||||||||||||||
| 118 | 1 | my ($package, @params) = @_; | |||||||||||||
| 119 | 1 | my @new_params = (); | |||||||||||||
| 120 | 1 | foreach my $param (@params) { | |||||||||||||
| 121 | 1 | ▶ | if ($param eq 'EXTEND_DBI') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 122 | 1 | extend_DBI_interface(); | |||||||||||||
| 123 | 1 | next; | |||||||||||||
| 124 | } | ||||||||||||||
| 125 | 0 | push(@new_params, $param); | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | 1 | Exporter::import($package, @new_params); | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | |||||||||||||||
| 131 | sub extend_DBI_interface | ||||||||||||||
| 132 | 1 | ▶ | { | ||||||||||||
|
Subroutine
extend_DBI_interface: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 133 | # DBI database class infiltration (inserts new methods), but without | ||||||||||||||
| 134 | # direct namespace changes | ||||||||||||||
| 135 | 1 | push(@DBI::db::ISA, 'DBIx::NamedQuery::db'); | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | |||||||||||||||
| 139 | ################################################################################ | ||||||||||||||
| 140 | # Named query usage | ||||||||||||||
| 141 | |||||||||||||||
| 142 | package DBIx::NamedQuery::db; | ||||||||||||||
| 143 | |||||||||||||||
| 144 | sub prepare_named_query | ||||||||||||||
| 145 | 0 | ▶ | { | ||||||||||||
|
Subroutine
prepare_named_query: not called CC=3 SCAR=24.8
| |||||||||||||||
| 146 | 0 | my $db_handle = shift; | |||||||||||||
| 147 | 0 | my ($query_name, $prepare_attr) = @_; | |||||||||||||
| 148 | 0 | ▶ | if (not exists $DBIx::NamedQuery::NAMED_QUERY{$query_name}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 149 | 0 | $db_handle->set_err(1, "Named query '$query_name' has not " | |||||||||||||
| 150 | . 'been defined' | ||||||||||||||
| 151 | ); | ||||||||||||||
| 152 | 0 | return undef; | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | 0 | my $query_text = $DBIx::NamedQuery::NAMED_QUERY{$query_name}; | |||||||||||||
| 155 | 0 | my $query_handle = $db_handle->prepare($query_text, $prepare_attr); | |||||||||||||
| 156 | 0 | ▶ | if (not defined $query_handle) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | 0 | return undef; | |||||||||||||
| 158 | } | ||||||||||||||
| 159 | 0 | return $query_handle; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | |||||||||||||||
| 163 | sub execute_named_query | ||||||||||||||
| 164 | 0 | ▶ | { | ||||||||||||
|
Subroutine
execute_named_query: not called CC=3 SCAR=24.8
| |||||||||||||||
| 165 | 0 | my $db_handle = shift; | |||||||||||||
| 166 | 0 | my ($query_name, @bind_values) = @_; | |||||||||||||
| 167 | 0 | my $statement = $db_handle->prepare_named_query($query_name); | |||||||||||||
| 168 | 0 | ▶ | if (not defined $statement) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | 0 | return undef; | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | 0 | my $executed = $statement->execute(@bind_values); | |||||||||||||
| 172 | 0 | ▶ | if (not $executed) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 173 | 0 | return undef; | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | 0 | return $statement; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | |||||||||||||||
| 178 | |||||||||||||||
| 179 | sub select_row_from_named_query | ||||||||||||||
| 180 | 0 | ▶ | { | ||||||||||||
|
Subroutine
select_row_from_named_query: not called CC=3 SCAR=24.8
| |||||||||||||||
| 181 | 0 | my $db_handle = shift; | |||||||||||||
| 182 | 0 | my ($query_name, @bind_values) = @_; | |||||||||||||
| 183 | 0 | my $statement = $db_handle->prepare_named_query($query_name); | |||||||||||||
| 184 | 0 | ▶ | if (not defined $statement) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 185 | 0 | return undef; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | 0 | my $executed = $statement->execute(@bind_values); | |||||||||||||
| 188 | 0 | ▶ | if (not $executed) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 189 | 0 | return undef; | |||||||||||||
| 190 | } | ||||||||||||||
| 191 | 0 | my $first_row_arrayref = $statement->fetchrow_arrayref(); | |||||||||||||
| 192 | 0 | $statement->finish(); | |||||||||||||
| 193 | 0 | return $first_row_arrayref; | |||||||||||||
| 194 | } | ||||||||||||||
| 195 | |||||||||||||||
| 196 | |||||||||||||||
| 197 | ################################################################################ | ||||||||||||||
| 198 | |||||||||||||||
| 199 | 1 | 1; | |||||||||||||
| 200 | |||||||||||||||
| 201 | __END__ | ||||||||||||||