my ($func, @args) = @$rest;
return $self->join_query_parts('',
$self->_sqlcase($func),
- '(',
- $self->join_query_parts(', ', @args),
- ')'
+ $self->join_query_parts('',
+ '(',
+ $self->join_query_parts(', ', @args),
+ ')'
+ ),
);
}
return $self->join_query_parts(' ',
$lhs,
$self->format_keyword($op),
- '(',
- $self->join_query_parts(', ', @rhs),
- ')'
+ $self->join_query_parts(' ',
+ '(',
+ $self->join_query_parts(', ', @rhs),
+ ')'
+ ),
);
}
my ($self, $op, $args) = @_;
return undef unless @$args;
return $self->join_query_parts('', $args->[0]) if @$args == 1;
- return $self->join_query_parts(
- ' ' => '(', $self->_render_op_multop($op, $args), ')'
+ return $self->join_query_parts(' ',
+ '(', $self->_render_op_multop($op, $args), ')'
);
}
--- /dev/null
+package SQL::Abstract::Formatter;
+
+require SQL::Abstract::Parts; # it loads us too, don't cross the streams
+
+use Moo;
+
+has indent_by => (is => 'ro', default => ' ');
+has max_width => (is => 'ro', default => 78);
+
+sub _join {
+ shift;
+ SQL::Abstract::Parts::stringify(\@_);
+}
+
+sub format {
+ my ($self, $join, @parts) = @_;
+::Dwarn [ J => $join => @parts ];
+ my $sql = $self->_join($join, @parts);
+ return $sql unless length($sql) > $self->max_width;
+ local $self->{max_width} = $self->{max_width} - length($self->indent_by);
+ return join("\n", map $self->format(@$_), @parts);
+}
+
+1;
package SQL::Abstract::Parts;
+use Module::Runtime ();
+use Scalar::Util ();
use strict;
use warnings;
use overload '""' => 'stringify', fallback => 1;
sub new {
- my ($proto, @args) = @_;
- bless(\@args, ref($proto) || $proto);
+ my ($proto, $join, @parts) = @_;
+ bless([
+ $join, map Scalar::Util::blessed($_) ? [ @$_ ] : $_, @parts
+ ], ref($proto) || $proto);
}
sub stringify {
my ($self) = @_;
my ($join, @parts) = @$self;
- return join $join, @parts;
+ return join $join, map ref() ? stringify($_) : $_, @parts;
+}
+
+sub to_array { return @{$_[0]} }
+
+sub format {
+ my ($self, %opts) = @_;
+ Module::Runtime::use_module('SQL::Abstract::Formatter')
+ ->new(%opts)
+ ->format($self->to_array);
}
1;
warn $ARGV[1];
-my $sqlac = SQL::Abstract::ExtraClauses->new(unknown_unop_always_func => 1);
+my $sqlac = SQL::Abstract::ExtraClauses->new(
+ unknown_unop_always_func => 1,
+ lazy_join_sql_parts => 1,
+);
my @args = ($ARGV[1] =~ /^\.\// ? do $ARGV[1] : eval '+('.$ARGV[1].')');
die $@ if $@;
-Dwarn([ $sqlac->${\$ARGV[0]}(@args) ]);
+my ($q, @bind) = $sqlac->${\$ARGV[0]}(@args);
+
+Dwarn [ Q => @$q ];
+
+Dwarn [ $q->format, @bind ];