Search in sources :

Example 6 with QBSelect

use of com.servoy.j2db.querybuilder.impl.QBSelect in project servoy-client by Servoy.

the class FoundSet method getFoundSetFilterParams.

public Object[][] getFoundSetFilterParams(String filterName) {
    List<Object[]> result = new ArrayList<Object[]>();
    for (TableFilter f : iterate(foundSetFilters)) {
        if (filterName == null || filterName.equals(f.getName())) {
            if (f.getTableFilterdefinition() instanceof DataproviderTableFilterdefinition) {
                DataproviderTableFilterdefinition tableFilterdefinition = (DataproviderTableFilterdefinition) f.getTableFilterdefinition();
                result.add(new Object[] { f.getTableName(), tableFilterdefinition.getDataprovider(), RelationItem.getOperatorAsString(tableFilterdefinition.getOperator()), tableFilterdefinition.getValue(), f.getName() });
            }
            if (f.getTableFilterdefinition() instanceof QueryTableFilterdefinition) {
                QuerySelect querySelect = ((QueryTableFilterdefinition) f.getTableFilterdefinition()).getQuerySelect();
                result.add(new Object[] { new QBSelect(fsm, fsm.getScopesScopeProvider(), fsm.getApplication().getFlattenedSolution(), fsm.getApplication().getScriptEngine().getSolutionScope(), querySelect.getTable().getDataSource(), null, AbstractBaseQuery.deepClone(querySelect, true)), f.getName() });
            }
        }
    }
    return result.toArray(new Object[result.size()][]);
}
Also used : QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) QuerySelect(com.servoy.j2db.query.QuerySelect)

Example 7 with QBSelect

use of com.servoy.j2db.querybuilder.impl.QBSelect in project servoy-client by Servoy.

the class FoundSet method loadByQuery.

public boolean loadByQuery(IQueryBuilder query) throws ServoyException {
    // check if this query is on our base table
    if (!Utils.stringSafeEquals(getDataSource(), query.getDataSource())) {
        throw new RepositoryException(// $NON-NLS-1$//$NON-NLS-2$
        "Cannot load foundset with query based on another table (" + getDataSource() + " != " + query.getDataSource() + ')').setContext(this.toString());
    }
    // makes a clone
    QuerySelect sqlSelect = ((QBSelect) query).build();
    if (sqlSelect.getColumns() == null) {
        // no columns, add pk
        // note that QBSelect.build() already returns a clone
        Iterator<Column> pkIt = ((Table) getTable()).getRowIdentColumns().iterator();
        if (!pkIt.hasNext()) {
            throw new RepositoryException(ServoyException.InternalCodes.PRIMARY_KEY_NOT_FOUND, new Object[] { getTable().getName() }).setContext(this.toString());
        }
        while (pkIt.hasNext()) {
            Column c = pkIt.next();
            sqlSelect.addColumn(c.queryColumn(sqlSelect.getTable()));
        }
    }
    Placeholder dynamicPKplaceholder = sqlSelect.getPlaceholder(new TablePlaceholderKey(sqlSelect.getTable(), SQLGenerator.PLACEHOLDER_FOUNDSET_PKS));
    if (dynamicPKplaceholder != null && dynamicPKplaceholder.isSet() && dynamicPKplaceholder.getValue() instanceof Object[]) {
        // loading from saved query, dynamic pk was replaced by array in serialization, make dynamic again
        dynamicPKplaceholder.setValue(new DynamicPkValuesArray(getSQLSheet().getTable().getRowIdentColumns(), SQLGenerator.createPKValuesDataSet(getSQLSheet().getTable().getRowIdentColumns(), (Object[][]) dynamicPKplaceholder.getValue())));
    }
    if (sqlSelect.getSorts() == null) {
        // query does not define sort, use last sorts
        fsm.getSQLGenerator().addSorts(sqlSelect, sqlSelect.getTable(), this, sheet.getTable(), lastSortColumns == null ? defaultSort : lastSortColumns, true, false);
    } else {
        // try to determine the SortColumns from the query-sort
        lastSortColumns = determineSortColumns(sqlSelect);
    }
    return loadByQuery(addFilterconditions(sqlSelect, foundSetFilters));
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) RepositoryException(com.servoy.j2db.persistence.RepositoryException) QuerySelect(com.servoy.j2db.query.QuerySelect)

Example 8 with QBSelect

use of com.servoy.j2db.querybuilder.impl.QBSelect in project servoy-client by Servoy.

the class FoundSetManager method getFoundSet.

public IFoundSet getFoundSet(IQueryBuilder query) throws ServoyException {
    QBSelect select = (QBSelect) query;
    IFoundSet fs = getNewFoundSet(select.getDataSource(), select.getQuery(), null);
    fs.loadAllRecords();
    return fs;
}
Also used : QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect)

Example 9 with QBSelect

use of com.servoy.j2db.querybuilder.impl.QBSelect in project servoy-client by Servoy.

the class FoundSetManager method getTableFilterParams.

