<commit_before>/**
 *  Copyright (c) 2012, 2015 Sme.UP and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *
 * Contributors:
 *   <NAME> - Initial API and implementation
 */
package org.smeup.sys.il.esam.jdbc;

import java.util.Arrays;
import java.util.List;

import org.eclipse.datatools.modelbase.sql.schema.helper.SQLObjectNameHelper;
import org.eclipse.datatools.modelbase.sql.tables.Column;
import org.eclipse.datatools.modelbase.sql.tables.Table;
import org.smeup.sys.il.data.QBufferedData;
import org.smeup.sys.il.data.QDecimal;
import org.smeup.sys.il.data.QRecord;
import org.smeup.sys.il.data.QString;
import org.smeup.sys.il.esam.OperationDirection;
import org.smeup.sys.il.esam.OperationRead;
import org.smeup.sys.il.esam.OperationSet;
import org.smeup.sys.il.esam.QIndex;
import org.smeup.sys.il.esam.QIndexColumn;

public class JDBCAccessHelper {

	private SQLObjectNameHelper sqlObjectNameHelper = new SQLObjectNameHelper();

	protected SQLObjectNameHelper getSQLObjectNameHelper() {
		return this.sqlObjectNameHelper;
	}

	public String buildDelete(Table table, QRecord record, int recordNumber) {

		StringBuffer sbUpdate = new StringBuffer();

		sbUpdate.append("DELETE");
		sbUpdate.append(" " + getSQLObjectNameHelper().getQualifiedNameInSQLFormat(table));
		sbUpdate.append(" WHERE QASRRN=" + recordNumber);

		return sbUpdate.toString();
	}
	
	public String buildSelect(Table table, QIndex index, OperationSet opSet, Object[] keySet, OperationRead opRead, Object[] keyRead) {

		StringBuffer querySelect = new StringBuffer();

		querySelect.append("SELECT " + getSQLObjectNameHelper().getQualifiedNameInSQLFormat(table) + ".*, digits(QASRRN)");

		querySelect.append(" FROM " + getSQLObjectNameHelper().getQualifiedNameInSQLFormat(table));

		if (keySet != null || keyRead != null)
			querySelect.append(" WHERE " + buildWhere(opSet, keySet, opRead, keyRead, index));

		OperationDirection opDir = (opRead == OperationRead.READ_PRIOR || opRead == OperationRead.READ_PRIOR_EQUAL) ? OperationDirection.BACKWARD : OperationDirection.FORWARD;
		String orderBy = buildOrderBy(opDir, index);
		if (orderBy != null && !orderBy.isEmpty())
			querySelect.append(" ORDER BY " + buildOrderBy(opDir, index));

		return querySelect.toString();
	}

	public String buildWhere(OperationSet opSet, Object[] keySet, OperationRead opRead, Object[] keyRead, QIndex index) {

		StringBuffer sbWhere = new StringBuffer();

		if (keySet == null || keySet.length <= 0)
			return sbWhere.toString();

		if (opSet.equals(OperationSet.SET_LOWER_LIMIT) || opSet.equals(OperationSet.SET_GREATER_THAN) || (opSet.equals(OperationSet.CHAIN) && !opRead.equals(OperationRead.CHAIN)))
			buildWhereSet(opSet, keySet, opRead, keyRead, sbWhere, index);

		if (opRead.equals(OperationRead.CHAIN))
			buildWhereChain(opSet, keySet, opRead, keyRead, sbWhere, index);

		if (opRead.equals(OperationRead.READ) || opRead.equals(OperationRead.READ_EQUAL) || opRead.equals(OperationRead.READ_PRIOR) || opRead.equals(OperationRead.READ_PRIOR_EQUAL))
			buildWhereRead(opSet, keySet, opRead, keyRead, sbWhere, index);

		return sbWhere.toString();
	}

