package com.eactive.eai.rms.client.util;

/**
 * The Class QueryBuffer.
 * StringBuilder 이다. Query를 작성시 사용한다. StringBuffer와 같다. append 시 뒤에 \n 를 추가해준다.
 * <ul>
 * <li>1. Function :</li>
 * <li>2. Summary :</li>
 * <li>3. Notification :</li>
 * </ul>
 * 
 * @author  : $Author: 박기섭 $
 * @version : $Revision: 1.2 $
 */
public class QueryBuffer {

	/** The sb buffer. */
	private StringBuilder sbBuffer;

	/**
	 * Instantiates a new query buffer.
	 */
	public QueryBuffer() {
		sbBuffer = new StringBuilder(2048);
	}

	/**
	 * Instantiates a new query buffer.
	 * 
	 * @param sQuery the s query
	 */
	public QueryBuffer(String sQuery) {
		sbBuffer = new StringBuilder(2048);
		sbBuffer.append(sQuery);
	}

	/**
	 * Instantiates a new query buffer.
	 * 
	 * @param length the length
	 */
	public QueryBuffer(int length) {
		sbBuffer = new StringBuilder(length);
	}

	/**
	 * 해당 쿼리를 page 가능하게 바꿔준다.
	 * 
	 * @param currentPage :
	 * 현재 가져오려는 page
	 * @param pageSize :
	 * 한번에 보여지는 page size
	 * 
	 * @return paging 된 쿼리
	 * 
	 * @throws Exception 	 * 
	 */
	public String getPagingQuery(int currentPage, int pageSize) {

		QueryBuffer pagingQuery = new QueryBuffer();

		if (currentPage == 0) {
			currentPage = 1;
		}

		if (pageSize == 0) {
			pageSize = 30;
		}

		/*
		 * 첫번째 페이지[currentPage:1] firstRow : 1 lastRow : pageSize
		 * 
		 * 두번째 페이지[currentPage:2] firstRow : pageSize + 1 lastRow : 2 * pageSize
		 */
		int firstRow = pageSize * (currentPage - 1) + 1;
		int lastRow = firstRow + pageSize - 1;
		pagingQuery.append("SELECT *                                    ");
		pagingQuery.append("FROM  (                                     ");
		pagingQuery.append("       SELECT A.*, ROWNUM AS R              ");
		pagingQuery.append("       FROM  (                              ");
		pagingQuery.append(this);
		pagingQuery.append("              ) A                           ");
		pagingQuery.append("       WHERE ROWNUM <= ").append(
				String.valueOf(lastRow));
		pagingQuery.append("       ) B                                  ");
		pagingQuery.append("WHERE R >= ").append(String.valueOf(firstRow));

		return pagingQuery.toString();
	}

	/**
	 * Gets the paging query.
	 * 
	 * @param currentPage the current page
	 * 
	 * @return String - 페이징 적용된 쿼리
	 */
	public String getPagingQuery(int currentPage) {
		return getPagingQuery(currentPage, 30);
	}

	/**
	 * Delete.
	 * 
	 * @param start the start
	 * @param end the end
	 */
	public void delete(int start, int end) {
		this.sbBuffer.delete(start, end);
	}

	/**
	 * Length.
	 * 
	 * @return the int
	 */
	public int length() {
		return this.sbBuffer.length();
	}

	/**
	 * Append.
	 * 
	 * @param sQuery the s query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer append(String sQuery) {
		sbBuffer.append(" \n").append(sQuery);
		return this;
	}

	/**
	 * Append.
	 * 
	 * @param sQuery the s query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer append(int sQuery) {
		sbBuffer.append(sQuery);
		return this;
	}

	/**
	 * Append.
	 * 
	 * @param sQuery the s query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer append(long sQuery) {
		sbBuffer.append(sQuery);
		return this;
	}

	/**
	 * Append.
	 * 
	 * @param sQuery the s query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer append(char sQuery) {
		sbBuffer.append(sQuery);
		return this;
	}

	/**
	 * Append.
	 * 
	 * @param sQuery the s query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer append(char[] sQuery) {
		sbBuffer.append(sQuery);
		return this;
	}

	/**
	 * Append.
	 * 
	 * @param sQuery the s query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer append(double sQuery) {
		sbBuffer.append(sQuery);
		return this;
	}

	/**
	 * Append.
	 * 
	 * @param sQuery the s query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer append(boolean sQuery) {
		sbBuffer.append(sQuery);
		return this;
	}

	/**
	 * Append.
	 * 
	 * @param sQuery the s query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer append(QueryBuffer sQuery) {
		sbBuffer.append(sQuery);
		return this;
	}

	/**
	 * Append.
	 * 
	 * @param sQuery the s query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer append(float sQuery) {
		sbBuffer.append(sQuery);
		return this;
	}

	/**
	 * Insert.
	 * 
	 * @param offset the offset
	 * @param query the query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer insert(int offset, String query) {
		sbBuffer.insert(offset, query);
		return this;
	}

	/**
	 * Insert.
	 * 
	 * @param offset the offset
	 * @param query the query
	 * 
	 * @return the query buffer
	 */
	public QueryBuffer insert(int offset, QueryBuffer query) {
		sbBuffer.insert(offset, query);
		return this;
	}

	@Override
	public String toString() {
		return sbBuffer.toString();
	}

}
