line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::NamedQuery; |
2
|
|
|
|
|
|
|
# $Id: NamedQuery.pm 23 2006-06-14 13:21:39Z BoleslavB $ |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
109418
|
use 5.006; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
43
|
|
5
|
1
|
|
|
1
|
|
8
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
6
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
65
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
6
|
use Carp (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
14
|
|
9
|
1
|
|
|
1
|
|
5
|
use Exporter (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
19
|
|
10
|
1
|
|
|
1
|
|
990
|
use FileHandle (); |
|
1
|
|
|
|
|
35847
|
|
|
1
|
|
|
|
|
185
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our %NAMED_QUERY = (); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
################################################################################ |
35
|
|
|
|
|
|
|
# Named query manipulation |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub load_named_queries |
38
|
|
|
|
|
|
|
{ |
39
|
2
|
|
|
2
|
1
|
10
|
my ($stream_handle) = (@_); |
40
|
2
|
100
|
|
|
|
476
|
if (not defined $stream_handle) { |
|
|
50
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# By default use DATA stream from main program |
42
|
1
|
|
|
|
|
4
|
$stream_handle = \*main::DATA; |
43
|
|
|
|
|
|
|
} elsif (eof($stream_handle)) { |
44
|
0
|
|
|
|
|
0
|
Carp::carp("Not an open filehandle: <$stream_handle>"); |
45
|
0
|
|
|
|
|
0
|
return undef; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
# Load complete contents of the stream and preprocess it |
48
|
2
|
|
|
|
|
4
|
my $stream_contents; |
49
|
2
|
|
|
|
|
5
|
eval { |
50
|
1
|
|
|
1
|
|
12
|
no warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1362
|
|
51
|
2
|
|
|
|
|
8
|
local $/ = undef; |
52
|
2
|
|
|
|
|
44
|
$stream_contents = <$stream_handle>; |
53
|
2
|
|
|
|
|
28
|
close($stream_handle); |
54
|
|
|
|
|
|
|
}; |
55
|
2
|
50
|
|
|
|
8
|
if (not defined $stream_contents) { |
56
|
0
|
|
|
|
|
0
|
return undef; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
# Preprocess the loaded text |
59
|
2
|
|
|
|
|
7
|
$stream_contents =~ s/^\s*#.*?\n//gm; |
60
|
2
|
|
|
|
|
36
|
$stream_contents =~ s/\s+\n/\n/gm; |
61
|
|
|
|
|
|
|
# Divide stream into headers and bodies |
62
|
2
|
|
|
|
|
26
|
my @parts = split(/^(--\[.*?\])\s*$/m, $stream_contents); |
63
|
2
|
|
|
|
|
5
|
undef($stream_contents); |
64
|
2
|
|
|
|
|
3
|
my $actual_label = undef; |
65
|
2
|
|
|
|
|
10
|
foreach my $part (@parts) { |
66
|
12
|
100
|
|
|
|
48
|
if ($part =~ /^--\[\s*(.*?)\s*\]\s*$/) { |
67
|
|
|
|
|
|
|
# Header part found, begin a new named query |
68
|
5
|
|
|
|
|
13
|
$actual_label = $1; |
69
|
5
|
|
|
|
|
9
|
next; |
70
|
|
|
|
|
|
|
} |
71
|
7
|
100
|
|
|
|
18
|
next unless defined $actual_label; |
72
|
|
|
|
|
|
|
# Process body part following the header |
73
|
5
|
|
|
|
|
222
|
$part =~ s/(?:\A\s*)|(?:\s*\z)//g; |
74
|
5
|
|
|
|
|
15
|
$NAMED_QUERY{$actual_label} = $part; |
75
|
5
|
|
|
|
|
10
|
undef($actual_label); |
76
|
|
|
|
|
|
|
} |
77
|
2
|
|
|
|
|
7
|
my $query_count = scalar keys %NAMED_QUERY; |
78
|
2
|
|
|
|
|
13
|
return $query_count; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub load_named_queries_from_file |
83
|
|
|
|
|
|
|
{ |
84
|
1
|
|
|
1
|
1
|
953
|
my ($filename) = @_; |
85
|
1
|
|
|
|
|
12
|
my $stream = FileHandle->new($filename, '<'); |
86
|
1
|
50
|
|
|
|
153
|
if (not defined $stream) { |
87
|
0
|
|
|
|
|
0
|
return undef; |
88
|
|
|
|
|
|
|
} |
89
|
1
|
|
|
|
|
5
|
return load_named_queries($stream); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub get_named_query |
94
|
|
|
|
|
|
|
{ |
95
|
0
|
|
|
0
|
1
|
0
|
my ($query_name) = @_; |
96
|
0
|
0
|
|
|
|
0
|
if (not exists $NAMED_QUERY{$query_name}) { |
97
|
0
|
|
|
|
|
0
|
return undef; |
98
|
|
|
|
|
|
|
} |
99
|
0
|
|
|
|
|
0
|
return $NAMED_QUERY{$query_name}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub set_named_query |
104
|
|
|
|
|
|
|
{ |
105
|
0
|
|
|
0
|
1
|
0
|
while (my @pair = splice(@_, 0, 2)) { |
106
|
0
|
0
|
|
|
|
0
|
last if 2 != scalar @pair; |
107
|
0
|
|
|
|
|
0
|
my ($query_name, $query_text) = @pair; |
108
|
0
|
|
|
|
|
0
|
$NAMED_QUERY{$query_name} = $query_text; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
################################################################################ |
114
|
|
|
|
|
|
|
# Initialization operations |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub import |
117
|
|
|
|
|
|
|
{ |
118
|
1
|
|
|
1
|
|
12
|
my ($package, @params) = @_; |
119
|
1
|
|
|
|
|
3
|
my @new_params = (); |
120
|
1
|
|
|
|
|
3
|
foreach my $param (@params) { |
121
|
1
|
50
|
|
|
|
5
|
if ($param eq 'EXTEND_DBI') { |
122
|
1
|
|
|
|
|
3
|
extend_DBI_interface(); |
123
|
1
|
|
|
|
|
3
|
next; |
124
|
|
|
|
|
|
|
} |
125
|
0
|
|
|
|
|
0
|
push(@new_params, $param); |
126
|
|
|
|
|
|
|
} |
127
|
1
|
|
|
|
|
33
|
Exporter::import($package, @new_params); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub extend_DBI_interface |
132
|
|
|
|
|
|
|
{ |
133
|
|
|
|
|
|
|
# DBI database class infiltration (inserts new methods), but without |
134
|
|
|
|
|
|
|
# direct namespace changes |
135
|
1
|
|
|
1
|
0
|
10
|
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
|
|
|
|
|
|
|
{ |
146
|
0
|
|
|
0
|
|
|
my $db_handle = shift; |
147
|
0
|
|
|
|
|
|
my ($query_name, $prepare_attr) = @_; |
148
|
0
|
0
|
|
|
|
|
if (not exists $DBIx::NamedQuery::NAMED_QUERY{$query_name}) { |
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
|
0
|
|
|
|
|
if (not defined $query_handle) { |
157
|
0
|
|
|
|
|
|
return undef; |
158
|
|
|
|
|
|
|
} |
159
|
0
|
|
|
|
|
|
return $query_handle; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub execute_named_query |
164
|
|
|
|
|
|
|
{ |
165
|
0
|
|
|
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
|
0
|
|
|
|
|
if (not defined $statement) { |
169
|
0
|
|
|
|
|
|
return undef; |
170
|
|
|
|
|
|
|
} |
171
|
0
|
|
|
|
|
|
my $executed = $statement->execute(@bind_values); |
172
|
0
|
0
|
|
|
|
|
if (not $executed) { |
173
|
0
|
|
|
|
|
|
return undef; |
174
|
|
|
|
|
|
|
} |
175
|
0
|
|
|
|
|
|
return $statement; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub select_row_from_named_query |
180
|
|
|
|
|
|
|
{ |
181
|
0
|
|
|
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
|
0
|
|
|
|
|
if (not defined $statement) { |
185
|
0
|
|
|
|
|
|
return undef; |
186
|
|
|
|
|
|
|
} |
187
|
0
|
|
|
|
|
|
my $executed = $statement->execute(@bind_values); |
188
|
0
|
0
|
|
|
|
|
if (not $executed) { |
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; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
__END__ |