&eq_sql_bind &eq_sql &eq_bind
$case_sensitive $sql_differ/;
+my $sqlat = SQL::Abstract::Tree->new;
+
our $case_sensitive = 0;
our $parenthesis_significant = 0;
our $sql_differ; # keeps track of differing portion between SQLs
my ($sql1, $sql2) = @_;
# parse
- my $tree1 = parse($sql1);
- my $tree2 = parse($sql2);
+ my $tree1 = $sqlat->parse($sql1);
+ my $tree2 = $sqlat->parse($sql2);
return 1 if _eq_sql($tree1, $tree2);
}
}
# one is a list, the other is an op with a list
elsif (ref $left->[0] xor ref $right->[0]) {
- $sql_differ = sprintf ("left: %s\nright: %s\n", map { unparse ($_) } ($left, $right) );
+ $sql_differ = sprintf ("left: %s\nright: %s\n", map { $sqlat->unparse ($_) } ($left, $right) );
return 0;
}
# one is a list, so is the other
# if operators are different
if ( $left->[0] ne $right->[0] ) {
$sql_differ = sprintf "OP [$left->[0]] != [$right->[0]] in\nleft: %s\nright: %s\n",
- unparse($left),
- unparse($right);
+ $sqlat->unparse($left),
+ $sqlat->unparse($right);
return 0;
}
# elsif operators are identical, compare operands
}
else {
my $eq = _eq_sql($left->[1], $right->[1]);
- $sql_differ ||= sprintf ("left: %s\nright: %s\n", map { unparse ($_) } ($left, $right) ) if not $eq;
+ $sql_differ ||= sprintf ("left: %s\nright: %s\n", map { $sqlat->unparse ($_) } ($left, $right) ) if not $eq;
return $eq;
}
}
}
-sub parse { goto &SQL::Abstract::Tree::parse }
-
-sub unparse { goto &SQL::Abstract::Tree::unparse }
-
-
1;
return $keyword
}
+sub whitespace {
+ my ($self, $keyword, $depth) = @_;
+ if (lc $keyword eq 'from') {
+ return ['', "\n"];
+ }
+ return ['', ''];
+}
+
+sub newline { "\n" }
+
+sub indent { ' ' x $_[1] }
+
sub unparse {
- my ($self, $tree) = @_;
+ my ($self, $tree, $depth) = @_;
+
+ $depth ||= 1;
if (not $tree ) {
return '';
}
- elsif (ref $tree->[0]) {
- return join (" ", map $self->unparse ($_), @$tree);
+
+ my $car = $tree->[0];
+ my $cdr = $tree->[1];
+
+ if (ref $car) {
+ return join (" ", map $self->unparse($_), @$tree);
}
- elsif ($tree->[0] eq 'LITERAL') {
- return $tree->[1][0];
+ elsif ($car eq 'LITERAL') {
+ return $cdr->[0];
}
- elsif ($tree->[0] eq 'PAREN') {
- return sprintf '(%s)', join (" ", map $self->unparse($_), @{$tree->[1]});
+ elsif ($car eq 'PAREN') {
+ return '(' . $self->newline .
+ join(' ',
+ map $self->indent($depth) . $self->unparse($_, $depth + 1), @{$cdr})
+ . $self->newline . ')';
}
- elsif ($tree->[0] eq 'OR' or $tree->[0] eq 'AND' or (grep { $tree->[0] =~ /^ $_ $/xi } @binary_op_keywords ) ) {
- return join (" $tree->[0] ", map $self->unparse($_), @{$tree->[1]});
+ elsif ($car eq 'OR' or $car eq 'AND' or (grep { $car =~ /^ $_ $/xi } @binary_op_keywords ) ) {
+ return join (" $car ", map $self->unparse($_), @{$cdr});
}
else {
- return sprintf '%s %s', $self->format_keyword($tree->[0]), $self->unparse ($tree->[1]);
+ my ($l, $r) = @{$self->whitespace($car, $depth)};
+ return sprintf "%s %s$r", $self->format_keyword($car), $self->unparse($cdr);
}
}
print $sqlat->format($sql) . "\n";
}
+{
+ my $sql = "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'";
+
+ print "$sql\n";
+ print $sqlat->format($sql) . "\n";
+}
+
# stuff we want:
# Nested indentation
# Max Width