Search in sources :

Example 1 with DatasourceConnectionPool

use of lucee.runtime.db.DatasourceConnectionPool in project Lucee by lucee.

the class IKHandlerDatasource method loadData.

@Override
public IKStorageValue loadData(PageContext pc, String appName, String name, String strType, int type, Log log) throws PageException {
    ConfigImpl config = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
    DatasourceConnectionPool pool = config.getDatasourceConnectionPool();
    DatasourceConnection dc = pool.getDatasourceConnection(config, pc.getDataSource(name), null, null);
    SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
    Query query;
    try {
        if (!dc.getDatasource().isStorage())
            throw new ApplicationException("storage usage for this datasource is disabled, you can enable this in the Lucee administrator.");
        query = executor.select(config, pc.getCFID(), pc.getApplicationContext().getName(), dc, type, log, true);
    } catch (SQLException se) {
        throw Caster.toPageException(se);
    } finally {
        if (dc != null)
            pool.releaseDatasourceConnection(dc);
    }
    if (query != null && config.debug()) {
        boolean debugUsage = DebuggerUtil.debugQueryUsage(pc, query);
        pc.getDebugger().addQuery(debugUsage ? query : null, name, "", query.getSql(), query.getRecordcount(), ((PageContextImpl) pc).getCurrentPageSource(null), query.getExecutionTime());
    }
    boolean _isNew = query.getRecordcount() == 0;
    if (_isNew) {
        ScopeContext.info(log, "create new " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID() + " in datasource [" + name + "]");
        return null;
    }
    String str = Caster.toString(query.getAt(KeyConstants._data, 1));
    if (str.startsWith("struct:"))
        return null;
    try {
        IKStorageValue data = (IKStorageValue) JavaConverter.deserialize(str);
        ScopeContext.info(log, "load existing data from [" + name + "." + PREFIX + "_" + strType + "_data] to create " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID());
        return data;
    } catch (Exception e) {
        ScopeContext.error(log, e);
        return null;
    // throw Caster.toPageException(e);
    }
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) DatasourceConnectionPool(lucee.runtime.db.DatasourceConnectionPool) DatasourceConnection(lucee.runtime.db.DatasourceConnection) Query(lucee.runtime.type.Query) SQLExecutor(lucee.runtime.type.scope.storage.db.SQLExecutor) SQLException(java.sql.SQLException) ConfigImpl(lucee.runtime.config.ConfigImpl) SQLException(java.sql.SQLException) ApplicationException(lucee.runtime.exp.ApplicationException) PageException(lucee.runtime.exp.PageException)

Example 2 with DatasourceConnectionPool

use of lucee.runtime.db.DatasourceConnectionPool in project Lucee by lucee.

the class IKHandlerDatasource method store.

@Override
public void store(IKStorageScopeSupport storageScope, PageContext pc, String appName, final String name, String cfid, MapPro<Key, IKStorageScopeItem> data, Log log) {
    DatasourceConnection dc = null;
    ConfigImpl ci = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
    DatasourceConnectionPool pool = ci.getDatasourceConnectionPool();
    try {
        pc = ThreadLocalPageContext.get(pc);
        DataSource ds;
        if (pc != null)
            ds = pc.getDataSource(name);
        else
            ds = ci.getDataSource(name);
        dc = pool.getDatasourceConnection(null, ds, null, null);
        SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
        IKStorageValue existingVal = loadData(pc, appName, name, storageScope.getTypeAsString(), storageScope.getType(), log);
        IKStorageValue sv = new IKStorageValue(IKStorageScopeSupport.prepareToStore(data, existingVal, storageScope.lastModified()));
        executor.update(ci, cfid, appName, dc, storageScope.getType(), sv, storageScope.getTimeSpan(), log);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        ScopeContext.error(log, t);
    } finally {
        if (dc != null)
            pool.releaseDatasourceConnection(dc);
    }
}
Also used : DatasourceConnection(lucee.runtime.db.DatasourceConnection) DatasourceConnectionPool(lucee.runtime.db.DatasourceConnectionPool) SQLExecutor(lucee.runtime.type.scope.storage.db.SQLExecutor) ConfigImpl(lucee.runtime.config.ConfigImpl) DataSource(lucee.runtime.db.DataSource)

Example 3 with DatasourceConnectionPool

use of lucee.runtime.db.DatasourceConnectionPool in project Lucee by lucee.

the class IKHandlerDatasource method unstore.

@Override
public void unstore(IKStorageScopeSupport storageScope, PageContext pc, String appName, String name, String cfid, Log log) {
    ConfigImpl ci = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
    DatasourceConnection dc = null;
    DatasourceConnectionPool pool = ci.getDatasourceConnectionPool();
    try {
        // FUTURE change method interface
        pc = ThreadLocalPageContext.get(pc);
        DataSource ds;
        if (pc != null)
            ds = pc.getDataSource(name);
        else
            ds = ci.getDataSource(name);
        dc = pool.getDatasourceConnection(null, ds, null, null);
        SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
        executor.delete(ci, cfid, appName, dc, storageScope.getType(), log);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        ScopeContext.error(log, t);
    } finally {
        if (dc != null)
            pool.releaseDatasourceConnection(dc);
    }
}
Also used : DatasourceConnection(lucee.runtime.db.DatasourceConnection) DatasourceConnectionPool(lucee.runtime.db.DatasourceConnectionPool) SQLExecutor(lucee.runtime.type.scope.storage.db.SQLExecutor) ConfigImpl(lucee.runtime.config.ConfigImpl) DataSource(lucee.runtime.db.DataSource)

Example 4 with DatasourceConnectionPool

use of lucee.runtime.db.DatasourceConnectionPool in project Lucee by lucee.

the class StorageScopeDatasource method unstore.

@Override
public void unstore(PageContext pc) {
    ConfigImpl ci = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
    DatasourceConnection dc = null;
    DatasourceConnectionPool pool = ci.getDatasourceConnectionPool();
    Log log = ci.getLog("scope");
    try {
        // FUTURE change method interface
        pc = ThreadLocalPageContext.get(pc);
        DataSource ds;
        if (pc != null)
            ds = pc.getDataSource(datasourceName);
        else
            ds = ci.getDataSource(datasourceName);
        dc = pool.getDatasourceConnection(null, ds, null, null);
        SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
        executor.delete(ci, cfid, appName, dc, getType(), log);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        ScopeContext.error(log, t);
    } finally {
        if (dc != null)
            pool.releaseDatasourceConnection(dc);
    }
}
Also used : DatasourceConnection(lucee.runtime.db.DatasourceConnection) DatasourceConnectionPool(lucee.runtime.db.DatasourceConnectionPool) Log(lucee.commons.io.log.Log) SQLExecutor(lucee.runtime.type.scope.storage.db.SQLExecutor) ConfigImpl(lucee.runtime.config.ConfigImpl) DataSource(lucee.runtime.db.DataSource)

Example 5 with DatasourceConnectionPool

use of lucee.runtime.db.DatasourceConnectionPool in project Lucee by lucee.

the class StorageScopeDatasource method _loadData.

protected static Struct _loadData(PageContext pc, String datasourceName, String strType, int type, Log log, boolean mxStyle) throws PageException {
    ConfigImpl config = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
    DatasourceConnectionPool pool = config.getDatasourceConnectionPool();
    DatasourceConnection dc = pool.getDatasourceConnection(config, pc.getDataSource(datasourceName), null, null);
    SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
    Query query;
    try {
        if (!dc.getDatasource().isStorage())
            throw new ApplicationException("storage usage for this datasource is disabled, you can enable this in the Lucee administrator.");
        query = executor.select(config, pc.getCFID(), pc.getApplicationContext().getName(), dc, type, log, true);
    } catch (SQLException se) {
        throw Caster.toPageException(se);
    } finally {
        if (dc != null)
            pool.releaseDatasourceConnection(dc);
    }
    if (query != null && config.debug()) {
        boolean debugUsage = DebuggerUtil.debugQueryUsage(pc, query);
        pc.getDebugger().addQuery(debugUsage ? query : null, datasourceName, "", query.getSql(), query.getRecordcount(), ((PageContextImpl) pc).getCurrentPageSource(null), query.getExecutionTime());
    }
    boolean _isNew = query.getRecordcount() == 0;
    if (_isNew) {
        ScopeContext.info(log, "create new " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID() + " in datasource [" + datasourceName + "]");
        return null;
    }
    String str = Caster.toString(query.get(KeyConstants._data));
    if (str != null && str.startsWith("struct:"))
        str = str.substring(7);
    if (mxStyle)
        return null;
    try {
        Struct s = (Struct) pc.evaluate(str);
        ScopeContext.info(log, "load existing data from [" + datasourceName + "." + PREFIX + "_" + strType + "_data] to create " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID());
        return s;
    } catch (Exception e) {
        ScopeContext.error(log, e);
        return null;
    }
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) DatasourceConnectionPool(lucee.runtime.db.DatasourceConnectionPool) DatasourceConnection(lucee.runtime.db.DatasourceConnection) Query(lucee.runtime.type.Query) SQLExecutor(lucee.runtime.type.scope.storage.db.SQLExecutor) SQLException(java.sql.SQLException) ConfigImpl(lucee.runtime.config.ConfigImpl) SQLException(java.sql.SQLException) ApplicationException(lucee.runtime.exp.ApplicationException) PageException(lucee.runtime.exp.PageException) Struct(lucee.runtime.type.Struct)

Aggregations

DatasourceConnectionPool (lucee.runtime.db.DatasourceConnectionPool)8 ConfigImpl (lucee.runtime.config.ConfigImpl)7 DatasourceConnection (lucee.runtime.db.DatasourceConnection)7 SQLExecutor (lucee.runtime.type.scope.storage.db.SQLExecutor)7 DataSource (lucee.runtime.db.DataSource)4 Log (lucee.commons.io.log.Log)3 SQLException (java.sql.SQLException)2 ApplicationException (lucee.runtime.exp.ApplicationException)2 PageException (lucee.runtime.exp.PageException)2 Query (lucee.runtime.type.Query)2 ConfigWebImpl (lucee.runtime.config.ConfigWebImpl)1 Struct (lucee.runtime.type.Struct)1