All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jk.data.dynamic.query.Query Maven / Gradle / Ivy

Go to download

This contains a set of API's that ease the database programming with Java, in both: JDBC and JPA Persisitnce).

There is a newer version: 7.0.0-M7
Show newest version
/*
 * Copyright 2002-2022 Dr. Jalal Kiswani. 
 * Email: [email protected]
 * Check out https://smart-api.com for more details
 * 
 * All the opensource projects of Dr. Jalal Kiswani are free for personal and academic use only, 
 * for commercial usage and support, please contact the author.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jk.data.dynamic.query;

import java.util.ArrayList;
import java.util.List;

// TODO: Auto-generated Javadoc
/**
 * The Class Query.
 */
public class Query {

	/** The structor. */
	ArrayList structor = new ArrayList();

	/**
	 * Adds the component.
	 *
	 * @param component the component
	 * @return the query
	 */
	// ///////////////////////////////////////////////////////////////////
	public Query addComponent(final QueryComponent component) {
		this.structor.add(component);
		return this;
	}

	/**
	 * Adds the component.
	 *
	 * @param component                 the component
	 * @param count                     the count
	 * @param separator                 the separator
	 * @param surroundWithParenchthisis the surround with parenchthisis
	 * @return the query
	 */
	// ///////////////////////////////////////////////////////////////////
	public Query addComponent(final QueryComponent component, final int count, final QueryComponent separator,
			final boolean surroundWithParenchthisis) {
		final ArrayList components = new ArrayList();
		for (int i = 0; i < count; i++) {
			components.add(component);
		}
		addComponents(components, separator, surroundWithParenchthisis);
		return this;
	}

	/**
	 * Adds the components.
	 *
	 * @param components                the components
	 * @param separator                 the separator
	 * @param surroundWithParenchthisis the surround with parenchthisis
	 * @return the query
	 */
	// ///////////////////////////////////////////////////////////////////
	public Query addComponents(final List components, final QueryComponent separator, final boolean surroundWithParenchthisis) {
		if (surroundWithParenchthisis) {
			addComponent(Keyword.LEFT_PARENTHESES);
		}
		for (int i = 0; i < components.size(); i++) {
			if (i != 0) {
				addComponent(separator);
			}
			addComponent(components.get(i));
		}
		if (surroundWithParenchthisis) {
			addComponent(Keyword.RIGHT_PARENTHESES);
		}
		return this;
	}

	/**
	 * Adds the value.
	 *
	 * @param value the value
	 * @return the query
	 */
	// ///////////////////////////////////////////////////////////////////
	public Query addValue(final Object value) {
		addComponent(new ObjectComponent(value));
		return this;
	}

	/**
	 * Adds the values.
	 *
	 * @param values                    the values
	 * @param separator                 the separator
	 * @param surroundWithParenchthisis the surround with parenchthisis
	 * @return the query
	 */
	// ///////////////////////////////////////////////////////////////////
	public Query addValues(final List values, final QueryComponent separator, final boolean surroundWithParenchthisis) {
		final List components = new ArrayList();
		for (final Object value : values) {
			components.add(new ObjectComponent(value));
		}
		addComponents(components, separator, surroundWithParenchthisis);
		return this;
	}
	// ///////////////////////////////////////////////////////////////////

	/**
	 * Compile.
	 *
	 * @return the string
	 */
	// ///////////////////////////////////////////////////////////////////
	public String compile() {
		final StringBuffer buf = new StringBuffer();
		for (final QueryComponent comp : this.structor) {
			buf.append(comp.toQueryElement());
			buf.append(" ");
//			buf.append(comp.isInline() ? " " : JK.NEW_LINE);
		}
		buf.append(";");
		return buf.toString();
	}
}