|
| 1 | +/* |
| 2 | + * Copyright (c) 2023. Smart Operating Block |
| 3 | + * |
| 4 | + * Use of this source code is governed by an MIT-style |
| 5 | + * license that can be found in the LICENSE file or at |
| 6 | + * https://opensource.org/licenses/MIT. |
| 7 | + */ |
| 8 | + |
| 9 | +package infrastructure.digitaltwins.query; |
| 10 | + |
| 11 | +/** |
| 12 | + * Helper class to construct query for Azure Digital Twins service. |
| 13 | + */ |
| 14 | +public class AdtQuery { |
| 15 | + private final String query; |
| 16 | + |
| 17 | + /** |
| 18 | + * Default constructor. |
| 19 | + */ |
| 20 | + public AdtQuery() { |
| 21 | + this.query = ""; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Private constructor used to create the query. |
| 26 | + * @param query the part of the query to pre-pend. |
| 27 | + */ |
| 28 | + private AdtQuery(final String query) { |
| 29 | + this.query = query; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Select query considering returned elements. |
| 34 | + * @param elements the elements to return from the query |
| 35 | + * @return the updated query |
| 36 | + */ |
| 37 | + public AdtQuery select(final String... elements) { |
| 38 | + return new AdtQuery("SELECT " + String.join(", ", elements)); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Select query but return only numberOfElements top elements. |
| 43 | + * @param numberOfElementsToSelect the number of top elements to return |
| 44 | + * @param elements the elements to return from the query |
| 45 | + * @return the updated query |
| 46 | + */ |
| 47 | + public AdtQuery selectTop(final int numberOfElementsToSelect, final String... elements) { |
| 48 | + return new AdtQuery("SELECT TOP(" + numberOfElementsToSelect + ") " + String.join(", ", elements)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Specify the FROM digital twins clause with its alias. This alias can be used later to refer to it. |
| 53 | + * @param alias the alias for the digital twin |
| 54 | + * @return the updated query |
| 55 | + */ |
| 56 | + public AdtQuery fromDigitalTwins(final String alias) { |
| 57 | + return new AdtQuery(this.query + " FROM DIGITALTWINS " + alias); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Join the query with a relationship, specified by its relationshipName of the srcAlias digital twin. |
| 62 | + * Specify also the dstAlias used to describe the destination digital twin. |
| 63 | + * @param dstAlias the alias of the target of the relationship |
| 64 | + * @param srcAlias the alias of the source of the relationship |
| 65 | + * @param relationshipName the name of the relationship to join |
| 66 | + * @return the updated query |
| 67 | + */ |
| 68 | + public AdtQuery joinRelationship(final String dstAlias, final String srcAlias, final String relationshipName) { |
| 69 | + return new AdtQuery(this.query + " JOIN " + dstAlias + " RELATED " + srcAlias + "." + relationshipName); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Where clause to include in the query. |
| 74 | + * @param whereClause the where clause |
| 75 | + * @return the updated query |
| 76 | + */ |
| 77 | + public AdtQuery where(final String whereClause) { |
| 78 | + return new AdtQuery(this.query + " WHERE " + whereClause); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Chain in and another where clause. |
| 83 | + * @param whereClause the where clause to chain |
| 84 | + * @return the updated query |
| 85 | + */ |
| 86 | + public AdtQuery and(final String whereClause) { |
| 87 | + return new AdtQuery(this.query + " AND " + whereClause); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Method used to export the final query. |
| 92 | + * @return the query |
| 93 | + */ |
| 94 | + public String toQuery() { |
| 95 | + return this.query; |
| 96 | + } |
| 97 | +} |
0 commit comments