Testing a value against a constraint? - Mailing list pgsql-general

From Benjamin Smith
Subject Testing a value against a constraint?
Date
Msg-id 200407111753.20904.bens@effortlessis.com
Whole thread Raw
Responses Re: Testing a value against a constraint?  (Michael Glaesemann <grzm@myrealbox.com>)
List pgsql-general
I'm writing an intranet app in PHP, and having issues around constraints.
Specifically, the error messages coming back from PG aren't very user
friendly. I'm looking for a way to make this a bit more smooth to the end
user, and ensure that my conditional checks really do match the requirements
set forth in the database.

For example, given a check constraint called "check_productcode" is it
possible to test a value against that constraint alone, without attempting to
insert anything?

Something like

    "select check constraint check_productcode('testvalue')"

 where "testvalue" is the string you wish to check against constraint called
"check_productcode". It might return an error message or not.

Can this be done? How? I can't find this in the docs anywhere...

Right now, I have lots of code like this:

<?

Function CheckInvoice($invoice)
{
$productcodes=array('E1', 'E2', 'E3', 'F11');
if (!in_array($productcodes($invoice['productcode'], $productcodes))
 return "Error: Product code is not valid!";
...
}

This is a tedious, duplicative, and error-prone way just to get some
plain-english error messages, since anytime a constraint needs to be changed,
it requires a code audit. Ugh.

Is there a better way?

-Ben


--
"I kept looking around for somebody to solve the problem.
Then I realized I am somebody"
   -Anonymous


pgsql-general by date:

Previous
From: "Marcus Klinge"
Date:
Subject: Re: what happened to pghoster.com?
Next
From: "Charles Su"
Date:
Subject: Problem Report