Skip Menu |

This queue is for tickets about the SQL-Beautify CPAN distribution.

Report information
The Basics
Id: 47687
Status: resolved
Priority: 0/
Queue: SQL-Beautify

People
Owner: Nobody in particular
Requestors: cornelius [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: convert SQL keywords to upper case
well, this is not a bug , but i like sql keywords in uppercase. :D filter select order.id,order.amount from something; to SELECT order.id, order.amount FROM something;
On Wed Jul 08 01:54:29 2009, CORNELIUS wrote: Show quoted text
> well, this is not a bug , but i like sql keywords in uppercase. :D > > filter > > select order.id,order.amount from something; > > to > > SELECT order.id, order.amount FROM something;
so i think if we can have a uppercase option that will be great! :p
From: lecar_red [...] yahoo.com
I have attached a patch and test case for this new feature. This includes a documentation update. I've done the patch with a git client and I've never done that before so I hope I didn't muck it up too bad :) I added the constructor option: 'uc_keywords' to be set to true and default to false Thanks for a great library!
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
Thanks for the patch and sorry for letting you wait. https://github.com/jkramer/sql-beautify/ commit/7b289c4f4cc8f34bd653823d4e9b0db3b6805219