Reducing duplicated business rules

Reducing duplicated business rules

am 04.11.2003 17:50:46 von Michael Glaesemann

Hi all

I've been coding in PHP and PostgreSQL for a while now=97nothing=20
complicated but what I've put together has gotten the job done. I=20
haven't employed templating yet, but have managed to separate a good=20
deal of the application logic from the presentation by calling=20
functions, and am happily building libraries of functions I commonly=20
use. One thing I'm particularly pleased about is that I've reached a=20
point where I can look back at the first implementations and say to=20
myself that I know I can code and refactor it better now. Nice to be=20
able to see improvement.

One area I'd definitely like to improve is filtering form data. I don't=20
mean preventing SQL injection or other security issues=97those are=20
definitely important and something I'm working on as well=97but rather=20
type and constraint checking before inserting or updating data into the=20
database. I've been including as much business logic as I can into the=20
database to maintain data integrity (regardless of what I might=20
foolishly allow a user to do via one of the scripts I write :) ), but I=20
find myself duplicating these same checks in my code so I can (a)=20
prevent getting errors from the database when inserting/updating and=20
(b) give feedback to the user, letting them know what it is I'd like=20
them to correct.

A simple example is when I'd like to make sure a given date is before=20
or after another. For example, an examination date must follow a=20
registration date. This is enforced by the database (check=20
registration_date < examination_date) but I will do a similar check in=20
the PHP code as well, providing feedback to the user that the=20
examination date must be after the registration date.

Of course PostgreSQL will throw back an error if I tried to insert or=20
update data that will make the check constraint untrue, which is as it=20
should be. Is there any way to use this feedback from Postgres instead=20
of running my own checks in PHP, and still provide useful feedback to=20
the user (rather than the naked PostgreSQL error code)?

Along similar lines, often I have a requirement in another table, for=20
example the start and end dates of a registration period. Of course=20
registration dates should fall between these two. As far as I can tell,=20
this kind of constraint isn't possible in PostgreSQL via CHECK=20
constraints. (I'm guessing it's possible via before triggers, but I=20
haven't looked at triggers yet. I'm still quite wet behind the ears!)=20
In this case I grab the start and end dates in a select, and use them=20
as bounds to check the submitted registration date in the PHP code.

Are there other ways to handle these situations that I'm missing? Any=20
opinions, suggestions, helpful advice, or pointers in the direction of=20
further knowledge gratefully accepted.

Regards,
Michael
grzm myrealbox com


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Re: Reducing duplicated business rules

am 06.11.2003 02:46:32 von Peter Bayley

My preference would be towards identifying bad data as soon as possible and
thus provide the user a better (more interactive) experience. For me this
means more clevers on the client - ie some JavaScript to check dates,
formats, etc. This is relatively simple to implement as a function invoked
by the "SEND" or "OK" button and which eventually does a form.submit() if
everything is okay. If there is a problem somewhere, you can alert the user
to the problem, set the form focus to the offending input value and then
allow the user to re-enter data. Even better would be to call validation
after each field has been changed so the user doesn't have to fill in the
entire form before his or her errors are identified.

The other idea is to create some PostgreSQL-stored metadata with the form so
that the rules for validation can be read from the database. This would
allow PHP to read the validation rules for an input field (eg date must be
in the future) and validate the field generically. It would also allow the
PHP creating the input form in the first place to provide extra information
in the form so that the pre-submit JavaScript function could also be
generic - the same validation script would then read the validation rules -
perhaps from hidden input fields and validate the corresponding input value.
The metadata could include a PROMPT message that tells the user what is
needed in a particular field.

Hope this helps

Peter

----- Original Message -----
From: "Michael Glaesemann"
To:
Sent: Wednesday, 05 November, 2003 3:50 AM
Subject: [PHP] Reducing duplicated business rules


Hi all

I've been coding in PHP and PostgreSQL for a while now—nothing
complicated but what I've put together has gotten the job done. I
haven't employed templating yet, but have managed to separate a good
deal of the application logic from the presentation by calling
functions, and am happily building libraries of functions I commonly
use. One thing I'm particularly pleased about is that I've reached a
point where I can look back at the first implementations and say to
myself that I know I can code and refactor it better now. Nice to be
able to see improvement.

