Search in sources :

Example 11 with DatasourceConnection

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

the class DatasourceResourceProvider method getCore.

private Core getCore(ConnectionData data) throws PageException {
    Core core = (Core) cores.get(data.datasourceName);
    if (core == null) {
        DatasourceConnection dc = getManager().getConnection(ThreadLocalPageContext.get(), data.getDatasourceName(), data.getUsername(), data.getPassword());
        try {
            dc.getConnection().setAutoCommit(false);
            dc.getConnection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
            if ("com.microsoft.jdbc.sqlserver.SQLServerDriver".equals(dc.getDatasource().getClassDefinition().getClassName()))
                core = new MSSQL(dc, data.getPrefix());
            else if ("com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(dc.getDatasource().getClassDefinition().getClassName()))
                core = new MSSQL(dc, data.getPrefix());
            else if ("net.sourceforge.jtds.jdbc.Driver".equals(dc.getDatasource().getClassDefinition().getClassName()))
                core = new MSSQL(dc, data.getPrefix());
            else if ("org.gjt.mm.mysql.Driver".equals(dc.getDatasource().getClassDefinition().getClassName()))
                core = new MySQL(dc, data.getPrefix());
            else
                throw new ApplicationException("there is no DatasourceResource driver for this database [" + data.getPrefix() + "]");
            cores.put(data.datasourceName, core);
        } catch (SQLException e) {
            throw new DatabaseException(e, dc);
        } finally {
            release(dc);
        // manager.releaseConnection(CONNECTION_ID,dc);
        }
    }
    return core;
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) DatasourceConnection(lucee.runtime.db.DatasourceConnection) SQLException(java.sql.SQLException) MySQL(lucee.commons.io.res.type.datasource.core.MySQL) MSSQL(lucee.commons.io.res.type.datasource.core.MSSQL) DatabaseException(lucee.runtime.exp.DatabaseException) Core(lucee.commons.io.res.type.datasource.core.Core)

Example 12 with DatasourceConnection

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

the class DatasourceResourceProvider method delete.

public void delete(ConnectionData data, int fullPathHash, String path, String name) throws IOException {
    Attr attr = getAttr(data, fullPathHash, path, name);
    if (attr == null)
        throw new IOException("can't delete resource " + path + name + ", resource does not exist");
    DatasourceConnection dc = null;
    try {
        dc = getDatasourceConnection(data);
        getCore(data).delete(dc, data.getPrefix(), attr);
    } catch (SQLException e) {
        throw new IOException(e.getMessage());
    } catch (PageException e) {
        throw new PageRuntimeException(e);
    } finally {
        removeFromCache(data, path, name);
        release(dc);
    // manager.releaseConnection(CONNECTION_ID,dc);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) DatasourceConnection(lucee.runtime.db.DatasourceConnection) SQLException(java.sql.SQLException) IOException(java.io.IOException) PageRuntimeException(lucee.runtime.exp.PageRuntimeException)

Example 13 with DatasourceConnection

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

the class DatasourceResourceProvider method getAttrs.

public Attr[] getAttrs(ConnectionData data, int pathHash, String path) throws PageException {
    if (StringUtil.isEmpty(data.getDatasourceName()))
        return null;
    // Attr[] attrs = getFromCache(data, path);
    // if(attrs!=null) return attrs;
    DatasourceConnection dc = null;
    try {
        dc = getDatasourceConnection(data);
        List list = getCore(data).getAttrs(dc, data.getPrefix(), pathHash, path);
        if (list != null) {
            Iterator it = list.iterator();
            Attr[] rtn = new Attr[list.size()];
            int index = 0;
            while (it.hasNext()) {
                rtn[index] = (Attr) it.next();
                putToCache(data, rtn[index].getParent(), rtn[index].getName(), rtn[index]);
                index++;
            }
            // putToCache(data, path, rtn);
            return rtn;
        }
    } catch (SQLException e) {
        throw new DatabaseException(e, dc);
    } finally {
        release(dc);
    // manager.releaseConnection(CONNECTION_ID,dc);
    }
    return null;
}
Also used : DatasourceConnection(lucee.runtime.db.DatasourceConnection) SQLException(java.sql.SQLException) Iterator(java.util.Iterator) List(java.util.List) DatabaseException(lucee.runtime.exp.DatabaseException)

Example 14 with DatasourceConnection

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

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

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