factor quote method out of Generator::Utils
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Generator / Role / Quote.pm
1 package # hide from pause
2   SQL::Translator::Generator::Role::Quote;
3
4 use Moo::Role;
5
6 requires qw(quote_chars name_sep);
7
8 sub quote {
9   my ($self, $label) = @_;
10
11   return '' unless defined $label;
12   return $$label if ref($label) eq 'SCALAR';
13
14   my @quote_chars = @{$self->quote_chars};
15   return $label unless scalar @quote_chars;
16
17   my ($l, $r);
18   if (@quote_chars == 1) {
19     ($l, $r) = (@quote_chars) x 2;
20   } elsif (@quote_chars == 2) {
21     ($l, $r) = @quote_chars;
22   } else {
23     die 'too many quote chars!';
24   }
25
26   my $sep = $self->name_sep || '';
27   # parts containing * are naturally unquoted
28   join $sep, map "$l$_$r", ( $sep ? split (/\Q$sep\E/, $label ) : $label )
29 }
30
31 1;