File Coverage

blib/lib/SQL/Composer/Quoter.pm
Criterion Covered Total %
statement 37 37 100.0
branch 8 8 100.0
condition 8 9 88.8
subroutine 6 6 100.0
pod 0 4 0.0
total 59 64 92.1


line stmt bran cond sub pod time code
1             package SQL::Composer::Quoter;
2              
3 10     10   13043 use strict;
  10         12  
  10         212  
4 10     10   27 use warnings;
  10         12  
  10         3248  
5              
6             sub new {
7 87     87 0 9036 my $class = shift;
8 87         178 my (%params) = @_;
9              
10 87         92 my $self = {};
11 87         89 bless $self, $class;
12              
13 87   100     378 $self->{quote_char} = $params{quote_char} ||= '`';
14 87   100     253 $self->{name_separator} = $params{name_separator} ||= '.';
15 87   50     250 $self->{quote_string_char} = $params{quote_string_char} ||= "'";
16              
17 87 100       155 if (my $driver = $params{driver}) {
18 5 100       31 if ($driver =~ /Pg/i) {
19 2         3 $self->{quote_char} = '"';
20             }
21             }
22              
23 87         306 return $self;
24             }
25              
26             sub quote {
27 212     212 0 165 my $self = shift;
28 212         197 my ($column, $prefix) = @_;
29              
30 212         681 my @parts = split /\Q$self->{name_separator}\E/, $column;
31 212         230 foreach my $part (@parts) {
32 221         297 $part = $self->{quote_char} . $part . $self->{quote_char};
33             }
34              
35 212 100 100     460 if ($prefix && @parts == 1) {
36 80         121 unshift @parts, $self->{quote_char} . $prefix . $self->{quote_char};
37             }
38              
39 212         705 return join $self->{name_separator}, @parts;
40             }
41              
42             sub quote_string {
43 2     2 0 18 my $self = shift;
44 2         4 my ($string) = @_;
45              
46 2         21 $string =~ s{$self->{quote_string_char}}{\\$self->{quote_string_char}}g;
47              
48 2         9 return $self->{quote_string_char} . $string . $self->{quote_string_char};
49             }
50              
51             sub split {
52 2     2 0 7 my $self = shift;
53 2         3 my ($quoted_column) = @_;
54              
55 2         13 my ($table, $column) = split /\Q$self->{name_separator}\E/, $quoted_column;
56 2 100       7 ($column, $table) = ($table, '') unless $column;
57              
58             return
59 2         3 map { s/^\Q$self->{quote_char}\E//; s/\Q$self->{quote_char}\E$//; $_ }
  4         14  
  4         9  
  4         15  
60             ($table, $column);
61             }
62              
63             1;
64             __END__