use Carp;
-use Term::ANSIColor 'color';
use base 'Class::Accessor::Grouped';
__PACKAGE__->mk_group_accessors( simple => $_ ) for qw(
indent_string => ' ',
indent_amount => 2,
newline => "\n",
- colormap => {
- select => [color('red'), color('reset')],
- where => [color('green'), color('reset')],
- from => [color('cyan'), color('reset')],
+ colormap => {},
+ indentmap => {
+ select => 0,
+ where => 1,
+ from => 1,
},
- indentmap => {
- select => 0,
+ },
+ console_monochrome => {
+ indent_string => ' ',
+ indent_amount => 2,
+ newline => "\n",
+ colormap => {},
+ indentmap => {
+ select => 0,
where => 1,
from => 1,
},
},
);
+eval {
+ require Term::ANSIColor;
+ $profiles{console}->{colormap} = {
+ select => [Term::ANSIColor::color('red'), Term::ANSIColor::color('reset')],
+ where => [Term::ANSIColor::color('green'), Term::ANSIColor::color('reset')],
+ from => [Term::ANSIColor::color('cyan'), Term::ANSIColor::color('reset')],
+ };
+};
+
sub new {
my ($class, $args) = @_;
1;
+=pod
+
+=head1 SYNOPSIS
+
+ my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' });
+
+ print $sqla_tree->format('SELECT * FROM foo WHERE foo.a > 2');
+
+ # SELECT *
+ # FROM foo
+ # WHERE foo.a > 2
+
use strict;
use warnings;
+use Test::More;
use SQL::Abstract::Tree;
-my $sqlat = SQL::Abstract::Tree->new({});
+my $sqlat = SQL::Abstract::Tree->new({
+ profile => 'console_monochrome',
+});
{
my $sql = "SELECT a, b, c FROM foo WHERE foo.a =1 and foo.b LIKE 'station'";
-
- print "$sql\n";
- print $sqlat->format($sql) . "\n";
+ my $expected_sql =
+ qq{SELECT a, b, c \n} .
+ qq{ FROM foo \n} .
+ qq{ WHERE foo.a = 1 AND foo.b LIKE 'station' };
+ is($sqlat->format($sql), $expected_sql,
+ 'simple statement formatted correctly'
+ );
}
{
my $sql = "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a =1 and foo.b LIKE 'station'";
-
- print "$sql\n";
- print $sqlat->format($sql) . "\n";
+ my $expected_sql =
+ qq{SELECT * \n} .
+ qq{ FROM (\n} .
+ qq{ SELECT * \n} .
+ qq{ FROM foobar \n} .
+ qq{ ) \n} .
+ qq{ WHERE foo.a = 1 AND foo.b LIKE 'station' };
+
+ is($sqlat->format($sql), $expected_sql,
+ 'subquery statement formatted correctly'
+ );
}
{
my $sql = "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'";
-
- print "$sql\n";
- print $sqlat->format($sql) . "\n";
+ my $expected_sql =
+ qq{SELECT * \n} .
+ qq{ FROM lolz \n} .
+ qq{ WHERE (foo.a = 1) AND foo.b LIKE 'station' };
+
+ is($sqlat->format($sql), $expected_sql,
+ 'simple statement with parens in where formatted correctly'
+ );
}
+done_testing;
# stuff we want:
# Nested indentation
# Max Width