Обсуждение: Best Practice when Encounter Invalid Stored Procedure Parameters
In c++, whenever we encounter an unexpected parameters, here is what we usually did : bool fun(int i) { if (i < 0) { return false; } } void fun(int i) { if (i < 0) { throw std::exception("Invalid parameter"); } } void fun(int i) { assert (i >= 0); } How about stored procedure? Now, I have the following stored procedure : CREATE OR REPLACE FUNCTION insert_unit(text[], text[]) RETURNS unit AS $BODY$DECLARE _measurement_types ALIAS FOR $1; _measurement_units ALIAS FOR $2; _unit unit; BEGIN IF array_upper(_measurement_values, 1) != array_upper(_measurement_units, 1) THEN RAISE NOTICE 'What I should do here to return early???'; END IF; May I know what is the good practice to handle invalid parameters? I am using libpq to interface with PostgreSQL. Thanks and Regards Yan Cheng CHEOK
hello 2010/1/12 Yan Cheng Cheok <yccheok@yahoo.com>: > In c++, whenever we encounter an unexpected parameters, here is what we usually did : > > bool fun(int i) { > if (i < 0) { > return false; > } > } > > void fun(int i) { > if (i < 0) { > throw std::exception("Invalid parameter"); > } > } > > void fun(int i) { > assert (i >= 0); > } > > How about stored procedure? Now, I have the following stored procedure : > > CREATE OR REPLACE FUNCTION insert_unit(text[], text[]) > RETURNS unit AS > $BODY$DECLARE > _measurement_types ALIAS FOR $1; > _measurement_units ALIAS FOR $2; > _unit unit; > BEGIN > IF array_upper(_measurement_values, 1) != array_upper(_measurement_units, 1) THEN > RAISE NOTICE 'What I should do here to return early???'; > END IF; > > May I know what is the good practice to handle invalid parameters? I am using libpq to interface with PostgreSQL. see RAISE EXCEPTION http://www.depesz.com/index.php/2008/05/14/waiting-for-84-plpgsql-raise/ Regards Pavel Stehule > > Thanks and Regards > Yan Cheng CHEOK > > > > > > -- > Sent via pgsql-general mailing list (pgsql-general@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-general >
Very nice. Thanks! Thanks and Regards Yan Cheng CHEOK --- On Tue, 1/12/10, Pavel Stehule <pavel.stehule@gmail.com> wrote: > From: Pavel Stehule <pavel.stehule@gmail.com> > Subject: Re: [GENERAL] Best Practice when Encounter Invalid Stored Procedure Parameters > To: "Yan Cheng Cheok" <yccheok@yahoo.com> > Cc: pgsql-general@postgresql.org > Date: Tuesday, January 12, 2010, 2:33 PM > hello > > 2010/1/12 Yan Cheng Cheok <yccheok@yahoo.com>: > > In c++, whenever we encounter an unexpected > parameters, here is what we usually did : > > > > bool fun(int i) { > > if (i < 0) { > > return false; > > } > > } > > > > void fun(int i) { > > if (i < 0) { > > throw std::exception("Invalid parameter"); > > } > > } > > > > void fun(int i) { > > assert (i >= 0); > > } > > > > How about stored procedure? Now, I have the following > stored procedure : > > > > CREATE OR REPLACE FUNCTION insert_unit(text[], > text[]) > > RETURNS unit AS > > $BODY$DECLARE > > _measurement_types ALIAS FOR $1; > > _measurement_units ALIAS FOR $2; > > _unit unit; > > BEGIN > > IF array_upper(_measurement_values, 1) != > array_upper(_measurement_units, 1) THEN > > RAISE NOTICE 'What I should do here to > return early???'; > > END IF; > > > > May I know what is the good practice to handle invalid > parameters? I am using libpq to interface with PostgreSQL. > > see RAISE EXCEPTION > > http://www.depesz.com/index.php/2008/05/14/waiting-for-84-plpgsql-raise/ > > Regards > Pavel Stehule > > > > > > Thanks and Regards > > Yan Cheng CHEOK > > > > > > > > > > > > -- > > Sent via pgsql-general mailing list (pgsql-general@postgresql.org) > > To make changes to your subscription: > > http://www.postgresql.org/mailpref/pgsql-general > > > > -- > Sent via pgsql-general mailing list (pgsql-general@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-general >