Search in sources :

Example 16 with ITable

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

the class FoundSetManager method addTableFilterParam.

public boolean addTableFilterParam(String filterName, String serverName, ITable table, TableFilterdefinition tableFilterdefinition) throws ServoyException {
    TableFilter filter = new TableFilter(filterName, serverName, table == null ? null : table.getName(), table == null ? null : table.getSQLName(), tableFilterdefinition);
    List<TableFilter> params = tableFilterParams.get(serverName);
    if (params == null) {
        tableFilterParams.put(serverName, params = new ArrayList<TableFilter>());
    }
    if (// do not add the same filter, will add same AND-condition anyway
    !filter.isContainedIn(params)) {
        params.add(filter);
        for (ITable affectedtable : getFilterUpdateAffectedTables(getDataSource(table), tableFilterdefinition)) {
            fireTableEvent(affectedtable);
        }
    }
    if (Messages.isI18NTable(serverName, table != null ? table.getName() : null, application)) {
        ((ClientState) application).refreshI18NMessages(false);
    }
    return true;
}
Also used : ClientState(com.servoy.j2db.ClientState) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ITable(com.servoy.j2db.persistence.ITable)

Example 17 with ITable

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

the class FoundSetManager method createDataSourceFromQuery.

public String createDataSourceFromQuery(String name, String serverName, ISQLSelect sqlSelect, boolean useTableFilters, int maxNumberOfRowsToRetrieve, int[] types, String[] pkNames) throws ServoyException {
    if (name == null) {
        return null;
    }
    try {
        String queryTid = getTransactionID(serverName);
        String dataSource = DataSourceUtils.createInmemDataSource(name);
        ITable table = inMemDataSources.get(dataSource);
        GlobalTransaction gt = getGlobalTransaction();
        String targetTid = null;
        String targetServerName = table == null ? IServer.INMEM_SERVER : table.getServerName();
        if (gt != null) {
            targetTid = gt.getTransactionID(targetServerName);
        }
        if (table != null) {
            // temp table was used before, delete all data in it
            FoundSet foundSet = (FoundSet) getSharedFoundSet(dataSource);
            foundSet.removeLastFound();
            try {
                QueryDelete delete = new QueryDelete(new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema(), true));
                SQLStatement deleteStatement = new SQLStatement(ISQLActionTypes.DELETE_ACTION, table.getServerName(), table.getName(), null, targetTid, delete, null);
                application.getDataServer().performUpdates(application.getClientID(), new ISQLStatement[] { deleteStatement });
            } catch (Exception e) {
                Debug.log(e);
                table = null;
            }
        }
        table = application.getDataServer().insertQueryResult(application.getClientID(), serverName, queryTid, sqlSelect, useTableFilters ? getTableFilterParams(serverName, sqlSelect) : null, false, 0, maxNumberOfRowsToRetrieve, IDataServer.CUSTOM_QUERY, dataSource, table == null ? IServer.INMEM_SERVER : table.getServerName(), table == null ? null : table.getName(), /* create temp table when null */
        targetTid, ColumnType.getColumnTypes(types), pkNames);
        if (table != null) {
            inMemDataSources.put(dataSource, table);
            fireTableEvent(table);
            refreshFoundSetsFromDB(dataSource, null, false);
            return dataSource;
        }
    } catch (RemoteException e) {
        throw new RepositoryException(e);
    }
    return null;
}
Also used : QueryDelete(com.servoy.j2db.query.QueryDelete) ITable(com.servoy.j2db.persistence.ITable) RepositoryException(com.servoy.j2db.persistence.RepositoryException) RemoteException(java.rmi.RemoteException) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) MarshallException(org.jabsorb.serializer.MarshallException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 18 with ITable

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

the class FoundSetManager method getRowManager.

public synchronized RowManager getRowManager(String dataSource) throws ServoyException {
    if (getDataServer() == null) {
        // no data access yet
        return null;
    }
    ITable t = getTable(dataSource);
    if (t != null) {
        RowManager rm = rowManagers.get(dataSource);
        if (rm == null) {
            try {
                // first time this client uses this table
                getDataServer().addClientAsTableUser(application.getClientID(), t.getServerName(), t.getName());
            } catch (RemoteException e) {
                throw new RepositoryException(e);
            }
            rm = new RowManager(this, getSQLGenerator().getCachedTableSQLSheet(dataSource));
            rowManagers.put(dataSource, rm);
        }
        return rm;
    }
    return null;
}
Also used : ITable(com.servoy.j2db.persistence.ITable) RepositoryException(com.servoy.j2db.persistence.RepositoryException) RemoteException(java.rmi.RemoteException)

Example 19 with ITable

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

the class SQLGenerator method addSorts.

public void addSorts(QuerySelect sqlSelect, BaseQueryTable selectTable, IGlobalValueEntry provider, ITable table, List<SortColumn> orderByFields, boolean includeRelated, boolean permanentJoins) throws RepositoryException {
    List<Column> unusedRowidentColumns = new ArrayList<Column>(table.getRowIdentColumns());
    for (int i = 0; orderByFields != null && i < orderByFields.size(); i++) {
        SortColumn sc = orderByFields.get(i);
        // can be column or aggregate
        IColumn column = sc.getColumn();
        if (column.getDataProviderType() == MEDIA && (column.getFlags() & (IDENT_COLUMNS | UUID_COLUMN)) == 0) {
            // skip cannot sort blob columns
            continue;
        }
        SortOptions sortOptions = application.getFoundSetManager().getSortOptions(sc.getColumn());
        Relation[] relations = sc.getRelations();
        // compare on server objects, relation.foreignServerName may be different in case of duplicates
        boolean doRelatedJoin = (includeRelated && relations != null);
        if (doRelatedJoin) {
            FlattenedSolution fs = application.getFlattenedSolution();
            for (Relation relation : relations) {
                if (relation.isMultiServer() && !fs.getTable(relation.getForeignDataSource()).getServerName().equals(table.getServerName())) {
                    doRelatedJoin = false;
                    break;
                }
            }
        }
        if (doRelatedJoin) // related sort, cannot join across multiple servers
        {
            BaseQueryTable primaryQtable = selectTable;
            BaseQueryTable foreignQtable = null;
            for (Relation relation : relations) {
                // join must be re-created as it is possible to have globals involved;
                // first remove, then create it
                ISQLTableJoin join = (ISQLTableJoin) sqlSelect.getJoin(primaryQtable, relation.getName());
                if (join != null)
                    sqlSelect.getJoins().remove(join);
                if (join == null) {
                    ITable foreignTable = application.getFlattenedSolution().getTable(relation.getForeignDataSource());
                    foreignQtable = new QueryTable(foreignTable.getSQLName(), foreignTable.getDataSource(), foreignTable.getCatalog(), foreignTable.getSchema());
                } else {
                    foreignQtable = join.getForeignTable();
                }
                sqlSelect.addJoin(createJoin(application.getFlattenedSolution(), relation, primaryQtable, foreignQtable, permanentJoins, provider));
                primaryQtable = foreignQtable;
            }
            IQuerySelectValue queryColumn;
            if (column instanceof Column) {
                queryColumn = ((Column) column).queryColumn(foreignQtable);
                unusedRowidentColumns.remove(column);
            } else if (column instanceof AggregateVariable) {
                AggregateVariable aggregate = (AggregateVariable) column;
                queryColumn = new QueryAggregate(aggregate.getType(), new QueryColumn(foreignQtable, -1, aggregate.getColumnNameToAggregate(), aggregate.getDataProviderType(), aggregate.getLength(), 0, null, aggregate.getFlags()), aggregate.getName());
                // there has to be a group-by clause for all selected fields
                List<IQuerySelectValue> columns = sqlSelect.getColumns();
                for (IQuerySelectValue selectVal : columns) {
                    List<IQuerySelectValue> groupBy = sqlSelect.getGroupBy();
                    if (selectVal instanceof QueryColumn && (groupBy == null || !groupBy.contains(selectVal))) {
                        sqlSelect.addGroupBy(selectVal);
                    }
                }
                // if the aggregate has not been selected yet, add it and skip it in the result
                QueryAggregate skippedAggregate = new QueryAggregate(aggregate.getType(), QueryAggregate.ALL, new QueryColumn(foreignQtable, -1, aggregate.getColumnNameToAggregate(), aggregate.getDataProviderType(), aggregate.getLength(), 0, null, aggregate.getFlags()), aggregate.getName(), null, true);
                if (!columns.contains(skippedAggregate)) {
                    sqlSelect.addColumn(skippedAggregate);
                }
            } else {
                // $NON-NLS-1$
                Debug.log("Skipping sort on unexpected related column type " + column.getClass());
                continue;
            }
            sqlSelect.addSort(new QuerySort(queryColumn, sc.getSortOrder() == ASCENDING, sortOptions));
        } else {
            // make sure an invalid sort is not possible
            if (column instanceof Column && column.getTable().getName().equals(table.getName())) {
                sqlSelect.addSort(new QuerySort(((Column) column).queryColumn(selectTable), sc.getSortOrder() == ASCENDING, sortOptions));
                unusedRowidentColumns.remove(column);
            } else {
                // $NON-NLS-1$ //$NON-NLS-2$
                Debug.log("Skipping sort on unrelated column " + column.getName() + '.' + column.getTable().getName() + " for table " + table.getName());
            }
        }
    }
    // Make sure pk is part of the sort, in case of non-unique sort columns, the sorted result may not be the same in each fetch
    if (enforcePkInSort) {
        for (Column column : unusedRowidentColumns) {
            SortOptions sortOptions = application.getFoundSetManager().getSortOptions(column);
            sqlSelect.addSort(new QuerySort(column.queryColumn(selectTable), true, sortOptions));
        }
    }
}
Also used : QueryAggregate(com.servoy.j2db.query.QueryAggregate) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) FlattenedSolution(com.servoy.j2db.FlattenedSolution) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) Relation(com.servoy.j2db.persistence.Relation) IRelation(com.servoy.j2db.persistence.IRelation) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) QuerySort(com.servoy.j2db.query.QuerySort) IQuerySort(com.servoy.j2db.query.IQuerySort) ITable(com.servoy.j2db.persistence.ITable) List(java.util.List) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) Collectors.toList(java.util.stream.Collectors.toList) SortOptions(com.servoy.j2db.query.SortOptions) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 20 with ITable

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

the class SQLGenerator method makeRelatedSQL.

synchronized void makeRelatedSQL(SQLSheet relatedSheet, Relation r) {
    if (relatedSheet.getRelatedSQLDescription(r.getName()) != null)
        return;
    try {
        FlattenedSolution fs = application.getFlattenedSolution();
        if (!Relation.isValid(r, fs) || r.isParentRef()) {
            return;
        }
        ITable ft = fs.getTable(r.getForeignDataSource());
        if (ft == null) {
            return;
        }
        // add primary keys if missing
        QueryTable foreignQTable = new QueryTable(ft.getSQLName(), ft.getDataSource(), ft.getCatalog(), ft.getSchema());
        QuerySelect relatedSelect = new QuerySelect(foreignQTable);
        List<String> parentRequiredDataProviderIDs = new ArrayList<String>();
        Column[] relcols = r.getForeignColumns(fs);
        for (Column column : relcols) {
            parentRequiredDataProviderIDs.add(column.getDataProviderID());
        }
        relatedSelect.setCondition(CONDITION_RELATION, createRelatedCondition(application, r, foreignQTable));
        Collection<Column> rcolumns = ft.getColumns();
        relatedSelect.setColumns(makeQueryColumns(rcolumns.iterator(), foreignQTable, null));
        // fill dataprovider map
        List<String> dataProviderIDsDilivery = new ArrayList<String>();
        Iterator<Column> it = rcolumns.iterator();
        while (it.hasNext()) {
            Column col = it.next();
            dataProviderIDsDilivery.add(col.getDataProviderID());
        }
        relatedSheet.addRelatedSelect(r.getName(), relatedSelect, dataProviderIDsDilivery, parentRequiredDataProviderIDs, null);
        createAggregates(relatedSheet, foreignQTable);
    } catch (RepositoryException e) {
        Debug.error(e);
    }
}
Also used : QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) FlattenedSolution(com.servoy.j2db.FlattenedSolution) ITable(com.servoy.j2db.persistence.ITable) RepositoryException(com.servoy.j2db.persistence.RepositoryException) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable)

Aggregations

ITable (com.servoy.j2db.persistence.ITable)51 RepositoryException (com.servoy.j2db.persistence.RepositoryException)23 BaseQueryTable (com.servoy.base.query.BaseQueryTable)15 QueryTable (com.servoy.j2db.query.QueryTable)15 Column (com.servoy.j2db.persistence.Column)14 ServoyException (com.servoy.j2db.util.ServoyException)14 ArrayList (java.util.ArrayList)14 RemoteException (java.rmi.RemoteException)13 ApplicationException (com.servoy.j2db.ApplicationException)11 IColumn (com.servoy.j2db.persistence.IColumn)9 Relation (com.servoy.j2db.persistence.Relation)9 Table (com.servoy.j2db.persistence.Table)9 QueryColumn (com.servoy.j2db.query.QueryColumn)8 Form (com.servoy.j2db.persistence.Form)7 QuerySelect (com.servoy.j2db.query.QuerySelect)7 IBaseColumn (com.servoy.base.persistence.IBaseColumn)6 IServer (com.servoy.j2db.persistence.IServer)6 SQLException (java.sql.SQLException)6 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)5 IOException (java.io.IOException)5