checking select query syntax and semantics via php without executing

Lists: Postg토토 사이트 순위SQL : Postg토토 사이트 순위SQL 메일 링리스트 : 2009-10-05 이후 PGSQL-PHP 21:34
From: Marco Dieckhoff <dieck(at)gmx(dot)de>
To: pgsql-php(at)postgresql(dot)org
Subject: checking select query syntax and semantics via php without executing
Date: 2009-10-05 21:34:32
Message-ID: 4ACA6668.2000701@gmx.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: Postg토토 사이트 순위SQL : Postg토토 사이트 순위SQL 메일 링리스트 : 2009-10-05 이후 PGSQL-PHP 21:34


Hi there!

Is it possible in PHP to give a sql (select) query to Postgres via pg_*
so that it is NOT executed but merely checked for syntax including
correct, existing field and table names, and data types?

I'm working on a system where users may assemble their own query in a
construction kit, and want that to be checked, so that they don't e.g.
compare UUID "<" datetime or something like this.

If I try to execute the generated query, bad queries are instantly found
by pg_query.

But good ones are fully executed, which can take a long time depending
on joins and aggregations.

It seems that pg_prepare doesn't return errors on bad queries,
pg_last_error() is empty.

The asynchronous pg_send_query (and pg_cancel_query) seem not to return
any errors, either.

I tried with three queries:

"SELECT * FROM tableok"
"SELECT nonexistingfield FROM nonexistingtable"
"SELECT complete bad / sql query FROM nonsense"

Any ideas?


From: Raymond O'Donnell <rod(at)iol(dot)ie>
To: Marco Dieckhoff <dieck(at)gmx(dot)de>
Cc: pgsql-php(at)postgresql(dot)org
Subject: Re: checking select query syntax and semantics via php without executing
Date: 2009-10-05 21:50:51
Message-ID: 4ACA6A3B.50005@iol.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-php

On 05/10/2009 22:34, Marco Dieckhoff wrote:

> Is it possible in PHP to give a sql (select) query to Postgres via pg_*
> so that it is NOT executed but merely checked for syntax including
> correct, existing field and table names, and data types?

How about prepending "explain" to the query? Just a wild notion.

Ray.

------------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod(at)iol(dot)ie
Galway Cathedral Recitals: http://www.galwaycathedral.org/recitals
------------------------------------------------------------------


From: Bill Moran <wmoran(at)potentialtech(dot)com>
To: Marco Dieckhoff <dieck(at)gmx(dot)de>
Cc: pgsql-php(at)postgresql(dot)org
Subject: Re: checking select query syntax and semantics via php without executing
Date: 2009-10-05 22:01:39
Message-ID: 20091005180139.25cc9511.wmoran@potentialtech.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-php

In response to Marco Dieckhoff <dieck(at)gmx(dot)de>:
>
> Hi there!
>
> Is it possible in PHP to give a sql (select) query to Postgres via pg_*
> so that it is NOT executed but merely checked for syntax including
> correct, existing field and table names, and data types?
>
>
> I'm working on a system where users may assemble their own query in a
> construction kit, and want that to be checked, so that they don't e.g.
> compare UUID "<" datetime or something like this.
>
>
> If I try to execute the generated query, bad queries are instantly found
> by pg_query.
>
>
> But good ones are fully executed, which can take a long time depending
> on joins and aggregations.
>
> It seems that pg_prepare doesn't return errors on bad queries,
> pg_last_error() is empty.
>
> The asynchronous pg_send_query (and pg_cancel_query) seem not to return
> any errors, either.
>
> I tried with three queries:
>
> "SELECT * FROM tableok"
> "SELECT nonexistingfield FROM nonexistingtable"
> "SELECT complete bad / sql query FROM nonsense"
>
>
> Any ideas?

Off the top of my head, you could prefix the query with EXPLAIN, which
will cause it to be planned (thus checking syntax) and return an
execution plan, but not actually run the query.

--
Bill Moran
http://www.potentialtech.com
http://people.collaborativefusion.com/~wmoran/


From: Marco Dieckhoff <dieck(at)gmx(dot)de>
To: pgsql-php(at)postgresql(dot)org
Subject: Re: checking select query syntax and semantics via php without executing
Date: 2009-10-05 22:03:36
Message-ID: 4ACA6D38.4010005@gmx.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-php

Bill Moran schrieb:
> In response to Marco Dieckhoff <dieck(at)gmx(dot)de>:
>> Hi there!
>>
>> Is it possible in PHP to give a sql (select) query to Postgres via pg_*
>> so that it is NOT executed but merely checked for syntax including
>> correct, existing field and table names, and data types?

> Off the top of my head, you could prefix the query with EXPLAIN, which
> will cause it to be planned (thus checking syntax) and return an
> execution plan, but not actually run the query.

Works as a charm, thanks to both of you.

Must be one of the quickest "case solved, thread closed" I have ever seen :)


From: Christophe Pettus <xof(at)thebuild(dot)com>
To: pgsql-php(at)postgresql(dot)org
Subject: Re: checking select query syntax and semantics via php without executing
Date: 2009-10-05 22:06:45
Message-ID: D14DA293-B92D-4540-8CC7-D789A7632868@thebuild.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-php


On Oct 5, 2009, at 3:03 PM, Marco Dieckhoff wrote:
> Must be one of the quickest "case solved, thread closed" I have ever
> seen :)

And you can present the user with the query plan, so they can tell if
they are about to do something horrible to the database. :)

--
-- Christophe Pettus
xof(at)thebuild(dot)com