| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
SQLib - A simple module to manage and store SQL queries in separate files. |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 VERSION |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Version 0.05 |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 AUTHOR |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Mateusz Szczyrzyca, mateusz at szczyrzyca.pl |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
The module allows to store SQL queries in separate files and provides easy |
|
18
|
|
|
|
|
|
|
access to them. This functionality is helpful if you want to separate a Perl |
|
19
|
|
|
|
|
|
|
code from a SQL code. |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
A file with list of queries has the following syntax: |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
[NAME_OF_QUERY1] |
|
24
|
|
|
|
|
|
|
-- A SQL query with {vars} to replace |
|
25
|
|
|
|
|
|
|
[/NAME_OF_QUERY1] |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
[NAME_OF_QUERY2] |
|
28
|
|
|
|
|
|
|
-- A SQL query with {vars} to replace |
|
29
|
|
|
|
|
|
|
[/NAME_OF_QUERY2] |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
... |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
[NAME_OF_QUERY_N] |
|
34
|
|
|
|
|
|
|
-- A SQL query with {vars} to replace |
|
35
|
|
|
|
|
|
|
[/NAME_OF_QUERY_N] |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
[ QUERIES_WITH_SPACES_IN_NAME_ARE_POSSIBLE ] |
|
38
|
|
|
|
|
|
|
-- A SQL query with spaces |
|
39
|
|
|
|
|
|
|
[ / QUERIES_WITH_SPACES_IN_NAME_ARE_POSSIBLE ] |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
First parenthesis "[" always starts from a new line (don't use whitespaces). |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Empty lines between queries are ignored. If there are two or more SQL queries |
|
44
|
|
|
|
|
|
|
with same [NAME], then only one (first) will be used. |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
If a query with a specified name doesn't exist then undef is returned as soon |
|
47
|
|
|
|
|
|
|
as if a file or query has an invalid syntax. |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
[QUERY_NAME]A sql code[/QUERY_NAME] isn't a valid syntax as well. |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Simple example (file_with_queries.sql): |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
[CHECK_PASSWORD] |
|
55
|
|
|
|
|
|
|
-- Comments for SQL debug |
|
56
|
|
|
|
|
|
|
-- Some::Program @ CHECK_PASSWORD |
|
57
|
|
|
|
|
|
|
-- Check a user password |
|
58
|
|
|
|
|
|
|
SELECT |
|
59
|
|
|
|
|
|
|
login,password |
|
60
|
|
|
|
|
|
|
FROM |
|
61
|
|
|
|
|
|
|
{table} |
|
62
|
|
|
|
|
|
|
WHERE |
|
63
|
|
|
|
|
|
|
( |
|
64
|
|
|
|
|
|
|
login = '{login}', |
|
65
|
|
|
|
|
|
|
AND |
|
66
|
|
|
|
|
|
|
password = '{password}' |
|
67
|
|
|
|
|
|
|
); |
|
68
|
|
|
|
|
|
|
[/CHECK_PASSWORD] |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
And how to use it in a perl code: |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
use SQLib; |
|
73
|
|
|
|
|
|
|
my $SQLib = SQLib->new( './file_with_queries.sql' ); |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my %sql_params = |
|
76
|
|
|
|
|
|
|
( |
|
77
|
|
|
|
|
|
|
table => 'cms_users', |
|
78
|
|
|
|
|
|
|
login => 'someuser', |
|
79
|
|
|
|
|
|
|
password => 'somepass' |
|
80
|
|
|
|
|
|
|
); |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $check_auth_query = $SQLib->get_query( 'CHECK_PASSWORD', \%sql_params ); |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
In the above example $check_auth_query contains: |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
-- Comments for SQL debug |
|
87
|
|
|
|
|
|
|
-- Some::Program @ CHECK_PASSWORD |
|
88
|
|
|
|
|
|
|
-- Check a user password |
|
89
|
|
|
|
|
|
|
SELECT |
|
90
|
|
|
|
|
|
|
login,password |
|
91
|
|
|
|
|
|
|
FROM |
|
92
|
|
|
|
|
|
|
cms_users |
|
93
|
|
|
|
|
|
|
WHERE |
|
94
|
|
|
|
|
|
|
( |
|
95
|
|
|
|
|
|
|
login = 'someuser', |
|
96
|
|
|
|
|
|
|
AND |
|
97
|
|
|
|
|
|
|
password = 'somepass' |
|
98
|
|
|
|
|
|
|
); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
##################################################################### |
|
103
|
|
|
|
|
|
|
##################################################################### |
|
104
|
|
|
|
|
|
|
##################################################################### |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
package SQLib; |
|
107
|
8
|
|
|
8
|
|
245838
|
use utf8; |
|
|
8
|
|
|
|
|
99
|
|
|
|
8
|
|
|
|
|
54
|
|
|
108
|
8
|
|
|
8
|
|
314
|
use strict; |
|
|
8
|
|
|
|
|
19
|
|
|
|
8
|
|
|
|
|
308
|
|
|
109
|
8
|
|
|
8
|
|
12214
|
use Tie::File; |
|
|
8
|
|
|
|
|
271047
|
|
|
|
8
|
|
|
|
|
11485
|
|
|
110
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub new |
|
113
|
|
|
|
|
|
|
{ |
|
114
|
6
|
|
|
6
|
0
|
5307
|
my $class = shift; |
|
115
|
6
|
|
|
|
|
29
|
my $self = { file => $_[ 0 ] }; |
|
116
|
|
|
|
|
|
|
|
|
117
|
6
|
50
|
|
|
|
219
|
die 'ERROR: Cannot find: ' . $self->{'file'} if ( ! -e $self->{'file'} ); |
|
118
|
|
|
|
|
|
|
|
|
119
|
6
|
50
|
|
|
|
68
|
tie my @queries, 'Tie::File', $self->{'file'} |
|
120
|
|
|
|
|
|
|
or die 'SQLib: I cannot open the file with queries: ' . $self->{'file'}; |
|
121
|
6
|
|
|
|
|
1297
|
$self->{'queries'} = \@queries; |
|
122
|
6
|
|
|
|
|
42
|
bless $self, $class; |
|
123
|
|
|
|
|
|
|
}; |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub get_query |
|
126
|
|
|
|
|
|
|
{ |
|
127
|
119
|
|
|
119
|
0
|
110751
|
my $self = shift; |
|
128
|
119
|
|
|
|
|
252
|
my $name = shift; |
|
129
|
119
|
|
|
|
|
227
|
my $params = shift; |
|
130
|
|
|
|
|
|
|
|
|
131
|
119
|
|
|
|
|
184
|
my @queries = @{ $self->{'queries'} }; |
|
|
119
|
|
|
|
|
1007
|
|
|
132
|
|
|
|
|
|
|
|
|
133
|
119
|
|
|
|
|
1593966
|
my $sql = "\n"; |
|
134
|
119
|
|
|
|
|
310
|
my $reading; |
|
135
|
|
|
|
|
|
|
|
|
136
|
119
|
|
|
|
|
487
|
for my $i ( 0 .. $#queries ) |
|
137
|
|
|
|
|
|
|
{ |
|
138
|
7657
|
100
|
|
|
|
33848
|
last if ( $queries[ $i ] =~ m/^\[\s*\/\s*$name\s*\]$/ ); |
|
139
|
7644
|
100
|
100
|
|
|
42512
|
next if ( $queries[ $i ] =~ m/^\s*$/ && !$reading ); |
|
140
|
3963
|
100
|
100
|
|
|
8783
|
return undef if $reading && $queries[ $i ] =~ m/^\[/; |
|
141
|
|
|
|
|
|
|
|
|
142
|
3960
|
100
|
|
|
|
7369
|
if ( $reading ) |
|
143
|
|
|
|
|
|
|
{ |
|
144
|
63
|
|
|
|
|
196
|
$sql .= $queries[ $i ] . "\n"; |
|
145
|
63
|
|
|
|
|
159
|
next; |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
3897
|
100
|
|
|
|
22520
|
if ( $queries[ $i ] =~ m/^\[\s*$name\s*\]\s*/ ) |
|
149
|
|
|
|
|
|
|
{ |
|
150
|
14
|
|
|
|
|
27
|
$reading = 1; |
|
151
|
14
|
|
|
|
|
44
|
next; |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
### The requested query doesn't exist |
|
156
|
116
|
100
|
|
|
|
2720
|
return undef if !$reading; |
|
157
|
|
|
|
|
|
|
|
|
158
|
11
|
|
|
|
|
23
|
my %hash = %{ $params }; |
|
|
11
|
|
|
|
|
163
|
|
|
159
|
|
|
|
|
|
|
|
|
160
|
11
|
|
|
|
|
151
|
foreach my $key ( sort ( keys ( %hash ) ) ) |
|
161
|
|
|
|
|
|
|
{ |
|
162
|
134
|
|
|
|
|
201
|
my $tmp = $hash{ $key }; |
|
163
|
134
|
|
|
|
|
1603
|
$sql =~ s/\{$key\}/$tmp/g; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
11
|
|
|
|
|
245
|
return $sql; |
|
167
|
|
|
|
|
|
|
}; |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
1; |