Move ProducerUtils into the new dir layout
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Generator / Utils.pm
CommitLineData
c13f5f6a 1package # hide from pause
fd9f0e09 2 SQL::Translator::Generator::Utils;
27ae9ae7 3
4use Moo;
5use Sub::Quote 'quote_sub';
6
7# this should be ro, but I have to modify it in BUILD so bleh
8has quote_chars => ( is => 'rw' );
9
10has name_sep => (
11 is => 'ro',
12 default => quote_sub q{ '.' },
13);
14
15sub BUILD {
16 my $self = shift;
17
18 unless (ref($self->quote_chars)) {
19 if ($self->quote_chars) {
20 $self->quote_chars([$self->quote_chars])
21 } else {
22 $self->quote_chars([])
23 }
24 }
25
26 $self
27}
28
29sub quote {
30 my ($self, $label) = @_;
31
32 return '' unless defined $label;
33 return $$label if ref($label) eq 'SCALAR';
34
35 my @quote_chars = @{$self->quote_chars};
36 return $label unless scalar @quote_chars;
37
38 my ($l, $r);
39 if (@quote_chars == 1) {
40 ($l, $r) = (@quote_chars) x 2;
41 } elsif (@quote_chars == 2) {
42 ($l, $r) = @quote_chars;
43 } else {
44 die 'too many quote chars!';
45 }
46
47 my $sep = $self->name_sep || '';
48 # parts containing * are naturally unquoted
49 join $sep, map "$l$_$r", ( $sep ? split (/\Q$sep\E/, $label ) : $label )
50}
51
521;