CREATE OR REPLACE FUNCTION addupdateitems(items varchar[][],
											feed_resource_id integer,
											feed1_title varchar,
											feed1_url varchar,
											feed_description varchar,
											feed_published_date timestamp,
											feed_encoding varchar,
											feed_version varchar) RETURNS INTEGER[] AS $$
declare
	i integer=1;
	-- CAUTION: itemid might overflow (after an insane amount of time.. )
	action integer[];
	resid integer;
	itemid integer;
	--action integer;
	--hash_code integer;
	--new_hash_code integer;
	publishdate timestamp;
	lastupdatedate timestamp;
	lastupdated timestamp;
	new_title varchar;
	new_description varchar;
	feedurl varchar;
	feedtitle varchar;
	feeddescription varchar;
	new_author varchar;
	result varchar[];
begin
	--feedurl = decode(items[i][8],'base64');
	feedurl = replace(feed1_url,'&quot;','"');
	--feedtitle = decode(items[i][9],'base64');
	feedtitle = replace(feed1_title,'&quot;','"');
	feeddescription = replace(feed_description,'&quot;','"');

	IF NOT isUrlExist(feedurl) THEN
		BEGIN
			--raise notice '%',feed_resource_id;
			insert into feed (resource_id,title,url,description,publish_date,encoding,rss_version) 
			values (feed_resource_id,feedtitle,feedurl,feed_description,feed_published_date,feed_encoding,feed_version);
			--addfeed(resource_id,feed_title,feed_url,feed_description,feed_publish_date,feed_last_update_date);
        EXCEPTION
            WHEN unique_violation THEN
				RAISE EXCEPTION 'Unique key violation for resid % and url %' , feed_resource_id, feedurl;
                --result[i] = 'unique_violation';
            WHEN not_null_violation THEN
                --result[i] = 'not_null_violation';
            WHEN restrict_violation THEN
                --result[i] = 'restrict_violation';
            WHEN check_violation THEN
                --result[i] = 'check_violation';
        END;
	END IF;

	--raise notice '%',items[i][1];
	WHILE NOT items[i][1] IS NULL LOOP
		BEGIN	
			resid = feed_resource_id;--items[i][1];
			--new_hash_code = items[i][10];
			publishdate = items[i][4];
			lastupdatedate = items[i][6];
			--new_title = decode(items[i][2],'base64');
			new_title = replace(items[i][1],'&quot;','"');
			--new_description = decode(items[i][4],'base64');
			new_description = replace(items[i][3],'&quot;','"');
			new_author = replace(items[i][5],'&quot;','"');
		EXCEPTION
			WHEN syntax_error THEN
				result[i] = 'Error In data Assignment';
		END;


		BEGIN
			SELECT INTO itemid,lastupdated
						item_id,last_update_date
			FROM feeditem
			WHERE url = items[i][2] AND title = new_title;

			IF NOT FOUND THEN
				action[i] := -1;
				raise notice 'inserting item for url %',items[i][2];
				insert into feeditem values (nextval('feeditem_item_id_seq'),
														resid,
														new_title,
														items[i][2],
														new_description,
														publishdate,
														new_author,
														lastupdatedate);
														--feedurl,
														--feedtitle);
				i=i+1;
				CONTINUE;
			END IF;

			IF NOT lastupdatedate IS NULL AND lastupdatedate  > lastupdated THEN
				raise notice 'updating item for item id  %',itemid;
				action[i] := itemid;
				update feeditem set resource_id = resid , 
													title = new_title , 
													url = items[i][2] , 
													description = new_description , 
													publish_date = publishdate , 
													author = new_author, 
													last_update_date = lastupdatedate  
													--feed_url = feedurl , 
													--feed_title = feedtitle
													where item_id = itemid ;
				i=i+1;
				CONTINUE;
			END IF;

			action[i] := 0;
			i=i+1;
			raise notice 'nothing';
		EXCEPTION
			WHEN unique_violation THEN
				result[i] = 'unique_violation';
			WHEN not_null_violation THEN
				result[i] = 'not_null_violation';
			WHEN restrict_violation THEN
				result[i] = 'restrict_violation';
			WHEN check_violation THEN
				result[i] = 'check_violation';
		END;
END LOOP;
return action;
END;
$$
LANGUAGE 'plpgsql' VOLATILE;