	public String buildOrderBy(OperationDirection dir, QIndex index) {

		StringBuffer sbOrderBy = new StringBuffer();

		for (QIndexColumn indexColumn : index.getColumns()) {
			if (sbOrderBy.length() != 0)
				sbOrderBy.append(", ");

			String indexColumnName = indexColumn.getName().replaceAll("ç", "§").toUpperCase();

			sbOrderBy.append(getSQLObjectNameHelper().getIdentifierQuoteString() + indexColumnName + getSQLObjectNameHelper().getIdentifierQuoteString());
			if (dir == OperationDirection.BACKWARD) {
				if (indexColumn.getDirection() != OperationDirection.BACKWARD)
					sbOrderBy.append(" DESC");
			} else if (indexColumn.getDirection() == OperationDirection.BACKWARD)
				sbOrderBy.append(" DESC");
		}
		return sbOrderBy.toString();
	}

	@SuppressWarnings("unchecked")
	public String buildUpdate(Table table, QRecord record, int recordNumber) {

		StringBuffer sbUpdate = new StringBuffer();

		sbUpdate.append("UPDATE");
		sbUpdate.append(" " + getSQLObjectNameHelper().getQualifiedNameInSQLFormat(table));
		sbUpdate.append(" SET ");

		boolean first = true;
		for (Column column : (List<Column>) table.getColumns()) {

			String columnName = column.getName().replaceAll("ç", "§").toUpperCase();
			if (columnName.equalsIgnoreCase("QASRRN"))
				continue;

			if (!first)
				sbUpdate.append(", ");

			sbUpdate.append("\"" + columnName + "\"" + "=");

			// TODO remove me
			QBufferedData element = record.getElement(columnName.toLowerCase());
			if (element instanceof QString)
				sbUpdate.append("'" + element.toString() + "'");
			else if (element instanceof QDecimal) {
				QDecimal decimal = (QDecimal) element;
				if (decimal.getScale() > 0)
					sbUpdate.append(decimal.asDouble());
				else
					sbUpdate.append(decimal.asLong());
			} else
				System.err.println("Unexpected condition: dsjhflsdjhfsdfo9s98");

			first = false;
		}

		sbUpdate.append(" WHERE QASRRN=" + recordNumber);
<commit_msg>Patch JDBCAccessHelper<commit_after>/**
 *  Copyright (c) 2012, 2015 Sme.UP and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *
 * Contributors:
 *   <NAME> - Initial API and implementation
 */
package org.smeup.sys.il.esam.jdbc;

import java.util.Arrays;
import java.util.List;

import org.eclipse.datatools.modelbase.sql.schema.helper.SQLObjectNameHelper;
import org.eclipse.datatools.modelbase.sql.tables.Column;
import org.eclipse.datatools.modelbase.sql.tables.Table;
import org.smeup.sys.il.data.QBufferedData;
import org.smeup.sys.il.data.QDecimal;
import org.smeup.sys.il.data.QRecord;
import org.smeup.sys.il.data.QString;
import org.smeup.sys.il.esam.OperationDirection;
import org.smeup.sys.il.esam.OperationRead;
import org.smeup.sys.il.esam.OperationSet;
import org.smeup.sys.il.esam.QIndex;
import org.smeup.sys.il.esam.QIndexColumn;

public class JDBCAccessHelper {

	private SQLObjectNameHelper sqlObjectNameHelper = new SQLObjectNameHelper();

	protected SQLObjectNameHelper getSQLObjectNameHelper() {
		return this.sqlObjectNameHelper;
	}

	public String buildDelete(Table table, QRecord record, int recordNumber) {

		StringBuffer sbUpdate = new StringBuffer();

		sbUpdate.append("DELETE");
		sbUpdate.append(" " + getSQLObjectNameHelper().getQualifiedNameInSQLFormat(table));
		sbUpdate.append(" WHERE QASRRN=" + recordNumber);

		return sbUpdate.toString();
	}
	
	public String buildSelect(Table table, QIndex index, OperationSet opSet, Object[] keySet, OperationRead opRead, Object[] keyRead) {

		StringBuffer querySelect = new StringBuffer();

		querySelect.append("SELECT " + getSQLObjectNameHelper().getQualifiedNameInSQLFormat(table) + ".*, digits(QASRRN)");

		querySelect.append(" FROM " + getSQLObjectNameHelper().getQualifiedNameInSQLFormat(table));

		if (keySet != null || keyRead != null)
			querySelect.append(" WHERE " + buildWhere(opSet, keySet, opRead, keyRead, index));

		OperationDirection opDir = (opRead == OperationRead.READ_PRIOR || opRead == OperationRead.READ_PRIOR_EQUAL) ? OperationDirection.BACKWARD : OperationDirection.FORWARD;
		String orderBy = buildOrderBy(opDir, index);
		if (orderBy != null && !orderBy.isEmpty())
			querySelect.append(" ORDER BY " + buildOrderBy(opDir, index));

		return querySelect.toString();
	}

	public String buildWhere(OperationSet opSet, Object[] keySet, OperationRead opRead, Object[] keyRead, QIndex index) {

		StringBuffer sbWhere = new StringBuffer();

		if (keySet == null || keySet.length <= 0)
			return sbWhere.toString();

		if (opSet.equals(OperationSet.SET_LOWER_LIMIT) || opSet.equals(OperationSet.SET_GREATER_THAN) || (opSet.equals(OperationSet.CHAIN) && !opRead.equals(OperationRead.CHAIN)))
			buildWhereSet(opSet, keySet, opRead, keyRead, sbWhere, index);

		if (opRead.equals(OperationRead.CHAIN))
			buildWhereChain(opSet, keySet, opRead, keyRead, sbWhere, index);

		if (opRead.equals(OperationRead.READ) || opRead.equals(OperationRead.READ_EQUAL) || opRead.equals(OperationRead.READ_PRIOR) || opRead.equals(OperationRead.READ_PRIOR_EQUAL))
			buildWhereRead(opSet, keySet, opRead, keyRead, sbWhere, index);

		return sbWhere.toString();
	}

	public String buildOrderBy(OperationDirection dir, QIndex index) {

		StringBuffer sbOrderBy = new StringBuffer();

		for (QIndexColumn indexColumn : index.getColumns()) {
			if (sbOrderBy.length() != 0)
				sbOrderBy.append(", ");

			String indexColumnName = indexColumn.getName().replaceAll("ç", "§").toUpperCase();

			sbOrderBy.append(getSQLObjectNameHelper().getIdentifierQuoteString() + indexColumnName + getSQLObjectNameHelper().getIdentifierQuoteString());
			if (dir == OperationDirection.BACKWARD) {
				if (indexColumn.getDirection() != OperationDirection.BACKWARD)
					sbOrderBy.append(" DESC");
			} else if (indexColumn.getDirection() == OperationDirection.BACKWARD)
				sbOrderBy.append(" DESC");
		}
		return sbOrderBy.toString();
	}

	@SuppressWarnings("unchecked")
	public String buildUpdate(Table table, QRecord record, int recordNumber) {

		StringBuffer sbUpdate = new StringBuffer();

		sbUpdate.append("UPDATE");
		sbUpdate.append(" " + getSQLObjectNameHelper().getQualifiedNameInSQLFormat(table));
		sbUpdate.append(" SET ");

		boolean first = true;
		for (Column column : (List<Column>) table.getColumns()) {

			String columnName = column.getName().replaceAll("ç", "§").toUpperCase();
			if (columnName.equalsIgnoreCase("QASRRN"))
				continue;

			if (!first)
				sbUpdate.append(", ");

			sbUpdate.append("\"" + columnName + "\"" + "=");

			// TODO remove me
			QBufferedData element = record.getElement(columnName.replaceAll("ç", "§").toLowerCase());
			if (element instanceof QString)
				sbUpdate.append("'" + element.toString() + "'");
			else if (element instanceof QDecimal) {
				QDecimal decimal = (QDecimal) element;
				if (decimal.getScale() > 0)
					sbUpdate.append(decimal.asDouble());
				else
					sbUpdate.append(decimal.asLong());
			} else
				System.err.println("Unexpected condition: dsjhflsdjhfsdfo9s98");

			first = false;
		}

		sbUpdate.append(" WHERE QASRRN=" + recordNumber);

{'fraction_non_alphanumeric': 0.1308172531214529, 'fraction_numerical': 0.00340522133938706, 'mean_word_length': 5.1186342592592595, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4247930', 'n_tokens_mistral': 3598, 'n_tokens_neox': 3250, 'n_words': 678}