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

com.jk.data.datasource.JKDataSource 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.datasource;

import java.sql.Connection;
import java.util.Properties;

import com.jk.core.config.JKConstants;
import com.jk.core.exceptions.JKDataAccessException;
import com.jk.core.synchronization.Synchronizable;
import com.jk.core.util.JK;

import jakarta.persistence.EntityManager;

// TODO: Auto-generated Javadoc
/**
 * The Interface JKDataSource.
 */
public interface JKDataSource extends Synchronizable {

	/**
	 * Creates the connection.
	 *
	 * @return the connection
	 */
	public Connection createConnection();
	
	/**
	 * Gets the connection.
	 *
	 * @return the connection
	 * @throws JKDataAccessException the JK data access exception
	 */
	public Connection getConnection() ;	
	
	/**
	 * Gets the query connection.
	 *
	 * @return the query connection
	 * @throws JKDataAccessException the JK data access exception
	 */
	public Connection getQueryConnection();	
	
	/**
	 * Close.
	 *
	 * @param con the con
	 */
	public void close(Connection con);

	/**
	 * Close.
	 *
	 * @param connection the connection
	 * @param commit     the commit
	 */
	public void close(Connection connection, boolean commit);

	/**
	 * Creates the entity manager.
	 *
	 * @return the entity manager
	 */
	public EntityManager createEntityManager();
	
	/**
	 * Checks if is entity available.
	 *
	 * @param clas the clas
	 * @return true, if is entity available
	 */
	public boolean isEntityAvailable(Class clas);

	/**
	 * Close.
	 *
	 * @param manager the manager
	 * @param commit  the commit
	 */
	public void close(EntityManager manager, boolean commit);
	
	/**
	 * Gets the properties.
	 *
	 * @return the properties
	 */
	public Properties getProperties();

	/**
	 * Sets the property.
	 *
	 * @param property the property
	 * @param value the value
	 */
	public void setProperty(String property, String value);

	/**
	 * Gets the database type.
	 *
	 * @return the database type
	 */
	public JKDatabase getDatabaseType();

	/**
	 * Gets the database name.
	 *
	 * @return the database name
	 */
	public default String getDatabaseName() {
		try (Connection connnectino = getConnection()) {
			return getConnection().getCatalog();
		} catch (Exception e) {
			JK.throww(e);
			return null;
		}
	}
	
	/**
	 * Gets the base script path.
	 *
	 * @param databaseType the database type
	 * @return the base script path
	 */
	public default String getBaseScriptPath(JKDatabase databaseType) {
		return "/scripts/" + databaseType.toString().toLowerCase() + "/base.sql";
	}

	/**
	 * Gets the database url.
	 *
	 * @return the database url
	 */
	public default String getDatabaseUrl() {
		return getProperties().getProperty(JKConstants.Hibernate.JDBC_URL);
	}

	/**
	 * Gets the rows limit.
	 *
	 * @return the rows limit
	 */
	public default int getRowsLimit() {
		return JK.toInt(getProperties().getProperty(JKConstants.Database.QUERY_ROWS_COUNT));
	}
	
	/**
	 * Gets the database username.
	 *
	 * @return the database username
	 */
	public default String getDatabaseUsername() {
		return getProperties().getProperty(JKConstants.Hibernate.JDBC_USERNAME);
	}

	/**
	 * Gets the databasepassword.
	 *
	 * @return the databasepassword
	 */
	public default String getDatabasepassword() {
		JK.fixMe("check this for security");
		return getProperties().getProperty(JKConstants.Hibernate.JDBC_PASSWORD);
	}

	/**
	 * Gets the max allowed connections.
	 *
	 * @return the max allowed connections
	 */
	public int getMaxAllowedConnections();
	
	/**
	 * Close.
	 */
	public void close();

	/**
	 * Inits the.
	 */
	public void init();



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy