Search in sources :

Example 31 with DatabaseException

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

the class LikeCompareJRE method createPattern.

private static Pattern createPattern(SQL sql, String wildcard, String escape) throws PageException {
    Pattern pattern = (Pattern) patterns.get(wildcard + escape);
    if (pattern != null)
        return pattern;
    char esc = 0;
    if (!StringUtil.isEmpty(escape)) {
        esc = escape.charAt(0);
        if (escape.length() > 1)
            throw new DatabaseException("Invalid escape character [" + escape + "] has been specified in a LIKE conditional", null, sql, null);
    }
    StringBuilder sb = new StringBuilder(wildcard.length());
    int len = wildcard.length();
    // boolean isEscape=false;
    char c;
    for (int i = 0; i < len; i++) {
        c = wildcard.charAt(i);
        if (c == esc) {
            if (i + 1 == len)
                throw new DatabaseException("Invalid Escape Sequence. Valid sequence pairs for this escape character are: [" + esc + "%] or [" + esc + "_]", null, sql, null);
            c = wildcard.charAt(++i);
            if (c == '%')
                sb.append(c);
            else if (c == '_')
                sb.append(c);
            else
                throw new DatabaseException("Invalid Escape Sequence [" + esc + "" + c + "]. Valid sequence pairs for this escape character are: [" + esc + "%] or [" + esc + "_]", null, sql, null);
        } else {
            if (c == '%')
                sb.append(".*");
            else if (c == '_')
                sb.append('.');
            else if (specials.indexOf(c) != -1)
                sb.append('\\').append(c);
            else
                sb.append(c);
        }
    }
    try {
        patterns.put(wildcard + escape, pattern = Pattern.compile(sb.toString(), Pattern.DOTALL));
    } catch (PatternSyntaxException e) {
        throw Caster.toPageException(e);
    }
    return pattern;
}
Also used : Pattern(java.util.regex.Pattern) DatabaseException(lucee.runtime.exp.DatabaseException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 32 with DatabaseException

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

the class QoQ method execute.

/**
 * execute a SQL Statement against CFML Scopes
 */
public Query execute(PageContext pc, SQL sql, Selects selects, int maxrows) throws PageException {
    Column[] orders = selects.getOrderbys();
    Select[] arrSelects = selects.getSelects();
    QueryImpl target = new QueryImpl(new Collection.Key[0], 0, "query", sql);
    for (int i = 0; i < arrSelects.length; i++) {
        arrSelects[i].getFroms();
        Column[] froms = arrSelects[i].getFroms();
        if (froms.length > 1)
            throw new DatabaseException("can only work with single tables yet", null, sql, null);
        executeSingle(pc, arrSelects[i], getSingleTable(pc, froms[0]), target, arrSelects.length > 1 ? -1 : maxrows, sql, orders.length > 0);
    }
    // Order By
    if (orders.length > 0) {
        order(target, orders);
        if (maxrows > -1)
            target.cutRowsTo(maxrows);
    }
    // Distinct
    if (selects.isDistinct()) {
        // order to filter
        order(target, selects.getDistincts());
        // print.e(selects.getDistincts());
        Key[] _keys = target.getColumnNames();
        QueryColumn[] columns = new QueryColumn[_keys.length];
        for (int i = 0; i < columns.length; i++) {
            columns[i] = target.getColumn(_keys[i]);
        }
        int i;
        Object l, r;
        outer: for (int row = target.getRecordcount(); row > 1; row--) {
            for (i = 0; i < columns.length; i++) {
                l = columns[i].get(row, null);
                r = columns[i].get(row - 1, null);
                if (l == null || r == null) {
                    if (l != r)
                        continue outer;
                } else if (!Operator.equals(l, r, true))
                    continue outer;
            }
            target.removeRow(row);
        }
    }
    order(target, orders);
    return target;
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) QueryColumn(lucee.runtime.type.QueryColumn) Column(lucee.runtime.sql.exp.Column) QueryColumn(lucee.runtime.type.QueryColumn) Select(lucee.runtime.sql.Select) Collection(lucee.runtime.type.Collection) DatabaseException(lucee.runtime.exp.DatabaseException) Key(lucee.runtime.type.Collection.Key)

Example 33 with DatabaseException

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

the class QoQ method executeSingle.

private void executeSingle(PageContext pc, Select select, Query qr, QueryImpl target, int maxrows, SQL sql, boolean hasOrders) throws PageException {
    ValueNumber oTop = select.getTop();
    if (oTop != null) {
        int top = (int) oTop.getValueAsDouble();
        if (maxrows == -1 || maxrows > top)
            maxrows = top;
    }
    int recCount = qr.getRecordcount();
    Expression[] expSelects = select.getSelects();
    int selCount = expSelects.length;
    Map<Collection.Key, Object> selects = new HashMap<Collection.Key, Object>();
    Iterator<Key> it;
    Key k;
    // headers
    for (int i = 0; i < selCount; i++) {
        Expression expSelect = expSelects[i];
        if (expSelect.getAlias().equals("*")) {
            it = qr.keyIterator();
            while (it.hasNext()) {
                k = it.next();
                selects.put(k, k);
                queryAddColumn(target, k, qr.getColumn(k).getType());
            }
        } else {
            Key alias = Caster.toKey(expSelect.getAlias());
            selects.put(alias, expSelect);
            int type = Types.OTHER;
            if (expSelect instanceof ColumnExpression)
                type = qr.getColumn(Caster.toKey(((ColumnExpression) expSelect).getColumnName())).getType();
            queryAddColumn(target, alias, type);
        }
    }
    Collection.Key[] headers = selects.keySet().toArray(new Collection.Key[selects.size()]);
    // loop records
    Operation where = select.getWhere();
    boolean hasMaxrow = maxrows > -1 && !hasOrders;
    // get target columns
    QueryColumn[] trgColumns = new QueryColumn[headers.length];
    Object[] trgValues = new Object[headers.length];
    for (int cell = 0; cell < headers.length; cell++) {
        trgColumns[cell] = target.getColumn(headers[cell]);
        trgValues[cell] = selects.get(headers[cell]);
    }
    for (int row = 1; row <= recCount; row++) {
        sql.setPosition(0);
        if (hasMaxrow && maxrows <= target.getRecordcount())
            break;
        boolean useRow = where == null || Caster.toBooleanValue(executeExp(pc, sql, qr, where, row));
        if (useRow) {
            target.addRow(1);
            for (int cell = 0; cell < headers.length; cell++) {
                // Object value = selects.get(headers[cell]);
                trgColumns[cell].set(target.getRecordcount(), getValue(pc, sql, qr, row, headers[cell], trgValues[cell]));
            /*target.setAt(
							headers[cell],
							target.getRecordcount(),
							getValue(pc,sql,qr,row,headers[cell],trgValues[cell])
						);*/
            }
        }
    }
    // Group By
    if (select.getGroupbys().length > 0)
        throw new DatabaseException("group by are not supported at the moment", null, sql, null);
    if (select.getHaving() != null)
        throw new DatabaseException("having is not supported at the moment", null, sql, null);
}
Also used : HashMap(java.util.HashMap) Operation(lucee.runtime.sql.exp.op.Operation) BracketExpression(lucee.runtime.sql.exp.BracketExpression) ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) QueryColumn(lucee.runtime.type.QueryColumn) ValueNumber(lucee.runtime.sql.exp.value.ValueNumber) Collection(lucee.runtime.type.Collection) DatabaseException(lucee.runtime.exp.DatabaseException) Key(lucee.runtime.type.Collection.Key)

Example 34 with DatabaseException

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

the class ConfigImpl method getDataSource.

@Override
public DataSource getDataSource(String datasource) throws DatabaseException {
    DataSource ds = (datasource == null) ? null : (DataSource) datasources.get(datasource.toLowerCase());
    if (ds != null)
        return ds;
    // create error detail
    DatabaseException de = new DatabaseException("datasource [" + datasource + "] doesn't exist", null, null, null);
    de.setDetail(ExceptionUtil.createSoundexDetail(datasource, datasources.keySet().iterator(), "datasource names"));
    de.setAdditional(KeyConstants._Datasource, datasource);
    throw de;
}
Also used : DatabaseException(lucee.runtime.exp.DatabaseException) DataSource(lucee.runtime.db.DataSource)

Example 35 with DatabaseException

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

the class Query_ method call.

public static Query call(PageContext pc, Object[] arr) throws DatabaseException {
    String[] names = new String[arr.length];
    Array[] columns = new Array[arr.length];
    int count = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] instanceof FunctionValue) {
            FunctionValue vf = (FunctionValue) arr[i];
            if (vf.getValue() instanceof Array) {
                names[count] = vf.getNameAsString();
                columns[count] = (Array) vf.getValue();
                count++;
            } else
                throw new DatabaseException("invalid argument for function query, only array as value are allowed", "example: query(column1:array(1,2,3))", null, null);
        } else
            throw new DatabaseException("invalid argument for function query, only named argument are allowed", "example: query(column1:array(1,2,3))", null, null);
    }
    Query query = new QueryImpl(names, columns, "query");
    return query;
}
Also used : Array(lucee.runtime.type.Array) QueryImpl(lucee.runtime.type.QueryImpl) Query(lucee.runtime.type.Query) FunctionValue(lucee.runtime.type.FunctionValue) 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