Skip to content

Instantly share code, notes, and snippets.

@dylanwh
Created February 3, 2020 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanwh/57f13ace22ffdd047a57ba125c8d6919 to your computer and use it in GitHub Desktop.
Save dylanwh/57f13ace22ffdd047a57ba125c8d6919 to your computer and use it in GitHub Desktop.
sub quote_columns {
my ($self, @columns) = @_;
my @quoted_columns = ();
state $sql_date_re = do {
my $format = quotemeta(quotemeta($self->quote('__FORMAT__')));
my $sql = quotemeta($self->sql_date_format('__COLUMN__', '__FORMAT__'));
$sql =~ s/__COLUMN__/(.+)/;
$sql =~ s/$format/(.+)/;
$sql;
};
warn $sql_date_re;
foreach (@columns) {
if (/^\w+$/) {
push @quoted_columns, $self->quote_identifier($_);
}
elsif (my ($t, $c) = /^(\w+)\.(\w+)$/) {
push @quoted_columns,
$self->quote_identifier($t) . "." . $self->quote_identifier($c);
}
elsif (my ($expr, $alias) = /^\s*(.+?)\s+AS\s+(\w+)\s*$/i) {
push @quoted_columns, $self->quote_columns($expr) . " AS " . $self->quote_identifier($alias);
}
elsif (my ($column, $format) = /$sql_date_re/) {
push @quoted_columns, $self->sql_date_format($self->quote_columns($column), $format);
}
else {
die "unable to quote column: $_";
}
}
return join(", ", @quoted_columns);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment