tags:

views:

97

answers:

3

Hi,

with

#!/usr/bin/perl
use warnings;
use strict;
use 5.010;
use SQL::Parser;                                 

my $parser = SQL::Parser->new( 'ANSI', {RaiseError=>1} );

my $word = 'BETWEEN';

my $success = $parser->feature( 'reserved_words', $word );
$success = $success ? '' : 'NOT';
say "$word is $success a reserved word";

I can check if a word is a reserved word.

Is there a function that gives me a list of all reserved words?

+7  A: 

SQL::Dialects::ANSI is a simple interface to information about ANSI SQL in INI format. So you get that and parse it... except its not in INI format because it doesn't contain key = value but just key which chokes Config::INI. Alas, this is one of the few INI parsers I could find that will deal with a string.

So you might have to parse it by hand. That's what SQL::Parser does.

Alternatively you can pull the list out of SQL::Parser's guts.

use Data::Dumper;
use SQL::Parser;

my $s = SQL::Parser->new;
print Dumper $s->{opts}{reserved_words};

This is a hack and will eventually fail.

As per my comments above, the list of ANSI SQL reserved words (hey, which version of ANSI SQL?) is not definitive. The database itself may reserve additional words. And different versions may reserve different words. If you can find a way to do whatever it is you're doing that doesn't rely on a list of reserved words, do that.

Schwern
My scripts don't rely an the list. It's for getting an idea of how this list looks like. I tried before to write down and to test a list with all words which just came to mind. But now I have seen that my list was not complete.I found SQL::ReservedWords with gives me about 100 words more, from which only one ( END-EXEC ) results to be a reserved word in my case.In which case would SQL::ReservedWords be the right thing?
sid_com
Oh, well, in that case read the standard.
Schwern
+1  A: 

Michael gave me this page via RT.

How about a method of SQL::Parser which returns all features of a given class (e.g. 'features($)'), analog to feature()? Would that help you? If yes, please open a feature request on CPAN against SQL::Statement.

I wouldn't break working interfaces others rely on without a good reason - missing feature is not a good reason.

Jens Rehsack
Thanks for posting; it's not that important for me. Please see my comment above.
sid_com
+1  A: 

If you don't want it programmatically, ignore everything I just said about SQL::Dialects and read the standard. Always check the standard as derivative works may be incomplete, incorrect or working off dialects. Even though there are updated ANSI SQL standards, most databases use some mutation of SQL-92 or elements of SQL:1999. After that they got silly.

I can't find a copy of the SQL:1999 standard, but here's a BNF which looks very thorough.

Schwern