Search in sources :

Example 11 with Table

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

the class FoundSet method matchesMainTablename.

/**
 * Check if the tableName matches the main table.
 *
 * For example: "THECAT"."THESCHEMA"."THETABLE" matches thetable.
 */
private boolean matchesMainTablename(String tableName) {
    Table table = sheet.getTable();
    if (table.getName().equalsIgnoreCase(tableName) || table.getSQLName().equalsIgnoreCase(tableName)) {
        return true;
    }
    String[] catschemtab = tableName.split("\\.");
    if (catschemtab.length == 2) {
        // catalogorschema.table
        String catschem = dequote(catschemtab[0]);
        String tab = dequote(catschemtab[1]);
        if ((catschem.equalsIgnoreCase(table.getCatalog()) || catschem.equalsIgnoreCase(table.getSchema())) && (table.getName().equalsIgnoreCase(tab) || table.getSQLName().equalsIgnoreCase(tab))) {
            return true;
        }
    } else if (catschemtab.length == 3) {
        // catalog.schema.table
        String cat = dequote(catschemtab[0]);
        String schem = dequote(catschemtab[1]);
        String tab = dequote(catschemtab[2]);
        if (cat.equalsIgnoreCase(table.getCatalog()) && schem.equalsIgnoreCase(table.getSchema()) && (table.getName().equalsIgnoreCase(tab) || table.getSQLName().equalsIgnoreCase(tab))) {
            return true;
        }
    }
    // no match
    return false;
}
Also used : BaseQueryTable(com.servoy.base.query.BaseQueryTable) Table(com.servoy.j2db.persistence.Table) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable)

Example 12 with Table

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

the class SQLGenerator method createTableFiltercondition.

private static QueryFilter createTableFiltercondition(BaseQueryTable qTable, Table table, QuerySelect filterQuery) {
    QuerySelect filterQueryClone = deepClone(filterQuery);
    filterQueryClone.relinkTable(filterQueryClone.getTable(), qTable);
    List<QueryColumn> pkColumns = table.getRowIdentColumns().stream().map(column -> column.queryColumn(qTable)).collect(toList());
    return new QueryFilter(filterQueryClone.getJoins(), pkColumns, filterQueryClone.getWhere());
}
Also used : Array(java.lang.reflect.Array) QuerySort(com.servoy.j2db.query.QuerySort) QueryFunctionType.upper(com.servoy.j2db.query.QueryFunction.QueryFunctionType.upper) CompareCondition(com.servoy.j2db.query.CompareCondition) QueryColumn(com.servoy.j2db.query.QueryColumn) Debug(com.servoy.j2db.util.Debug) IQueryConstants(com.servoy.base.query.IQueryConstants) BaseQueryTable(com.servoy.base.query.BaseQueryTable) SetCondition(com.servoy.j2db.query.SetCondition) QueryTable(com.servoy.j2db.query.QueryTable) Utils(com.servoy.j2db.util.Utils) AbstractBase(com.servoy.j2db.persistence.AbstractBase) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) IServiceProvider(com.servoy.j2db.IServiceProvider) Map(java.util.Map) IBaseSQLCondition(com.servoy.base.query.IBaseSQLCondition) IPlaceholderKey(com.servoy.j2db.query.IPlaceholderKey) OrCondition(com.servoy.j2db.query.OrCondition) ISQLJoin(com.servoy.j2db.query.ISQLJoin) AbstractBaseQuery(com.servoy.j2db.query.AbstractBaseQuery) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) Collection(java.util.Collection) QueryDelete(com.servoy.j2db.query.QueryDelete) IDENT_COLUMNS(com.servoy.base.persistence.IBaseColumn.IDENT_COLUMNS) IVisitor(com.servoy.j2db.util.visitor.IVisitor) ConverterInfo(com.servoy.j2db.dataprocessing.SQLSheet.ConverterInfo) ISQLCondition(com.servoy.j2db.query.ISQLCondition) ITable(com.servoy.j2db.persistence.ITable) QueryFunction(com.servoy.j2db.query.QueryFunction) IDataProvider(com.servoy.j2db.persistence.IDataProvider) Table(com.servoy.j2db.persistence.Table) ParsedFormat(com.servoy.j2db.util.FormatParser.ParsedFormat) IApplication(com.servoy.j2db.IApplication) Wrapper(org.mozilla.javascript.Wrapper) ASCENDING(com.servoy.j2db.dataprocessing.SortColumn.ASCENDING) List(java.util.List) ColumnType(com.servoy.j2db.query.ColumnType) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) QueryJoin(com.servoy.j2db.query.QueryJoin) Types(java.sql.Types) IValueConverter(com.servoy.base.dataprocessing.IValueConverter) QueryInsert(com.servoy.j2db.query.QueryInsert) UUID_COLUMN(com.servoy.base.persistence.IBaseColumn.UUID_COLUMN) IQuerySort(com.servoy.j2db.query.IQuerySort) SortOptions(com.servoy.j2db.query.SortOptions) Placeholder(com.servoy.j2db.query.Placeholder) IDataProviderHandler(com.servoy.j2db.persistence.IDataProviderHandler) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) FlattenedSolution(com.servoy.j2db.FlattenedSolution) ObjectPlaceholderKey(com.servoy.j2db.query.ObjectPlaceholderKey) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) HashMap(java.util.HashMap) IColumn(com.servoy.j2db.persistence.IColumn) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) QueryFilter(com.servoy.j2db.query.QueryFilter) SafeArrayList(com.servoy.j2db.util.SafeArrayList) ISQLSelect(com.servoy.j2db.query.ISQLSelect) Relation(com.servoy.j2db.persistence.Relation) QueryFactory(com.servoy.j2db.query.QueryFactory) ServoyException(com.servoy.j2db.util.ServoyException) QueryCustomSelect(com.servoy.j2db.query.QueryCustomSelect) BaseColumnType(com.servoy.base.query.BaseColumnType) ITypeConverter(com.servoy.base.dataprocessing.ITypeConverter) QueryUpdate(com.servoy.j2db.query.QueryUpdate) RelatedFindState(com.servoy.j2db.dataprocessing.FindState.RelatedFindState) QueryAggregate(com.servoy.j2db.query.QueryAggregate) Iterator(java.util.Iterator) BaseSQLGenerator(com.servoy.base.dataprocessing.BaseSQLGenerator) IColumnTypes(com.servoy.j2db.persistence.IColumnTypes) Column.mapToDefaultType(com.servoy.j2db.persistence.Column.mapToDefaultType) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) ExistsCondition(com.servoy.j2db.query.ExistsCondition) QuerySelect(com.servoy.j2db.query.QuerySelect) AndCondition(com.servoy.j2db.query.AndCondition) Collectors.toList(java.util.stream.Collectors.toList) QueryFunctionType.castfrom(com.servoy.j2db.query.QueryFunction.QueryFunctionType.castfrom) IRelation(com.servoy.j2db.persistence.IRelation) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) LiteralDataprovider(com.servoy.j2db.persistence.LiteralDataprovider) QueryFunctionType.cast(com.servoy.j2db.query.QueryFunction.QueryFunctionType.cast) Column(com.servoy.j2db.persistence.Column) RelationItem(com.servoy.j2db.persistence.RelationItem) RepositoryException(com.servoy.j2db.persistence.RepositoryException) MEDIA(com.servoy.j2db.persistence.IColumnTypes.MEDIA) Collections(java.util.Collections) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) AbstractBaseQuery.deepClone(com.servoy.j2db.query.AbstractBaseQuery.deepClone) QueryFilter(com.servoy.j2db.query.QueryFilter) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) QuerySelect(com.servoy.j2db.query.QuerySelect)

Example 13 with Table

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

the class SQLGenerator method createAggregates.

private void createAggregates(SQLSheet sheet, QueryTable queryTable) throws RepositoryException {
    Table table = sheet.getTable();
    Iterator<AggregateVariable> it = application.getFlattenedSolution().getAggregateVariables(table, false);
    while (it.hasNext()) {
        AggregateVariable aggregate = it.next();
        QuerySelect sql = new QuerySelect(queryTable);
        sql.addColumn(new QueryAggregate(aggregate.getType(), new QueryColumn(queryTable, -1, aggregate.getColumnNameToAggregate(), aggregate.getDataProviderType(), aggregate.getLength(), 0, null, aggregate.getFlags()), aggregate.getName()));
        sheet.addAggregate(aggregate.getDataProviderID(), aggregate.getDataProviderIDToAggregate(), sql);
    }
}
Also used : QueryAggregate(com.servoy.j2db.query.QueryAggregate) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) QuerySelect(com.servoy.j2db.query.QuerySelect)

Example 14 with Table

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

the class SQLGenerator method createDynamicPKSetConditionForFoundset.

static SetCondition createDynamicPKSetConditionForFoundset(FoundSet foundSet, BaseQueryTable queryTable, IDataSet pks) {
    Table table = (Table) foundSet.getTable();
    List<Column> rowIdentColumns = table.getRowIdentColumns();
    QueryColumn[] pkQueryColumns = new QueryColumn[rowIdentColumns.size()];
    // getPrimaryKeys from table
    for (int i = 0; i < rowIdentColumns.size(); i++) {
        Column column = rowIdentColumns.get(i);
        pkQueryColumns[i] = column.queryColumn(queryTable);
    }
    // Dynamic PK condition, the special placeholder will be updated when the foundset pk set changes
    Placeholder placeHolder = new Placeholder(new TablePlaceholderKey(queryTable, SQLGenerator.PLACEHOLDER_FOUNDSET_PKS));
    placeHolder.setValue(new DynamicPkValuesArray(rowIdentColumns, pks.clone()));
    return new SetCondition(IBaseSQLCondition.EQUALS_OPERATOR, pkQueryColumns, placeHolder, true);
}
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) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) 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) SetCondition(com.servoy.j2db.query.SetCondition)

Example 15 with Table

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

the class JSFoundSetUpdater method js_performUpdate.

/**
 * Do the actual update in the database, returns true if successful. It will first try to save all editing records (from all foundsets), if cannot save will return false before doing the update.
 * There are 3 types of possible use with the foundset updater
 * 1) update entire foundset by a single sql statement; that is not possible when the table of the foundset has tracking enabled then it will loop over the whole foundset.
 *    When a single sql statement is done, modification columns will not be updated and associated Table Events won't be triggered, because it does the update directly in the database, without getting the records.
 *   NOTE: this mode will refresh all foundsets based on same datasource
 * 2) update part of foundset, for example the first 4 row (starts with selected row)
 * 3) safely loop through foundset (starts with selected row)
 *
 * after the perform update call there are no records in edit mode, that where not already in edit mode, because all of them are saved directly to the database,
 * or in mode 1 the records are not touched at all and the database is updated directly.
 *
 * @sample
 * //1) update entire foundset
 * var fsUpdater = databaseManager.getFoundSetUpdater(foundset)
 * fsUpdater.setColumn('customer_type',1)
 * fsUpdater.setColumn('my_flag',0)
 * fsUpdater.performUpdate()
 *
 * //2) update part of foundset, for example the first 4 row (starts with selected row)
 * var fsUpdater = databaseManager.getFoundSetUpdater(foundset)
 * fsUpdater.setColumn('customer_type',new Array(1,2,3,4))
 * fsUpdater.setColumn('my_flag',new Array(1,0,1,0))
 * fsUpdater.performUpdate()
 *
 * //3) safely loop through foundset (starts with selected row)
 * controller.setSelectedIndex(1)
 * var count = 0
 * var fsUpdater = databaseManager.getFoundSetUpdater(foundset)
 * while(fsUpdater.next())
 * {
 * 	fsUpdater.setColumn('my_flag',count++)
 * }
 *
 * @return true if succeeded, false if failed.
 */
public boolean js_performUpdate() throws ServoyException {
    if (list.size() == 0 || foundset.getTable() == null) {
        return false;
    }
    if (!foundset.hasAccess(IRepository.UPDATE)) {
        throw new ApplicationException(ServoyException.NO_MODIFY_ACCESS, new Object[] { foundset.getTable().getName() });
    }
    // first stop all edits, 'force' stop the edit by saying that it is a javascript stop
    if (application.getFoundSetManager().getEditRecordList().stopEditing(true) != ISaveConstants.STOPPED)
        return false;
    try {
        QuerySelect sqlParts;
        IDataSet currentPKs;
        synchronized (foundset.getPksAndRecords()) {
            sqlParts = foundset.getPksAndRecords().getQuerySelectForReading();
            currentPKs = foundset.getPksAndRecords().getPks();
        }
        FoundSetManager fsm = (FoundSetManager) application.getFoundSetManager();
        if (rowsToUpdate == -1 && !foundset.hasAccess(IRepository.TRACKING) && sqlParts.getJoins() == null && // does not have join to other table
        !fsm.hasTableFiltersWithJoins(foundset.getTable().getServerName(), sqlParts)) {
            // all rows at once, via sql
            Table table = (Table) foundset.getTable();
            SQLSheet sheet = foundset.getSQLSheet();
            QueryUpdate sqlUpdate = new QueryUpdate(sqlParts.getTable());
            for (Pair<String, Object> p : list) {
                String name = p.getLeft();
                Object val = p.getRight();
                int columnIndex = sheet.getColumnIndex(name);
                VariableInfo variableInfo = sheet.getCalculationOrColumnVariableInfo(name, columnIndex);
                if (// do not convert null to 0 incase of numbers, this means the calcs the value whould change each time //$NON-NLS-1$
                val != null && !("".equals(val) && Column.mapToDefaultType(variableInfo.type) == IColumnTypes.TEXT)) {
                    val = sheet.convertObjectToValue(name, val, foundset.getFoundSetManager().getColumnConverterManager(), foundset.getFoundSetManager().getColumnValidatorManager(), null);
                }
                Column c = table.getColumn(name);
                if (val == null) {
                    val = ValueFactory.createNullValue(c.getType());
                }
                sqlUpdate.addValue(c.queryColumn(sqlParts.getTable()), val);
            }
            sqlUpdate.setCondition(sqlParts.getWhereClone());
            IDataSet pks;
            boolean allFoundsetRecordsLoaded = currentPKs != null && currentPKs.getRowCount() <= fsm.config.pkChunkSize() && !currentPKs.hadMoreRows();
            if (allFoundsetRecordsLoaded) {
                pks = currentPKs;
            } else {
                pks = new BufferedDataSet();
                pks.addRow(new Object[] { ValueFactory.createTableFlushValue() });
            }
            String transaction_id = fsm.getTransactionID(foundset.getSQLSheet());
            try {
                SQLStatement statement = new SQLStatement(ISQLActionTypes.UPDATE_ACTION, table.getServerName(), table.getName(), pks, transaction_id, sqlUpdate, fsm.getTableFilterParams(table.getServerName(), sqlUpdate));
                if (allFoundsetRecordsLoaded) {
                    statement.setExpectedUpdateCount(pks.getRowCount());
                }
                Object[] results = fsm.getDataServer().performUpdates(fsm.getApplication().getClientID(), new ISQLStatement[] { statement });
                for (int i = 0; results != null && i < results.length; i++) {
                    if (results[i] instanceof ServoyException) {
                        if (((ServoyException) results[i]).getErrorCode() == ServoyException.UNEXPECTED_UPDATE_COUNT) {
                            performLoopUpdate();
                            clear();
                            return true;
                        }
                        fsm.flushCachedDatabaseData(fsm.getDataSource(table));
                        throw (ServoyException) results[i];
                    }
                }
                fsm.flushCachedDatabaseData(fsm.getDataSource(table));
                clear();
                return true;
            } catch (ApplicationException aex) {
                if (allFoundsetRecordsLoaded || aex.getErrorCode() != ServoyException.RECORD_LOCKED) {
                    throw aex;
                }
                // a record was locked by another client, try per-record
                Debug.log("foundsetUpdater could not update all records in 1 statement (a record may be locked), trying per-record");
            }
        }
        performLoopUpdate();
    } catch (Exception ex) {
        // $NON-NLS-1$
        application.handleException(// $NON-NLS-1$
        application.getI18NMessage("servoy.foundsetupdater.updateFailed"), new ApplicationException(ServoyException.SAVE_FAILED, ex));
        return false;
    }
    clear();
    return true;
}
Also used : Table(com.servoy.j2db.persistence.Table) VariableInfo(com.servoy.j2db.dataprocessing.SQLSheet.VariableInfo) QuerySelect(com.servoy.j2db.query.QuerySelect) ServoyException(com.servoy.j2db.util.ServoyException) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) ApplicationException(com.servoy.j2db.ApplicationException) Column(com.servoy.j2db.persistence.Column) QueryUpdate(com.servoy.j2db.query.QueryUpdate)

Aggregations

Table (com.servoy.j2db.persistence.Table)43 ITable (com.servoy.j2db.persistence.ITable)27 QueryTable (com.servoy.j2db.query.QueryTable)26 RepositoryException (com.servoy.j2db.persistence.RepositoryException)23 BaseQueryTable (com.servoy.base.query.BaseQueryTable)22 Column (com.servoy.j2db.persistence.Column)21 ServoyException (com.servoy.j2db.util.ServoyException)17 QueryColumn (com.servoy.j2db.query.QueryColumn)16 QuerySelect (com.servoy.j2db.query.QuerySelect)16 ArrayList (java.util.ArrayList)16 RemoteException (java.rmi.RemoteException)14 ApplicationException (com.servoy.j2db.ApplicationException)13 IColumn (com.servoy.j2db.persistence.IColumn)10 IBaseColumn (com.servoy.base.persistence.IBaseColumn)9 IServer (com.servoy.j2db.persistence.IServer)7 Relation (com.servoy.j2db.persistence.Relation)7 HashMap (java.util.HashMap)7 SortColumn (com.servoy.j2db.dataprocessing.SortColumn)6 SafeArrayList (com.servoy.j2db.util.SafeArrayList)6 ColumnInfo (com.servoy.j2db.persistence.ColumnInfo)5