);
use Moo::Role;
+sub _render_slice_limit {
+ my ($self, $dq) = @_;
+ return [
+ ($dq->{from} ? $self->_render($dq->{from}) : ()),
+ $self->_format_keyword('FETCH FIRST'),
+ sprintf("%i", $dq->{limit}{value}),
+ $self->_format_keyword('ROWS ONLY')
+ ];
+}
+
+sub _slice_type { 'FetchFirst' }
+
sub _render_slice {
my ($self, $dq) = @_;
unless ($dq->{offset}) {
- return [
- ($dq->{from} ? $self->_render($dq->{from}) : ()),
- $self->_format_keyword('FETCH FIRST'),
- sprintf("%i", $dq->{limit}{value}),
- $self->_format_keyword('ROWS ONLY')
- ];
+ return $self->_render_slice_limit($dq);
}
unless ($dq->{order_is_stable}) {
- die "FetchFirst limit style requires a stable order";
+ die $self->_slice_type." limit style requires a stable order";
}
die "Slice's inner is not a Select"
unless (my $orig_select = $dq->{from})->{type} eq DQ_SELECT;
}
my $key;
if ($s->{type} eq DQ_IDENTIFIER) {
- if (@{$s->{elements}} == 2) {
+ if (!$name and @{$s->{elements}} == 2) {
$default_inside_alias ||= $s->{elements}[0];
if ($s->{elements}[0] eq $default_inside_alias) {
$alias_map{join('.',@{$s->{elements}})} = $s;
$name ||= join('__', @{$s->{elements}});
$key = join('.', @{$s->{elements}});
} else {
- die "XXX not implemented yet";
+ die "XXX not implemented yet" unless $name;
+ $key = "$s";
}
$name ||= sprintf("GENSYM__%03i",++$gensym_count);
push @inside_select_list, +{
while ($order->{type} eq DQ_ORDER) {
my $by = $order->{by};
if ($by->{type} eq DQ_IDENTIFIER) {
+ $default_inside_alias ||= $by->{elements}[0]
+ if @{$by->{elements}} == 2;
$order_map{$by}
= $alias_map{join('.', @{$by->{elements}})}
||= do {
- my $name = sprintf("ORDER__BY__%03i",++$order_gensym_count);
- push @inside_select_list, +{
- type => DQ_ALIAS,
- from => $by,
- to => $name
- };
- +{
- type => DQ_IDENTIFIER,
- elements => [ $name ],
- };
+ if (
+ @{$by->{elements}} == 2
+ and $by->{elements}[0] eq $default_inside_alias
+ ) {
+ $by;
+ } else {
+ my $name = sprintf("ORDER__BY__%03i",++$order_gensym_count);
+ push @inside_select_list, +{
+ type => DQ_ALIAS,
+ from => $by,
+ to => $name
+ };
+ +{
+ type => DQ_IDENTIFIER,
+ elements => [ $name ],
+ };
+ }
};
} else {
die "XXX not implemented yet";
--- /dev/null
+package Data::Query::Renderer::SQL::Slice::Top;
+
+use Data::Query::Constants qw(
+ DQ_SELECT DQ_ALIAS DQ_IDENTIFIER DQ_ORDER DQ_SLICE
+);
+use Moo::Role;
+
+with 'Data::Query::Renderer::SQL::Slice::FetchFirst';
+
+sub _render_slice_limit {
+ my ($self, $dq) = @_;
+ my $basic = $self->_render($dq->{from});
+ return [
+ $basic->[0],
+ $self->_format_keyword('TOP'),
+ sprintf("%i", $dq->{limit}{value}),
+ @{$basic}[1..$#$basic]
+ ];
+}
+
+sub _slice_type { 'Top' }
+
+1;