Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.empire.spring;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.sql.DataSource;
import org.apache.empire.db.DBColumnExpr;
import org.apache.empire.db.DBCommand;
import org.apache.empire.db.DBReader;
import org.apache.empire.db.DBRecord;
import org.apache.empire.db.DBRecordData;
import org.apache.empire.db.DBRowSet;
import org.apache.empire.db.DBTable;
import org.apache.empire.db.exceptions.RecordNotFoundException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
/**
* This class simplifies database access methods with Empire. Database queries
* are executed with the help of callback interfaces, while hides connection
* handling from the developer. Only the query logic has to be implemented and
* leave getting and releasing the connection to the template.
*
* Some methods (delete, update) delegates to DBRecord, DBDatabase and DBCommand
* methods while hiding connection handling from developers.
*
* This class is working with a wrapped JdbcTemplate, so either jdbcTemplate or
* dataSource must be set.
*
* Allows customization of creating DBReader and DBRecord instances through
* ObjectFactories.
*
* This class is based on {@link org.springframework.jdbc.core.JdbcTemplate}.
*
*
*/
public class EmpireTemplate implements InitializingBean {
/** Wrapped JdbcTemplate instance */
private JdbcTemplate jdbcTemplate;
/** Factory to create a new DBReader instance, by default returns DBReader */
private ObjectFactory readerFactory = new ObjectFactory() {
@Override
public DBReader getObject() throws BeansException {
return new DBReader();
}
};
/** Factory to create a new DBRecord instance, by default returns DBRecord */
private ObjectFactory recordFactory = new ObjectFactory() {
@Override
public DBRecord getObject() throws BeansException {
return new DBRecord();
}
};
/** Default constructor */
public EmpireTemplate() {
super();
}
/**
* Setting a custom ObjectFactory to allow custom DBRecord create with
* newRecord().
*
* @param recordFactory
*/
public void setDBRecordFactory(ObjectFactory recordFactory) {
this.recordFactory = recordFactory;
}
/**
* Setting a custom DBRecord class to use in newRecord().
*
* @param recordClass
* the class which extends DBRecord.class
*/
public void setDBRecordClass(final Class extends DBRecord> recordClass) {
this.recordFactory = new ObjectFactory() {
@Override
public DBRecord getObject() throws BeansException {
try {
return recordClass.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
}
/**
* Setting a custom ObjectFactory to allow custom DBReaders to use in
* queries.
*
* @param readerFactory
*/
public void setDBReaderFactory(ObjectFactory readerFactory) {
this.readerFactory = readerFactory;
}
/**
* Setting a custom DBReader class to use in queries.
*
* @param readerClass
* the class which extends DBReader.class
*/
public void setDBReaderClass(final Class extends DBReader> readerClass) {
this.readerFactory = new ObjectFactory() {
@Override
public DBReader getObject() throws BeansException {
try {
return readerClass.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
}
/** Setting the wrapped JdbcTemplate */
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public JdbcTemplate getJdbcTemplate() {
return this.jdbcTemplate;
}
/** Setting the datasource */
public final void setDataSource(DataSource dataSource) {
if (this.jdbcTemplate == null
|| dataSource != this.jdbcTemplate.getDataSource()) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.jdbcTemplate.afterPropertiesSet();
}
}
public void afterPropertiesSet() {
if (getJdbcTemplate() == null) {
throw new IllegalArgumentException(
"Property 'jdbcTemplate' is required, either jdbcTemplate or dataSource must be set.");
}
}
/**
* Executes a given DBCommand, mapping each row to a Java object via a
* DBRecordMapper.
*
* @param cmd
* the DBCommand to execute
* @param recordMapper
* the mapper which maps each DBRecordData to a Java object
* @return the extracted list
*/
public List query(final DBCommand cmd,
final DBRecordMapper recordMapper) {
return query(cmd, new DbRecordMapperExtractor(recordMapper));
}
/**
* Executes a given DBCommand, mapping a single row to a Java object using
* the provided DBRecordMapper.
*
* @param cmd
* the DBCommand to execute
* @param recordMapper
* the DBRecordMapper to map
* @return the single Object
* @throws IncorrectResultSizeDataAccessException
* if more than one result object has been found
*/
public K queryForObject(final DBCommand cmd,
final DBRecordMapper recordMapper) {
return DataAccessUtils.uniqueResult(query(cmd, recordMapper));
}
/**
* Executes a given DBCommand, mapping a single column to a Java object
* using on DBRecordData.getValue() method.
*
* @param cmd
* the DBCommand to execute
* @param col
* the column to map
* @return the extracted list
*/
public List