-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathb00_breeze.uriBuilder.odata.js
225 lines (187 loc) · 6.96 KB
/
b00_breeze.uriBuilder.odata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
(function (factory) {
if (typeof breeze === "object") {
factory(breeze);
} else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node: hard-coded dependency on "breeze-client"
factory(require("breeze-client"));
} else if (typeof define === "function" && define["amd"]) {
// AMD anonymous module with hard-coded dependency on "breeze-client"
define(["breeze-client"], factory);
}
}(function (breeze) {
"use strict";
var EntityType = breeze.EntityType;
var toODataFragmentVisitor;
var ctor = function UriBuilderODataAdapter() {
this.name = "odata";
};
var proto = ctor.prototype;
proto.initialize = function() {};
proto.buildUri = function (entityQuery, metadataStore) {
// force entityType validation;
var entityType = entityQuery._getFromEntityType(metadataStore, false);
if (!entityType) {
// anonymous type but still has naming convention info avail
entityType = new EntityType(metadataStore);
}
var queryOptions = {};
queryOptions["$filter"] = toWhereODataFragment(entityQuery.wherePredicate);
queryOptions["$orderby"] = toOrderByODataFragment(entityQuery.orderByClause);
if (entityQuery.skipCount) {
queryOptions["$skip"] = entityQuery.skipCount;
}
if (entityQuery.takeCount != null) {
queryOptions["$top"] = entityQuery.takeCount;
}
queryOptions["$expand"] = toExpandODataFragment(entityQuery.expandClause);
queryOptions["$select"] = toSelectODataFragment(entityQuery.selectClause);
if (entityQuery.inlineCountEnabled) {
queryOptions["$inlinecount"] = "allpages";
}
var qoText = toQueryOptionsString(queryOptions);
return entityQuery.resourceName + qoText;
// private methods to this func.
function toWhereODataFragment(wherePredicate) {
if (!wherePredicate) return undefined;
// validation occurs inside of the toODataFragment call here.
return wherePredicate.visit({ entityType: entityType}, toODataFragmentVisitor );
}
function toOrderByODataFragment(orderByClause) {
if (!orderByClause) return undefined;
orderByClause.validate(entityType);
var strings = orderByClause.items.map(function (item) {
return entityType.clientPropertyPathToServer(item.propertyPath, "/") + (item.isDesc ? " desc" : "");
});
// should return something like CompanyName,Address/City desc
return strings.join(',');
}
function toSelectODataFragment(selectClause) {
if (!selectClause) return undefined;
selectClause.validate(entityType);
var frag = selectClause.propertyPaths.map(function (pp) {
return entityType.clientPropertyPathToServer(pp, "/");
}).join(",");
return frag;
}
function toExpandODataFragment(expandClause) {
if (!expandClause) return undefined;
// no validate on expand clauses currently.
// expandClause.validate(entityType);
var frag = expandClause.propertyPaths.map(function (pp) {
return entityType.clientPropertyPathToServer(pp, "/");
}).join(",");
return frag;
}
function toQueryOptionsString(queryOptions) {
var qoStrings = [];
for (var qoName in queryOptions) {
var qoValue = queryOptions[qoName];
if (qoValue !== undefined) {
if (qoValue instanceof Array) {
qoValue.forEach(function (qov) {
qoStrings.push(qoName + "=" + encodeURIComponent(qov));
});
} else {
qoStrings.push(qoName + "=" + encodeURIComponent(qoValue));
}
}
}
if (qoStrings.length > 0) {
return "?" + qoStrings.join("&");
} else {
return "";
}
}
};
breeze.Predicate.prototype.toODataFragment = function(context) {
return this.visit( context, toODataFragmentVisitor);
}
toODataFragmentVisitor = (function () {
var visitor = {
passthruPredicate: function () {
return this.value;
},
unaryPredicate: function (context) {
var predVal = this.pred.visit(context);
return odataOpFrom(this) + " " + "(" + predVal + ")";
},
binaryPredicate: function (context) {
var expr1Val = this.expr1.visit(context);
var expr2Val = this.expr2.visit(context);
var prefix = context.prefix;
if (prefix) {
expr1Val = prefix + "/" + expr1Val;
}
var odataOp = odataOpFrom(this);
if (this.op.key === 'in') {
var result = expr2Val.map(function (v) {
return "(" + expr1Val + " eq " + v + ")";
}).join(" or ");
return result;
} else if (this.op.isFunction) {
if (odataOp === "substringof") {
return odataOp + "(" + expr2Val + "," + expr1Val + ") eq true";
} else {
return odataOp + "(" + expr1Val + "," + expr2Val + ") eq true";
}
} else {
return expr1Val + " " + odataOp + " " + expr2Val;
}
},
andOrPredicate: function (context) {
var result = this.preds.map(function (pred) {
var predVal = pred.visit(context);
return "(" + predVal + ")";
}).join(" " + odataOpFrom(this) + " ");
return result;
},
anyAllPredicate: function (context) {
var exprVal = this.expr.visit(context);
if (!this.pred.op) {
return exprVal + "/" + odataOpFrom(this) + "()";
}
var prefix = context.prefix;
if (prefix) {
exprVal = prefix + "/" + exprVal;
prefix = "x" + (parseInt(prefix.substring(1)) + 1);
} else {
prefix = "x1";
}
// need to create a new context because of 'prefix'
var newContext = breeze.core.extend({}, context);
newContext.entityType = this.expr.dataType;
newContext.prefix = prefix;
var newPredVal = this.pred.visit(newContext);
return exprVal + "/" + odataOpFrom(this) + "(" + prefix + ": " + newPredVal + ")";
},
litExpr: function () {
if (Array.isArray(this.value)) {
return this.value.map(function(v) { return this.dataType.fmtOData(v)}, this);
} else {
return this.dataType.fmtOData(this.value);
}
},
propExpr: function (context) {
var entityType = context.entityType;
// '/' is the OData path delimiter
return entityType ? entityType.clientPropertyPathToServer(this.propertyPath, "/") : this.propertyPath;
},
fnExpr: function (context) {
var exprVals = this.exprs.map(function(expr) {
return expr.visit(context);
});
return this.fnName + "(" + exprVals.join(",") + ")";
}
};
var _operatorMap = {
'contains': 'substringof'
};
function odataOpFrom(node) {
var op = node.op.key;
var odataOp = _operatorMap[op];
return odataOp || op;
}
return visitor;
}());
breeze.config.registerAdapter("uriBuilder", ctor);
}));