Search in sources :

Example 46 with ITable

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

the class ComponentFormat method getComponentFormat.

public static ComponentFormat getComponentFormat(String format, IDataProvider dataProvider, IServiceProvider application, boolean autoFillMaxLength) {
    int dpType = IColumnTypes.TEXT;
    String formatProperty = format;
    if (dataProvider != null) {
        dpType = dataProvider.getDataProviderType();
        IColumn column = null;
        if (dataProvider instanceof ColumnWrapper) {
            column = ((ColumnWrapper) dataProvider).getColumn();
        } else if (dataProvider instanceof Column) {
            column = (Column) dataProvider;
        } else if (dataProvider instanceof ScriptCalculation) {
            // When it is a stored calculation, the name of the calc is the name of he column
            ScriptCalculation calc = (ScriptCalculation) dataProvider;
            try {
                ITable table = calc.getTable();
                if (table != null) {
                    column = table.getColumn(calc.getName());
                }
            } catch (RepositoryException e) {
                Debug.error(e);
            }
        }
        if (column instanceof AggregateVariable) {
            Column columnToAggregate = null;
            try {
                ITable table = column.getTable();
                if (table != null) {
                    columnToAggregate = table.getColumn(((AggregateVariable) column).getDataProviderIDToAggregate());
                }
            } catch (RepositoryException e) {
                Debug.error(e);
            }
            // Use aggregated column when they are of the same type (so not count(textcolumn))
            if (columnToAggregate != null && column.getDataProviderType() == columnToAggregate.getDataProviderType()) {
                column = columnToAggregate;
            }
        }
        if (column instanceof Column) {
            ColumnInfo ci = ((Column) column).getColumnInfo();
            if (ci != null) {
                if (formatProperty == null || formatProperty.length() == 0) {
                    if (ci.getDefaultFormat() != null && ci.getDefaultFormat().length() > 0) {
                        formatProperty = ci.getDefaultFormat();
                    }
                }
                dpType = application.getFoundSetManager().getConvertedTypeForColumn(column, true);
            }
        }
    }
    ComponentFormat componentFormat = ComponentFormat.getComponentFormat(formatProperty, dpType, application);
    if (autoFillMaxLength && dataProvider != null && dataProvider.getLength() > 0 && componentFormat.parsedFormat != null && componentFormat.parsedFormat.getMaxLength() == null && (dpType == IColumnTypes.TEXT || dpType == IColumnTypes.MEDIA)) {
        componentFormat.parsedFormat.updateMaxLength(Integer.valueOf(dataProvider.getLength()));
    }
    return componentFormat;
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) IColumn(com.servoy.j2db.persistence.IColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) ColumnWrapper(com.servoy.j2db.persistence.ColumnWrapper) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) ITable(com.servoy.j2db.persistence.ITable) RepositoryException(com.servoy.j2db.persistence.RepositoryException) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable)

Example 47 with ITable

use of com.servoy.j2db.persistence.ITable 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)

Example 48 with ITable

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

the class JSDatabaseManager method js_updateTableFilterParam.

/**
 * Updates a filter with a new condition. The server/table name should be unchanged.
 *
 * @sample
 *	databaseManager.updateTableFilterParam('database', 'myfilter', 'your_i18n_table', 'message_variant', 'in', [1, 2])
 *
 * @param serverName The name of the database server connection for the specified table name.
 * @param filterName The name of the filter that should be updated.
 * @param tableName The name of the specified table.
 * @param dataprovider A specified dataprovider column name.
 * @param operator One of "=, <, >, >=, <=, !=, LIKE, or IN" optionally augmented with modifiers "#" (ignore case) or "^||" (or-is-null), prefix with "sql:" to allow the value to be interpreted as a custom query.
 * @param value The specified filter value.
 *
 * @return true if the tablefilter could be updated.
 */
public boolean js_updateTableFilterParam(String serverName, String filterName, String tableName, String dataprovider, String operator, Object value) throws ServoyException {
    checkAuthorized();
    try {
        if (value instanceof Wrapper) {
            value = ((Wrapper) value).unwrap();
        }
        IServer server = application.getSolution().getServer(serverName);
        if (server == null) {
            application.reportJSError("Table filter not applied to unknown server '" + serverName + "', tableName = '" + tableName + "', dataprovider = '" + dataprovider + "', operator = '" + operator + "', value = '" + value + "', filterName = '" + filterName + "'", null);
            return false;
        }
        ITable table = null;
        if (tableName != null) {
            table = server.getTable(tableName);
            if (table == null) {
                application.reportJSError("Table filter not applied to unknown table: serverName = '" + serverName + "', tableName = '" + tableName + "', dataprovider = '" + dataprovider + "', operator = '" + operator + "', value = '" + value + "', filterName = '" + filterName + "'", null);
                return false;
            }
        }
        // else table remains null: apply to all tables with that column
        DataproviderTableFilterdefinition dataproviderTableFilterdefinition = application.getFoundSetManager().createDataproviderTableFilterdefinition(table, dataprovider, operator, value);
        if (dataproviderTableFilterdefinition == null) {
            application.reportJSError("Table filter not created, column not found in table or operator invalid, filterName = '" + filterName + "', serverName = '" + serverName + "', table = '" + table + "', dataprovider = '" + dataprovider + "', operator = '" + operator + "'", null);
            return false;
        }
        return (((FoundSetManager) application.getFoundSetManager()).updateTableFilterParam(serverName, filterName, table, dataproviderTableFilterdefinition));
    } catch (Exception ex) {
        Debug.error(ex);
    }
    return false;
}
Also used : Wrapper(org.mozilla.javascript.Wrapper) IServer(com.servoy.j2db.persistence.IServer) ITable(com.servoy.j2db.persistence.ITable) 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)

Example 49 with ITable

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

the class JSDatabaseManager method convertFoundSet.

public FoundSet convertFoundSet(Object foundset, Object related) throws ServoyException {
    checkAuthorized();
    if (foundset instanceof FoundSet && ((FoundSet) foundset).getTable() != null) {
        FoundSet fs_old = (FoundSet) foundset;
        try {
            String relationName;
            if (related instanceof RelatedFoundSet) {
                relationName = ((RelatedFoundSet) related).getRelationName();
            } else if (related instanceof String) {
                relationName = (String) related;
            } else {
                // $NON-NLS-1$
                Debug.warn("convertFoundSet: invalid argument " + related);
                return null;
            }
            Relation relation = application.getFlattenedSolution().getRelation(relationName);
            if (relation == null || relation.isMultiServer() || fs_old.getTable() == null || !fs_old.getTable().equals(application.getFlattenedSolution().getTable(relation.getPrimaryDataSource()))) {
                // $NON-NLS-1$
                Debug.warn("convertFoundSet: cannot use relation " + relationName);
                return null;
            }
            ITable ft = application.getFlattenedSolution().getTable(relation.getForeignDataSource());
            FoundSet fs_new = (FoundSet) application.getFoundSetManager().getNewFoundSet(ft, null, application.getFoundSetManager().getDefaultPKSortColumns(ft.getDataSource()));
            QuerySelect sql = fs_old.getPksAndRecords().getQuerySelectForModification();
            SQLSheet sheet_new = fs_old.getSQLSheet().getRelatedSheet(relation, ((FoundSetManager) application.getFoundSetManager()).getSQLGenerator());
            if (sheet_new != null) {
                BaseQueryTable oldTable = sql.getTable();
                ISQLTableJoin join = (ISQLTableJoin) sql.getJoin(oldTable, relation.getName());
                if (join == null) {
                    join = SQLGenerator.createJoin(application.getFlattenedSolution(), relation, oldTable, new QueryTable(ft.getSQLName(), ft.getDataSource(), ft.getCatalog(), ft.getSchema()), true, fs_old);
                    sql.addJoin(join);
                }
                BaseQueryTable mainTable = join.getForeignTable();
                // invert the join
                sql.setTable(mainTable);
                // $NON-NLS-1$
                join.invert("INVERTED." + join.getName());
                // set the columns to be the PKs from the related table
                ArrayList<IQuerySelectValue> pkColumns = new ArrayList<IQuerySelectValue>();
                Iterator<Column> pks = sheet_new.getTable().getRowIdentColumns().iterator();
                while (pks.hasNext()) {
                    Column column = pks.next();
                    pkColumns.add(column.queryColumn(mainTable));
                }
                sql.setColumns(pkColumns);
                // sorting will be on the original columns, when distinct is set, this will conflict with the related pk columns
                sql.setDistinct(false);
                fs_new.setSQLSelect(sql);
                return fs_new;
            }
        } catch (Exception e) {
            Debug.error(e);
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) QueryString(com.servoy.j2db.persistence.QueryString) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) 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) Relation(com.servoy.j2db.persistence.Relation) BaseQueryTable(com.servoy.base.query.BaseQueryTable) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) 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) ITable(com.servoy.j2db.persistence.ITable) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 50 with ITable

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

the class FoundSetManager method registerViewFoundSet.

@Override
public boolean registerViewFoundSet(ViewFoundSet foundset, boolean onlyWeak) {
    if (foundset == null)
        return false;
    if (onlyWeak) {
        noneRegisteredVFS.put(foundset, TRUE);
    } else {
        ViewFoundSet oldValue = viewFoundSets.put(foundset.getDataSource(), foundset);
        ITable table = foundset.getTable();
        if (!viewDataSources.containsKey(foundset.getDataSource()))
            viewDataSources.put(foundset.getDataSource(), table);
        if (oldValue != null) {
            for (IFormController controller : application.getFormManager().getCachedFormControllers()) {
                if (controller.getFormModel() == oldValue) {
                    controller.loadAllRecords();
                }
            }
        }
    }
    return true;
}
Also used : ITable(com.servoy.j2db.persistence.ITable) IFormController(com.servoy.j2db.IFormController)

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