Thread: Generic way to test input arguments

Generic way to test input arguments

From
said assemlal
Date:
Hello,

I am looking for a way to test generically input arguments to raise an exception if one is either null or empty.

I was thinking to create a function who takes an array to check them but not sure if it's really good.

What would be the best way to achieve that ?

Thanks
Saïd

Re: Generic way to test input arguments

From
Raymond O'Donnell
Date:
On 17/10/16 16:40, said assemlal wrote:
> Hello,
>
> I am looking for a way to test generically input arguments to raise an
> exception if one is either null or empty.
>
> I was thinking to create a function who takes an array to check them but
> not sure if it's really good.

It's not clear what you want to do here. Can you explain in more detail?

What do you mean by "input arguments"? Are you writing functions in the
database? If so, which language? Some examples of what you've tried so
far would help too.

Ray.




Re: Generic way to test input arguments

From
Pavel Stehule
Date:
Hi

2016-10-17 21:09 GMT+02:00 Raymond O'Donnell <rod@iol.ie>:
On 17/10/16 16:40, said assemlal wrote:
Hello,

I am looking for a way to test generically input arguments to raise an
exception if one is either null or empty.

I was thinking to create a function who takes an array to check them but
not sure if it's really good.

It's not clear what you want to do here. Can you explain in more detail?

What do you mean by "input arguments"? Are you writing functions in the database? If so, which language? Some examples of what you've tried so far would help too.

Ray.


9.6 has functions for this purpose - num_nulls and num_nonnulls

Regards

Pavel
 



--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Re: Generic way to test input arguments

From
Saïd Assemlal
Date:

I am writing database functions with plpgsql. (I am using Postgresql 9.4 with centos 6)

Here an example on what I would like to improve:

CREATE OR REPLACE FUNCTION usp_locking_trial(p_trial_code VARCHAR(50), p_trial_key VARCHAR(500))
RETURNS TEXT AS $$
DECLARE
BEGIN
    IF is_empty_or_null(p_trial_code) THEN
        RAISE EXCEPTION 'trial code argument is empty/null.';
    END IF;

    IF is_empty_or_null(p_trial_key) THEN
        RAISE EXCEPTION 'trial key argument is empty/null';
    END IF; ..................

END;
$$ LANGUAGE plpgsql;

I have many functions where I check if the arguments are null or empty. This code is repetitive and could be the almost the same between functions.

For a given example: a function who takes all input arguments and it checks one by one if it's null and raise an exception with the name of the argument.

Would it be a good idea ?

Thanks.

Le 2016-10-17 à 3:09 PM, Raymond O'Donnell a écrit :
On 17/10/16 16:40, said assemlal wrote:
Hello,

I am looking for a way to test generically input arguments to raise an
exception if one is either null or empty.

I was thinking to create a function who takes an array to check them but
not sure if it's really good.

It's not clear what you want to do here. Can you explain in more detail?

What do you mean by "input arguments"? Are you writing functions in the database? If so, which language? Some examples of what you've tried so far would help too.

Ray.



Re: Generic way to test input arguments

From
oyoun
Date:

I will try to reproduce this way on postgresql9.4


Thanks

Le 2016-10-17 à 3:30 PM, Pavel Stehule a écrit :
Hi

2016-10-17 21:09 GMT+02:00 Raymond O'Donnell <rod@iol.ie>:
On 17/10/16 16:40, said assemlal wrote:
Hello,

I am looking for a way to test generically input arguments to raise an
exception if one is either null or empty.

I was thinking to create a function who takes an array to check them but
not sure if it's really good.

It's not clear what you want to do here. Can you explain in more detail?

What do you mean by "input arguments"? Are you writing functions in the database? If so, which language? Some examples of what you've tried so far would help too.

Ray.


9.6 has functions for this purpose - num_nulls and num_nonnulls

Regards

Pavel
 



--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: Generic way to test input arguments

From
Pavel Stehule
Date:


2016-10-18 16:42 GMT+02:00 Saïd Assemlal <said.assemlal@gmail.com>:

I am writing database functions with plpgsql. (I am using Postgresql 9.4 with centos 6)

Here an example on what I would like to improve:

CREATE OR REPLACE FUNCTION usp_locking_trial(p_trial_code VARCHAR(50), p_trial_key VARCHAR(500))
RETURNS TEXT AS $$
DECLARE
BEGIN
    IF is_empty_or_null(p_trial_code) THEN
        RAISE EXCEPTION 'trial code argument is empty/null.';
    END IF;

    IF is_empty_or_null(p_trial_key) THEN
        RAISE EXCEPTION 'trial key argument is empty/null';
    END IF; ..................

END;
$$ LANGUAGE plpgsql;

I have many functions where I check if the arguments are null or empty. This code is repetitive and could be the almost the same between functions.

For a given example: a function who takes all input arguments and it checks one by one if it's null and raise an exception with the name of the argument.

Would it be a good idea ?

some smarter (generic) function can be written in C language (you should to use polymorphics type "any"). With plpgsql you cannot do enything else what you do.

Regards

Pavel
 

Thanks.

Le 2016-10-17 à 3:09 PM, Raymond O'Donnell a écrit :
On 17/10/16 16:40, said assemlal wrote:
Hello,

I am looking for a way to test generically input arguments to raise an
exception if one is either null or empty.

I was thinking to create a function who takes an array to check them but
not sure if it's really good.

It's not clear what you want to do here. Can you explain in more detail?

What do you mean by "input arguments"? Are you writing functions in the database? If so, which language? Some examples of what you've tried so far would help too.

Ray.