Search in sources :

Example 26 with DatabaseException

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

the class DatasourceManagerImpl method commit.

@Override
public void commit() throws DatabaseException {
    if (autoCommit || transConns.size() == 0)
        return;
    Iterator<DatasourceConnection> it = this.transConns.values().iterator();
    DatasourceConnection dc = null;
    Pair<DatasourceConnection, Exception> pair = null;
    while (it.hasNext()) {
        dc = it.next();
        try {
            dc.getConnection().commit();
        } catch (Exception e) {
            // we only keep the first exception
            if (pair == null) {
                pair = new Pair<DatasourceConnection, Exception>(dc, e);
            }
        }
    }
    if (pair != null) {
        if (pair.getValue() instanceof SQLException) {
            throw new DatabaseException((SQLException) pair.getValue(), pair.getName());
        }
        throw new PageRuntimeException(pair.getValue());
    }
}
Also used : ORMDatasourceConnection(lucee.runtime.orm.ORMDatasourceConnection) SQLException(java.sql.SQLException) PageRuntimeException(lucee.runtime.exp.PageRuntimeException) DatabaseException(lucee.runtime.exp.DatabaseException) DatabaseException(lucee.runtime.exp.DatabaseException) DeprecatedException(lucee.runtime.exp.DeprecatedException) SQLException(java.sql.SQLException) PageRuntimeException(lucee.runtime.exp.PageRuntimeException) PageException(lucee.runtime.exp.PageException) Pair(lucee.commons.lang.Pair)

Example 27 with DatabaseException

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

the class DatasourceManagerImpl method end.

public void end(boolean onlyORM) {
    autoCommit = true;
    Pair<DatasourceConnection, Exception> pair = null;
    if (transConns.size() > 0) {
        Map<DataSource, DatasourceConnection> tmp = null;
        if (onlyORM)
            tmp = new HashMap<DataSource, DatasourceConnection>();
        Iterator<Entry<DataSource, DatasourceConnection>> it = this.transConns.entrySet().iterator();
        DatasourceConnection dc;
        Entry<DataSource, DatasourceConnection> entry;
        while (it.hasNext()) {
            entry = it.next();
            dc = entry.getValue();
            try {
                if (onlyORM && !(dc.getConnection() instanceof ORMConnection)) {
                    tmp.put(entry.getKey(), entry.getValue());
                    continue;
                }
                dc.getConnection().setAutoCommit(true);
            } catch (Exception e) {
                // we only keep the first exception
                if (pair == null) {
                    pair = new Pair<DatasourceConnection, Exception>(dc, e);
                }
            }
            releaseConnection(null, dc);
        }
        transConns.clear();
        if (onlyORM)
            transConns = tmp;
    }
    this.isolation = Connection.TRANSACTION_NONE;
    if (pair != null) {
        if (pair.getValue() instanceof SQLException) {
            throw new PageRuntimeException(new DatabaseException((SQLException) pair.getValue(), pair.getName()));
        }
        throw new PageRuntimeException(pair.getValue());
    }
}
Also used : ORMDatasourceConnection(lucee.runtime.orm.ORMDatasourceConnection) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) DatabaseException(lucee.runtime.exp.DatabaseException) DeprecatedException(lucee.runtime.exp.DeprecatedException) SQLException(java.sql.SQLException) PageRuntimeException(lucee.runtime.exp.PageRuntimeException) PageException(lucee.runtime.exp.PageException) Entry(java.util.Map.Entry) PageRuntimeException(lucee.runtime.exp.PageRuntimeException) DatabaseException(lucee.runtime.exp.DatabaseException) ORMConnection(lucee.runtime.orm.ORMConnection) Pair(lucee.commons.lang.Pair)

Example 28 with DatabaseException

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

the class DatasourceManagerImpl method savepoint.

@Override
public void savepoint() throws DatabaseException {
    if (autoCommit || transConns.size() == 0)
        return;
    Iterator<DatasourceConnection> it = this.transConns.values().iterator();
    DatasourceConnection dc = null;
    Pair<DatasourceConnection, Exception> pair = null;
    while (it.hasNext()) {
        dc = it.next();
        try {
            dc.getConnection().setSavepoint();
        } catch (Exception e) {
            // we only keep the first exception
            if (pair == null) {
                pair = new Pair<DatasourceConnection, Exception>(dc, e);
            }
        }
    }
    if (pair != null) {
        if (pair.getValue() instanceof SQLException) {
            throw new DatabaseException((SQLException) pair.getValue(), pair.getName());
        }
        throw new PageRuntimeException(pair.getValue());
    }
}
Also used : ORMDatasourceConnection(lucee.runtime.orm.ORMDatasourceConnection) SQLException(java.sql.SQLException) PageRuntimeException(lucee.runtime.exp.PageRuntimeException) DatabaseException(lucee.runtime.exp.DatabaseException) DatabaseException(lucee.runtime.exp.DatabaseException) DeprecatedException(lucee.runtime.exp.DeprecatedException) SQLException(java.sql.SQLException) PageRuntimeException(lucee.runtime.exp.PageRuntimeException) PageException(lucee.runtime.exp.PageException) Pair(lucee.commons.lang.Pair)

