From 41e93241e19580713b9cb159626ef6989eb0ac72 Mon Sep 17 00:00:00 2001
From: Lee <lcarmich@lcarmich-laptop.(none)>
Date: Wed, 16 Dec 2009 14:24:58 -0600
Subject: [PATCH] Added upper case keyword option and test
---
lib/SQL/Beautify.pm | 9 ++++++++
t/uc_keywords.t | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 0 deletions(-)
create mode 100644 t/uc_keywords.t
diff --git a/lib/SQL/Beautify.pm b/lib/SQL/Beautify.pm
index 1b0e16e..2138d66 100644
--- a/lib/SQL/Beautify.pm
+++ b/lib/SQL/Beautify.pm
@@ -31,6 +31,7 @@ sub new {
$self->{wrap} = {} unless defined($self->{wrap});
$self->{keywords} = [] unless defined($self->{keywords});
$self->{rules} = {} unless defined($self->{rules});
+ $self->{uc_keywords} = 0 unless defined $self->{uc_keywords};
push @{$self->{keywords}}, KEYWORDS;
@@ -195,6 +196,10 @@ sub _add_token {
$self->{_output} .= $self->_indent;
}
+ # uppercase keywords
+ $token = uc $token
+ if $self->_is_keyword( $token ) and $self->{uc_keywords};
+
$self->{_output} .= $token;
# This can't be the beginning of a new line anymore.
@@ -391,6 +396,10 @@ color escape sequences.
{ keywords => [ "\x1B[0;31m", "\x1B[0m" ] }
+=item B<uc_keywords> => 1|0
+
+When true (1) all SQL keywords will be uppercased in output. Default is false (0).
+
=back
=item B<add>($more_sql)
diff --git a/t/uc_keywords.t b/t/uc_keywords.t
new file mode 100644
index 0000000..8ae76d1
--- /dev/null
+++ b/t/uc_keywords.t
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 10;
+use SQL::Beautify;
+
+my $sql = new SQL::Beautify(spaces => 2, uc_keywords => 1);
+my $query;
+my $beauty;
+
+ok($sql, 'got instance');
+
+# Test plain text formatting.
+$query = <DATA>;
+$beauty = <DATA>;
+
+$beauty = eval $beauty;
+
+ok($sql->query($query) eq $query, 'query set');
+ok($sql->query eq $query, 'query get');
+
+ok($sql->beautify eq $beauty, 'beautified');
+
+# test mixed cases
+$query = <DATA>;
+$beauty = <DATA>;
+
+$beauty = eval $beauty;
+
+ok($sql->query($query) eq $query, 'query set');
+ok($sql->query eq $query, 'query get');
+
+ok($sql->beautify eq $beauty, 'beautified');
+
+# all keywords
+$query = <DATA>;
+$beauty = <DATA>;
+
+$beauty = eval $beauty;
+
+ok($sql->query($query) eq $query, 'query set');
+ok($sql->query eq $query, 'query get');
+
+ok($sql->beautify eq $beauty, 'beautified');
+
+__DATA__
+select * from foo, bar, baz where foo.id = bar.id and bar.id = baz.id
+"SELECT\n *\nFROM\n foo,\n bar,\n baz\nWHERE\n foo.id = bar.id\n AND\n bar.id = baz.id\n"
+select * FROM foo, bar, baz where foo.id = bar.id AND bar.id = baz.id
+"SELECT\n *\nFROM\n foo,\n bar,\n baz\nWHERE\n foo.id = bar.id\n AND\n bar.id = baz.id\n"
+select foo.id, bar.name FROM foo, bar, baz where foo.id = bar.id AND bar.id = baz.id OR bar.id != foo.id
+"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"
--
1.6.3.3