
org.frameworkset.persitent.util.SQLCache Maven / Gradle / Ivy
/*
* Copyright 2008 biaoping.yin
*
* Licensed 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.frameworkset.persitent.util;
import com.frameworkset.common.poolman.sql.PoolManResultSetMetaData;
import com.frameworkset.util.VariableHandler;
import com.frameworkset.util.VariableHandler.SQLStruction;
import java.lang.ref.SoftReference;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Title: SQLCache.java
* Description:
* bboss workgroup
* Copyright (c) 2007
* @Date 2012-12-5 下午6:15:07
* @author biaoping.yin
* @version 1.0
*/
public class SQLCache {
private Lock lock = new ReentrantLock();
private Lock vtplLock = new ReentrantLock();
private Map parserSQLStructions = new java.util.HashMap();
private Map parsertotalsizeSQLStructions = new java.util.HashMap();
private Map> parserTPLSQLStructions = new java.util.HashMap>();
private Map> parserTPLTotalsizeSQLStructions = new java.util.HashMap>();
protected Map>> metas = new HashMap>>();
public SQLCache() {
// TODO Auto-generated constructor stub
}
public void clear()
{
metas.clear();
parserSQLStructions.clear();
parsertotalsizeSQLStructions.clear();
parserTPLSQLStructions.clear();
parserTPLTotalsizeSQLStructions.clear();
}
private boolean needRefreshMeta(PoolManResultSetMetaData meta,ResultSetMetaData rsmetadata) throws SQLException
{
if(meta.getColumnCount() != rsmetadata.getColumnCount())//列数发生变化
return true;
String[] labels = meta.get_columnLabel();//判断列名称是否变化
int coltypes[] = meta.get_columnType();
for(int i = 0; i < labels.length;i ++)
{
if(!labels[i].equals(rsmetadata.getColumnLabel(i + 1)))//列名变化
{
return true;
}
if(coltypes[i] != rsmetadata.getColumnType(i + 1))//类型变化
return true;
}
return false;
}
public PoolManResultSetMetaData getPoolManResultSetMetaData(com.frameworkset.orm.adapter.DB db,String dbname,String sqlkey,ResultSetMetaData rsmetadata) throws SQLException
{
PoolManResultSetMetaData meta = null;
Map> dbmetas = metas.get(dbname);
if(dbmetas == null)
{
synchronized(metas)
{
dbmetas = metas.get(dbname);
if(dbmetas == null)
{
dbmetas = new HashMap>();
metas.put(dbname, dbmetas);
}
}
}
// sqlkey = sqlkey + "__pagine" ;
// if (dbmetas.containsKey(sqlkey)) {
SoftReference wr = dbmetas.get(sqlkey);
if (wr != null) {
meta = (PoolManResultSetMetaData) wr.get();
if (meta == null) {
meta = PoolManResultSetMetaData.getCopy(db,rsmetadata);
SoftReference wr1 = new SoftReference(meta);
dbmetas.put(sqlkey, wr1);
}
else
{
if(needRefreshMeta(meta,rsmetadata))
{
meta = PoolManResultSetMetaData.getCopy(db,rsmetadata);
wr = new SoftReference(meta);
dbmetas.put(sqlkey, wr);
}
}
} else {
meta = PoolManResultSetMetaData.getCopy(db,rsmetadata);
wr = new SoftReference(meta);
dbmetas.put(sqlkey, wr);
}
return meta;
}
public SQLStruction getSQLStruction(SQLInfo sqlinfo,String newsql)
{
// String sql = newsql;
// String key = null;
if(sqlinfo.getSqlutil() == null
|| sqlinfo.getSqlutil() == SQLUtil.getGlobalSQLUtil()) {
// key = sql;
return this._getVTPLSQLStruction(parserTPLSQLStructions,sqlinfo,newsql,"___GlobalSQLUtil_");
}
else
{
// if(sqlinfo.istpl() )
// {
// key = sql;
// }
// else
// {
// key = sqlinfo.getSqlname();
// }
if(sqlinfo.istpl() )
{
return this._getVTPLSQLStruction(parserTPLSQLStructions,sqlinfo,newsql,sqlinfo.getSqlname());
}
else
{
return _getSQLStruction(parserSQLStructions,sqlinfo, newsql);
}
}
// SQLStruction sqlstruction = parserSQLStructions.get(key);
// if(sqlstruction == null)
// {
// synchronized(lock)
// {
// sqlstruction = parserSQLStructions.get(key);
// if(sqlstruction == null)
// {
// sqlstruction = VariableHandler.parserSQLStruction(sql);
// parserSQLStructions.put(key,sqlstruction);
// }
// }
// }
// return sqlstruction;
}
private VariableHandler.SQLStruction _getSQLStruction(Map parserSQLStructions ,SQLInfo sqlinfo, String newsql)
{
String key = sqlinfo.getSqlname();
VariableHandler.SQLStruction sqlstruction = parserSQLStructions.get(key);
if(sqlstruction == null)
{
try
{
lock.lock();
sqlstruction = parserSQLStructions.get(key);
if(sqlstruction == null)
{
sqlstruction = VariableHandler.parserSQLStruction(newsql);
parserSQLStructions.put(key,sqlstruction);
}
}
finally {
lock.unlock();
}
}
return sqlstruction;
}
/**
* vtpl需要进行分级缓存
* @param sqlinfo
* @param newsql
* @return
*/
private VariableHandler.SQLStruction _getVTPLSQLStruction(Map> parserTPLSQLStructions,SQLInfo sqlinfo, String newsql,String okey)
{
String ikey = newsql;
// String okey = sqlinfo.getSqlname();
Map sqlstructionMap = parserTPLSQLStructions.get(okey);
if(sqlstructionMap == null)
{
try
{
this.vtplLock.lock();
sqlstructionMap = parserTPLSQLStructions.get(okey);
if(sqlstructionMap == null)
{
sqlstructionMap = new java.util.WeakHashMap();
parserTPLSQLStructions.put(okey,sqlstructionMap);
}
}
finally {
vtplLock.unlock();
}
}
VariableHandler.SQLStruction urlStruction = sqlstructionMap.get(ikey);
if(urlStruction == null){
try
{
this.vtplLock.lock();
urlStruction = sqlstructionMap.get(ikey);
if(urlStruction == null){
urlStruction = VariableHandler.parserSQLStruction(newsql);
sqlstructionMap.put(ikey,urlStruction);
}
}
finally {
this.vtplLock.unlock();
}
}
return urlStruction;
}
public SQLStruction getTotalsizeSQLStruction(SQLInfo totalsizesqlinfo,String newtotalsizesql)
{
// String totalsizesql = newtotalsizesql;
// String key = null;
if(totalsizesqlinfo.getSqlutil() == null
|| totalsizesqlinfo.getSqlutil() == SQLUtil.getGlobalSQLUtil()) {
// key = totalsizesql;
return this._getVTPLSQLStruction(this.parserTPLTotalsizeSQLStructions, totalsizesqlinfo, newtotalsizesql, "___GlobalSQLUtil_");
}
else
{
// if(totalsizesqlinfo.istpl() )
// {
// key = totalsizesql;
// }
// else
// {
// key = totalsizesqlinfo.getSqlname();
// }
if(totalsizesqlinfo.istpl() )
{
return this._getVTPLSQLStruction(this.parserTPLTotalsizeSQLStructions,totalsizesqlinfo,newtotalsizesql,totalsizesqlinfo.getSqlname());
}
else
{
return _getSQLStruction(parsertotalsizeSQLStructions,totalsizesqlinfo, newtotalsizesql);
}
}
// SQLStruction totalsizesqlstruction = parsertotalsizeSQLStructions.get(key);
// if(totalsizesqlstruction == null)
// {
// synchronized(lock)
// {
// totalsizesqlstruction = parsertotalsizeSQLStructions.get(key);
// if(totalsizesqlstruction == null)
// {
// totalsizesqlstruction = VariableHandler.parserSQLStruction(totalsizesql);
// parsertotalsizeSQLStructions.put(key,totalsizesqlstruction);
// }
// }
// }
// return totalsizesqlstruction;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy