/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* 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.jkiss.dbeaver.model.impl.jdbc.cache;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement;
import org.jkiss.dbeaver.model.struct.DBSObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Simple objects cache.
*/
public final class JDBCObjectSimpleCache<OWNER extends DBSObject, OBJECT extends DBSObject>
extends JDBCObjectCache<OWNER, OBJECT>
{
private final String query;
private final Class<OBJECT> objectType;
private final Object[] queryParameters;
private Constructor<OBJECT> objectConstructor;
public JDBCObjectSimpleCache(Class<OBJECT> objectType, String query, Object ... args)
{
this.query = query;
this.objectType = objectType;
this.queryParameters = args;
}
@Override
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull OWNER owner)
throws SQLException
{
JDBCPreparedStatement dbStat = session.prepareStatement(query);
if (queryParameters != null) {
for (int i = 0; i < queryParameters.length; i++) {
dbStat.setObject(i + 1, queryParameters[i]);
}
}
return dbStat;
}
@Override
protected OBJECT fetchObject(@NotNull JDBCSession session, @NotNull OWNER owner, @NotNull JDBCResultSet resultSet)
throws SQLException, DBException
{
try {
if (objectConstructor == null) {
for (Class<?> argType = owner.getClass(); argType != null; argType = argType.getSuperclass()) {
try {
objectConstructor = objectType.getConstructor(argType, ResultSet.class);
break;
} catch (Exception e) {
// Not found - check interfaces
for (Class<?> intType : argType.getInterfaces()) {
try {
objectConstructor = objectType.getConstructor(intType, ResultSet.class);
break;
} catch (Exception e2) {
// Not found
}
}
if (objectConstructor != null) {
break;
}
}
}
if (objectConstructor == null) {
throw new DBException("Can't find proper constructor for object '" + objectType.getName() + "'");
}
}
return objectConstructor.newInstance(owner, resultSet);
} catch (Exception e) {
throw new DBException(
"Error creating cache object",
e instanceof InvocationTargetException ? ((InvocationTargetException)e).getTargetException() : e);
}
}
}