Search in sources :

Example 21 with DatabaseException

use of lucee.runtime.exp.DatabaseException in project Lucee by lucee.

the class Ansi92 method select.

@Override
public Query select(Config config, String cfid, String applicationName, DatasourceConnection dc, int type, Log log, boolean createTableIfNotExist) throws PageException {
    String strType = VariableInterpreter.scopeInt2String(type);
    Query query = null;
    SQL sqlSelect = new SQLImpl("select data from " + PREFIX + "_" + strType + "_data where cfid=? and name=? and expires > ?", new SQLItem[] { new SQLItemImpl(cfid, Types.VARCHAR), new SQLItemImpl(applicationName, Types.VARCHAR), new SQLItemImpl(now(config), Types.VARCHAR) });
    PageContext pc = ThreadLocalPageContext.get();
    try {
        query = new QueryImpl(pc, dc, sqlSelect, -1, -1, null, "query");
    } catch (DatabaseException de) {
        if (dc == null || !createTableIfNotExist)
            throw de;
        // table does not exist???
        try {
            SQL sql = createSQL(dc, DataSourceUtil.isMySQL(dc) ? "longtext" : "ntext", strType);
            ScopeContext.info(log, sql.toString());
            new QueryImpl(pc, dc, sql, -1, -1, null, "query");
        } catch (DatabaseException _de) {
            // don't like "ntext", try text
            try {
                SQL sql = createSQL(dc, "text", strType);
                ScopeContext.info(log, sql.toString());
                new QueryImpl(pc, dc, sql, -1, -1, null, "query");
            } catch (DatabaseException __de) {
                // don't like text, try "memo"
                try {
                    SQL sql = createSQL(dc, "memo", strType);
                    ScopeContext.info(log, sql.toString());
                    new QueryImpl(pc, dc, sql, -1, -1, null, "query");
                } catch (DatabaseException ___de) {
                    // don't like "memo", try clob
                    try {
                        SQL sql = createSQL(dc, "clob", strType);
                        ScopeContext.info(log, sql.toString());
                        new QueryImpl(pc, dc, sql, -1, -1, null, "query");
                    } catch (DatabaseException ____de) {
                        ___de.initCause(__de);
                        __de.initCause(_de);
                        _de.initCause(de);
                        // we could not create the table, so there seem to be an other ecception we cannot solve
                        DatabaseException exp = new DatabaseException("Unable to select from your client storage database, and was also unable to create the tables. Here's the exceptions we encountered.", null, null, dc);
                        exp.initCause(de);
                        throw exp;
                    }
                }
            }
        }
        query = new QueryImpl(pc, dc, sqlSelect, -1, -1, null, "query");
    }
    ScopeContext.info(log, sqlSelect.toString());
    return query;
}
Also used : SQLImpl(lucee.runtime.db.SQLImpl) QueryImpl(lucee.runtime.type.QueryImpl) Query(lucee.runtime.type.Query) SQLItemImpl(lucee.runtime.db.SQLItemImpl) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) DatabaseException(lucee.runtime.exp.DatabaseException) SQL(lucee.runtime.db.SQL)

Example 22 with DatabaseException

use of lucee.runtime.exp.DatabaseException in project Lucee by lucee.

the class Transaction method doCatch.

@Override
public void doCatch(Throwable t) throws Throwable {
    ExceptionUtil.rethrowIfNecessary(t);
    if (innerTag || ignore)
        throw t;
    DataSourceManager manager = pageContext.getDataSourceManager();
    try {
        manager.rollback();
    } catch (DatabaseException e) {
    // print.printST(e);
    }
    throw t;
}
Also used : DataSourceManager(lucee.runtime.db.DataSourceManager) DatabaseException(lucee.runtime.exp.DatabaseException)

Example 23 with DatabaseException

use of lucee.runtime.exp.DatabaseException in project Lucee by lucee.

the class QueryImpl method addColumn.

@Override
public boolean addColumn(Collection.Key columnName, Array content, int type) throws DatabaseException {
    // disconnectCache();
    // TODO Meta type
    content = (Array) Duplicator.duplicate(content, false);
    if (getIndexFromKey(columnName) != -1)
        throw new DatabaseException("column name [" + columnName.getString() + "] already exist", null, sql, null);
    if (content.size() != getRecordcount()) {
        // throw new DatabaseException("array for the new column has not the same size like the query (arrayLen!=query.recordcount)");
        if (content.size() > getRecordcount())
            addRow(content.size() - getRecordcount());
        else
            content.setEL(getRecordcount(), "");
    }
    QueryColumnImpl[] newColumns = new QueryColumnImpl[columns.length + 1];
    Collection.Key[] newColumnNames = new Collection.Key[columns.length + 1];
    boolean logUsage = false;
    for (int i = 0; i < columns.length; i++) {
        newColumns[i] = columns[i];
        newColumnNames[i] = columnNames[i];
        if (!logUsage && columns[i] instanceof DebugQueryColumn)
            logUsage = true;
    }
    newColumns[columns.length] = new QueryColumnImpl(this, columnName, content, type);
    newColumnNames[columns.length] = columnName;
    columns = newColumns;
    columnNames = newColumnNames;
    columncount++;
    if (logUsage)
        enableShowQueryUsage();
    return true;
}
Also used : DatabaseException(lucee.runtime.exp.DatabaseException)

Example 24 with DatabaseException

use of lucee.runtime.exp.DatabaseException in project Lucee by lucee.

the class DatasourceConnectionPool method loadDatasourceConnection.

private DatasourceConnectionImpl loadDatasourceConnection(Config config, DataSource ds, String user, String pass) throws PageException {
    Connection conn = null;
    try {
        conn = ds.getConnection(config, user, pass);
        conn.setAutoCommit(true);
    } catch (SQLException e) {
        throw new DatabaseException(e, null);
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
    return new DatasourceConnectionImpl(conn, ds, user, pass);
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseException(lucee.runtime.exp.DatabaseException) DatabaseException(lucee.runtime.exp.DatabaseException) SQLException(java.sql.SQLException) PageException(lucee.runtime.exp.PageException)

Example 25 with DatabaseException

use of lucee.runtime.exp.DatabaseException in project Lucee by lucee.

the class DatasourceManagerImpl method getConnection.

@Override
public DatasourceConnection getConnection(PageContext pc, DataSource ds, String user, String pass) throws PageException {
    if (autoCommit) {
        return config.getDatasourceConnectionPool().getDatasourceConnection(ThreadLocalPageContext.getConfig(pc), ds, user, pass);
    }
    pc = ThreadLocalPageContext.get(pc);
    // DatasourceConnection newDC = _getConnection(pc,ds,user,pass);
    DatasourceConnectionPro existingDC = null;
    try {
        existingDC = (DatasourceConnectionPro) transConns.get(ds);
        // first time that datasource is used within this transaction
        if (existingDC == null) {
            DatasourceConnection newDC = config.getDatasourceConnectionPool().getDatasourceConnection(config, ds, user, pass);
            newDC.getConnection().setAutoCommit(false);
            if (isolation != Connection.TRANSACTION_NONE)
                newDC.getConnection().setTransactionIsolation(isolation);
            transConns.put(ds, newDC);
            return newDC;
        }
        // we have already the same datasource but with different credentials
        if (!DatasourceConnectionImpl.equals(existingDC, ds, user, pass)) {
            if (QOQ_DATASOURCE_NAME.equalsIgnoreCase(ds.getName()))
                return existingDC;
            throw new DatabaseException("can't use different connections to the same datasource inside a single transaction.", null, null, existingDC);
        }
        // make sure we have auto commit disabled TODO i dont think this is necessary anymore
        if (existingDC.isAutoCommit()) {
            existingDC.setAutoCommit(false);
        }
        return existingDC;
    } catch (SQLException e) {
        throw new DatabaseException(e, null, existingDC);
    // ExceptionHandler.printStackTrace(e);
    }
// return newDC;
}
Also used : ORMDatasourceConnection(lucee.runtime.orm.ORMDatasourceConnection) SQLException(java.sql.SQLException) DatabaseException(lucee.runtime.exp.DatabaseException)

Aggregations

DatabaseException (lucee.runtime.exp.DatabaseException)37 SQLException (java.sql.SQLException)16 PageException (lucee.runtime.exp.PageException)9 QueryImpl (lucee.runtime.type.QueryImpl)9 DatasourceConnection (lucee.runtime.db.DatasourceConnection)8 ORMDatasourceConnection (lucee.runtime.orm.ORMDatasourceConnection)6 Key (lucee.runtime.type.Collection.Key)6 Pair (lucee.commons.lang.Pair)4 ConfigImpl (lucee.runtime.config.ConfigImpl)4 SQLImpl (lucee.runtime.db.SQLImpl)4 ApplicationException (lucee.runtime.exp.ApplicationException)4 Connection (java.sql.Connection)3 ArrayList (java.util.ArrayList)3 Log (lucee.commons.io.log.Log)3 SQLItemImpl (lucee.runtime.db.SQLItemImpl)3 DeprecatedException (lucee.runtime.exp.DeprecatedException)3 PageRuntimeException (lucee.runtime.exp.PageRuntimeException)3 Collection (lucee.runtime.type.Collection)3 Query (lucee.runtime.type.Query)3 QueryColumn (lucee.runtime.type.QueryColumn)3