Search in sources :

Example 6 with DatabaseException

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

the class Update method createSQL.

/**
 * @param keys primary Keys
 * @return return SQL String for update
 * @throws PageException
 */
private SQL createSQL(DatasourceConnection dc, String[] keys, Struct meta) throws PageException {
    String[] fields = null;
    Form form = pageContext.formScope();
    if (formfields != null)
        fields = ListUtil.toStringArray(ListUtil.listToArrayRemoveEmpty(formfields, ','));
    else
        fields = CollectionUtil.keysAsString(pageContext.formScope());
    StringBuffer set = new StringBuffer();
    StringBuffer where = new StringBuffer();
    ArrayList setItems = new ArrayList();
    ArrayList whereItems = new ArrayList();
    String field;
    for (int i = 0; i < fields.length; i++) {
        field = StringUtil.trim(fields[i], null);
        if (StringUtil.startsWithIgnoreCase(field, "form."))
            field = field.substring(5);
        if (!field.equalsIgnoreCase("fieldnames")) {
            if (ArrayUtil.indexOfIgnoreCase(keys, field) == -1) {
                if (set.length() == 0)
                    set.append(" set ");
                else
                    set.append(",");
                set.append(field);
                set.append("=?");
                ColumnInfo ci = (ColumnInfo) meta.get(field);
                if (ci != null)
                    setItems.add(new SQLItemImpl(form.get(field, null), ci.getType()));
                else
                    setItems.add(new SQLItemImpl(form.get(field, null)));
            } else {
                if (where.length() == 0)
                    where.append(" where ");
                else
                    where.append(" and ");
                where.append(field);
                where.append("=?");
                whereItems.add(new SQLItemImpl(form.get(field, null)));
            }
        }
    }
    if ((setItems.size() + whereItems.size()) == 0)
        return null;
    if (whereItems.size() == 0)
        throw new DatabaseException("can't find primary keys [" + ListUtil.arrayToList(keys, ",") + "] of table [" + tablename + "] in form scope", null, null, dc);
    StringBuffer sql = new StringBuffer();
    sql.append("update ");
    if (tablequalifier != null && tablequalifier.length() > 0) {
        sql.append(tablequalifier);
        sql.append('.');
    }
    if (tableowner != null && tableowner.length() > 0) {
        sql.append(tableowner);
        sql.append('.');
    }
    sql.append(tablename);
    sql.append(set);
    sql.append(where);
    return new SQLImpl(sql.toString(), arrayMerge(setItems, whereItems));
}
Also used : SQLImpl(lucee.runtime.db.SQLImpl) Form(lucee.runtime.type.scope.Form) ArrayList(java.util.ArrayList) SQLItemImpl(lucee.runtime.db.SQLItemImpl) DatabaseException(lucee.runtime.exp.DatabaseException)

Example 7 with DatabaseException

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

the class QueryColumnImpl method removeRow.

@Override
public Object removeRow(int row) throws DatabaseException {
    // query.disconnectCache();
    if (row < 1 || row > size)
        throw new DatabaseException("invalid row number [" + row + "]", "valid rows goes from 1 to " + size, null, null);
    synchronized (sync) {
        Object o = data[row - 1];
        for (int i = row; i < size; i++) {
            data[i - 1] = data[i];
        }
        size--;
        if (NullSupportHelper.full())
            return o;
        return o == null ? "" : o;
    }
}
Also used : SerializableObject(lucee.commons.lang.SerializableObject) DatabaseException(lucee.runtime.exp.DatabaseException)

Example 8 with DatabaseException

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

the class QueryColumnImpl method set.

@Override
public Object set(int row, Object value) throws DatabaseException {
    // query.disconnectCache();
    if (row < 1)
        throw new DatabaseException("invalid row number [" + row + "]", "valid row numbers a greater or equal to one", null, null);
    if (row > size) {
        if (size == 0)
            throw new DatabaseException("cannot set a value to a empty query, you first have to add a row", null, null, null);
        throw new DatabaseException("invalid row number [" + row + "]", "valid row numbers goes from 1 to " + size, null, null);
    }
    synchronized (sync) {
        value = reDefineType(value);
        data[row - 1] = value;
        return value;
    }
}
Also used : DatabaseException(lucee.runtime.exp.DatabaseException)

Example 9 with DatabaseException

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

the class QueryImpl method execute.

private static void execute(PageContext pc, DatasourceConnection dc, SQL sql, int maxrow, int fetchsize, TimeSpan timeout, boolean createUpdateData, boolean allowToCachePreperadeStatement, QueryImpl qry, QueryResult qr, Collection.Key keyName) throws PageException {
    TimeZone tz = ThreadLocalPageContext.getTimeZone(pc);
    // check if datasource support Generated Keys
    boolean createGeneratedKeys = createUpdateData;
    if (createUpdateData) {
        if (!dc.supportsGetGeneratedKeys())
            createGeneratedKeys = false;
    }
    // check SQL Restrictions
    if (dc.getDatasource().hasSQLRestriction()) {
        QueryUtil.checkSQLRestriction(dc, sql);
    }
    Statement stat = null;
    // Stopwatch stopwatch=new Stopwatch();
    long start = System.nanoTime();
    // stopwatch.start();
    boolean hasResult = false;
    // boolean closeStatement=true;
    try {
        SQLItem[] items = sql.getItems();
        if (items.length == 0) {
            stat = dc.getConnection().createStatement();
            setAttributes(stat, maxrow, fetchsize, timeout);
            // some driver do not support second argument
            // hasResult=createGeneratedKeys?stat.execute(sql.getSQLString(),Statement.RETURN_GENERATED_KEYS):stat.execute(sql.getSQLString());
            hasResult = QueryUtil.execute(pc, stat, createGeneratedKeys, sql);
        } else {
            // some driver do not support second argument
            PreparedStatement preStat = dc.getPreparedStatement(sql, createGeneratedKeys, allowToCachePreperadeStatement);
            // closeStatement=false;
            stat = preStat;
            setAttributes(preStat, maxrow, fetchsize, timeout);
            setItems(pc, ThreadLocalPageContext.getTimeZone(pc), preStat, items);
            hasResult = QueryUtil.execute(pc, preStat);
        }
        int uc;
        // ResultSet res;
        do {
            if (hasResult) {
                // if(fillResult(dc,res, maxrow, true,createGeneratedKeys,tz))break;
                if (fillResult(qry, qr, keyName, dc, stat.getResultSet(), maxrow, true, createGeneratedKeys, tz))
                    break;
            } else if ((uc = setUpdateCount(qry != null ? qry : qr, stat)) != -1) {
                if (uc > 0 && createGeneratedKeys && qry != null)
                    qry.setGeneratedKeys(dc, stat, tz);
            } else
                break;
            try {
                // hasResult=stat.getMoreResults(Statement.CLOSE_CURRENT_RESULT);
                hasResult = stat.getMoreResults();
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
                break;
            }
        } while (true);
    } catch (SQLException e) {
        throw new DatabaseException(e, sql, dc);
    } catch (Throwable e) {
        ExceptionUtil.rethrowIfNecessary(e);
        throw Caster.toPageException(e);
    } finally {
        // if(closeStatement)
        DBUtil.closeEL(stat);
    }
    if (qry != null) {
        qry.exeTime = System.nanoTime() - start;
        if (qry.columncount == 0) {
            if (qry.columnNames == null)
                qry.columnNames = new Collection.Key[0];
            if (qry.columns == null)
                qry.columns = new QueryColumnImpl[0];
        }
    } else {
        qr.setExecutionTime(System.nanoTime() - start);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) SQLItem(lucee.runtime.db.SQLItem) TimeZone(java.util.TimeZone) DatabaseException(lucee.runtime.exp.DatabaseException)

Example 10 with DatabaseException

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

the class DatasourceManagerImpl method rollback.

@Override
public void rollback() 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().rollback();
        } 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)

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