Search in sources :

Example 56 with RepositoryException

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

the class UsedDataProviderTracker method usedFromRecord.

/**
 * Used name from Record,
 * @param record may be null (for global relations)
 * @param name
 */
protected void usedFromRecord(IRecordInternal record, String name) {
    IRecordInternal currentRecord = record;
    try {
        // $NON-NLS-1$
        String[] parts = name.split("\\.");
        for (String part : parts) {
            Relation relation = flattenedSolution.getRelation(part);
            if (relation != null) {
                // calc depends on the relation, add a dependency for the primary data providers for the relation
                IDataProvider[] primaryDataProviders = relation.getPrimaryDataProviders(flattenedSolution);
                for (IDataProvider prim : primaryDataProviders) {
                    if (prim instanceof LiteralDataprovider)
                        continue;
                    String primdp = prim.getDataProviderID();
                    if (ScopesUtils.isVariableScope(primdp)) {
                        // global
                        usedGlobal(primdp);
                    } else {
                        // column
                        if (currentRecord != null) {
                            if (currentRecord.getRawData() == null) {
                                if (currentRecord instanceof PrototypeState) {
                                    Debug.trace("Calculation '" + name + "' depends on field '" + part + "' from PrototypeState " + currentRecord);
                                } else {
                                    // should not happen
                                    Debug.error("Unexpected state: calculation '" + name + "' depends on column '" + part + "' from record without pk: " + currentRecord, new IllegalStateException());
                                }
                            } else {
                                usedColumn(currentRecord.getParentFoundSet().getDataSource(), currentRecord.getRawData().getPKHashKey(), primdp);
                            }
                        }
                    }
                }
                IFoundSetInternal foundSet = null;
                if (currentRecord != null)
                    foundSet = currentRecord.getRelatedFoundSet(relation.getName());
                currentRecord = null;
                if (foundSet instanceof RelatedFoundSet) {
                    usedRelatedFoundSet(relation.getName(), (RelatedFoundSet) foundSet);
                    currentRecord = foundSet.getRecord(foundSet.getSelectedIndex());
                }
            } else {
                if (currentRecord != null) {
                    IFoundSetInternal foundSet = currentRecord.getParentFoundSet();
                    if (foundSet.getSQLSheet().containsAggregate(part)) {
                        // aggregate
                        usedAggregate(foundSet, part);
                    } else {
                        // field or calc
                        if (currentRecord.has(part)) {
                            if (currentRecord.getRawData() == null) {
                                if (currentRecord instanceof PrototypeState) {
                                    Debug.trace("Calculation '" + name + "' depends on field '" + part + "' from PrototypeState " + currentRecord);
                                } else {
                                    // should not happen
                                    Debug.error("Unexpected state: calculation '" + name + "' depends on field '" + part + "' from record without pk: " + currentRecord, new IllegalStateException());
                                }
                            } else {
                                usedColumn(foundSet.getDataSource(), currentRecord.getRawData().getPKHashKey(), part);
                            }
                        }
                    }
                }
                return;
            }
            if (currentRecord == null) {
                return;
            }
        }
    } catch (RepositoryException e) {
        Debug.error(e);
    }
}
Also used : Relation(com.servoy.j2db.persistence.Relation) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) RelatedFoundSet(com.servoy.j2db.dataprocessing.RelatedFoundSet) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IDataProvider(com.servoy.j2db.persistence.IDataProvider) LiteralDataprovider(com.servoy.j2db.persistence.LiteralDataprovider) PrototypeState(com.servoy.j2db.dataprocessing.PrototypeState)

Example 57 with RepositoryException

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

the class SortModel method setValue.

void setValue(IApplication app, String notused) {
    application = app;
    try {
        FormManager fm = (FormManager) application.getFormManager();
        FormController fc = fm.getCurrentMainShowingFormController();
        if (fc != null) {
            Form form = fc.getForm();
            ITable t = application.getFoundSetManager().getTable(form.getDataSource());
            if (t != null) {
                List<SortColumn> list = application.getFoundSetManager().getSortColumns(t, form.getInitialSort());
                init(app, t, list);
            }
        }
    } catch (RepositoryException e) {
        Debug.error(e);
    }
}
Also used : FormController(com.servoy.j2db.FormController) FormManager(com.servoy.j2db.FormManager) Form(com.servoy.j2db.persistence.Form) ITable(com.servoy.j2db.persistence.ITable) RepositoryException(com.servoy.j2db.persistence.RepositoryException) SortColumn(com.servoy.j2db.dataprocessing.SortColumn)

Example 58 with RepositoryException

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

the class SQLGenerator method createRelatedCondition.

public static ISQLCondition createRelatedCondition(IServiceProvider app, Relation relation, QueryTable foreignTable) throws RepositoryException {
    IDataProvider[] primary = relation.getPrimaryDataProviders(app.getFlattenedSolution());
    Column[] foreign = relation.getForeignColumns(app.getFlattenedSolution());
    int[] operators = relation.getOperators();
    IQuerySelectValue[] keys = new IQuerySelectValue[primary.length];
    int[] swapped = new int[primary.length];
    for (int x = 0; x < primary.length; x++) {
        // need all keys as columns on the left side......
        int operator = RelationItem.swapOperator(operators[x]);
        if (operator == -1) {
            throw new RepositoryException("Cannot swap relation operator for relation " + relation.getName());
        }
        // column = ? construct
        IQuerySelectValue key = foreign[x].queryColumn(foreignTable);
        // When we have a text and non-text column we can cast the non-text column to string
        int primaryType = primary[x].getDataProviderType();
        int foreignType = mapToDefaultType(key.getColumn().getColumnType());
        if (!"uuid".equalsIgnoreCase(key.getColumn().getNativeTypename()) && foreignType == IColumnTypes.TEXT && primaryType != IColumnTypes.TEXT && primaryType != 0) {
            // key is text, value is non-text, cast the value to text when we supply it
            operator |= IBaseSQLCondition.CAST_TO_MODIFIER;
        } else if (primaryType == IColumnTypes.TEXT && foreignType != IColumnTypes.TEXT) {
            // value is text, key is non-text, cast the key to text
            key = new QueryFunction(cast, new IQuerySelectValue[] { key, new QueryColumnValue(IQueryConstants.TYPE_STRING, null, true) }, null);
        }
        keys[x] = key;
        swapped[x] = operator;
    }
    return new SetCondition(swapped, keys, new Placeholder(createRelationKeyPlaceholderKey(foreignTable, relation.getName())), true);
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IDataProvider(com.servoy.j2db.persistence.IDataProvider) SetCondition(com.servoy.j2db.query.SetCondition) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) QueryFunction(com.servoy.j2db.query.QueryFunction) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 59 with RepositoryException

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

the class SQLGenerator method createJoin.

/**
 * Join clause for this relation.
 */
public static ISQLTableJoin createJoin(IDataProviderHandler flattenedSolution, IRelation relation, BaseQueryTable primaryTable, BaseQueryTable foreignTable, boolean permanentJoin, final IGlobalValueEntry provider) throws RepositoryException {
    if (relation instanceof AbstractBase) {
        ISQLTableJoin queryJoin = ((AbstractBase) relation).getRuntimeProperty(Relation.RELATION_JOIN);
        if (queryJoin != null) {
            // a query join was defined for this relation, just relink the tables for the first and last in the joins
            queryJoin = deepClone(queryJoin);
            queryJoin = AbstractBaseQuery.relinkTable(queryJoin.getPrimaryTable(), primaryTable, queryJoin);
            queryJoin = AbstractBaseQuery.relinkTable(queryJoin.getForeignTable(), foreignTable, queryJoin);
            // update the placeholders for globals
            queryJoin.acceptVisitor(new IVisitor() {

                public Object visit(Object o) {
                    if (o instanceof Placeholder && ((Placeholder) o).getKey() instanceof ObjectPlaceholderKey) {
                        Object value = provider.getDataProviderValue(((ObjectPlaceholderKey<int[]>) ((Placeholder) o).getKey()).getName());
                        int[] args = ((ObjectPlaceholderKey<int[]>) ((Placeholder) o).getKey()).getObject();
                        int dataProviderType = args[0];
                        int flags = args[1];
                        if (value == null) {
                            return ValueFactory.createNullValue(dataProviderType);
                        }
                        return Column.getAsRightType(dataProviderType, flags, value, Integer.MAX_VALUE, false, false);
                    }
                    return o;
                }
            });
            return queryJoin;
        }
    }
    // build a join from the relation items
    IDataProvider[] primary = relation.getPrimaryDataProviders(flattenedSolution);
    Column[] foreign = relation.getForeignColumns(flattenedSolution);
    int[] operators = relation.getOperators();
    AndCondition joinCondition = new AndCondition();
    for (int x = 0; x < primary.length; x++) {
        Column primaryColumn = null;
        // check if stored script calc or table column
        if (primary[x] instanceof ScriptCalculation) {
            ScriptCalculation sc = ((ScriptCalculation) primary[x]);
            // null when not stored
            primaryColumn = sc.getTable().getColumn(sc.getName());
        } else if (primary[x] instanceof Column) {
            primaryColumn = (Column) primary[x];
        }
        QueryColumn foreignColumn = foreign[x].queryColumn(foreignTable);
        Object value;
        if (primaryColumn == null) {
            if (primary[x] instanceof LiteralDataprovider) {
                value = ((LiteralDataprovider) primary[x]).getValue();
                value = foreign[x].getAsRightType(value);
            } else {
                value = provider.getDataProviderValue(primary[x].getDataProviderID());
                if (value == null) {
                    value = ValueFactory.createNullValue(primary[x].getDataProviderType());
                } else if (value instanceof Placeholder) {
                    if (((Placeholder) value).getKey() instanceof ObjectPlaceholderKey<?>) {
                        ((ObjectPlaceholderKey) ((Placeholder) value).getKey()).setObject(new int[] { primary[x].getDataProviderType(), primary[x].getFlags() });
                    }
                } else {
                    value = Column.getAsRightType(primary[x].getDataProviderType(), primary[x].getFlags(), value, Integer.MAX_VALUE, false, false);
                }
            }
        } else // table type, can be stored calc
        {
            value = primaryColumn.queryColumn(primaryTable);
        }
        // all operators are swappable because only relation operators in RelationItem.RELATION_OPERATORS can be defined.
        // NOTE: elements in joinCondition MUST be CompareConditions (expected in QueryGenerator and SQLGenerator.createConditionFromFindState)
        joinCondition.addCondition(new CompareCondition(RelationItem.swapOperator(operators[x]), foreignColumn, value));
    }
    if (joinCondition.getConditions().size() == 0) {
        // $NON-NLS-1$
        throw new RepositoryException("Missing join condition in relation " + relation.getName());
    }
    return new QueryJoin(relation.getName(), primaryTable, foreignTable, joinCondition, relation.getJoinType(), permanentJoin);
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) IVisitor(com.servoy.j2db.util.visitor.IVisitor) AbstractBase(com.servoy.j2db.persistence.AbstractBase) QueryJoin(com.servoy.j2db.query.QueryJoin) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IDataProvider(com.servoy.j2db.persistence.IDataProvider) LiteralDataprovider(com.servoy.j2db.persistence.LiteralDataprovider) AndCondition(com.servoy.j2db.query.AndCondition) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) CompareCondition(com.servoy.j2db.query.CompareCondition) ObjectPlaceholderKey(com.servoy.j2db.query.ObjectPlaceholderKey)

Example 60 with RepositoryException

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

the class SQLGenerator method createTableSQL.

private SQLSheet createTableSQL(String dataSource, boolean cache) throws ServoyException {
    if (dataSource == null) {
        return createNoTableSQL(cache);
    }
    Table table = (Table) application.getFoundSetManager().getTable(dataSource);
    if (table == null) {
        // $NON-NLS-1$
        throw new RepositoryException("Cannot create sql: table not found for data source '" + dataSource + '\'');
    }
    SQLSheet retval = new SQLSheet(application, table.getServerName(), table);
    // never remove this line, due to recursive behaviour, register a state when immediately!
    if (cache)
        cachedDataSourceSQLSheets.put(dataSource, retval);
    QueryTable queryTable = new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema());
    QuerySelect select = new QuerySelect(queryTable);
    QueryDelete delete = new QueryDelete(queryTable);
    QueryInsert insert = new QueryInsert(queryTable);
    QueryUpdate update = new QueryUpdate(queryTable);
    List<Column> columns = new ArrayList<Column>();
    Iterator<Column> it1 = table.getColumns().iterator();
    while (it1.hasNext()) {
        Column c = it1.next();
        ColumnInfo ci = c.getColumnInfo();
        if (ci != null && ci.isExcluded()) {
            continue;
        }
        columns.add(c);
    }
    List<String> requiredDataProviderIDs = new ArrayList<String>();
    Iterator<Column> pks = table.getRowIdentColumns().iterator();
    if (!pks.hasNext()) {
        throw new RepositoryException(ServoyException.InternalCodes.PRIMARY_KEY_NOT_FOUND, new Object[] { table.getName() });
    }
    List<QueryColumn> pkQueryColumns = new ArrayList<QueryColumn>();
    while (pks.hasNext()) {
        Column column = pks.next();
        if (!columns.contains(column))
            columns.add(column);
        requiredDataProviderIDs.add(column.getDataProviderID());
        pkQueryColumns.add(column.queryColumn(queryTable));
    }
    Iterator<Column> it2 = columns.iterator();
    select.setColumns(makeQueryColumns(it2, queryTable, insert));
    SetCondition pkSelect = new SetCondition(IBaseSQLCondition.EQUALS_OPERATOR, pkQueryColumns.toArray(new QueryColumn[pkQueryColumns.size()]), new Placeholder(new TablePlaceholderKey(queryTable, PLACEHOLDER_PRIMARY_KEY)), true);
    select.setCondition(CONDITION_SEARCH, pkSelect);
    delete.setCondition(deepClone(pkSelect));
    update.setCondition(deepClone(pkSelect));
    // fill dataprovider map
    List<String> dataProviderIDsDilivery = new ArrayList<String>();
    for (Column col : columns) {
        dataProviderIDsDilivery.add(col.getDataProviderID());
    }
    retval.addSelect(select, dataProviderIDsDilivery, requiredDataProviderIDs, null);
    retval.addDelete(delete, requiredDataProviderIDs);
    retval.addInsert(insert, dataProviderIDsDilivery);
    retval.addUpdate(update, dataProviderIDsDilivery, requiredDataProviderIDs);
    // related stuff
    createAggregates(retval, queryTable);
    return retval;
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) QueryDelete(com.servoy.j2db.query.QueryDelete) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) RepositoryException(com.servoy.j2db.persistence.RepositoryException) SetCondition(com.servoy.j2db.query.SetCondition) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) QueryInsert(com.servoy.j2db.query.QueryInsert) QueryUpdate(com.servoy.j2db.query.QueryUpdate)

Aggregations

RepositoryException (com.servoy.j2db.persistence.RepositoryException)116 RemoteException (java.rmi.RemoteException)45 QuerySelect (com.servoy.j2db.query.QuerySelect)25 ITable (com.servoy.j2db.persistence.ITable)22 JSFunction (org.mozilla.javascript.annotations.JSFunction)22 ServoyException (com.servoy.j2db.util.ServoyException)21 Column (com.servoy.j2db.persistence.Column)19 ArrayList (java.util.ArrayList)19 BaseQueryTable (com.servoy.base.query.BaseQueryTable)16 Table (com.servoy.j2db.persistence.Table)16 QueryTable (com.servoy.j2db.query.QueryTable)16 FlattenedSolution (com.servoy.j2db.FlattenedSolution)14 Point (java.awt.Point)14 IDataProvider (com.servoy.j2db.persistence.IDataProvider)13 QueryColumn (com.servoy.j2db.query.QueryColumn)13 IColumn (com.servoy.j2db.persistence.IColumn)12 ApplicationException (com.servoy.j2db.ApplicationException)11 ScriptNameValidator (com.servoy.j2db.persistence.ScriptNameValidator)10 SafeArrayList (com.servoy.j2db.util.SafeArrayList)10 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)8