package gu.sql2java.manager;
import java.lang.reflect.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.base.Predicates;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.primitives.Ints;
import gu.sql2java.BaseBean;
import gu.sql2java.BaseRow;
import gu.sql2java.ColumnCodec;
import gu.sql2java.Constant;
import gu.sql2java.ForeignKeyMetaData;
import gu.sql2java.IDataSourceConfig;
import gu.sql2java.IndexMetaData;
import gu.sql2java.ListenerContainer;
import gu.sql2java.RowMetaData;
import gu.sql2java.SimpleLog;
import gu.sql2java.TableListener;
import gu.sql2java.TableManager;
import gu.sql2java.ForeignKeyMetaData.ForeignKeyRule;
import gu.sql2java.exception.DaoException;
import gu.sql2java.exception.DataAccessException;
import gu.sql2java.exception.DataRetrievalException;
import gu.sql2java.exception.ObjectRetrievalException;
import gu.sql2java.exception.OptimisticLockingException;
import gu.sql2java.exception.QueueTimeoutException;
import gu.sql2java.exception.RuntimeDaoException;
import gu.sql2java.geometry.GeometryDataCodec;
import gu.sql2java.manager.Manager.AutoKeyRetrieveType;
import gu.sql2java.manager.parser.ParserSupport;
import static com.google.common.base.Preconditions.*;
import static gu.sql2java.SimpleLog.*;
import static gu.sql2java.exception.DaoException.stripSQLException;
import static gu.sql2java.manager.Managers.*;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;
import static gu.sql2java.Sql2javaSupport.hasNull;
import static gu.sql2java.Sql2javaSupport.hasNullPk;
/**
* implementation of {@link TableManager}
* @author guyadong
*
* @param <B> java bean type
*/
public class BaseTableManager<B extends BaseBean> implements TableManager<B>,Constant{
protected final RowMetaData metaData;
private volatile Manager manager;
/** lazy load */
private volatile Map<String,TableListener<BaseBean>> foreignKeyDeleteListeners;
/** lazy load */
private volatile ListenerContainerLocal<B> listenerContainer;
/** lazy load */
private volatile String generatedkeyStatement;
private static boolean debug = false;
protected BaseTableManager(String tablename){
metaData = RowMetaData.getMetaData(tablename);
}
@Override
public ListenerContainer<B> getListenerContainer() {
// double checking
if(listenerContainer == null){
synchronized (this) {
if(listenerContainer == null){
listenerContainer = new ListenerContainerLocal<B>(getManager().getFireType());
}
}
}
return listenerContainer;
}
@Override
public IDataSourceConfig getDataSourceConfig(){
return getManager().config;
}
/**
* @return map with foreignKey name TO TableListener
*/
public Map<String, TableListener<BaseBean>> getForeignKeyDeleteListeners(){
// double checking
if(foreignKeyDeleteListeners == null){
synchronized (this) {
if(foreignKeyDeleteListeners == null){
LinkedHashMap<String, TableListener<BaseBean>> map = Maps.newLinkedHashMap();
for(ForeignKeyMetaData fk : metaData.getForeignKeysForListener()){
map.put(fk.name, new DeleteRuleListener<BaseBean>(fk.name));
}
foreignKeyDeleteListeners = Collections.unmodifiableMap(map);
}
}
}
return foreignKeyDeleteListeners;
}
private String getGeneratedkeyStatement(){
// double check
if(generatedkeyStatement == null){
synchronized (this) {
if(generatedkeyStatement == null){
generatedkeyStatement = checkNotNull(getManager().getGeneratedkeyStatement(),"INVALID generatedkeyStatement")
.replaceAll("<TABLE>", metaData.tablename )
.replaceAll("<KEY>", metaData.columnNameOf(metaData.autoincrementColumnId));
if(debug){
log("generatedkeyStatement={}",generatedkeyStatement);
}
}
}
}
return generatedkeyStatement;
}
@Override
@SuppressWarnings("unchecked")
public final B createBean()
{
try {
return (B) metaData.beanType.newInstance();
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
/**
* Creates a new B instance.
* @param primaryValues values of primary keys
* @return B instance OR null if exist null value in primaryValues
*/
protected final B createBean(Object... primaryValues)
{
checkArgument(primaryValues != null && primaryValues.length== metaData.primaryKeyNames.length,"INVALID primaryValues");
B bean = createBean();
int[] pkIds = metaData.primaryKeyIds;
for(int i=0;i<primaryValues.length;++i){
int columnId = pkIds[i];
Object value = primaryValues[i];
if(null == value) {
return null;
}
try {
bean.setValue(columnId, value);
} catch (RuntimeException e) {
throw new IllegalArgumentException(
String.format("INVALID primkey type %s for %s.%s,Cased by %s",
value.getClass().getName(),
metaData.tablename,
metaData.columnNameOf(columnId),
e.getMessage()),
e);
}
}
return bean;
}
private void prepareAutoincrement( Connection c,PreparedStatement ps, B bean) throws SQLException
{
if (!bean.isModified(metaData.autoincrementColumnId))
{
PreparedStatement ps2 = null;
ResultSet rs = null;
try {
AutoKeyRetrieveType retrieveType = getManager().getGeneratedkeyRetrieveType();
if(AutoKeyRetrieveType.auto.equals(retrieveType)){
rs = ps.getGeneratedKeys();
}else{
ps2 = getManager().getStatementCache().prepareStatement(c, getGeneratedkeyStatement(), false, false, null);
rs = ps2.executeQuery();
}
if(rs.next()) {
setColumnValue(bean,
metaData.autoincrementColumnId,
getManager().getObject(rs,1,metaData.fieldTypeOf(metaData.autoincrementColumnId)));
} else {
throw new IllegalStateException(logString("ATTENTION: Could not retrieve generated key!(retrieveType:{})",retrieveType));
}
} finally {
getManager().close(ps2, rs);
}
}
}
//13
/**
* Insert the B bean into the database.
*
* @param bean the B bean to be saved
* @return the inserted bean
* @throws RuntimeDaoException
*/
protected B insert(final B bean)
{
// mini checks
if (null == bean || !bean.beModified()) {
return bean;
}
String productName = this.getManager().getProductName();
if (!bean.isNew() && !productName.equals(PRODUCT_NAME_PHOENIX)){
return this.update(bean);
}
Connection c = null;
PreparedStatement ps = null;
try
{
c = this.getConnection();
final AutoKeyRetrieveType retrieveType = getManager().getGenerated
lsx202406
- 粉丝: 2988
- 资源: 5702
最新资源
- PLC按时间启停设备运转,设备到期停止设备运转 1,设备启停时间可以自行设定 2,可多次调用子程序,只需修改对应参数即可设定多段启停时间段 3,到时间后输入动态密码继续继续运行 4,配合上位机组态
- 关键词:需求响应 主从博弈 热电联产 综合能源系统 编程语言:matlab 主题:基于主从博弈的综合能源微网需求响应优化调度模型
- Average Curve:基于MATLAB Simulink的通过线性插值返回多条曲线的平均曲线 返回的平均曲线也具有唯一的和排序的横坐标
- 前端分析-2023071100789
- WindRose:基于MATLAB Simulink的方向强度直方图(风玫瑰图),还可以将图形中所表示的数据返回数据表中
- VIENNA-Rectifier:基于MATLAB Simulink的VIENNA(维也纳)整流器仿真模型 控制算法采用电压电流双环控制,电压外环采用PI控制器,电流内环采用bang bang滞环控
- ATV61 71变频器驱动板图纸,30-45Kw可参考通用,图清晰
- 手机组态软件 APP监控西门子200smart PLC C#全套源代码 1,C#开发上位机手机APP,自己写的程序可提供部分 2,通过VS2019开发安卓手机app 3,全套源代码,admin
- 滚动轴承故障诊断MATLAB程序:快速谱峭度、谱峭度+包络谱分析 可以很好的提取出滚动轴承故障特征
- c#工业自动化通信开发库,工业自动软件必备的基本程序 包括串口通信,TCP客户端,tcp服务器端,高并发物联网接收服务器端,udp通信,can总线通信,profinet,modbus tcp rtu
- opc ua客户端实例源码,带ef6+sqlite 代码有完整的注解,及包括所有的链接库和程序结构思维图 纯学习资料
- C# 高并发高性能socket源代码 包括tcp客户端和服务器端,udp客户端和服务器端 所有都包括socket流控制 此代码属于上层代码,主要应用于大批量物联网项目,mes系统及游戏服务器
- pmsm电阻电感磁链常数辨识源码 电阻,电感,磁链常数辨识 程序在ti dsp实现 在ti开源foc框架基础上开发 能够辨识电机电阻,电感,磁链常数 精度较高,能够满足foc控制需要
- BP神经网络电力负荷预测MATLAB程序 MATLAB完整程序代大量注释,只提供程序和数据,不负责 提供原始数据 3年负荷Excel表格,2年作为训练,1年作为测试 数据提供全年365天,每天
- 前端分析-2023071100789
- comsol 5.6固态电池仿真,有参考文献,本模型为二维模型
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