Search in sources :

Example 1 with IQuerySelectValue

use of com.servoy.j2db.query.IQuerySelectValue in project servoy-client by Servoy.

the class FoundSetManager method getViewFoundSet.

@SuppressWarnings("nls")
@Override
public ViewFoundSet getViewFoundSet(String name, QBSelect query) {
    if (query.getQuery().getColumns() == null || query.getQuery().getColumns().size() == 0) {
        throw new RuntimeException("Can't create a ViewFoundset with name: " + name + " and query  " + query + " that has no columns");
    }
    String dataSource = DataSourceUtils.createViewDataSource(name);
    ViewFoundSet vfs = new ViewFoundSet(dataSource, query.build(), application.getFoundSetManager(), pkChunkSize);
    // if this datasource defintion is created already in the developer then we need to check if the query columns are correctly matching it.
    ServoyJSONObject columnsDef = null;
    Iterator<TableNode> tblIte = application.getFlattenedSolution().getTableNodes(dataSource);
    while (tblIte.hasNext() && columnsDef == null) {
        TableNode tn = tblIte.next();
        columnsDef = tn.getColumns();
        if (columnsDef != null) {
            TableDef def = DatabaseUtils.deserializeTableInfo(columnsDef);
            for (ColumnInfoDef col : def.columnInfoDefSet) {
                IQuerySelectValue selectValue = getSelectvalue(query, col.name);
                if (selectValue == null) {
                    Debug.error("Column " + col.name + " of type " + col.columnType.toString() + " defined in view datasource '" + dataSource + "' was not found in the provided query.");
                    return null;
                }
                BaseColumnType columnType = selectValue.getColumnType();
                // relax the mapping on default Servoy types
                if (columnType != null && Column.mapToDefaultType(columnType.getSqlType()) != Column.mapToDefaultType(col.columnType.getSqlType())) {
                    Debug.error("Column type for column '" + col.name + " of type " + col.columnType.toString() + "' defined in view datasource '" + dataSource + "' does not match the one " + columnType + " provided in the query.");
                    return null;
                }
            }
        }
    }
    return vfs;
}
Also used : ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) TableNode(com.servoy.j2db.persistence.TableNode) TableDef(com.servoy.j2db.util.xmlxport.TableDef) ColumnInfoDef(com.servoy.j2db.util.xmlxport.ColumnInfoDef) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) BaseColumnType(com.servoy.base.query.BaseColumnType)

Example 2 with IQuerySelectValue

use of com.servoy.j2db.query.IQuerySelectValue in project servoy-client by Servoy.

the class ViewFoundSet method loadAllRecordsImpl.

private void loadAllRecordsImpl() throws ServoyException {
    String serverName = DataSourceUtils.getDataSourceServerName(select.getTable().getDataSource());
    String transaction_id = manager.getTransactionID(serverName);
    try {
        IDataSet ds = manager.getApplication().getDataServer().performQuery(manager.getApplication().getClientID(), serverName, transaction_id, select, null, manager.getTableFilterParams(serverName, select), select.isUnique(), 0, currentChunkSize, IDataServer.FOUNDSET_LOAD_QUERY);
        refresh = false;
        ArrayList<IQuerySelectValue> cols = select.getColumns();
        int currentSize = records.size();
        List<ViewRecord> old = records;
        records = new ArrayList<>(ds.getRowCount());
        pkByDatasourceCache.clear();
        String[] colNames = columnNames.values().toArray(new String[columnNames.size()]);
        try (FireCollector fireCollector = getFireCollector()) {
            for (int i = 0; i < ds.getRowCount(); i++) {
                Object[] rowData = ds.getRow(i);
                if (i < currentSize) {
                    ViewRecord current = old.get(i);
                    records.add(current);
                    current.updateValues(colNames, rowData);
                } else {
                    records.add(new ViewRecord(colNames, rowData, this));
                }
            }
        }
        hasMore = ds.hadMoreRows();
        fireDifference(currentSize, records.size());
    } catch (RemoteException e) {
        throw new RepositoryException(e);
    }
}
Also used : FireCollector.getFireCollector(com.servoy.j2db.dataprocessing.FireCollector.getFireCollector) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IConstantsObject(com.servoy.j2db.scripting.IConstantsObject) RemoteException(java.rmi.RemoteException) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 3 with IQuerySelectValue

use of com.servoy.j2db.query.IQuerySelectValue in project servoy-client by Servoy.

the class QBResult method getColumns.

/**
 * returns an array with all the columns that will be in the select of this query.
 * can return empty array. Then the system will auto append the pk when this query is used.
 * @sample
 * var columns = query.result.getColumns();
 *
 * @return An array of QBColumn thats in the select of this query.
 */
@JSFunction
public QBColumn[] getColumns() {
    ArrayList<IQuerySelectValue> columns = getParent().getQuery().getColumns();
    QBColumn[] result = new QBColumn[columns == null ? 0 : columns.size()];
    for (int i = 0; i < result.length; i++) {
        IQuerySelectValue selectValue = columns.get(i);
        if (selectValue instanceof QueryColumn) {
            result[i] = new QBColumn(getRoot(), getParent(), selectValue);
        } else if (selectValue instanceof QueryAggregate) {
            result[i] = new QBAggregate(getRoot(), getParent(), selectValue, ((QueryAggregate) selectValue).getType(), ((QueryAggregate) selectValue).getQuantifier());
        } else if (selectValue instanceof QueryFunction) {
            result[i] = new QBFunction(getRoot(), getParent(), ((QueryFunction) selectValue).getFunction(), ((QueryFunction) selectValue).getArgs());
        }
    }
    return result;
}
Also used : QueryAggregate(com.servoy.j2db.query.QueryAggregate) QueryColumn(com.servoy.j2db.query.QueryColumn) QueryFunction(com.servoy.j2db.query.QueryFunction) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 4 with IQuerySelectValue

use of com.servoy.j2db.query.IQuerySelectValue in project servoy-client by Servoy.

the class QBResult method add.

public QBResult add(IQueryBuilderColumn column, String alias) {
    if (column == null) {
        throw new RuntimeException("Cannot add null or undefined column to a query");
    }
    IQuerySelectValue querySelectValue = ((QBColumn) column).getQuerySelectValue();
    getParent().getQuery().addColumn(alias == null ? querySelectValue : querySelectValue.asAlias(alias));
    return this;
}
Also used : IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 5 with IQuerySelectValue

use of com.servoy.j2db.query.IQuerySelectValue in project servoy-client by Servoy.

the class QBSelect method createOperand.

IQuerySelectValue createOperand(Object value, BaseColumnType columnType, int flags) {
    if (value instanceof QBColumn) {
        return ((QBColumn) value).getQuerySelectValue();
    }
    Object val = value;
    if (value instanceof QBParameter) {
        TablePlaceholderKey key = ((QBParameter) value).getPlaceholderKey();
        Placeholder placeholder = null;
        if (query != null) {
            placeholder = query.getPlaceholder(key);
        }
        val = placeholder == null ? new Placeholder(key) : placeholder;
    } else if (columnType == null) {
        if (value instanceof Date && !(value instanceof Timestamp) && !(value instanceof Time)) {
            // make sure a date is a timestamp
            val = new Timestamp(((Date) value).getTime());
        }
    } else if (!(value instanceof IQuerySelectValue)) {
        // convert the value (especially UUID) to the type of the column
        val = getAsRightType(value, columnType, flags);
    }
    if (val instanceof IQuerySelectValue) {
        return (IQuerySelectValue) val;
    }
    return new QueryColumnValue(val, null);
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.util.Date) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Aggregations

IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)30 QueryColumn (com.servoy.j2db.query.QueryColumn)18 Column (com.servoy.j2db.persistence.Column)15 QuerySelect (com.servoy.j2db.query.QuerySelect)15 ArrayList (java.util.ArrayList)15 BaseQueryTable (com.servoy.base.query.BaseQueryTable)14 QueryTable (com.servoy.j2db.query.QueryTable)11 IColumn (com.servoy.j2db.persistence.IColumn)10 SafeArrayList (com.servoy.j2db.util.SafeArrayList)10 RepositoryException (com.servoy.j2db.persistence.RepositoryException)9 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)7 SetCondition (com.servoy.j2db.query.SetCondition)7 ITable (com.servoy.j2db.persistence.ITable)6 IQuerySort (com.servoy.j2db.query.IQuerySort)6 QuerySort (com.servoy.j2db.query.QuerySort)6 IBaseColumn (com.servoy.base.persistence.IBaseColumn)5 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)5 Placeholder (com.servoy.j2db.query.Placeholder)5 RemoteException (java.rmi.RemoteException)5 BaseColumnType (com.servoy.base.query.BaseColumnType)4