X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FAbstract%2FTree.pm;h=d9b67f9f42919ed26e80e0c355519e187f455d1b;hb=c54740ba9963ea408e5b8d0dd8e8cb8fc4886dc6;hp=decc5ed208b4da1f6bf1cea1bb66d6a97c9a1ca5;hpb=70c6f0e91c090ef8fe0d2ceb1466bcf9e484cfb9;p=scpubgit%2FQ-Branch.git
diff --git a/lib/SQL/Abstract/Tree.pm b/lib/SQL/Abstract/Tree.pm
index decc5ed..d9b67f9 100644
--- a/lib/SQL/Abstract/Tree.pm
+++ b/lib/SQL/Abstract/Tree.pm
@@ -1,38 +1,22 @@
package SQL::Abstract::Tree;
+# DO NOT edit away without talking to riba first, he will just put it back
+# BEGIN pre-Moo2 import block
+BEGIN {
+ require warnings;
+ my $initial_fatal_bits = (${^WARNING_BITS}||'') & $warnings::DeadBits{all};
+ local $ENV{PERL_STRICTURES_EXTRA} = 0;
+ require Moo; Moo->import;
+ require Sub::Quote; Sub::Quote->import('quote_sub');
+ ${^WARNING_BITS} &= ( $initial_fatal_bits | ~ $warnings::DeadBits{all} );
+}
+# END pre-Moo2 import block
+
use strict;
use warnings;
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;
my $op_look_ahead = '(?: (?= [\s\)\(\;] ) | \z)';
my $op_look_behind = '(?: (?<= [\,\s\)\(] ) | \A )';
@@ -196,18 +180,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,74 +251,79 @@ 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 {