From 40a80a8d6d0569c8154f216eb572c537b905d62a Mon Sep 17 00:00:00 2001 From: kontro Date: Tue, 9 Jan 2007 17:17:57 +0000 Subject: [PATCH] Feature request: [ 1614574 ] Port sqlj functions to pl/pgsql Gavin Dunse's functions: charAt and firstOf Thank you Gavin! Keep up good work. --- db/ddlutils/postgresql/functions/charAt.sql | 34 ++++++++++ db/ddlutils/postgresql/functions/firstOf.sql | 70 ++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 db/ddlutils/postgresql/functions/charAt.sql create mode 100644 db/ddlutils/postgresql/functions/firstOf.sql diff --git a/db/ddlutils/postgresql/functions/charAt.sql b/db/ddlutils/postgresql/functions/charAt.sql new file mode 100644 index 0000000000..f7e87f111b --- /dev/null +++ b/db/ddlutils/postgresql/functions/charAt.sql @@ -0,0 +1,34 @@ +/* + *This file is part of Adempiere ERP Bazaar + *http://www.adempiere.org + * + *Copyright (C) 2006 Gavin Dunse + *Copyright (C) 1999-2006 ComPiere, inc + * + *This program is free software; you can redistribute it and/or + *modify it under the terms of the GNU General Public License + *as published by the Free Software Foundation; either version 2 + *of the License, or (at your option) any later version. + * + *This program is distributed in the hope that it will be useful, + *but WITHOUT ANY WARRANTY; without even the implied warranty of + *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + *GNU General Public License for more details. + * + *You should have received a copy of the GNU General Public License + *along with this program; if not, write to the Free Software + *Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.of + */ + +/** Get Character at Position */ +SET search_path = adempiere, pg_catalog; + +CREATE OR REPLACE FUNCTION charAt ( + IN VARCHAR, -- $1 the string + IN INTEGER -- $2 the position +) RETURNS VARCHAR AS +$$ + BEGIN + RETURN SUBSTR($1, $2, 1); + END; +$$ LANGUAGE plpgsql; diff --git a/db/ddlutils/postgresql/functions/firstOf.sql b/db/ddlutils/postgresql/functions/firstOf.sql new file mode 100644 index 0000000000..c3ee8af88e --- /dev/null +++ b/db/ddlutils/postgresql/functions/firstOf.sql @@ -0,0 +1,70 @@ +/* +*This file is part of Adempiere ERP Bazaar +*http://www.adempiere.org +* +*Copyright (C) 2006 Gavin Dunse +*Copyright (C) 1999-2006 ComPiere, inc +* +*This program is free software; you can redistribute it and/or +*modify it under the terms of the GNU General Public License +*as published by the Free Software Foundation; either version 2 +*of the License, or (at your option) any later version. +* +*This program is distributed in the hope that it will be useful, +*but WITHOUT ANY WARRANTY; without even the implied warranty of +*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +*GNU General Public License for more details. +* +*You should have received a copy of the GNU General Public License +*along with this program; if not, write to the Free Software +*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.of +*/ + +/** Get Character at Position */ +SET search_path = adempiere, pg_catalog; + +CREATE OR REPLACE FUNCTION firstOf ( +IN TIMESTAMP WITH TIME ZONE, -- $1 date +IN VARCHAR -- $2 part of date +) RETURNS TIMESTAMP WITH TIME ZONE AS +$$ +DECLARE +datepart VARCHAR; +BEGIN +datepart = $2; +IF $2 IN ('') THEN +datepart = 'millennium'; +ELSEIF $2 IN ('') THEN +datepart = 'century'; +ELSEIF $2 IN ('') THEN +datepart = 'decade'; +ELSEIF $2 IN ('IYYY','IY','I') THEN +datepart = 'year'; +ELSEIF $2 IN ('SYYYY','YYYY','YEAR','SYEAR','YYY','YY','Y') THEN +datepart = 'year'; +ELSEIF $2 IN ('Q') THEN +datepart = 'quarter'; +ELSEIF $2 IN ('MONTH','MON','MM','RM') THEN +datepart = 'month'; +ELSEIF $2 IN ('IW') THEN +datepart = 'week'; +ELSEIF $2 IN ('W') THEN +datepart = 'week'; +ELSEIF $2 IN ('DDD','DD','J') THEN +datepart = 'day'; +ELSEIF $2 IN ('DAY','DY','D') THEN +datepart = 'day'; +ELSEIF $2 IN ('HH','HH12','HH24') THEN +datepart = 'hour'; +ELSEIF $2 IN ('MI') THEN +datepart = 'minute'; +ELSEIF $2 IN ('') THEN +datepart = 'second'; +ELSEIF $2 IN ('') THEN +datepart = 'milliseconds'; +ELSEIF $2 IN ('') THEN +datepart = 'microseconds'; +END IF; +RETURN date_trunc(datepart, CAST($1 AS DATE)); +END; +$$ LANGUAGE plpgsql;