Search in sources :

Example 16 with DataSource

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

the class DBInfo method doStartTag.

@Override
public int doStartTag() throws PageException {
    Object ds = getDatasource(pageContext, datasource);
    DataSourceManager manager = pageContext.getDataSourceManager();
    DatasourceConnection dc = ds instanceof DataSource ? manager.getConnection(pageContext, (DataSource) ds, username, password) : manager.getConnection(pageContext, Caster.toString(ds), username, password);
    try {
        if (type == TYPE_TABLE_COLUMNS)
            typeColumns(dc.getConnection().getMetaData());
        else if (type == TYPE_DBNAMES)
            typeDBNames(dc.getConnection().getMetaData());
        else if (type == TYPE_FOREIGNKEYS)
            typeForeignKeys(dc.getConnection().getMetaData());
        else if (type == TYPE_INDEX)
            typeIndex(dc.getConnection().getMetaData());
        else if (type == TYPE_PROCEDURES)
            typeProcedures(dc.getConnection().getMetaData());
        else if (type == TYPE_PROCEDURE_COLUMNS)
            typeProcedureColumns(dc.getConnection().getMetaData());
        else if (type == TYPE_TERMS)
            typeTerms(dc.getConnection().getMetaData());
        else if (type == TYPE_TABLES)
            typeTables(dc.getConnection().getMetaData());
        else if (type == TYPE_VERSION)
            typeVersion(dc.getConnection().getMetaData());
        else if (type == TYPE_USERS)
            typeUsers(dc.getConnection().getMetaData());
    } catch (SQLException sqle) {
        throw new DatabaseException(sqle, dc);
    } finally {
        manager.releaseConnection(pageContext, dc);
    }
    return SKIP_BODY;
}
Also used : DatasourceConnection(lucee.runtime.db.DatasourceConnection) SQLException(java.sql.SQLException) DataSourceManager(lucee.runtime.db.DataSourceManager) DatabaseException(lucee.runtime.exp.DatabaseException) DataSource(lucee.runtime.db.DataSource)

Example 17 with DataSource

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

the class ColumnInfo method doEndTag.

@Override
public int doEndTag() throws PageException {
    Object ds = DBInfo.getDatasource(pageContext, datasource);
    DataSourceManager manager = pageContext.getDataSourceManager();
    DatasourceConnection dc = ds instanceof DataSource ? manager.getConnection(pageContext, (DataSource) ds, username, password) : manager.getConnection(pageContext, Caster.toString(ds), username, password);
    try {
        Struct meta = null;
        try {
            meta = getMeta(dc, tablequalifier, tableowner, tablename);
        } catch (SQLException se) {
            meta = new StructImpl();
        }
        SQL sql = createSQL(meta);
        if (sql != null) {
            lucee.runtime.type.Query query = new QueryImpl(pageContext, dc, sql, -1, -1, null, "query");
            if (pageContext.getConfig().debug()) {
                String dsn = ds instanceof DataSource ? ((DataSource) ds).getName() : Caster.toString(ds);
                boolean logdb = ((ConfigImpl) pageContext.getConfig()).hasDebugOptions(ConfigImpl.DEBUG_DATABASE);
                if (logdb) {
                    boolean debugUsage = DebuggerImpl.debugQueryUsage(pageContext, query);
                    pageContext.getDebugger().addQuery(debugUsage ? query : null, dsn, "", sql, query.getRecordcount(), pageContext.getCurrentPageSource(), query.getExecutionTime());
                }
            }
            // log
            Log log = pageContext.getConfig().getLog("datasource");
            if (log.getLogLevel() >= Log.LEVEL_INFO) {
                log.info("insert tag", "executed [" + sql.toString().trim() + "] in " + DecimalFormat.call(pageContext, query.getExecutionTime() / 1000000D) + " ms");
            }
        }
        return EVAL_PAGE;
    } catch (PageException pe) {
        pageContext.getConfig().getLog("datasource").error("insert tag", pe);
        throw pe;
    } finally {
        manager.releaseConnection(pageContext, dc);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) DatasourceConnection(lucee.runtime.db.DatasourceConnection) SQLException(java.sql.SQLException) Log(lucee.commons.io.log.Log) DataSourceManager(lucee.runtime.db.DataSourceManager) DataSource(lucee.runtime.db.DataSource) Struct(lucee.runtime.type.Struct) SQL(lucee.runtime.db.SQL) QueryImpl(lucee.runtime.type.QueryImpl) StructImpl(lucee.runtime.type.StructImpl) ConfigImpl(lucee.runtime.config.ConfigImpl)

Example 18 with DataSource

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

the class QueryUtil method checkSQLRestriction.

/**
 * check if there is a sql restriction
 * @param dc
 * @param sql
 * @throws PageException
 */
public static void checkSQLRestriction(DatasourceConnection dc, SQL sql) throws PageException {
    Array sqlparts = ListUtil.listToArrayRemoveEmpty(SQLUtil.removeLiterals(sql.getSQLString()), " \t" + System.getProperty("line.separator"));
    // print.ln(List.toStringArray(sqlparts));
    DataSource ds = dc.getDatasource();
    if (!ds.hasAllow(DataSource.ALLOW_ALTER))
        checkSQLRestriction(dc, "alter", sqlparts, sql);
    if (!ds.hasAllow(DataSource.ALLOW_CREATE))
        checkSQLRestriction(dc, "create", sqlparts, sql);
    if (!ds.hasAllow(DataSource.ALLOW_DELETE))
        checkSQLRestriction(dc, "delete", sqlparts, sql);
    if (!ds.hasAllow(DataSource.ALLOW_DROP))
        checkSQLRestriction(dc, "drop", sqlparts, sql);
    if (!ds.hasAllow(DataSource.ALLOW_GRANT))
        checkSQLRestriction(dc, "grant", sqlparts, sql);
    if (!ds.hasAllow(DataSource.ALLOW_INSERT))
        checkSQLRestriction(dc, "insert", sqlparts, sql);
    if (!ds.hasAllow(DataSource.ALLOW_REVOKE))
        checkSQLRestriction(dc, "revoke", sqlparts, sql);
    if (!ds.hasAllow(DataSource.ALLOW_SELECT))
        checkSQLRestriction(dc, "select", sqlparts, sql);
    if (!ds.hasAllow(DataSource.ALLOW_UPDATE))
        checkSQLRestriction(dc, "update", sqlparts, sql);
}
Also used : Array(lucee.runtime.type.Array) DataSource(lucee.runtime.db.DataSource)

Example 19 with DataSource

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

the class DBUtilImpl method getDatasourceConnection.

public DatasourceConnection getDatasourceConnection(PageContext pc, String datasourceName, String user, String pass, boolean managed) throws PageException {
    DataSource datasource = null;
    pc = ThreadLocalPageContext.get(pc);
    if (pc != null) {
        // default datasource
        if ("__default__".equalsIgnoreCase(datasourceName)) {
            Object obj = pc.getApplicationContext().getDefDataSource();
            if (obj instanceof String)
                datasourceName = (String) obj;
            else
                datasource = (DataSource) obj;
        }
        // get datasource from application context
        if (datasource == null)
            datasource = pc.getApplicationContext().getDataSource(datasourceName, null);
    }
    // get datasource from config
    if (datasource == null) {
        Config config = ThreadLocalPageContext.getConfig(pc);
        datasource = config.getDataSource(datasourceName);
    }
    return getDatasourceConnection(pc, datasource, user, pass, managed);
}
Also used : Config(lucee.runtime.config.Config) DataSource(lucee.runtime.db.DataSource)

Example 20 with DataSource

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

the class ScopeContext method hasExistingCFSessionScope.

private boolean hasExistingCFSessionScope(PageContext pc) {
    ApplicationContext appContext = pc.getApplicationContext();
    // get Context
    Map<String, Scope> context = getSubMap(cfSessionContexts, appContext.getName());
    // get Session
    String storage = appContext.getSessionstorage();
    if (StringUtil.isEmpty(storage, true))
        storage = "memory";
    else if ("ram".equalsIgnoreCase(storage))
        storage = "memory";
    else if ("registry".equalsIgnoreCase(storage))
        storage = "file";
    else
        storage = storage.toLowerCase();
    Session session = (Session) context.get(pc.getCFID());
    if (!(session instanceof StorageScope) || session.isExpired() || !((StorageScope) session).getStorage().equalsIgnoreCase(storage)) {
        if ("memory".equals(storage))
            return false;
        else if ("file".equals(storage))
            return SessionFile.hasInstance(appContext.getName(), pc);
        else if ("cookie".equals(storage))
            return SessionCookie.hasInstance(appContext.getName(), pc);
        else {
            DataSource ds = pc.getConfig().getDataSource(storage, null);
            if (ds != null && ds.isStorage()) {
                if (INVIDUAL_STORAGE_KEYS) {
                    return IKStorageScopeSupport.hasInstance(Scope.SCOPE_SESSION, new IKHandlerDatasource(), appContext.getName(), storage, pc);
                } else {
                    if (SessionDatasource.hasInstance(storage, pc))
                        return true;
                }
            }
            if (INVIDUAL_STORAGE_KEYS)
                return IKStorageScopeSupport.hasInstance(Scope.SCOPE_SESSION, new IKHandlerCache(), appContext.getName(), storage, pc);
            return SessionCache.hasInstance(storage, appContext.getName(), pc);
        }
    }
    return true;
}
Also used : StorageScope(lucee.runtime.type.scope.storage.StorageScope) ApplicationContext(lucee.runtime.listener.ApplicationContext) StorageScope(lucee.runtime.type.scope.storage.StorageScope) MemoryScope(lucee.runtime.type.scope.storage.MemoryScope) IKHandlerCache(lucee.runtime.type.scope.storage.IKHandlerCache) HttpSession(javax.servlet.http.HttpSession) DataSource(lucee.runtime.db.DataSource) IKHandlerDatasource(lucee.runtime.type.scope.storage.IKHandlerDatasource)

Aggregations

DataSource (lucee.runtime.db.DataSource)27 PageException (lucee.runtime.exp.PageException)9 Struct (lucee.runtime.type.Struct)9 DatasourceConnection (lucee.runtime.db.DatasourceConnection)8 ApplicationException (lucee.runtime.exp.ApplicationException)8 ConfigImpl (lucee.runtime.config.ConfigImpl)7 StructImpl (lucee.runtime.type.StructImpl)7 SQLException (java.sql.SQLException)5 Map (java.util.Map)5 Log (lucee.commons.io.log.Log)5 Entry (java.util.Map.Entry)4 CacheConnection (lucee.runtime.cache.CacheConnection)4 ClassDefinition (lucee.runtime.db.ClassDefinition)4 DataSourceManager (lucee.runtime.db.DataSourceManager)4 DatasourceConnectionPool (lucee.runtime.db.DatasourceConnectionPool)4 ApplicationContext (lucee.runtime.listener.ApplicationContext)4 QueryImpl (lucee.runtime.type.QueryImpl)4 SQLExecutor (lucee.runtime.type.scope.storage.db.SQLExecutor)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3