Commit | Line | Data |
b84764fb |
1 | package SQL::Abstract::Parts; |
2 | |
b3b54441 |
3 | use Module::Runtime (); |
4 | use Scalar::Util (); |
b84764fb |
5 | use strict; |
6 | use warnings; |
7 | |
8 | use overload '""' => 'stringify', fallback => 1; |
9 | |
10 | sub new { |
b3b54441 |
11 | my ($proto, $join, @parts) = @_; |
12 | bless([ |
13 | $join, map Scalar::Util::blessed($_) ? [ @$_ ] : $_, @parts |
14 | ], ref($proto) || $proto); |
b84764fb |
15 | } |
16 | |
17 | sub stringify { |
18 | my ($self) = @_; |
19 | my ($join, @parts) = @$self; |
07070f1a |
20 | return join($join, map +(ref() ? stringify($_) : $_), @parts); |
b3b54441 |
21 | } |
22 | |
23 | sub to_array { return @{$_[0]} } |
24 | |
07070f1a |
25 | sub formatter { |
b3b54441 |
26 | my ($self, %opts) = @_; |
27 | Module::Runtime::use_module('SQL::Abstract::Formatter') |
28 | ->new(%opts) |
07070f1a |
29 | } |
30 | |
31 | sub format { |
32 | my ($self, %opts) = @_; |
33 | $self->formatter(%opts) |
34 | ->format($self->to_array); |
b84764fb |
35 | } |
36 | |
37 | 1; |