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

com.sap.cds.impl.JDBCDataStoreConnector Maven / Gradle / Ivy

The newest version!
/************************************************************************
 * © 2019-2023 SAP SE or an SAP affiliate company. All rights reserved. *
 ************************************************************************/
package com.sap.cds.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.function.Supplier;

import javax.sql.DataSource;

import com.sap.cds.CdsCommunicationException;
import com.sap.cds.DataStoreConfiguration;
import com.sap.cds.connector.CdsDataStoreConnectorBuilder;
import com.sap.cds.reflect.CdsModel;
import com.sap.cds.transaction.TransactionManager;

public class JDBCDataStoreConnector extends AbstractDataStoreConnector {

	private final TransactionManager transactionManager;
	private Supplier ds;

	private JDBCDataStoreConnector(CdsModel cdsModel, Supplier ds, TransactionManager transactionManager,
			DataStoreConfiguration dataStoreConfiguration) {
		super(cdsModel, ctx -> new JDBCClient(ctx, ds, transactionManager),
				DbContextFactoryRegister.context(dataStoreConfiguration, ds), dataStoreConfiguration);
		this.transactionManager = transactionManager;
		this.ds = ds;
	}

	public static Builder create(CdsModel model, TransactionManager transactionManager) {
		return new Builder(model, transactionManager);
	}

	public TransactionManager getTransactionManager() {
		return transactionManager;
	}

	public static class Builder implements CdsDataStoreConnectorBuilder {
		private final CdsModel model;
		private final TransactionManager transactionManager;
		private Supplier connectionSupplier;
		private DataStoreConfiguration dataStoreConfiguration = new SystemPropertyDataStoreConfiguration();

		private Builder(CdsModel model, TransactionManager transactionManager) {
			this.model = model;
			this.transactionManager = transactionManager;
		}

		@Override
		public CdsDataStoreConnectorBuilder datasource(DataSource datasource) {
			this.connectionSupplier = () -> {
				try {
					return datasource.getConnection();
				} catch (SQLException e) {
					throw new CdsCommunicationException("Cannot obtain connection to DB", e);
				}
			};
			return this;
		}

		@Override
		public CdsDataStoreConnectorBuilder connection(Supplier connectionSupplier) {
			this.connectionSupplier = connectionSupplier;
			return this;
		}

		@Override
		public CdsDataStoreConnectorBuilder config(DataStoreConfiguration dataStoreConfiguration) {
			this.dataStoreConfiguration = dataStoreConfiguration;
			return this;
		}

		@Override
		public JDBCDataStoreConnector build() {
			return new JDBCDataStoreConnector(model, connectionSupplier, transactionManager, dataStoreConfiguration);
		}
	}

	public Connection getConnection() {
		return ds.get();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy