Search in sources :

Example 6 with DatasourceConnection

use of lucee.runtime.db.DatasourceConnection 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 7 with DatasourceConnection

use of lucee.runtime.db.DatasourceConnection 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 8 with DatasourceConnection

use of lucee.runtime.db.DatasourceConnection 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 9 with DatasourceConnection

use of lucee.runtime.db.DatasourceConnection 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)

Example 10 with DatasourceConnection

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

the class DatasourceStorageScopeCleaner method clean.

private void clean(ConfigWeb config, DataSource dataSource) throws PageException, SQLException {
    ConfigWebImpl cwi = (ConfigWebImpl) config;
    DatasourceConnection dc = null;
    DatasourceConnectionPool pool = cwi.getDatasourceConnectionPool();
    try {
        dc = pool.getDatasourceConnection(null, dataSource, null, null);
        Log log = ((ConfigImpl) config).getLog("scope");
        SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
        executor.clean(config, dc, type, engine, this, listener, log);
    } finally {
        if (dc != null)
            pool.releaseDatasourceConnection(dc);
    }
}
Also used : ConfigWebImpl(lucee.runtime.config.ConfigWebImpl) 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)

Aggregations

DatasourceConnection (lucee.runtime.db.DatasourceConnection)24 SQLException (java.sql.SQLException)14 ConfigImpl (lucee.runtime.config.ConfigImpl)10 PageException (lucee.runtime.exp.PageException)9 DataSource (lucee.runtime.db.DataSource)8 DatabaseException (lucee.runtime.exp.DatabaseException)8 Log (lucee.commons.io.log.Log)7 DatasourceConnectionPool (lucee.runtime.db.DatasourceConnectionPool)7 SQLExecutor (lucee.runtime.type.scope.storage.db.SQLExecutor)7 ApplicationException (lucee.runtime.exp.ApplicationException)5 DataSourceManager (lucee.runtime.db.DataSourceManager)4 PageRuntimeException (lucee.runtime.exp.PageRuntimeException)4 QueryImpl (lucee.runtime.type.QueryImpl)4 Struct (lucee.runtime.type.Struct)4 IOException (java.io.IOException)3 ResultSet (java.sql.ResultSet)3 StructImpl (lucee.runtime.type.StructImpl)3 Connection (java.sql.Connection)2 Iterator (java.util.Iterator)2 SQL (lucee.runtime.db.SQL)2