[Bast-commits] r9727 - in SQL-Abstract/1.x/trunk: . examples
lib/SQL/Abstract t
frew at dev.catalyst.perl.org
frew at dev.catalyst.perl.org
Tue Sep 14 02:40:48 GMT 2010
Author: frew
Date: 2010-09-14 03:40:48 +0100 (Tue, 14 Sep 2010)
New Revision: 9727
Added:
SQL-Abstract/1.x/trunk/t/15placeholders.t
Modified:
SQL-Abstract/1.x/trunk/Changes
SQL-Abstract/1.x/trunk/examples/console.pl
SQL-Abstract/1.x/trunk/lib/SQL/Abstract/Tree.pm
Log:
placeholder coloring!
Modified: SQL-Abstract/1.x/trunk/Changes
===================================================================
--- SQL-Abstract/1.x/trunk/Changes 2010-09-14 01:00:24 UTC (rev 9726)
+++ SQL-Abstract/1.x/trunk/Changes 2010-09-14 02:40:48 UTC (rev 9727)
@@ -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
Modified: SQL-Abstract/1.x/trunk/examples/console.pl
===================================================================
--- SQL-Abstract/1.x/trunk/examples/console.pl 2010-09-14 01:00:24 UTC (rev 9726)
+++ SQL-Abstract/1.x/trunk/examples/console.pl 2010-09-14 02:40:48 UTC (rev 9727)
@@ -18,3 +18,6 @@
print "\n\n'" . $sqlat->format($_) . "'\n" for @sql;
+print "\n\n'" . $sqlat->format(
+ "UPDATE session SET expires = ? WHERE (id = ?)", ['1', 1]
+) . "'\n";
Modified: SQL-Abstract/1.x/trunk/lib/SQL/Abstract/Tree.pm
===================================================================
--- SQL-Abstract/1.x/trunk/lib/SQL/Abstract/Tree.pm 2010-09-14 01:00:24 UTC (rev 9726)
+++ SQL-Abstract/1.x/trunk/lib/SQL/Abstract/Tree.pm 2010-09-14 02:40:48 UTC (rev 9727)
@@ -31,6 +31,7 @@
__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 %profiles = (
console => {
fill_in_placeholders => 1,
+ placeholder_surround => ['?/', ''],
indent_string => ' ',
indent_amount => 2,
newline => "\n",
@@ -133,6 +135,7 @@
},
console_monochrome => {
fill_in_placeholders => 1,
+ placeholder_surround => ['?/', ''],
indent_string => ' ',
indent_amount => 2,
newline => "\n",
@@ -141,6 +144,7 @@
},
html => {
fill_in_placeholders => 1,
+ placeholder_surround => ['<span class="placeholder">', '</span>'],
indent_string => ' ',
indent_amount => 2,
newline => "<br />\n",
@@ -169,6 +173,10 @@
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 @@
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 @@
}
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 @@
$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 @@
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.
Added: SQL-Abstract/1.x/trunk/t/15placeholders.t
===================================================================
--- SQL-Abstract/1.x/trunk/t/15placeholders.t (rev 0)
+++ SQL-Abstract/1.x/trunk/t/15placeholders.t 2010-09-14 02:40:48 UTC (rev 9727)
@@ -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('<station>'),
+ 'placeholders are populated correctly and in order'
+ );
+}
+
+done_testing;
More information about the Bast-commits
mailing list