public Object[][] getTableFilterParams(String serverName, String filterName) {
    List<Object[]> result = new ArrayList<Object[]>();
    for (TableFilter f : iterate(tableFilterParams.get(serverName))) {
        if (filterName == null || filterName.equals(f.getName())) {
            if (f.getTableFilterdefinition() instanceof DataproviderTableFilterdefinition) {
                DataproviderTableFilterdefinition tableFilterdefinition = (DataproviderTableFilterdefinition) f.getTableFilterdefinition();
                result.add(new Object[] { f.getTableName(), tableFilterdefinition.getDataprovider(), RelationItem.getOperatorAsString(tableFilterdefinition.getOperator()), tableFilterdefinition.getValue(), f.getName() });
            }
            if (f.getTableFilterdefinition() instanceof QueryTableFilterdefinition) {
                QuerySelect querySelect = ((QueryTableFilterdefinition) f.getTableFilterdefinition()).getQuerySelect();
                result.add(new Object[] { new QBSelect(this, getScopesScopeProvider(), getApplication().getFlattenedSolution(), getApplication().getScriptEngine().getSolutionScope(), querySelect.getTable().getDataSource(), null, AbstractBaseQuery.deepClone(querySelect, true)), f.getName() });
            }
        }
    }
    return result.toArray(new Object[result.size()][]);
}
Also used : QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) QuerySelect(com.servoy.j2db.query.QuerySelect)

Example 10 with QBSelect

use of com.servoy.j2db.querybuilder.impl.QBSelect in project servoy-client by Servoy.

the class JSDatabaseManager method js_getSQL.

/**
 * Returns the internal SQL which defines the specified (related)foundset.
 * Optionally, the foundset and table filter params can be excluded in the sql (includeFilters=false).
 * Make sure to set the applicable filters when the sql is used in a loadRecords() call.
 * When the founset is in find mode, the find conditions are included in the resulting query.
 *
 * @sample var sql = databaseManager.getSQL(foundset)
 *
 * @param foundsetOrQBSelect The JSFoundset or QBSelect to get the sql for.
 * @param includeFilters include the foundset and table filters.
 *
 * @return String representing the sql of the JSFoundset.
 */
public String js_getSQL(Object foundsetOrQBSelect, boolean includeFilters) throws ServoyException {
    checkAuthorized();
    if (foundsetOrQBSelect instanceof IFoundSetInternal && ((IFoundSetInternal) foundsetOrQBSelect).getTable() != null) {
        try {
            QuerySet querySet = getQuerySet(((IFoundSetInternal) foundsetOrQBSelect).getCurrentStateQuery(true, false), includeFilters);
            StringBuilder sql = new StringBuilder();
            QueryString[] prepares = querySet.getPrepares();
            for (int i = 0; prepares != null && i < prepares.length; i++) {
            // TODO parameters from updates and cleanups
            // sql.append(updates[i].getSql());
            // sql.append("\n"); //$NON-NLS-1$
            }
            sql.append(querySet.getSelect().getSql());
            QueryString[] cleanups = querySet.getCleanups();
            for (int i = 0; cleanups != null && i < cleanups.length; i++) {
            // TODO parameters from updates and cleanups
            // sql.append("\n"); //$NON-NLS-1$
            // sql.append(cleanups[i].getSql());
            }
            return sql.toString();
        } catch (Exception e) {
            Debug.error(e);
        }
    } else if (foundsetOrQBSelect instanceof QBSelect) {
        try {
            QuerySelect select = ((QBSelect) foundsetOrQBSelect).build();
            if (select.getColumns() == null) {
                // no columns, add pk
                // note that QBSelect.build() already returns a clone
                ITable table = application.getFoundSetManager().getTable(select.getTable().getDataSource());
                Iterator<Column> pkIt = ((Table) table).getRowIdentColumns().iterator();
                if (!pkIt.hasNext()) {
                    throw new RepositoryException(ServoyException.InternalCodes.PRIMARY_KEY_NOT_FOUND, new Object[] { table.getName() });
                }
                while (pkIt.hasNext()) {
                    Column c = pkIt.next();
                    select.addColumn(c.queryColumn(select.getTable()));
                }
            }
            QuerySet querySet = getQuerySet(select, includeFilters);
            return querySet.getSelect().getSql();
        } catch (RemoteException e) {
            Debug.error(e);
        }
    }
    return null;
}
Also used : BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) QueryString(com.servoy.j2db.persistence.QueryString) QuerySet(com.servoy.j2db.persistence.QuerySet) RepositoryException(com.servoy.j2db.persistence.RepositoryException) QuerySelect(com.servoy.j2db.query.QuerySelect) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) SQLException(java.sql.SQLException) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect) QueryColumn(com.servoy.j2db.query.QueryColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) QBColumn(com.servoy.j2db.querybuilder.impl.QBColumn) Iterator(java.util.Iterator) ITable(com.servoy.j2db.persistence.ITable) NativeObject(org.mozilla.javascript.NativeObject) RemoteException(java.rmi.RemoteException)

Aggregations

QBSelect (com.servoy.j2db.querybuilder.impl.QBSelect)10 QuerySelect (com.servoy.j2db.query.QuerySelect)7 Column (com.servoy.j2db.persistence.Column)3 RepositoryException (com.servoy.j2db.persistence.RepositoryException)3 QueryColumn (com.servoy.j2db.query.QueryColumn)3 ServoyException (com.servoy.j2db.util.ServoyException)3 IBaseColumn (com.servoy.base.persistence.IBaseColumn)2 BaseQueryTable (com.servoy.base.query.BaseQueryTable)2 ApplicationException (com.servoy.j2db.ApplicationException)2 ITable (com.servoy.j2db.persistence.ITable)2 QuerySet (com.servoy.j2db.persistence.QuerySet)2 Table (com.servoy.j2db.persistence.Table)2 QueryTable (com.servoy.j2db.query.QueryTable)2 QBColumn (com.servoy.j2db.querybuilder.impl.QBColumn)2 RemoteException (java.rmi.RemoteException)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2