Skip to content

Commit a7befe8

Browse files
author
Diego van Haaster
committed
Combine
1 parent e4ed43c commit a7befe8

File tree

7 files changed

+135
-1
lines changed

7 files changed

+135
-1
lines changed

projects/angular-odata/src/lib/resources/query/expressions/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export abstract class Expression<T> implements Renderable {
3434
abstract clone(): Expression<T>;
3535

3636
children() {
37-
return this._children;
37+
return [...this._children];
3838
}
3939

4040
length() {
@@ -47,6 +47,7 @@ export abstract class Expression<T> implements Renderable {
4747
children: this._children.map((c) => c.toJson()),
4848
};
4949
}
50+
5051
resolve(parser: any) {
5152
return parser;
5253
}

projects/angular-odata/src/lib/resources/query/expressions/expand.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,8 @@ export class ExpandExpression<T> extends Expression<T> {
273273
if (opts !== undefined) opts(node);
274274
return this._add(node);
275275
}
276+
277+
combine(expression: ExpandExpression<T>): ExpandExpression<T> {
278+
return this._add(expression);
279+
}
276280
}

projects/angular-odata/src/lib/resources/query/expressions/filter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,9 @@ export class FilterExpression<F> extends Expression<F> {
284284
isof(left: any, type?: string): FilterExpression<F> {
285285
return this._add(syntax.isof(left, type));
286286
}
287+
288+
combine(exp: FilterExpression<F>, connector: FilterConnector = "and"): FilterExpression<F> {
289+
return this._add(exp, connector);
290+
}
291+
287292
}

projects/angular-odata/src/lib/resources/query/expressions/orderby.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class OrderByField implements Renderable {
5151
this.order,
5252
);
5353
}
54+
5455
resolve(parser: any) {
5556
return parser;
5657
}
@@ -138,4 +139,8 @@ export class OrderByExpression<T> extends Expression<T> {
138139
descending(field: any) {
139140
return this._add(new OrderByField(field, 'desc'));
140141
}
142+
143+
combine(expression: OrderByExpression<T>): OrderByExpression<T> {
144+
return this._add(expression);
145+
}
141146
}

projects/angular-odata/src/lib/resources/query/expressions/search.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export class SearchExpression<T> extends Expression<T> {
180180
negated: json['negated'],
181181
});
182182
}
183+
183184
connector() {
184185
return this._connector;
185186
}
@@ -209,4 +210,8 @@ export class SearchExpression<T> extends Expression<T> {
209210
term(value: any) {
210211
return this._add(new SearchTerm(value));
211212
}
213+
214+
combine(expression: SearchExpression<T>, connector: SearchConnector = 'AND'): SearchExpression<T> {
215+
return this._add(expression, connector);
216+
}
212217
}

projects/angular-odata/src/lib/resources/query/expressions/select.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,8 @@ export class SelectExpression<T> extends Expression<T> {
8787
fields.forEach((f) => this._add(f));
8888
return this;
8989
}
90+
91+
combine(expression: SelectExpression<T>): SelectExpression<T> {
92+
return this._add(expression);
93+
}
9094
}

projects/angular-odata/src/lib/resources/query/handlers.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,116 @@ export class ODataQueryOptionsHandler<T> {
679679
this.paging(options);
680680
}
681681

682+
/**
683+
* Combine the given query options with the current query.
684+
* @param options The query to be combined.
685+
*/
686+
combine(options: ODataQueryArguments<T>) {
687+
if (options.select !== undefined) {
688+
if (options.select instanceof SelectExpression) {
689+
const current = this.options.expression(QueryOption.select) as SelectExpression<T>;
690+
if (current === undefined) {
691+
this.options.expression(
692+
QueryOption.select,
693+
options.select as SelectExpression<T>,
694+
);
695+
} else {
696+
current.combine(options.select as SelectExpression<T>);
697+
}
698+
} else if (options.select !== null) {
699+
const current = this.options.option(QueryOption.select);
700+
if (current === undefined) {
701+
this.options.option(QueryOption.select, options.select);
702+
} else {
703+
this.options.option(QueryOption.select, [current, options.select]);
704+
}
705+
} else {
706+
this.options.remove(QueryOption.select);
707+
}
708+
}
709+
if (options.expand !== undefined) {
710+
if (options.expand instanceof ExpandExpression) {
711+
const current = this.options.expression(QueryOption.expand) as ExpandExpression<T>;
712+
if (current === undefined) {
713+
this.options.expression(
714+
QueryOption.expand,
715+
options.expand as ExpandExpression<T>,
716+
);
717+
} else {
718+
current.combine(options.expand as ExpandExpression<T>);
719+
}
720+
} else if (options.expand !== null) {
721+
const current = this.options.option(QueryOption.expand);
722+
if (current === undefined) {
723+
this.options.option(QueryOption.expand, options.expand);
724+
}
725+
} else {
726+
this.options.remove(QueryOption.expand);
727+
}
728+
}
729+
if (options.search !== undefined) {
730+
if (options.search instanceof SearchExpression) {
731+
const current = this.options.expression(QueryOption.search) as SearchExpression<T>;
732+
if (current === undefined) {
733+
this.options.expression(
734+
QueryOption.search,
735+
options.search as SearchExpression<T>,
736+
);
737+
} else {
738+
current.combine(options.search as SearchExpression<T>);
739+
}
740+
} else if (options.search !== null) {
741+
const current = this.options.option(QueryOption.search);
742+
if (current === undefined) {
743+
this.options.option(QueryOption.search, options.search);
744+
}
745+
} else {
746+
this.options.remove(QueryOption.search);
747+
}
748+
}
749+
if (options.filter !== undefined) {
750+
if (options.filter instanceof FilterExpression) {
751+
const current = this.options.expression(QueryOption.filter) as FilterExpression<T>;
752+
if (current === undefined) {
753+
this.options.expression(
754+
QueryOption.filter,
755+
options.filter as FilterExpression<T>,
756+
);
757+
} else {
758+
current.combine(options.filter as FilterExpression<T>);
759+
}
760+
} else if (options.filter !== null) {
761+
const current = this.options.option(QueryOption.filter);
762+
if (current === undefined) {
763+
this.options.option(QueryOption.filter, options.filter);
764+
}
765+
} else {
766+
this.options.remove(QueryOption.filter);
767+
}
768+
}
769+
if (options.orderBy !== undefined) {
770+
if (options.orderBy instanceof OrderByExpression) {
771+
const current = this.options.expression(QueryOption.orderBy) as OrderByExpression<T>;
772+
if (current === undefined) {
773+
this.options.expression(
774+
QueryOption.orderBy,
775+
options.orderBy as OrderByExpression<T>,
776+
);
777+
} else {
778+
current.combine(options.orderBy as OrderByExpression<T>);
779+
}
780+
} else if (options.orderBy !== null) {
781+
const current = this.options.option(QueryOption.filter);
782+
if (current === undefined) {
783+
this.options.option(QueryOption.orderBy, options.orderBy);
784+
}
785+
} else {
786+
this.options.remove(QueryOption.orderBy);
787+
}
788+
}
789+
this.paging(options);
790+
}
791+
682792
toJson() {
683793
return this.options.toJson();
684794
}

0 commit comments

Comments
 (0)