Search in sources :

Example 56 with Column

use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.

the class FlattenedSolution method getDataProviderForTable.

/**
 * Returns script calculations and aggregates and columns
 */
public IDataProvider getDataProviderForTable(ITable table, String dataProviderID) throws RepositoryException {
    if (table == null || dataProviderID == null)
        return null;
    // check for calculations first because of stored calculations
    Map<String, IDataProvider> dps = getAllDataProvidersForTable(table);
    IDataProvider dataProvider = null;
    if (dps != null) {
        dataProvider = dps.get(dataProviderID);
    }
    if (dataProvider != null)
        return dataProvider;
    Column column = table.getColumn(Ident.generateNormalizedName(dataProviderID));
    if (column != null) {
        return column;
    }
    return null;
}
Also used : QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) IDataProvider(com.servoy.j2db.persistence.IDataProvider)

Example 57 with Column

use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.

the class FoundSet method js_selectRecord.

/**
 * Select the record based on pk data.
 * Note that if the foundset has not loaded the record with the pk, selectrecord will fail.
 *
 * In case of a table with a composite key, the pk sequence must match the alphabetical
 * ordering of the pk column names.
 *
 * @sample %%prefix%%foundset.selectRecord(pkid1,pkid2,pkidn);//pks must be alphabetically set! It is also possible to use an array as parameter.
 *
 * @param pkid1 primary key
 *
 * @param pkid2 optional second primary key (in case of composite primary key)
 *
 * @param pkidn optional nth primary key
 *
 * @return true if succeeded.
 */
public boolean js_selectRecord(Object[] vargs) {
    if (sheet.getTable() == null) {
        return false;
    }
    List<Object> args = new ArrayList<Object>();
    if (vargs != null && vargs.length > 0) {
        List<Column> cols = sheet.getTable().getRowIdentColumns();
        for (int i = 0; i < Math.min(vargs.length, cols.size()); i++) {
            Column c = cols.get(i);
            args.add(c.getAsRightType(vargs[i]));
        }
        return selectRecord(args.toArray(), true);
    }
    return false;
}
Also used : QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList)

Example 58 with Column

use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.

the class FoundSet method getCalculationValue.

/**
 * Found set is using scriptengine to recalculate the specified calculation,check first with containsCalculation before calling
 */
public Object getCalculationValue(IRecordInternal state, String dataProviderID, Object[] vargs, UsedDataProviderTracker usedDataProviderTracker) {
    UsedDataProviderTracker current = null;
    try {
        Object obj;
        TableScope tableScope = (TableScope) fsm.getScriptEngine().getTableScope(sheet.getTable());
        tableScope.setArguments(vargs);
        current = tableScope.setUsedDataProviderTracker(usedDataProviderTracker);
        Scriptable previous = tableScope.getPrototype();
        try {
            // make sure its set correctly
            tableScope.setPrototype((Scriptable) state);
            obj = tableScope.getCalculationValue(dataProviderID, tableScope);
            if (// fix for postgress
            obj instanceof Byte) {
                obj = Integer.valueOf(((Byte) obj).intValue());
            } else {
                Column column = sheet.getTable().getColumn(dataProviderID);
                if (column != null) {
                    // TODO: check case with stored calc on column with column converter
                    if (column.getScale() > 0 && column.getDataProviderType() == IColumnTypes.NUMBER && obj != null) {
                        // if rounding results in the old value we do not have to save.
                        try {
                            obj = Utils.roundNumber(obj, column.getLength(), true);
                        } catch (Exception e) {
                            // $NON-NLS-1$
                            Debug.error("Could not round stored calculation '" + dataProviderID + '\'', e);
                        }
                    }
                }
            }
        // TODO: in developer we must check if the return type matches the one specified on a calculation otherwise relations will not work in some cases
        } finally {
            tableScope.setPrototype(previous);
            tableScope.setUsedDataProviderTracker(current);
        }
        return obj;
    } catch (Exception ex) {
        // fsm.getApplication().reportJSError(Messages.getString("servoy.error.executingCalculation",new Object[] {dataProviderID,getTable().getName(),ex.getMessage()}),ex) ;	 //$NON-NLS-1$
        fsm.getApplication().reportJSError(ex.getMessage(), ex);
        // $NON-NLS-1$
        Debug.error("error executing calc: " + dataProviderID, ex);
        return null;
    }
}
Also used : TableScope(com.servoy.j2db.scripting.TableScope) UsedDataProviderTracker(com.servoy.j2db.scripting.UsedDataProviderTracker) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) Scriptable(org.mozilla.javascript.Scriptable) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 59 with Column

use of com.servoy.j2db.persistence.Column 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 60 with Column

use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.

the class FoundSetManager method createDataproviderTableFilterdefinition.

public DataproviderTableFilterdefinition createDataproviderTableFilterdefinition(ITable table, String dataprovider, String operator, Object val) throws ServoyException {
    if (dataprovider == null || operator == null) {
        return null;
    }
    int op = RelationItem.getValidOperator(operator.trim(), IBaseSQLCondition.ALL_DEFINED_OPERATORS, IBaseSQLCondition.ALL_MODIFIERS);
    if (op == -1) {
        return null;
    }
    Object value = val;
    if (value instanceof Wrapper) {
        value = ((Wrapper) value).unwrap();
    }
    if (table != null) {
        Column column = ((Table) table).getColumn(dataprovider);
        if (column == null) {
            return null;
        }
        value = convertFilterValue(table, column, value);
    }
    return new DataproviderTableFilterdefinition(dataprovider, op, value);
}
Also used : Wrapper(org.mozilla.javascript.Wrapper) JSONSerializerWrapper(com.servoy.j2db.util.serialize.JSONSerializerWrapper) BaseQueryTable(com.servoy.base.query.BaseQueryTable) Table(com.servoy.j2db.persistence.Table) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Aggregations

Column (com.servoy.j2db.persistence.Column)76 QueryColumn (com.servoy.j2db.query.QueryColumn)44 IColumn (com.servoy.j2db.persistence.IColumn)37 RepositoryException (com.servoy.j2db.persistence.RepositoryException)32 IBaseColumn (com.servoy.base.persistence.IBaseColumn)31 QuerySelect (com.servoy.j2db.query.QuerySelect)29 ArrayList (java.util.ArrayList)29 QueryTable (com.servoy.j2db.query.QueryTable)27 ITable (com.servoy.j2db.persistence.ITable)23 BaseQueryTable (com.servoy.base.query.BaseQueryTable)22 Table (com.servoy.j2db.persistence.Table)22 ServoyException (com.servoy.j2db.util.ServoyException)21 SafeArrayList (com.servoy.j2db.util.SafeArrayList)19 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)18 RemoteException (java.rmi.RemoteException)17 ColumnInfo (com.servoy.j2db.persistence.ColumnInfo)16 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)14 IDataProvider (com.servoy.j2db.persistence.IDataProvider)12 Relation (com.servoy.j2db.persistence.Relation)12 Placeholder (com.servoy.j2db.query.Placeholder)12