One area I'd definitely like to improve is filtering form data. I don't
mean preventing SQL injection or other security issues—those are
definitely important and something I'm working on as well—but rather
type and constraint checking before inserting or updating data into the
database. I've been including as much business logic as I can into the
database to maintain data integrity (regardless of what I might
foolishly allow a user to do via one of the scripts I write :) ), but I
find myself duplicating these same checks in my code so I can (a)
prevent getting errors from the database when inserting/updating and
(b) give feedback to the user, letting them know what it is I'd like
them to correct.

A simple example is when I'd like to make sure a given date is before
or after another. For example, an examination date must follow a
registration date. This is enforced by the database (check
registration_date < examination_date) but I will do a similar check in
the PHP code as well, providing feedback to the user that the
examination date must be after the registration date.

Of course PostgreSQL will throw back an error if I tried to insert or
update data that will make the check constraint untrue, which is as it
should be. Is there any way to use this feedback from Postgres instead
of running my own checks in PHP, and still provide useful feedback to
the user (rather than the naked PostgreSQL error code)?

Along similar lines, often I have a requirement in another table, for
example the start and end dates of a registration period. Of course
registration dates should fall between these two. As far as I can tell,
this kind of constraint isn't possible in PostgreSQL via CHECK
constraints. (I'm guessing it's possible via before triggers, but I
haven't looked at triggers yet. I'm still quite wet behind the ears!)
In this case I grab the start and end dates in a select, and use them
as bounds to check the submitted registration date in the PHP code.

Are there other ways to handle these situations that I'm missing? Any
opinions, suggestions, helpful advice, or pointers in the direction of
further knowledge gratefully accepted.

Regards,
Michael
grzm myrealbox com


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Re: Reducing duplicated business rules

am 06.11.2003 16:52:10 von Daniel Struck

> Of course PostgreSQL will throw back an error if I tried to insert or=20
> update data that will make the check constraint untrue, which is as it=20
> should be. Is there any way to use this feedback from Postgres instead=20
> of running my own checks in PHP, and still provide useful feedback to=20
> the user (rather than the naked PostgreSQL error code)?

I am right now also looking how to get a feedback from a postgrsql-check ba=
ck to php.

I thought about using named constraints:

CREATE TABLE test (
id SERIAL,
[whatever fields you need]

CONSTRAINT "!!!#Please check XYZ in the formular#" CHECK ([whatever you lik=
e to test])
);

Next, I would check in php if an error occured in the sql-query, if the err=
or includes "!!!" in the error message from postgresql I would display the =
formular again with the values already entered and I would display as a fee=
dback to the user the string between the two "##":

Please check XYZ in the formular

One drawback is of course that it will only give feedback from one error at=
a time.


greetings,
Daniel

--=20
Retrovirology Laboratory Luxembourg
Centre Hospitalier de Luxembourg
4, rue E. Barbl=E9
L-1210 Luxembourg

phone: +352-44116105
fax: +352-44116113
web: http://www.retrovirology.lu
e-mail: struck.d@retrovirology.lu

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Re: Reducing duplicated business rules

am 06.11.2003 17:16:54 von Michael Glaesemann

Peter

Thanks for your reply. It's good to hear others thoughts on this.

On Thursday, November 6, 2003, at 10:46 AM, Peter Bayley wrote:

> My preference would be towards identifying bad data as soon as=20
> possible and
> thus provide the user a better (more interactive) experience. For me=20
> this
> means more clevers on the client -


> ie some JavaScript to check dates, formats, etc.

I've seen elsewhere people using JavaScript for checking these things.=20
I do the same thing within PHP, primarily because I don't know=20
JavaScript and have been able to get the job done (I think) in PHP=20
alone. I ask this from a position of ignorance, not of questioning your=20
judgement: What is the advantage of using JavaScript over PHP for=20
checking? This is completely a wild guess on my part, but does it have=20
to do with server load? I know to use PHP to check these things I end=20
up sending the form back to the server for processing. Looking again at=20
what you've written, I'm guessing this is exactly it. Using JavaScript=20
allows the client=97rather than the server=97do the checking. It seems to=
=20
me you'd pay an initial hit (downloading more into the client), but you=20
don't have to keep using the server to process/validate/check the data.=20
Am I close?

