Search in sources :

Example 1 with Placeholder

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

the class FlattenedSolution method getValuelistSortRelation.

/**
 * Get the internal relation that can be used to sort on this value list using the display values.
 * @param valueList
 * @param callingTable
 * @param dataProviderID
 * @param foundSetManager
 * @return
 * @throws RepositoryException
 */
public Relation getValuelistSortRelation(ValueList valueList, Table callingTable, String dataProviderID, IFoundSetManagerInternal foundSetManager) throws RepositoryException {
    if (callingTable == null || valueList == null) {
        return null;
    }
    String destDataSource;
    Relation[] relationSequence;
    String relationPrefix;
    switch(valueList.getDatabaseValuesType()) {
        case IValueListConstants.TABLE_VALUES:
            // create an internal relation
            relationSequence = null;
            // $NON-NLS-1$
            relationPrefix = "";
            destDataSource = valueList.getDataSource();
            break;
        case IValueListConstants.RELATED_VALUES:
            // replace the last relation in the sequence with an internal relation
            relationSequence = getRelationSequence(valueList.getRelationName());
            if (relationSequence == null) {
                return null;
            }
            if (relationSequence.length > 1) {
                for (Relation r : relationSequence) {
                    if (r.getJoinType() != INNER_JOIN) {
                        // outer join on the intermediate tables causes extra results that influence the sorting result
                        return null;
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            for (Relation r : relationSequence) {
                sb.append('-').append(r.getName());
            }
            relationPrefix = sb.toString();
            destDataSource = relationSequence[relationSequence.length - 1].getForeignDataSource();
            break;
        default:
            return null;
    }
    if (destDataSource == null || !DataSourceUtils.isSameServer(callingTable.getDataSource(), destDataSource)) {
        // do not create a cross-server relation
        return null;
    }
    Table destTable = (Table) foundSetManager.getTable(destDataSource);
    if (destTable == null) {
        return null;
    }
    String relationName = // $NON-NLS-1$
    Relation.INTERNAL_PREFIX + "VL-" + callingTable.getDataSource() + '-' + dataProviderID + relationPrefix + '-' + valueList.getName() + '-';
    synchronized (this) {
        Column callingColumn = callingTable.getColumn(dataProviderID);
        if (callingColumn == null) {
            return null;
        }
        Relation relation = getRelation(relationName);
        if (relation == null) {
            // create in internal relation
            String dp;
            int returnValues = valueList.getReturnDataProviders();
            if ((returnValues & 1) != 0) {
                dp = valueList.getDataProviderID1();
            } else if ((returnValues & 2) != 0) {
                dp = valueList.getDataProviderID2();
            } else if ((returnValues & 4) != 0) {
                dp = valueList.getDataProviderID3();
            } else {
                return null;
            }
            Column destColumn = destTable.getColumn(dp);
            if (destColumn == null) {
                return null;
            }
            // create internal value list relation
            QueryTable callingQTable = new QueryTable(callingTable.getSQLName(), callingTable.getDataSource(), callingTable.getCatalog(), callingTable.getSchema());
            QueryTable destQTable = new QueryTable(destTable.getSQLName(), destTable.getDataSource(), destTable.getCatalog(), destTable.getSchema());
            List<ISQLTableJoin> joins = new ArrayList<ISQLTableJoin>();
            ISQLTableJoin lastJoin = null;
            if (relationSequence == null) {
                // table values
                joins.add(lastJoin = new QueryJoin(relationName, callingQTable, destQTable, new AndCondition(), LEFT_OUTER_JOIN, false));
                if (// apply name as filter on column valuelist_name
                valueList.getUseTableFilter()) {
                    lastJoin.getCondition().addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, new QueryColumn(destQTable, DBValueList.NAME_COLUMN), valueList.getName()));
                }
            } else {
                // related values
                QueryTable primaryQTable = callingQTable;
                for (int i = 0; i < relationSequence.length; i++) {
                    Relation r = relationSequence[i];
                    QueryTable foreignQTable;
                    if (i == relationSequence.length - 1) {
                        // last one
                        foreignQTable = destQTable;
                    } else {
                        ITable relForeignTable = getTable(r.getForeignDataSource());
                        if (relForeignTable == null) {
                            return null;
                        }
                        foreignQTable = new QueryTable(relForeignTable.getSQLName(), relForeignTable.getDataSource(), relForeignTable.getCatalog(), relForeignTable.getSchema());
                    }
                    lastJoin = SQLGenerator.createJoin(this, r, primaryQTable, foreignQTable, false, new IGlobalValueEntry() {

                        public Object setDataProviderValue(String dpid, Object value) {
                            return null;
                        }

                        public Object getDataProviderValue(String dpid) {
                            // A value will be added when the relation is used, see SQLGenerator.createJoin
                            return new Placeholder(new ObjectPlaceholderKey<int[]>(null, dpid));
                        }

                        public boolean containsDataProvider(String dpid) {
                            return false;
                        }
                    });
                    joins.add(lastJoin);
                    primaryQTable = foreignQTable;
                }
            }
            // add condition for return dp id
            lastJoin.getCondition().addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, destColumn.queryColumn(destQTable), callingColumn.queryColumn(callingQTable)));
            relation = getSolutionCopy().createNewRelation(new ScriptNameValidator(this), relationName, callingTable.getDataSource(), destDataSource, LEFT_OUTER_JOIN);
            ISQLTableJoin join;
            if (joins.size() == 1) {
                join = lastJoin;
            } else {
                // combine joins
                join = new QueryCompositeJoin(relationName, joins);
            }
            relation.setRuntimeProperty(Relation.RELATION_JOIN, join);
        }
        return relation;
    }
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) Table(com.servoy.j2db.persistence.Table) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) ArrayList(java.util.ArrayList) QueryJoin(com.servoy.j2db.query.QueryJoin) QueryTable(com.servoy.j2db.query.QueryTable) AndCondition(com.servoy.j2db.query.AndCondition) Relation(com.servoy.j2db.persistence.Relation) IGlobalValueEntry(com.servoy.j2db.dataprocessing.IGlobalValueEntry) QueryCompositeJoin(com.servoy.j2db.query.QueryCompositeJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) CompareCondition(com.servoy.j2db.query.CompareCondition) ITable(com.servoy.j2db.persistence.ITable) JSONObject(org.json.JSONObject) IRootObject(com.servoy.j2db.persistence.IRootObject) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator)

Example 2 with Placeholder

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

the class QBSelect method createOperand.

IQuerySelectValue createOperand(Object value, BaseColumnType columnType, int flags) {
    if (value instanceof QBColumn) {
        return ((QBColumn) value).getQuerySelectValue();
    }
    Object val = value;
    if (value instanceof QBParameter) {
        TablePlaceholderKey key = ((QBParameter) value).getPlaceholderKey();
        Placeholder placeholder = null;
        if (query != null) {
            placeholder = query.getPlaceholder(key);
        }
        val = placeholder == null ? new Placeholder(key) : placeholder;
    } else if (columnType == null) {
        if (value instanceof Date && !(value instanceof Timestamp) && !(value instanceof Time)) {
            // make sure a date is a timestamp
            val = new Timestamp(((Date) value).getTime());
        }
    } else if (!(value instanceof IQuerySelectValue)) {
        // convert the value (especially UUID) to the type of the column
        val = getAsRightType(value, columnType, flags);
    }
    if (val instanceof IQuerySelectValue) {
        return (IQuerySelectValue) val;
    }
    return new QueryColumnValue(val, null);
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.util.Date) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 3 with Placeholder

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

the class SQLGenerator method makeQueryColumns.

private static ArrayList<IQuerySelectValue> makeQueryColumns(Iterator<Column> it, QueryTable queryTable, QueryInsert insert) {
    ArrayList<IQuerySelectValue> queryColumns = new ArrayList<IQuerySelectValue>();
    List<QueryColumn> insertColumns = new ArrayList<QueryColumn>();
    while (it.hasNext()) {
        Column column = it.next();
        ColumnInfo ci = column.getColumnInfo();
        if (ci != null && ci.isExcluded()) {
            continue;
        }
        QueryColumn queryColumn = column.queryColumn(queryTable);
        if (isBlobColumn(column)) {
            String alias = column.getDataProviderID().substring(0, Math.min(Column.MAX_SQL_OBJECT_NAME_LENGTH - (IDataServer.BLOB_MARKER_COLUMN_ALIAS.length() + 1), column.getDataProviderID().length())) + '_' + IDataServer.BLOB_MARKER_COLUMN_ALIAS;
            // make sure the alias is unique (2 media columns starting with the same name may clash here)
            char c = 'a';
            for (int i = 0; i < queryColumns.size(); i++) {
                IQuerySelectValue sv = queryColumns.get(i);
                if (alias.equals(sv.getAlias())) {
                    // alias not unique, replace first char to make it unique
                    alias = (c++) + alias.substring(1);
                    // search again
                    i = 0;
                }
            }
            queryColumns.add(new QueryColumnValue(Integer.valueOf(1), alias, true));
        } else {
            queryColumns.add(queryColumn);
        }
        if (insert != null && (ci == null || !ci.isDBManaged())) {
            insertColumns.add(queryColumn);
        }
    }
    if (insert != null) {
        insert.setColumnValues(insertColumns.toArray(new QueryColumn[insertColumns.size()]), new Placeholder(new TablePlaceholderKey(queryTable, PLACEHOLDER_INSERT_KEY)));
    }
    return queryColumns;
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) 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) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 4 with Placeholder

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

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

the class SQLGenerator method isDynamicPKSetCondition.

static boolean isDynamicPKSetCondition(ISQLCondition condition) {
    if (condition instanceof SetCondition) {
        Object values = ((SetCondition) condition).getValues();
        if (!(values instanceof Placeholder)) {
            return false;
        }
        IPlaceholderKey key = ((Placeholder) values).getKey();
        if (!(key instanceof TablePlaceholderKey)) {
            return false;
        }
        if (!SQLGenerator.PLACEHOLDER_FOUNDSET_PKS.equals(((TablePlaceholderKey) key).getName())) {
            return false;
        }
        return true;
    }
    return false;
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) SetCondition(com.servoy.j2db.query.SetCondition) IPlaceholderKey(com.servoy.j2db.query.IPlaceholderKey)

Aggregations

Placeholder (com.servoy.j2db.query.Placeholder)19 Column (com.servoy.j2db.persistence.Column)11 TablePlaceholderKey (com.servoy.j2db.query.TablePlaceholderKey)9 RepositoryException (com.servoy.j2db.persistence.RepositoryException)8 QueryColumn (com.servoy.j2db.query.QueryColumn)8 IColumn (com.servoy.j2db.persistence.IColumn)7 Relation (com.servoy.j2db.persistence.Relation)7 QuerySelect (com.servoy.j2db.query.QuerySelect)7 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)6 ArrayList (java.util.ArrayList)6 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)5 SetCondition (com.servoy.j2db.query.SetCondition)5 ServoyException (com.servoy.j2db.util.ServoyException)5 BaseQueryTable (com.servoy.base.query.BaseQueryTable)3 ColumnInfo (com.servoy.j2db.persistence.ColumnInfo)3 ITable (com.servoy.j2db.persistence.ITable)3 Table (com.servoy.j2db.persistence.Table)3 CompareCondition (com.servoy.j2db.query.CompareCondition)3 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)3 IBaseSQLCondition (com.servoy.base.query.IBaseSQLCondition)2