File Coverage

blib/lib/SQL/OOP/Base.pm
Criterion Covered Total %
statement 63 68 92.6
branch 23 30 76.6
condition 19 24 79.1
subroutine 14 14 100.0
pod 8 8 100.0
total 127 144 88.1


line stmt bran cond sub pod time code
1             package SQL::OOP::Base;
2 19     19   103 use strict;
  19         34  
  19         623  
3 19     19   90 use warnings;
  19         36  
  19         518  
4 19     19   91 use Scalar::Util qw(blessed);
  19         59  
  19         1143  
5 19     19   458 use 5.005;
  19         68  
  19         16745  
6              
7             our $quote_char;
8              
9             sub quote_char {
10 1111     1111 1 1338 my ($self, $val) = @_;
11 1111 100       1987 if (defined $val) {
12 2         6 $self->{quote_char} = $val;
13             }
14 1111 100       2354 if (! defined $self->{quote_char}) {
15 786         1360 $self->{quote_char} = q(");
16             }
17 1111   66     3240 return $quote_char || $self->{quote_char};
18             }
19              
20             sub escape_code_ref {
21 208     208 1 255 my ($self, $val) = @_;
22 208 50       417 if (defined $val) {
23 0         0 $self->{escape_code_ref} = $val;
24             }
25 208 100       468 if (! defined $self->{escape_code_ref}) {
26             $self->{escape_code_ref} = sub {
27 208     208   298 my ($str, $quote_char) = @_;
28 208         713 $str =~ s{$quote_char}{$quote_char$quote_char}g;
29 208         386 return $str;
30 206         1172 };
31             }
32 208         546 return $self->{escape_code_ref};
33             }
34              
35             ### ---
36             ### Constructor
37             ### ---
38             sub new {
39 608     608 1 12464 my ($class, $str, $bind_ref) = @_;
40 608 100 100     1806 if (ref $str && (ref($str) eq 'CODE')) {
41 15         41 $str = $str->();
42             }
43 608 100 66     3138 if (blessed($str) && $str->isa(__PACKAGE__)) {
    100          
44 165         689 return $str;
45             } elsif ($str) {
46 441 50 66     1084 if ($bind_ref && ! ref $bind_ref) {
47 0         0 die '$bind_ref must be an Array ref';
48             }
49 441   100     4574 return bless {
50             str => $str,
51             gen => undef,
52             bind => ($bind_ref || [])
53             }, $class;
54             }
55 2         6 return;
56             }
57              
58             ### ---
59             ### Get SQL snippet
60             ### ---
61             sub to_string {
62 1020     1020 1 3488 my ($self, $prefix) = @_;
63 1020         2374 local $SQL::OOP::Base::quote_char = $self->quote_char;
64 1020 100       2158 if (! defined $self->{gen}) {
65 772         1932 $self->generate;
66             }
67 1020 100 100     3836 if ($self->{gen} && $prefix) {
68 1         4 return $prefix. ' '. $self->{gen};
69             } else {
70 1019         5012 return $self->{gen};
71             }
72             }
73              
74             ### ---
75             ### Get SQL snippet with values embedded [EXPERIMENTAL]
76             ### ---
77             sub to_string_embedded {
78 2     2 1 9 my ($self, $quote_with) = @_;
79 2         7 local $SQL::OOP::Base::quote_char = $self->quote_char;
80 2   100     7 $quote_with ||= q{'};
81 2         5 my $format = $self->to_string;
82 2         11 $format =~ s{\?}{%s}g;
83             return
84 2         3 sprintf($format, map {$self->quote($_, $quote_with)} @{[$self->bind]});
  2         8  
  2         5  
85             }
86              
87             ### ---
88             ### Get binded values in array
89             ### ---
90             sub bind {
91 128     128 1 203 my ($self) = @_;
92 128 50       277 return @{$self->{bind} || []} if (wantarray);
  128 50       503  
93 0 0       0 return scalar @{$self->{bind} || []};
  0         0  
94             }
95              
96             ### ---
97             ### initialize generated SQL
98             ### ---
99             sub _init_gen {
100 471     471   588 my ($self) = @_;
101 471         1423 $self->{gen} = undef;
102             }
103              
104             ### ---
105             ### Generate SQL snippet
106             ### ---
107             sub generate {
108 441     441 1 527 my ($self) = @_;
109 441   50     1099 $self->{gen} = $self->{str} || '';
110 441         811 return $self;
111             }
112              
113             ### ---
114             ### quote
115             ### ---
116             sub quote {
117 208     208 1 334 my ($self, $val, $with) = @_;
118 208 100       445 if (! $with) {
119 206   66     429 $with = $quote_char || $self->quote_char;
120             }
121 208 50       399 if (defined $val) {
122 208         543 $val = $self->escape_code_ref->($val, $with);
123 208         761 return $with. $val. $with;
124             } else {
125 0           return undef;
126             }
127             }
128              
129             1;
130              
131             __END__