-- Fields

	-- get all fields

	PROCEDURE getAll(
		IN language_id INT,
		IN post_id INT,
		IN start INT,
		IN limit INT,
		OUT fetch_all, 
		OUT fetch_one,
	)
	BEGIN
		-- field
		SELECT taxonomy_item_field_value.*, field.settings, field.field_id as array_key
			FROM taxonomy_item_field_value
			LEFT JOIN field ON (field.field_id = taxonomy_item_field_value.field_id)

		WHERE 1 = 1
			
		@IF !empty(:field_group_id) 
		THEN			
			AND field.field_group_id = :field_group_id
		END @IF
		
		@IF !empty(:language_id) 
		THEN			
			AND taxonomy_item_field_value.language_id = :language_id
		END @IF
		
		@IF !empty(:post_id) 
		THEN			
			AND taxonomy_item_field_value.post_id = :post_id
		END @IF
		
		ORDER BY field.sort_order
		
		@SQL_LIMIT(:start, :limit);
		
		SELECT count(*) FROM (
			
			@SQL_COUNT(field.field_id, field) -- this takes previous query removes limit and replaces select columns with parameter product_id
			
		) as count;		
			
	END	
	
	-- get field

	PROCEDURE get(
		IN field_id INT,
		IN post_id INT,
		IN language_id INT,
		OUT fetch_row, 
	)
	BEGIN
		-- field
		SELECT *
			FROM taxonomy_item_field_value as _ 
			INNER JOIN field ON field_content.field_id = _.field_id
		WHERE _.field_id = :field_id

		@IF !empty(:language_id) 
		THEN			
			AND taxonomy_item_field_value.language_id = :language_id
		END @IF
		
		@IF !empty(:field_id) 
		THEN			
			AND taxonomy_item_field_value.field_id = :field_id
		END @IF
		
		@IF !empty(:post_id) 
		THEN			
			AND taxonomy_item_field_value.post_id = :post_id
		END @IF
		
		;
	END
	
	-- add field

	PROCEDURE add(
		IN taxonomy_item_field_value ARRAY,
		IN language_id INT,
		OUT insert_id,
		OUT insert_id
	)
	BEGIN
		
		-- allow only table fields and set defaults for missing values
		:taxonomy_item_field_value  = @FILTER(:taxonomy_item_field_value, taxonomy_item_field_value)
		
		INSERT INTO taxonomy_item_field_value 
			
			( @KEYS(:field_data) )
			
	  	VALUES ( :field_data);

	END
	
	-- edit field
	CREATE PROCEDURE edit(
		IN taxonomy_item_field_value ARRAY,
		IN taxonomy_item_field_value_id INT,
		IN post_id INT,
		IN field_id INT,
		IN language_id INT,
		OUT affected_rows,
		OUT affected_rows
	)
	BEGIN

		-- allow only table fields and set defaults for missing values
		:taxonomy_item_field_value  = @FILTER(:taxonomy_item_field_value, taxonomy_item_field_value)

		UPDATE taxonomy_item_field_value 
			
			SET @LIST(:taxonomy_item_field_value) 
			
		WHERE field_id = :field_id AND post_id = :post_id AND language_id = :language_id;


	END
	
	-- delete field

	PROCEDURE delete(
		IN post_id INT,
		IN field_id INT,
		IN language_id INT,
		OUT affected_rows, 
	)
	BEGIN
		-- taxonomy_item_field_value_id
		DELETE FROM taxonomy_item_field_value WHERE 
		
		@IF !empty(:language_id) 
		THEN			
			AND taxonomy_item_field_value.language_id IN (:language_id)
		END @IF
		
		@IF !empty(:field_id) 
		THEN			
			AND taxonomy_item_field_value.field_id IN (:field_id)
		END @IF
		
		@IF !empty(:post_id) 
		THEN			
			AND taxonomy_item_field_value.post_id IN (:post_id)
		END @IF
	END
