From: Arthur Axel "fREW" Schmidt Date: Tue, 14 Sep 2010 02:40:48 +0000 (+0000) Subject: placeholder coloring! X-Git-Tag: v1.70~59 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9d11f0d4173184b69711d7641557a21d2b6e3927;p=dbsrgits%2FSQL-Abstract.git placeholder coloring! --- diff --git a/Changes b/Changes index e2646ba..3ede2be 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for SQL::Abstract - Document methods on Tree + - Add affordances for color coding placeholders - Change ::Tree::whitespace to whitespace_keyword revision 1.67_03 2010-09-11 diff --git a/examples/console.pl b/examples/console.pl index 0b731d3..5a4747e 100644 --- a/examples/console.pl +++ b/examples/console.pl @@ -18,3 +18,6 @@ my @sql = ( print "\n\n'" . $sqlat->format($_) . "'\n" for @sql; +print "\n\n'" . $sqlat->format( + "UPDATE session SET expires = ? WHERE (id = ?)", ['1', 1] +) . "'\n"; diff --git a/lib/SQL/Abstract/Tree.pm b/lib/SQL/Abstract/Tree.pm index e8e5ff3..7fbf47a 100644 --- a/lib/SQL/Abstract/Tree.pm +++ b/lib/SQL/Abstract/Tree.pm @@ -31,6 +31,7 @@ use base 'Class::Accessor::Grouped'; __PACKAGE__->mk_group_accessors( simple => $_ ) for qw( newline indent_string indent_amount colormap indentmap fill_in_placeholders + placeholder_surround ); # Parser states for _recurse_parse() @@ -125,6 +126,7 @@ my %indents = ( my %profiles = ( console => { fill_in_placeholders => 1, + placeholder_surround => ['?/', ''], indent_string => ' ', indent_amount => 2, newline => "\n", @@ -133,6 +135,7 @@ my %profiles = ( }, console_monochrome => { fill_in_placeholders => 1, + placeholder_surround => ['?/', ''], indent_string => ' ', indent_amount => 2, newline => "\n", @@ -141,6 +144,7 @@ my %profiles = ( }, html => { fill_in_placeholders => 1, + placeholder_surround => ['', ''], indent_string => ' ', indent_amount => 2, newline => "
\n", @@ -169,6 +173,10 @@ my %profiles = ( eval { require Term::ANSIColor; + + $profiles{console}->{placeholder_surround} = + [Term::ANSIColor::color('black on_cyan'), Term::ANSIColor::color('reset')]; + $profiles{console}->{colormap} = { select => [Term::ANSIColor::color('red'), Term::ANSIColor::color('reset')], 'insert into' => [Term::ANSIColor::color('red'), Term::ANSIColor::color('reset')], @@ -330,14 +338,15 @@ sub _is_key { defined $tree && defined $self->indentmap->{lc $tree}; } -sub _fill_in_placeholder { +sub fill_in_placeholder { my ($self, $bindargs) = @_; if ($self->fill_in_placeholders) { my $val = pop @{$bindargs} || ''; + my ($left, $right) = @{$self->placeholder_surround}; $val =~ s/\\/\\\\/g; $val =~ s/'/\\'/g; - return qq('$val') + return qq('$left$val$right') } return '?' } @@ -359,7 +368,7 @@ sub unparse { } elsif ($car eq 'LITERAL') { if ($cdr->[0] eq '?') { - return $self->_fill_in_placeholder($bindargs) + return $self->fill_in_placeholder($bindargs) } return $cdr->[0]; } @@ -403,6 +412,8 @@ sub format { my $self = shift; $self->unparse($self->parse($_[0]), $_[1]) } $args = { profile => 'console', # predefined profile to use (default: 'none') fill_in_placeholders => 1, # true for placeholder population + placeholder_surround => # The strings that will be wrapped around + [GREEN, RESET], # populated placeholders if the above is set indent_string => ' ', # the string used when indenting indent_amount => 2, # how many of above string to use for a single # indent level @@ -463,3 +474,10 @@ Later on it may do more and allow for coderef based transforms. my ($before, $after) = @{$sqlat->whitespace_keyword('SELECT')}; Returns whitespace to be inserted around a keyword. + +=head2 fill_in_placeholder + + my $value = $sqlat->fill_in_placeholder(\@bindargs) + +Removes last arg from passed arrayref and returns it, surrounded with +the values in placeholder_surround, and then surrounded with single quotes. diff --git a/t/15placeholders.t b/t/15placeholders.t new file mode 100644 index 0000000..bba9b66 --- /dev/null +++ b/t/15placeholders.t @@ -0,0 +1,31 @@ +use strict; +use warnings; + +use Test::More; +use SQL::Abstract::Tree; + +my $placeholders = ['station', 'lolz']; + +{ + my $sqlat = SQL::Abstract::Tree->new({ + fill_in_placeholders => 1, + placeholder_surround => [qw(; -)], + }); + + is($sqlat->fill_in_placeholder($placeholders), q(';lolz-'), + 'placeholders are populated correctly' + ); +} + +{ + my $sqlat = SQL::Abstract::Tree->new({ + fill_in_placeholders => 1, + placeholder_surround => [qw(< >)], + }); + + is($sqlat->fill_in_placeholder($placeholders), q(''), + 'placeholders are populated correctly and in order' + ); +} + +done_testing;