Search in sources :

Example 16 with QueryTable

use of com.servoy.j2db.query.QueryTable 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)

Example 17 with QueryTable

use of com.servoy.j2db.query.QueryTable in project servoy-client by Servoy.

the class SQLGenerator method createUpdateLockSelect.

public static QuerySelect createUpdateLockSelect(Table table, Object[][] pkValues, boolean lockInDb) {
    QuerySelect lockSelect = new QuerySelect(new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema()));
    if (lockInDb)
        lockSelect.setLockMode(ISQLSelect.LOCK_MODE_LOCK_NOWAIT);
    LinkedHashMap<Column, QueryColumn> allQueryColumns = new LinkedHashMap<>();
    for (Column column : table.getColumns()) {
        allQueryColumns.put(column, column.queryColumn(lockSelect.getTable()));
    }
    lockSelect.setColumns(new ArrayList<IQuerySelectValue>(allQueryColumns.values()));
    // get the pk columns, make sure the order is in pk-order (alphabetical)
    ArrayList<QueryColumn> pkQueryColumns = new ArrayList<>();
    for (Column pkColumn : table.getRowIdentColumns()) {
        pkQueryColumns.add(allQueryColumns.get(pkColumn));
    }
    // values is an array as wide as the columns, each element consists of the values for that column
    Object[][] values = new Object[pkQueryColumns.size()][];
    for (int k = 0; k < pkQueryColumns.size(); k++) {
        values[k] = new Object[pkValues.length];
        for (int r = 0; r < pkValues.length; r++) {
            values[k][r] = pkValues[r][k];
        }
    }
    lockSelect.setCondition(CONDITION_LOCK, new SetCondition(IBaseSQLCondition.EQUALS_OPERATOR, pkQueryColumns.toArray(new QueryColumn[pkQueryColumns.size()]), values, true));
    return lockSelect;
}
Also used : ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) SetCondition(com.servoy.j2db.query.SetCondition) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) LinkedHashMap(java.util.LinkedHashMap) 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) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 18 with QueryTable

use of com.servoy.j2db.query.QueryTable 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 19 with QueryTable

use of com.servoy.j2db.query.QueryTable 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 20 with QueryTable

use of com.servoy.j2db.query.QueryTable in project servoy-client by Servoy.

the class MetaDataUtils method createTableMetadataQuery.

public static QuerySelect createTableMetadataQuery(ITable table, LinkedHashMap<Column, QueryColumn> queryColumns) {
    QuerySelect query = new QuerySelect(new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema()));
    // LinkedHashMap to keep order for column names
    LinkedHashMap<Column, QueryColumn> qColumns = queryColumns == null ? new LinkedHashMap<Column, QueryColumn>() : queryColumns;
    Iterator<Column> columns = table.getColumnsSortedByName();
    while (columns.hasNext()) {
        Column column = columns.next();
        if (!column.hasFlag(IBaseColumn.EXCLUDED_COLUMN)) {
            QueryColumn qColumn = column.queryColumn(query.getTable());
            query.addColumn(qColumn);
            qColumns.put(column, qColumn);
        }
    }
    for (Column column : table.getRowIdentColumns()) {
        if (qColumns.containsKey(column)) {
            query.addSort(new QuerySort(qColumns.get(column), true, SortOptions.NONE));
        }
    }
    return query;
}
Also used : QueryColumn(com.servoy.j2db.query.QueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) QueryColumn(com.servoy.j2db.query.QueryColumn) QuerySort(com.servoy.j2db.query.QuerySort) QuerySelect(com.servoy.j2db.query.QuerySelect) QueryTable(com.servoy.j2db.query.QueryTable)

Aggregations

QueryTable (com.servoy.j2db.query.QueryTable)35 BaseQueryTable (com.servoy.base.query.BaseQueryTable)22 QueryColumn (com.servoy.j2db.query.QueryColumn)22 QuerySelect (com.servoy.j2db.query.QuerySelect)19 Column (com.servoy.j2db.persistence.Column)18 ArrayList (java.util.ArrayList)18 ITable (com.servoy.j2db.persistence.ITable)15 RepositoryException (com.servoy.j2db.persistence.RepositoryException)13 IColumn (com.servoy.j2db.persistence.IColumn)12 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)10 Table (com.servoy.j2db.persistence.Table)10 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)9 SafeArrayList (com.servoy.j2db.util.SafeArrayList)9 ServoyException (com.servoy.j2db.util.ServoyException)9 RemoteException (java.rmi.RemoteException)9 CompareCondition (com.servoy.j2db.query.CompareCondition)8 QueryDelete (com.servoy.j2db.query.QueryDelete)8 ApplicationException (com.servoy.j2db.ApplicationException)7 SetCondition (com.servoy.j2db.query.SetCondition)7 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)6