Re: DBI::Statement parsing
- From: jeff@xxxxxxxxxxxxxx (Jeff Zucker)
- Date: Wed, 26 Oct 2005 09:53:50 -0700
John Cougar wrote:
Not that it matters, but the module is called SQL::Statement, not DBI::Statement.Heya DBI-Users
Can anyone help a DBI::Statement newbie with the following problems:
1. When parsing a SQL string, is there a preferred way for initially testingThe errstr() method returns true if there was a parsing error, so this is how to manually trap errors:
whether the statement did or did not parse successfully?
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,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.
causing quite a bit of guesswork on part of the developer.
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).2. The parsing seems to be very weak?
With RaiseError set to 1 or with manually trapping using $stmt->errstr(), that causes an error for me.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.
"SELECT *, junk FROM x" parses successfully!?
Why shouldn't it parse successfully? It's a valid SQL statement, AFAIK.
-- Jeff .
- Follow-Ups:
- RE: SQL::Statement parsing
- From: John Cougar
- RE: SQL::Statement parsing
- References:
- DBI::Statement parsing
- From: John Cougar
- DBI::Statement parsing
- Prev by Date: Re: DBD::Oracle and $sth->{PRECISION}
- Next by Date: RE: Newline inserted?
- Previous by thread: DBI::Statement parsing
- Next by thread: RE: SQL::Statement parsing
- Index(es):