Permalink
Browse files

Added lowercasing of non-keyword items

  • Loading branch information...
1 parent 103f986 commit 92266eabcad9cc20320986067a737d442b45c3a7 Nigel Metheringham committed Mar 21, 2011
Showing with 40 additions and 0 deletions.
  1. +10 −0 lib/SQL/Beautify.pm
  2. +30 −0 t/case_handling.t
View
@@ -64,6 +64,7 @@ sub new {
$self->{keywords} = [] unless defined($self->{keywords});
$self->{rules} = {} unless defined($self->{rules});
$self->{uc_keywords} = 0 unless defined $self->{uc_keywords};
+ $self->{lc_names} = 0 unless defined $self->{lc_names};
push @{$self->{keywords}}, KEYWORDS;
@@ -232,6 +233,10 @@ sub _add_token {
$token = uc $token
if $self->_is_keyword($token) and $self->{uc_keywords};
+ # lowercase name
+ $token = lc $token
+ if $self->{lc_names} and !$self->_is_keyword( $token ) and !$self->_is_constant( $token );
+
$self->{_output} .= $token;
# This can't be the beginning of a new line anymore.
@@ -432,6 +437,11 @@ color escape sequences.
When true (1) all SQL keywords will be uppercased in output. Default is false (0).
+=item B<lc_names> => 1|0
+
+When true (1) all SQL names (ie not keywords or constants) will be lowercased in output.
+Default is false (0).
+
=back
=item B<add>($more_sql)
View
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+use SQL::Beautify;
+
+my $sql = new SQL::Beautify( spaces => 2, uc_keywords => 1, lc_names => 1 );
+my $query;
+my $beauty;
+
+ok( $sql, 'got instance' );
+
+while ( my $in = <DATA> ) {
+ my $result = eval $in;
+ my $deformat = $result;
+ $deformat =~ tr/\r\n\t / /s;
+ $sql->query( uc($deformat) );
+ is( $result, $sql->beautify, 'Test convert from upper case' );
+ $sql->query( lc($deformat) );
+ is( $result, $sql->beautify, 'Test convert from lower case' );
+}
+
+done_testing;
+
+__DATA__
+"SELECT\n *\nFROM\n foo,\n bar,\n baz\nWHERE\n foo.id = bar.id\n AND\n bar.id = baz.id\n"
+"SELECT\n *\nFROM\n foo,\n bar,\n baz\nWHERE\n foo.id = bar.id\n AND\n bar.id = baz.id\n"
+"SELECT\n foo.id,\n bar.name\nFROM\n foo,\n bar,\n baz\nWHERE\n foo.id = bar.id\n AND\n bar.id = baz.id\n OR\n bar.id != foo.id\n"

0 comments on commit 92266ea

Please sign in to comment.