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