Example 29 with DatabaseException

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

the class Executer method testExecute.

private Query testExecute(PageContext pc, SQL sql, Query qr, ZQuery query, int maxrows) throws PageException {
    int recCount = qr.getRecordcount();
    Vector vSelects = query.getSelect();
    int selCount = vSelects.size();
    Map<Collection.Key, Object> selects = MapFactory.<Collection.Key, Object>getConcurrentMap();
    boolean isSMS = false;
    // headers
    for (int i = 0; i < selCount; i++) {
        ZSelectItem select = (ZSelectItem) vSelects.get(i);
        if (select.isWildcard() || (isSMS = select.getColumn().equals(SQLPrettyfier.PLACEHOLDER_ASTERIX))) {
            if (!isSMS && !select.getColumn().equals("*"))
                throw new DatabaseException("can't execute this type of query at the moment", null, sql, null);
            // Collection.Key[] keys = qr.keys();
            Iterator<Key> it = qr.keyIterator();
            Key k;
            while (it.hasNext()) {
                k = it.next();
                selects.put(k, k.getString());
            }
            isSMS = false;
        } else {
            // if(SQLPrettyfier.PLACEHOLDER_COUNT.equals(select.getAlias())) select.setAlias("count");
            // if(SQLPrettyfier.PLACEHOLDER_COUNT.equals(select.getColumn())) select.setExpression(new ZConstant("count",ZConstant.COLUMNNAME));
            String alias = select.getAlias();
            String column = select.getColumn();
            if (alias == null)
                alias = column;
            alias = alias.toLowerCase();
            selects.put(KeyImpl.init(alias), select);
        }
    }
    Key[] headers = selects.keySet().toArray(new Collection.Key[selects.size()]);
    // aHeaders.toArray(new String[aHeaders.size()]);
    QueryImpl rtn = new QueryImpl(headers, 0, "query", sql);
    // loop records
    Vector orders = query.getOrderBy();
    ZExp where = query.getWhere();
    // print.out(headers);
    // int newRecCount=0;
    boolean hasMaxrow = maxrows > -1 && (orders == null || orders.size() == 0);
    for (int row = 1; row <= recCount; row++) {
        sql.setPosition(0);
        if (hasMaxrow && maxrows <= rtn.getRecordcount())
            break;
        boolean useRow = where == null || Caster.toBooleanValue(executeExp(pc, sql, qr, where, row));
        if (useRow) {
            rtn.addRow(1);
            for (int cell = 0; cell < headers.length; cell++) {
                Object value = selects.get(headers[cell]);
                rtn.setAt(headers[cell], rtn.getRecordcount(), getValue(pc, sql, qr, row, headers[cell], value));
            }
        }
    }
    // Group By
    if (query.getGroupBy() != null)
        throw new DatabaseException("group by are not supported at the moment", null, sql, null);
    // Order By
    if (orders != null && orders.size() > 0) {
        int len = orders.size();
        for (int i = len - 1; i >= 0; i--) {
            ZOrderBy order = (ZOrderBy) orders.get(i);
            ZConstant name = (ZConstant) order.getExpression();
            rtn.sort(name.getValue().toLowerCase(), order.getAscOrder() ? Query.ORDER_ASC : Query.ORDER_DESC);
        }
        if (maxrows > -1) {
            rtn.cutRowsTo(maxrows);
        }
    }
    // Distinct
    if (query.isDistinct()) {
        String[] keys = rtn.getColumns();
        QueryColumn[] columns = new QueryColumn[keys.length];
        for (int i = 0; i < columns.length; i++) {
            columns[i] = rtn.getColumn(keys[i]);
        }
        int i;
        outer: for (int row = rtn.getRecordcount(); row > 1; row--) {
            for (i = 0; i < columns.length; i++) {
                if (!Operator.equals(QueryUtil.getValue(columns[i], row), QueryUtil.getValue(columns[i], row - 1), true))
                    continue outer;
            }
            rtn.removeRow(row);
        }
    }
    // UNION // TODO support it
    ZExpression set = query.getSet();
    if (set != null) {
        ZExp op = set.getOperand(0);
        if (op instanceof ZQuery)
            throw new DatabaseException("union is not supported at the moment", null, sql, null);
    // getInvokedTables((ZQuery)op, tablesNames);
    }
    return rtn;
}
Also used : ZOrderBy(lucee.runtime.sql.old.ZOrderBy) ZExpression(lucee.runtime.sql.old.ZExpression) ZExp(lucee.runtime.sql.old.ZExp) ZQuery(lucee.runtime.sql.old.ZQuery) ZConstant(lucee.runtime.sql.old.ZConstant) QueryImpl(lucee.runtime.type.QueryImpl) ZSelectItem(lucee.runtime.sql.old.ZSelectItem) QueryColumn(lucee.runtime.type.QueryColumn) Collection(lucee.runtime.type.Collection) Vector(java.util.Vector) DatabaseException(lucee.runtime.exp.DatabaseException) Key(lucee.runtime.type.Collection.Key)

Example 30 with DatabaseException

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

the class HSQLDBHandler method execute.

/**
 * executes a query on the queries inside the cld fusion enviroment
 * @param pc Page Context
 * @param sql
 * @param maxrows
 * @return result as Query
 * @throws PageException
 * @throws PageException
 */
public Query execute(PageContext pc, SQL sql, int maxrows, int fetchsize, TimeSpan timeout) throws PageException {
    Stopwatch stopwatch = new Stopwatch(Stopwatch.UNIT_NANO);
    stopwatch.start();
    String prettySQL = null;
    Selects selects = null;
    // First Chance
    try {
        SelectParser parser = new SelectParser();
        selects = parser.parse(sql.getSQLString());
        Query q = qoq.execute(pc, sql, selects, maxrows);
        q.setExecutionTime(stopwatch.time());
        return q;
    } catch (SQLParserException spe) {
        // lucee.print.printST(spe);
        // sp
        // lucee.print.out("sql parser crash at:");
        // lucee.print.out("--------------------------------");
        // lucee.print.out(sql.getSQLString().trim());
        // lucee.print.out("--------------------------------");
        // print.e("1:"+sql.getSQLString());
        prettySQL = SQLPrettyfier.prettyfie(sql.getSQLString());
        // print.e("2:"+prettySQL);
        try {
            Query query = executer.execute(pc, sql, prettySQL, maxrows);
            query.setExecutionTime(stopwatch.time());
            return query;
        } catch (PageException ex) {
        // lucee.print.printST(ex);
        // lucee.print.out("old executor/zql crash at:");
        // lucee.print.out("--------------------------------");
        // lucee.print.out(sql.getSQLString().trim());
        // lucee.print.out("--------------------------------");
        }
    } catch (PageException e) {
    // throw e;
    // print.out("new executor crash at:");
    // print.out("--------------------------------");
    // print.out(sql.getSQLString().trim());
    // print.out("--------------------------------");
    }
    // SECOND Chance with hsqldb
    try {
        boolean isUnion = false;
        Set<String> tables = null;
        if (selects != null) {
            HSQLUtil2 hsql2 = new HSQLUtil2(selects);
            isUnion = hsql2.isUnion();
            tables = hsql2.getInvokedTables();
        } else {
            if (prettySQL == null)
                prettySQL = SQLPrettyfier.prettyfie(sql.getSQLString());
            HSQLUtil hsql = new HSQLUtil(prettySQL);
            tables = hsql.getInvokedTables();
            isUnion = hsql.isUnion();
        }
        String strSQL = StringUtil.replace(sql.getSQLString(), "[", "", false);
        strSQL = StringUtil.replace(strSQL, "]", "", false);
        sql.setSQLString(strSQL);
        return _execute(pc, sql, maxrows, fetchsize, timeout, stopwatch, tables, isUnion);
    } catch (ParseException e) {
        throw new DatabaseException(e.getMessage(), null, sql, null);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) Query(lucee.runtime.type.Query) Stopwatch(lucee.runtime.timer.Stopwatch) SQLParserException(lucee.runtime.sql.SQLParserException) Selects(lucee.runtime.sql.Selects) ParseException(lucee.runtime.sql.old.ParseException) SelectParser(lucee.runtime.sql.SelectParser) 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