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

org.openstreetmap.osmosis.pgsimple.common.BaseDao Maven / Gradle / Ivy

There is a newer version: 0.49.2
Show newest version
// This software is released into the Public Domain.  See copying.txt for details.
package org.openstreetmap.osmosis.pgsimple.common;

import java.sql.PreparedStatement;

import org.openstreetmap.osmosis.core.database.ReleasableStatementContainer;
import org.openstreetmap.osmosis.core.lifecycle.Closeable;


/**
 * Provides functionality common to all dao implementations.
 * 
 * @author Brett Henderson
 */
public class BaseDao implements Closeable {
	
	private DatabaseContext dbCtx;
	private ReleasableStatementContainer statementContainer;
	
	
	/**
	 * Creates a new instance.
	 * 
	 * @param dbCtx
	 *            The database context to use for accessing the database.
	 */
	protected BaseDao(DatabaseContext dbCtx) {
		this.dbCtx = dbCtx;
		
		statementContainer = new ReleasableStatementContainer();
	}
	
	
	/**
	 * Provides access to the database context. In most cases alternative
	 * methods such as prepareStatement should be used.
	 * 
	 * @return The database context.
	 */
	protected DatabaseContext getDatabaseContext() {
		return dbCtx;
	}
	
	
	/**
	 * Creates a new database prepared statement. This statement will be
	 * automatically released when the dao is released.
	 * 
	 * @param sql
	 *            The statement to be created.
	 * @return The newly created statement.
	 */
	protected PreparedStatement prepareStatement(String sql) {
		return statementContainer.add(dbCtx.prepareStatement(sql));
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void close() {
		statementContainer.close();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy