Search in sources :

Example 11 with DbException

use of com.wplatform.ddal.message.DbException in project jdbc-shards by wplatform.

the class Command method executeQuery.

/**
     * Execute a query and return the result.
     * This method prepares everything and calls {@link #query(int)} finally.
     *
     * @param maxrows    the maximum number of rows to return
     * @param scrollable if the result set must be scrollable (ignored)
     * @return the result set
     */
@Override
public ResultInterface executeQuery(int maxrows, boolean scrollable) {
    startTime = 0;
    Database database = session.getDatabase();
    Object sync = session;
    boolean callStop = true;
    synchronized (sync) {
        session.setCurrentCommand(this);
        try {
            while (true) {
                try {
                    return query(maxrows);
                } catch (DbException e) {
                    throw e;
                } catch (OutOfMemoryError e) {
                    callStop = false;
                    // there is a serious problem:
                    // the transaction may be applied partially
                    // in this case we need to panic:
                    // close the database
                    database.shutdownImmediately();
                    throw DbException.convert(e);
                } catch (Throwable e) {
                    throw DbException.convert(e);
                }
            }
        } catch (DbException e) {
            e = e.addSQL(sql);
            SQLException s = e.getSQLException();
            if (s.getErrorCode() == ErrorCode.OUT_OF_MEMORY) {
                callStop = false;
                database.shutdownImmediately();
                throw e;
            }
            throw e;
        } finally {
            if (callStop) {
                stop();
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Database(com.wplatform.ddal.engine.Database) DbException(com.wplatform.ddal.message.DbException)

Example 12 with DbException

use of com.wplatform.ddal.message.DbException in project jdbc-shards by wplatform.

the class Command method executeUpdate.

@Override
public int executeUpdate() {
    Database database = session.getDatabase();
    Object sync = session;
    boolean callStop = true;
    synchronized (sync) {
        Session.Savepoint rollback = session.setSavepoint();
        session.setCurrentCommand(this);
        try {
            while (true) {
                try {
                    return update();
                } catch (DbException e) {
                    throw e;
                } catch (OutOfMemoryError e) {
                    callStop = false;
                    database.shutdownImmediately();
                    throw DbException.convert(e);
                } catch (Throwable e) {
                    throw DbException.convert(e);
                }
            }
        } catch (DbException e) {
            e = e.addSQL(sql);
            SQLException s = e.getSQLException();
            if (s.getErrorCode() == ErrorCode.OUT_OF_MEMORY) {
                callStop = false;
                database.shutdownImmediately();
                throw e;
            }
            if (s.getErrorCode() == ErrorCode.DEADLOCK_1) {
                session.rollback();
            } else {
                session.rollbackTo(rollback, false);
            }
            throw e;
        } finally {
            try {
                if (callStop) {
                    stop();
                }
            } finally {
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Database(com.wplatform.ddal.engine.Database) Session(com.wplatform.ddal.engine.Session) DbException(com.wplatform.ddal.message.DbException)

Example 13 with DbException

use of com.wplatform.ddal.message.DbException in project jdbc-shards by wplatform.

the class ReplaceExecutor method executeUpdate.

@Override
public int executeUpdate() {
    Column[] columns = prepared.getColumns();
    TableMate table = castTableMate(prepared.getTable());
    ArrayList<Expression[]> list = prepared.getList();
    table.check();
    int count;
    session.getUser().checkRight(table, Right.INSERT);
    session.getUser().checkRight(table, Right.UPDATE);
    prepared.setCurrentRowNumber(0);
    if (list.size() > 0) {
        count = 0;
        for (int x = 0, size = list.size(); x < size; x++) {
            prepared.setCurrentRowNumber(x + 1);
            Expression[] expr = list.get(x);
            Row newRow = table.getTemplateRow();
            for (int i = 0, len = columns.length; i < len; i++) {
                Column c = columns[i];
                int index = c.getColumnId();
                Expression e = expr[i];
                if (e != null) {
                    // e can be null (DEFAULT)
                    try {
                        Value v = c.convert(e.getValue(session));
                        newRow.setValue(index, v);
                    } catch (DbException ex) {
                        throw prepared.setRow(ex, count, Prepared.getSQL(expr));
                    }
                }
            }
            replace(newRow);
            count++;
        }
    } else {
        Query query = prepared.getQuery();
        ResultInterface rows = query.query(0);
        count = 0;
        while (rows.next()) {
            count++;
            Value[] r = rows.currentRow();
            Row newRow = table.getTemplateRow();
            prepared.setCurrentRowNumber(count);
            for (int j = 0; j < columns.length; j++) {
                Column c = columns[j];
                int index = c.getColumnId();
                try {
                    Value v = c.convert(r[j]);
                    newRow.setValue(index, v);
                } catch (DbException ex) {
                    throw prepared.setRow(ex, count, Prepared.getSQL(r));
                }
            }
            replace(newRow);
        }
        rows.close();
    }
    return count;
}
Also used : ResultInterface(com.wplatform.ddal.result.ResultInterface) Query(com.wplatform.ddal.command.dml.Query) DbException(com.wplatform.ddal.message.DbException) Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Row(com.wplatform.ddal.result.Row) SearchRow(com.wplatform.ddal.result.SearchRow)

Example 14 with DbException

use of com.wplatform.ddal.message.DbException in project jdbc-shards by wplatform.

the class UpdateExecutor method executeUpdate.

@Override
public int executeUpdate() {
    TableFilter tableFilter = prepared.getTableFilter();
    TableMate table = castTableMate(tableFilter.getTable());
    List<Column> columns = prepared.getColumns();
    Map<Column, Expression> valueMap = prepared.getExpressionMap();
    table.check();
    session.getUser().checkRight(table, Right.UPDATE);
    Row updateRow = table.getTemplateRow();
    for (int i = 0, size = columns.size(); i < size; i++) {
        Column c = columns.get(i);
        Expression e = valueMap.get(c);
        int index = c.getColumnId();
        if (e != null) {
            // e can be null (DEFAULT)
            e = e.optimize(session);
            try {
                Value v = c.convert(e.getValue(session));
                updateRow.setValue(index, v);
            } catch (DbException ex) {
                ex.addSQL("evaluate expression " + e.getSQL());
                throw ex;
            }
        }
    }
    return updateRow(table, updateRow, tableFilter.getIndexConditions());
}
Also used : TableFilter(com.wplatform.ddal.dbobject.table.TableFilter) Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Row(com.wplatform.ddal.result.Row) SearchRow(com.wplatform.ddal.result.SearchRow) DbException(com.wplatform.ddal.message.DbException)

Aggregations

DbException (com.wplatform.ddal.message.DbException)14 TableMate (com.wplatform.ddal.dbobject.table.TableMate)8 Column (com.wplatform.ddal.dbobject.table.Column)7 Value (com.wplatform.ddal.value.Value)6 Query (com.wplatform.ddal.command.dml.Query)5 Row (com.wplatform.ddal.result.Row)5 SearchRow (com.wplatform.ddal.result.SearchRow)5 Expression (com.wplatform.ddal.command.expression.Expression)4 ResultInterface (com.wplatform.ddal.result.ResultInterface)3 SQLException (java.sql.SQLException)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)2 Database (com.wplatform.ddal.engine.Database)2 Prepared (com.wplatform.ddal.command.Prepared)1 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)1 DefineCommand (com.wplatform.ddal.command.ddl.DefineCommand)1 Insert (com.wplatform.ddal.command.dml.Insert)1 Parameter (com.wplatform.ddal.command.expression.Parameter)1 IndexMate (com.wplatform.ddal.dbobject.index.IndexMate)1 Sequence (com.wplatform.ddal.dbobject.schema.Sequence)1 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)1