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

com.sap.cds.jdbc.sqlite.SqliteStatementResolver Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
/************************************************************************
 * © 2020-2023 SAP SE or an SAP affiliate company. All rights reserved. *
 ************************************************************************/
package com.sap.cds.jdbc.sqlite;

import static com.sap.cds.impl.sql.SQLHelper.commaSeparated;
import static java.util.stream.Collectors.joining;

import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.sap.cds.jdbc.generic.GenericStatementResolver;
import com.sap.cds.ql.cqn.CqnLock;

public class SqliteStatementResolver extends GenericStatementResolver {

	private static final Optional EMPTY = Optional.empty();

	@Override
	public Optional collate(Locale locale) {
		if (locale != null) {
			return Optional.of("COLLATE NOCASE");
		}
		return EMPTY;
	}

	/**
	 * UPSERT is a clause added to INSERT that causes the INSERT to behave as an
	 * UPDATE or a no-op if the INSERT would violate a uniqueness constraint. UPSERT
	 * is not standard SQL. UPSERT in SQLite follows the syntax established by
	 * PostgreSQL, with generalizations.
	 * 
	 * https://www.sqlite.org/lang_upsert.html
	 * 
	 * The REPLACE command is not used, since it deletes and inserts:
	 * https://www.sqlite.org/lang_replace.html
	 * https://www.sqlite.org/lang_conflict.html
	 */
	@Override
	public String upsert(String table, Stream keyColumns, Stream upsertColumns,
			Stream upsertValues) {
		List col = upsertColumns.collect(Collectors.toList());
		String columns = commaSeparated(col.stream());
		String insertValues = commaSeparated(upsertValues);
		String conflictTarget = commaSeparated(keyColumns);
		String updateValues = commaSeparated(col.stream().map(c -> "EXCLUDED." + c));

		return Stream.of("INSERT INTO", table, columns, "VALUES", insertValues, "ON CONFLICT", conflictTarget,
				"DO UPDATE SET", columns, "=", updateValues).collect(joining(" "));
	}

	@Override
	public Stream lockClause(CqnLock lock) {
		return Stream.empty();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy