X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FAbstract%2FTree.pm;h=3791fe9d2e94b69ccf025fb1cf4dd9d18b407327;hb=e67751dd9effc7f5066e264c1d6149613a7efa42;hp=9edc1d722dd41ef7cebaf9ef386b686e312c965f;hpb=a4d17ff1e4ca5f981aacbeab10b0efa93ac047d9;p=scpubgit%2FQ-Branch.git
diff --git a/lib/SQL/Abstract/Tree.pm b/lib/SQL/Abstract/Tree.pm
index 9edc1d7..3791fe9 100644
--- a/lib/SQL/Abstract/Tree.pm
+++ b/lib/SQL/Abstract/Tree.pm
@@ -1,38 +1,10 @@
package SQL::Abstract::Tree;
-use strict;
-use warnings;
+use Moo;
no warnings 'qw';
-use Carp;
-
-use Hash::Merge qw//;
-
-use base 'Class::Accessor::Grouped';
-
-__PACKAGE__->mk_group_accessors( simple => qw(
- newline indent_string indent_amount colormap indentmap fill_in_placeholders
- placeholder_surround
-));
-
-my $merger = Hash::Merge->new;
-$merger->specify_behavior({
- SCALAR => {
- SCALAR => sub { $_[1] },
- ARRAY => sub { [ $_[0], @{$_[1]} ] },
- HASH => sub { $_[1] },
- },
- ARRAY => {
- SCALAR => sub { $_[1] },
- ARRAY => sub { $_[1] },
- HASH => sub { $_[1] },
- },
- HASH => {
- SCALAR => sub { $_[1] },
- ARRAY => sub { [ values %{$_[0]}, @{$_[1]} ] },
- HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
- },
-}, 'SQLA::Tree Behavior' );
+use Carp;
+use Sub::Quote 'quote_sub';
my $op_look_ahead = '(?: (?= [\s\)\(\;] ) | \z)';
my $op_look_behind = '(?: (?<= [\,\s\)\(] ) | \A )';
@@ -106,7 +78,7 @@ my $alphanum_cmp_op_re = join ("\n\t|\n", map
);
$alphanum_cmp_op_re = qr/$alphanum_cmp_op_re/x;
-my $binary_op_re = '(?: NOT \s+)? (?:' . join ('|', qw/IN BETWEEN R?LIKE/) . ')';
+my $binary_op_re = '(?: NOT \s+)? (?:' . join ('|', qw/IN BETWEEN [RI]?LIKE REGEXP/) . ')';
$binary_op_re = join "\n\t|\n",
"$op_look_behind (?i: $binary_op_re | AS ) $op_look_ahead",
$alphanum_cmp_op_re,
@@ -196,18 +168,33 @@ my %indents = (
first => 1,
);
-my %profiles = (
- console => {
- fill_in_placeholders => 1,
- placeholder_surround => ['?/', ''],
- indent_string => ' ',
- indent_amount => 2,
- newline => "\n",
- colormap => {},
- indentmap => \%indents,
-
- eval { require Term::ANSIColor }
- ? do {
+
+has [qw(
+ newline indent_string indent_amount fill_in_placeholders placeholder_surround
+)] => (is => 'ro');
+
+has [qw( indentmap colormap )] => ( is => 'ro', default => quote_sub('{}') );
+
+# class global is in fact desired
+my $merger;
+
+sub BUILDARGS {
+ my $class = shift;
+ my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
+
+ if (my $p = delete $args->{profile}) {
+ my %extra_args;
+ if ($p eq 'console') {
+ %extra_args = (
+ fill_in_placeholders => 1,
+ placeholder_surround => ['?/', ''],
+ indent_string => ' ',
+ indent_amount => 2,
+ newline => "\n",
+ colormap => {},
+ indentmap => \%indents,
+
+ ! ( eval { require Term::ANSIColor } ) ? () : do {
my $c = \&Term::ANSIColor::color;
my $red = [$c->('red') , $c->('reset')];
@@ -252,79 +239,86 @@ my %profiles = (
offset => $green,
}
);
- } : (),
- },
- console_monochrome => {
- fill_in_placeholders => 1,
- placeholder_surround => ['?/', ''],
- indent_string => ' ',
- indent_amount => 2,
- newline => "\n",
- colormap => {},
- indentmap => \%indents,
- },
- html => {
- fill_in_placeholders => 1,
- placeholder_surround => ['', ''],
- indent_string => ' ',
- indent_amount => 2,
- newline => "
\n",
- colormap => {
- select => ['' , ''],
- 'insert into' => ['' , ''],
- update => ['' , ''],
- 'delete from' => ['' , ''],
-
- set => ['', ''],
- from => ['' , ''],
-
- where => ['' , ''],
- values => ['', ''],
-
- join => ['' , ''],
- 'left join' => ['',''],
- on => ['' , ''],
-
- 'group by' => ['', ''],
- having => ['', ''],
- 'order by' => ['', ''],
-
- skip => ['', ''],
- first => ['', ''],
- limit => ['', ''],
- offset => ['', ''],
-
- 'begin work' => ['', ''],
- commit => ['', ''],
- rollback => ['', ''],
- savepoint => ['', ''],
- 'rollback to savepoint' => ['', ''],
- 'release savepoint' => ['', ''],
- },
- indentmap => \%indents,
- },
- none => {
- colormap => {},
- indentmap => {},
- },
-);
-
-sub new {
- my $class = shift;
- my $args = shift || {};
-
- my $profile = delete $args->{profile} || 'none';
+ },
+ );
+ }
+ elsif ($p eq 'console_monochrome') {
+ %extra_args = (
+ fill_in_placeholders => 1,
+ placeholder_surround => ['?/', ''],
+ indent_string => ' ',
+ indent_amount => 2,
+ newline => "\n",
+ indentmap => \%indents,
+ );
+ }
+ elsif ($p eq 'html') {
+ %extra_args = (
+ fill_in_placeholders => 1,
+ placeholder_surround => ['', ''],
+ indent_string => ' ',
+ indent_amount => 2,
+ newline => "
\n",
+ colormap => { map {
+ (my $class = $_) =~ s/\s+/-/g;
+ ( $_ => [ qq||, '' ] )
+ } (
+ keys %indents,
+ qw(commit rollback savepoint),
+ 'begin work', 'rollback to savepoint', 'release savepoint',
+ ) },
+ indentmap => \%indents,
+ );
+ }
+ elsif ($p eq 'none') {
+ # nada
+ }
+ else {
+ croak "No such profile '$p'";
+ }
- die "No such profile '$profile'!" unless exists $profiles{$profile};
+ # see if we got any duplicates and merge if needed
+ if (scalar grep { exists $args->{$_} } keys %extra_args) {
+ # heavy-duty merge
+ $args = ($merger ||= do {
+ require Hash::Merge;
+ my $m = Hash::Merge->new;
+
+ $m->specify_behavior({
+ SCALAR => {
+ SCALAR => sub { $_[1] },
+ ARRAY => sub { [ $_[0], @{$_[1]} ] },
+ HASH => sub { $_[1] },
+ },
+ ARRAY => {
+ SCALAR => sub { $_[1] },
+ ARRAY => sub { $_[1] },
+ HASH => sub { $_[1] },
+ },
+ HASH => {
+ SCALAR => sub { $_[1] },
+ ARRAY => sub { [ values %{$_[0]}, @{$_[1]} ] },
+ HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
+ },
+ }, 'SQLA::Tree Behavior' );
+
+ $m;
+ })->merge(\%extra_args, $args );
- my $data = $merger->merge( $profiles{$profile}, $args );
+ }
+ else {
+ $args = { %extra_args, %$args };
+ }
+ }
- bless $data, $class
+ $args;
}
sub parse {
my ($self, $s) = @_;
+ return [] unless defined $s;
+
# tokenize string, and remove all optional whitespace
my $tokens = [];
foreach my $token (split $tokenizer_re, $s) {
@@ -346,7 +340,7 @@ sub _recurse_parse {
my @left;
while (1) { # left-associative parsing
- if ( ! @$tokens
+ if (! @$tokens
or
($state == PARSE_IN_PARENS && $tokens->[0] eq ')')
or
@@ -424,19 +418,19 @@ sub _recurse_parse {
}
# unary op keywords
- elsif ( $token =~ $unary_op_re ) {
+ elsif ($token =~ $unary_op_re) {
my $op = uc $token;
# normalize RNO explicitly
$op = 'ROW_NUMBER() OVER' if $op =~ /^$rno_re$/;
- my @right = $self->_recurse_parse ($tokens, PARSE_RHS);
+ my @right = $self->_recurse_parse($tokens, PARSE_RHS);
push @left, [ $op => \@right ];
}
# expression terminator keywords
- elsif ( $token =~ $expr_start_re ) {
+ elsif ($token =~ $expr_start_re) {
my $op = uc $token;
my @right = $self->_recurse_parse($tokens, PARSE_IN_EXPR);
@@ -444,7 +438,7 @@ sub _recurse_parse {
}
# a '?'
- elsif ( $token =~ $placeholder_re) {
+ elsif ($token =~ $placeholder_re) {
push @left, [ -PLACEHOLDER => [ $token ] ];
}
@@ -468,7 +462,7 @@ sub _recurse_parse {
and
$tokens->[0] !~ $all_std_keywords_re
and
- ! ( @$tokens > 1 and $tokens->[1] eq '(' )
+ ! (@$tokens > 1 and $tokens->[1] eq '(')
) {
push @lits, [ -LITERAL => [ shift @$tokens ] ];
}