Re: DBI::Statement parsing



John Cougar wrote:

Heya DBI-Users

Can anyone help a DBI::Statement newbie with the following problems:


Not that it matters, but the module is called SQL::Statement, not DBI::Statement.

1. When parsing a SQL string, is there a preferred way for initially testing
whether the statement did or did not parse successfully?


The errstr() method returns true if there was a parsing error, so this is how to manually trap errors:

my $stmt = SQL::Statement->new($sql);
die if $stmt->errstr;

You can also use the RaiseError and PrintError flags just as in DBI. The default behavior, like DBI is for RaiseError=0 and PrintError=1. In other words, it will not automatically die on error. If you want to automatically die on error, do this instead:

my $parser = SQL::Parser->new('AnyData',{RaiseError=>1,PrintError=>0});
for my $sql(@sqls) {
my $stmt = SQL::Statement->new($sql,$parser);
# will die here automatically if there's a parsing error
}

I am currently testing the $stmt->{original_string} element in the return
hash from creating the statement object; this element seems to only be
instantiated when the parsing succeeds. Would this be considered a valid way
to test?


No, use the errstr() method.

the doco for the return hash is rather light-on, however,
causing quite a bit of guesswork on part of the developer.


Documentation patches gratefully accepted :-). Just email them directly to me or send them to rt.cpan.org. But don't use the return hash, use the SQL::Statement methods wherever possible.

2. The parsing seems to be very weak?

The main purpose of the module is to support DBD::CSV and the eight other DBDs that depend on it. It attempts to also serve as a general SQL parser, but is admittedly weak in some areas (though not in the areas you seem to be having problems with now).

seems to let a
lot of things slip through ...

For instance:

"SELECT c1, c2 FROM x WHERE" seems to parse successfully, no error raised
for the empty WHERE-clause ... and things like that.


With RaiseError set to 1 or with manually trapping using $stmt->errstr(), that causes an error for me.

"SELECT *, junk FROM x" parses successfully!?


Why shouldn't it parse successfully? It's a valid SQL statement, AFAIK.

--
Jeff
.