-- newsletter_posts

	-- Get one newsletter_post

	CREATE PROCEDURE get(
		IN newsletter_post_id INT,
		IN name CHAR,
		OUT fetch_row, 
	)
	BEGIN
		-- newsletter_post
		SELECT *
			FROM newsletter_post as _ -- (underscore) _ means that data will be kept in main array
			
			WHERE 1 = 1 
			
			@IF isset(:newsletter_post_id) && !empty(:newsletter_post_id)
			THEN
				AND _.newsletter_post_id = :newsletter_post_id
			END @IF	
		
			
		LIMIT 1;
	END

	-- Edit newsletter_post

	CREATE PROCEDURE edit(
		IN newsletter_post ARRAY,
		IN newsletter_post_id INT,
		OUT affected_rows
	)
	BEGIN
		-- allow only table fields and set defaults for missing values
		@FILTER(:newsletter_post, newsletter_post)
		
		UPDATE newsletter_post 
			
			SET @LIST(:newsletter_post) 
			
		WHERE newsletter_post_id = :newsletter_post_id
	END	

	-- Add new newsletter_post

	CREATE PROCEDURE add(
		IN newsletter_post ARRAY,
		OUT insert_id
	)
	BEGIN
		
		-- allow only table fields and set defaults for missing values
		:newsletter_post  = @FILTER(:newsletter_post, newsletter_post)

		INSERT INTO newsletter_post 
		
			( @KEYS(:newsletter_post) )
			
		VALUES ( :newsletter_post );			

	END


	-- Get all newsletter_posts 

	CREATE PROCEDURE getAll(

		-- variables
		IN  language_id INT,
		IN  site_id INT,
		IN  search CHAR,
		
		-- pagination
		IN  start INT,
		IN  limit INT,
			
		-- return array of newsletter_posts for newsletter_posts query
		OUT fetch_all,
		-- return newsletter_posts count for count query
		OUT fetch_one,
	)
	BEGIN

		SELECT * 
			FROM newsletter_post 
		ORDER BY newsletter_post_id DESC
			
		LIMIT :limit OFFSET :start;
		
		SELECT count(*) FROM (
			
			@SQL_COUNT(newsletter_post.newsletter_post_id, newsletter_post) -- this takes previous query removes limit and replaces select columns with parameter product_id
			
		) as count;


	END


	-- Delete newsletter_post

	CREATE PROCEDURE delete(
		IN  newsletter_post_id ARRAY,
		OUT affected_rows
	)
	BEGIN

		DELETE FROM newsletter_post WHERE newsletter_post_id IN (:newsletter_post_id)
	 
	END