> Even better would be to call validation after each field has been=20
> changed so the user doesn't have to fill in the entire form before his=20
> or her errors are identified.

This is something I've been doing as well, or at least whenever a user=20
triggers some action (like a
dropdown (either multiple selection using a join table, or single selection)
the options in the list come from following the foreign key definitions.
Specially named views (schema.table/view.attribute) are used to get labels
for meaningful display,
so you don't have drop down lists with your externally meaningless
primary keys, but nice values (e.g. value = doc_id and label = doc_title)
if no view is found for the table/column, then just the values are used
as labels.

validation is doen using the folowing procedure
for each attribute in the tuple
"SELECT 'newvalue'::datatype"
if this fails, then it is a parse error, and the message "invalid
[attribute name]" is registered with the object against that attribute

then for each check constraint (both single and multiple column) defined
against the table (including where domains are used as attribute data types)
we run the following query
"SELECT [check constraint definition] AS [check constraint name] FROM
([for each attribute in the CHECK, output 'value'::type AS name,]) AS
values"
the result will be true or false, and the name of the only reutrned
column is the name of the check that passed/failed
if you have named your check contstraints sensibly, then the name is
used (if it failed) as an error message against the attribute(s) involved in
the check.

if all the values parse correctly, and all the check constraints are
passed - THEN we try to do the INSERT/UPDATE

other jobs the objects can do is insert parsed default values (from the
metadata) to display initially in the form.

Sadly, all this 'niceness' comes at a cost, because of all the extra queries
that must be executed in order to get back full validation failure messages
from PG.
Once we have released, then it might be possible to try and go back and make
some of this functionality available inside PHP, by adding to the pg_xxx
commands
available in PHP (particularly pg_meta_data())

This tuple object is then renderable as XHTML form elements (with
session/db/table/attirbute unique element names md5'd) so that multiple
tables can be exposed on a single form.

I'd be interested to hear from any other people who've built similar
systems, or about any database abstraction projects which attempt to do the
'possible value lookup' and full validation
descripbed above. Our code was working with 7.3.4 fairly well, and should be
back up and finished soon with 7.4. Once the project is compelted, I'll see
about releasig a more generic set
of classes (our are currently tied to a project-specific inheritance layer)
or expanding this into a gBorg project or similar.


--
--
Tom Hebbron
www.hebbron.com
+39 0444540626 (Vicenza, Italy)



---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Re: Reducing duplicated business rules

am 24.11.2003 20:08:44 von Ryan Mahoney

--=-G51eToX8oYDcg3zIKE4Y
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

We have done the same thing that you are doing - but we don't read the
database metadata at all, we just create an xml file when we create the
table that describes the datatype and html display type so we can have
custom types such as email address type, etc.

I thought about doing this the way you have, but ultimately I decided
that depending on the database metadata would be too limited for my
needs.

Attached is an xml file that describes one of our tables. We have a
custom application that automatically build interfaces to add, edit,
list, view, and delete data based on this xml file with validation.

-r

On Thu, 2003-11-20 at 14:36, Tom Hebbron wrote:
> Hi group,
>
> I'm interested to see that other people have hit this problem. In the last
> few months I've been writing and re-writing PHP classes to access a
> postgreSQL database for a CMS.
> Here is a rough overview of the way I've dealt with the validation problem.
>
> tuple class
> array of attribute class objects
>
> initialisation fetches metadata about the table/view the tuple (row)
> belongs to (attribute types, domains & built in CHECK constraints, foriegn
> keys, not null)
> this data comes from a set of attribute metadata views (built from
> system catalogs and optimised - further optimisation is envisaged for PG
> 7.4)
> the tuple / attribute metadata allows us to do the following things
>
> foreign key attributes can auto-build a list of options for a