| 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 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | 8 | ▶ | use Tie::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | 8 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | sub new | ||||||||||||||||||||||||||||
| 113 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 115 | 6 | my $self = { file => $_[ 0 ] }; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 6 | ▶ | die 'ERROR: Cannot find: ' . $self->{'file'} if ( ! -e $self->{'file'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | tie my @queries, 'Tie::File', $self->{'file'} | ||||||||||||||||||||||||||||
| 120 | 6 | ▶ | or die 'SQLib: I cannot open the file with queries: ' . $self->{'file'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 6 | $self->{'queries'} = \@queries; | |||||||||||||||||||||||||||
| 122 | 6 | bless $self, $class; | |||||||||||||||||||||||||||
| 123 | }; | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub get_query | ||||||||||||||||||||||||||||
| 126 | 119 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_query: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | 119 | my $self = shift; | |||||||||||||||||||||||||||
| 128 | 119 | my $name = shift; | |||||||||||||||||||||||||||
| 129 | 119 | my $params = shift; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 119 | my @queries = @{ $self->{'queries'} }; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 119 | my $sql = "\n"; | |||||||||||||||||||||||||||
| 134 | 119 | my $reading; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 119 | for my $i ( 0 .. $#queries ) | |||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||
| 138 | 7657 | ▶ | last if ( $queries[ $i ] =~ m/^\[\s*\/\s*$name\s*\]$/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 7644 | ▶ | next if ( $queries[ $i ] =~ m/^\s*$/ && !$reading ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$queries[$i] =~ /^\s*$/ and not $reading
MC/DC100%2/2
$queries[$i] =~ /^\s*$/ and not $reading
$queries[$i] =~ /^\s*$/
not $reading
Truth table100%3/3
A and not(B)
A= $queries[$i] =~ /^\s*$/B= not $reading
| |||||||||||||||||||||||||||||
| 140 | 3963 | ▶ | return undef if $reading && $queries[ $i ] =~ m/^\[/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$reading and $queries[$i] =~ /^\[/
MC/DC100%2/2
$reading and $queries[$i] =~ /^\[/
$reading
$queries[$i] =~ /^\[/
Truth table100%3/3
A and B
A= $readingB= $queries[$i] =~ /^\[/
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 3960 | ▶ | if ( $reading ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | { | ||||||||||||||||||||||||||||
| 144 | 63 | $sql .= $queries[ $i ] . "\n"; | |||||||||||||||||||||||||||
| 145 | 63 | next; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 3897 | ▶ | if ( $queries[ $i ] =~ m/^\[\s*$name\s*\]\s*/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||
| 150 | 14 | $reading = 1; | |||||||||||||||||||||||||||
| 151 | 14 | next; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | ### The requested query doesn't exist | ||||||||||||||||||||||||||||
| 156 | 116 | ▶ | return undef if !$reading; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 11 | my %hash = %{ $params }; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 11 | foreach my $key ( sort ( keys ( %hash ) ) ) | |||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||
| 162 | 134 | my $tmp = $hash{ $key }; | |||||||||||||||||||||||||||
| 163 | 134 | $sql =~ s/\{$key\}/$tmp/g; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 11 | return $sql; | |||||||||||||||||||||||||||
| 167 | }; | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 8 | 1; | |||||||||||||||||||||||||||