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

org.hibernate.hql.spi.id.cte.CteValuesListBuilder Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha1
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.hql.spi.id.cte;

import java.util.Collections;
import java.util.List;

/**
 * Builds the CTE with VALUES list clause that wraps the identifiers to be updated/deleted.
 *
 * @author Evandro Pires da Silva
 * @author Vlad Mihalcea
 */
public class CteValuesListBuilder {

	private final String tableName;

	private final String[] columns;

	private final List ids;

	private String cteStatement;

	public CteValuesListBuilder(
			String tableName,
			String[] columns,
			List ids) {
		this.tableName = tableName;
		this.columns = columns;
		this.ids = ids;

		this.cteStatement = buildStatement();
	}

	public List getIds() {
		return ids;
	}

	public String toStatement(String statement) {
		return cteStatement + statement;
	}

	private String buildStatement() {
		String columnNames = String.join(",", columns);

		String singleIdValuesParam = '(' + String.join( ",", Collections.nCopies( columns.length, "?")) + ')';
		String parameters = String.join(",", Collections.nCopies(ids.size(), singleIdValuesParam));

		return new StringBuilder()
				.append( "with " )
				.append( tableName )
				.append( " (" )
				.append( columnNames )
				.append( " ) as ( select " )
				.append( columnNames )
				.append( " from ( values  " )
				.append( parameters )
				.append( ") as HT " )
				.append( "(" )
				.append( columnNames )
				.append( ") ) " )
				.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy