protected Map<String, Field<Object>> createPropertyToField() { Map<String, Field<Object>> propertyToField = new HashMap<>(propertyTypeMap.size() + propertiesMap.size()); for (Entry<String, Integer> e : propertyTypeMap.entrySet()) { String property = e.getKey(); propertyToField.put(property, DSL.field(this.tableAlias.append(property))); } for (Entry<String, IntStringPair> e : propertiesMap.entrySet()) { String name = e.getValue().getValue(); propertyToField.put(e.getKey(), DSL.field(DSL.name(name.split("\\.")))); } return propertyToField; }
@Override protected Field<Object> getField(String property) { String tagKey; if (allPropertiesAreTags) { tagKey = property; } else if (property.startsWith("tags.")) { tagKey = property.substring("tags.".length()); } else { return super.getField(property); } Name columnName = columnNameForTagKey(tagKey); return DSL.field(DataPointTagsDao.DATA_POINT_TAGS_PIVOT_ALIAS.append(columnName)); }
static Name qualify(Named qualifier, Name name) { return qualifier == null || name.qualified() ? name : qualifier.getQualifiedName().append(name); } }
private final Name generatedName() { Name t = table.getQualifiedName(); StringBuilder sb = new StringBuilder(table.getName()); for (SortField<?> f : sortFields) sb.append('_').append(f.getName()); sb.append("_idx"); if (t.qualified()) return t.qualifier().append(sb.toString()); else return name(sb.toString()); }
/** * Factory method for path aliases. */ public static final Name createPathAlias(Table<?> child, ForeignKey<?, ?> path) { Name name = DSL.name(path.getName()); if (child instanceof TableImpl) { Table<?> ancestor = ((TableImpl<?>) child).child; if (ancestor != null) name = createPathAlias(ancestor, ((TableImpl<?>) child).childPath).append(name); else name = child.getQualifiedName().append(name); } return DSL.name("alias_" + Tools.hash(name)); }
public Set<String> getTagValuesForKey(String tagKey, Map<String, String> restrictions, User user) { if (restrictions.isEmpty()) { return getTagValuesForKey(tagKey, user); } Set<String> keys = new HashSet<>(); keys.addAll(restrictions.keySet()); keys.add(tagKey); Map<String, Name> tagKeyToColumn = tagKeyToColumn(keys); Name tagKeyColumn = tagKeyToColumn.get(tagKey); List<Condition> conditions = restrictions.entrySet().stream().map(e -> { return DSL.field(DATA_POINT_TAGS_PIVOT_ALIAS.append(tagKeyToColumn.get(e.getKey()))).eq(e.getValue()); }).collect(Collectors.toCollection(ArrayList::new)); Table<Record> from = createTagPivotSql(tagKeyToColumn).asTable().as(DATA_POINT_TAGS_PIVOT_ALIAS); if (!user.hasAdminPermission()) { from = joinPointPermissions(from, PIVOT_ALIAS_DATA_POINT_ID, user); } Select<Record1<String>> result = this.create .selectDistinct(DSL.field(DATA_POINT_TAGS_PIVOT_ALIAS.append(tagKeyColumn), String.class)) .from(from) .where(DSL.and(conditions)); try (Stream<Record1<String>> stream = result.stream()) { return stream.map(r -> r.value1()).collect(Collectors.toSet()); } }
/** * Gets data points for a set of tags that a user has access to (i.e. readPermission, setPermission or the datasource editPermission). * * @param restrictions * @param user * @param callback * @param sort (may be null) * @param limit (may be null) * @param offset (may be null) */ public void dataPointsForTags(Map<String, String> restrictions, User user, MappedRowCallback<DataPointVO> callback, List<SortField<Object>> sort, Integer limit, Integer offset) { if (restrictions.isEmpty()) { throw new IllegalArgumentException("restrictions should not be empty"); } Map<String, Name> tagKeyToColumn = DataPointTagsDao.getInstance().tagKeyToColumn(restrictions.keySet()); List<Condition> conditions = restrictions.entrySet().stream().map(e -> { return DSL.field(DATA_POINT_TAGS_PIVOT_ALIAS.append(tagKeyToColumn.get(e.getKey()))).eq(e.getValue()); }).collect(Collectors.toCollection(ArrayList::new)); if (!user.hasAdminPermission()) { conditions.add(this.userHasPermission(user)); } Table<Record> pivotTable = DataPointTagsDao.getInstance().createTagPivotSql(tagKeyToColumn).asTable().as(DATA_POINT_TAGS_PIVOT_ALIAS); SelectOnConditionStep<Record> select = this.create.select(this.fields).from(this.joinedTable).leftJoin(pivotTable) .on(DataPointTagsDao.PIVOT_ALIAS_DATA_POINT_ID.eq(ID)); this.customizedQuery(select, DSL.and(conditions), sort, limit, offset, callback); }
public Set<String> getTagValuesForKey(String tagKey, ASTNode restrictions, User user) { RQLToConditionWithTagKeys visitor = new RQLToConditionWithTagKeys(); Name tagKeyColumn = visitor.columnNameForTagKey(tagKey); List<Condition> conditionList = new ArrayList<>(); ConditionSortLimitWithTagKeys conditions = visitor.visit(restrictions); if (conditions.getCondition() != null) { conditionList.add(conditions.getCondition()); } Map<String, Name> tagKeyToColumn = conditions.getTagKeyToColumn(); Table<Record> from = createTagPivotSql(tagKeyToColumn).asTable().as(DATA_POINT_TAGS_PIVOT_ALIAS); if (!user.hasAdminPermission()) { from = joinPointPermissions(from, PIVOT_ALIAS_DATA_POINT_ID, user); } SelectJoinStep<Record1<String>> query = this.create .selectDistinct(DSL.field(DATA_POINT_TAGS_PIVOT_ALIAS.append(tagKeyColumn), String.class)) .from(from); Select<Record1<String>> result = query; if (!conditionList.isEmpty()) { result = query.where(DSL.and(conditionList)); } try (Stream<Record1<String>> stream = result.stream()) { return stream.map(r -> r.value1()).collect(Collectors.toSet()); } }
public int customizedCount(ConditionSortLimit conditions) { Condition condition = conditions.getCondition(); SelectSelectStep<Record1<Integer>> count; if (this.pkColumn != null && !this.pkColumn.isEmpty()) { count = this.create.select(DSL.countDistinct(DSL.field(tableAlias.append(this.pkColumn)))); } else { count = this.create.selectCount(); } SelectJoinStep<Record1<Integer>> select; if (condition == null) { select = count.from(this.table.as(tableAlias)); } else { select = count.from(this.joinedTable); select = joinTables(select, conditions); } return customizedCount(select, condition); }