Skip to content
This repository was archived by the owner on Apr 11, 2023. It is now read-only.

Commit b239b99

Browse files
borzavlazarv
authored andcommitted
ADD: OData V4 alpha
ADD: withCount alias for withInlineCount on Queryable
1 parent 05116e8 commit b239b99

File tree

6 files changed

+146
-81
lines changed

6 files changed

+146
-81
lines changed

JaySvcUtil/JaySvcUtil.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,8 @@ $data.Class.define('$data.MetadataLoaderClass', null, null, {
612612
"http://schemas.microsoft.com/ado/2008/09/edm": "V2",
613613
"http://schemas.microsoft.com/ado/2009/11/edm": "V3",
614614
"http://schemas.microsoft.com/ado/2007/05/edm": "V11",
615-
"http://schemas.microsoft.com/ado/2009/08/edm": "V22"
615+
"http://schemas.microsoft.com/ado/2009/08/edm": "V22",
616+
"http://docs.oasis-open.org/odata/ns/edm": "V4"
616617
}
617618
},
618619
_maxDataServiceVersions: {
@@ -621,7 +622,8 @@ $data.Class.define('$data.MetadataLoaderClass', null, null, {
621622
"http://schemas.microsoft.com/ado/2008/09/edm": "2.0",
622623
"http://schemas.microsoft.com/ado/2009/11/edm": "3.0",
623624
"http://schemas.microsoft.com/ado/2007/05/edm": "2.0",
624-
"http://schemas.microsoft.com/ado/2009/08/edm": "2.0"
625+
"http://schemas.microsoft.com/ado/2009/08/edm": "2.0",
626+
"http://docs.oasis-open.org/odata/ns/edm": "4.0"
625627
}
626628
},
627629
_supportedODataVersionXSLT: {
@@ -1109,6 +1111,7 @@ $data.Class.define('$data.MetadataLoaderClass', null, null, {
11091111
" <xsl:variable name=\"memberDefinition\">\r\n" +
11101112
" <xsl:if test=\"parent::edm:EntityType/edm:Key/edm:PropertyRef[@Name = current()/@Name]\"><attribute name=\"key\">true</attribute></xsl:if>\r\n" +
11111113
" <xsl:apply-templates select=\"@*[local-name() != 'Name']\" mode=\"render-field\" />\r\n" +
1114+
" <xsl:if test=\"local-name() = 'NavigationProperty' and current()[not(@Partner) and current()/@Type]\"><attribute name=\"inverseProperty\">'$$unbound'</attribute></xsl:if>\r\n" +
11121115
" </xsl:variable>'<xsl:value-of select=\"@Name\"/>': { <xsl:choose><xsl:when test=\"function-available('msxsl:node-set')\"><xsl:for-each select=\"msxsl:node-set($memberDefinition)/*\">'<xsl:if test=\"@extended = 'true'\">$</xsl:if><xsl:value-of select=\"@name\"/>':<xsl:value-of select=\".\"/>\r\n" +
11131116
" <xsl:if test=\"position() != last()\">,<xsl:text> </xsl:text>\r\n" +
11141117
" </xsl:if> </xsl:for-each></xsl:when>\r\n" +
@@ -1121,6 +1124,10 @@ $data.Class.define('$data.MetadataLoaderClass', null, null, {
11211124
" <xsl:template match=\"@Name\" mode=\"render-field\">\r\n" +
11221125
" </xsl:template>\r\n" +
11231126
"\r\n" +
1127+
" <xsl:template match=\"@Partner\" mode=\"render-field\">\r\n" +
1128+
" <attribute name=\"inverseProperty\">'<xsl:value-of select=\".\"/>'</attribute>\r\n" +
1129+
" </xsl:template>\r\n" +
1130+
"\r\n" +
11241131
" <xsl:template match=\"@Type\" mode=\"render-field\">\r\n" +
11251132
" <xsl:choose>\r\n" +
11261133
" <xsl:when test=\"starts-with(., 'Collection')\">\r\n" +

Types/Queryable.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,9 @@ $data.Class.define('$data.Queryable', null, null,
760760
var inlineCountExp = Container.createInlineCountExpression(this.expression, constExp);
761761
return Container.createQueryable(this, inlineCountExp);
762762
},
763+
withCount: function (selector) {
764+
return this.withInlineCount(selector);
765+
},
763766

764767
removeAll: function (onResult, transaction) {
765768
/// <summary>Delete the query result and returns the number of deleted entities in a query as the callback parameter.</summary>

Types/StorageProviders/modelBinderConfigCompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ $C('$data.modelBinder.ModelBinderConfigCompiler', $data.Expressions.EntityExpres
9393
var builder = Container.createqueryBuilder();
9494
builder.modelBinderConfig['$type'] = $data.Array;
9595
if (this._isoDataProvider) {
96-
builder.modelBinderConfig['$selector'] = ['json:d.results', 'json:d', 'json:results'];
96+
builder.modelBinderConfig['$selector'] = ['json:d.results', 'json:d', 'json:results', 'json:value'];
9797
}
9898
builder.modelBinderConfig['$item'] = {};
9999
builder.selectModelBinderProperty('$item');

Types/StorageProviders/oData/oDataCompiler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ $C('$data.storageProviders.oData.oDataCompiler', $data.Expressions.EntityExpress
4343
query.postData = queryFragments.postData;
4444
var result = {
4545
queryText: queryText,
46-
withInlineCount: '$inlinecount' in queryFragments,
46+
withInlineCount: '$inlinecount' in queryFragments || '$count' in queryFragments,
4747
method: queryFragments.method || 'GET',
4848
postData: queryFragments.postData,
4949
isBatchExecuteQuery: queryFragments._isBatchExecuteQuery,
@@ -144,7 +144,11 @@ $C('$data.storageProviders.oData.oDataCompiler', $data.Expressions.EntityExpress
144144
},
145145
VisitInlineCountExpression: function (expression, context) {
146146
this.Visit(expression.source, context);
147-
context["$inlinecount"] = expression.selector.value;
147+
if (this.provider.providerConfiguration.maxDataServiceVersion === "4.0") {
148+
context["$count"] = expression.selector.value === 'allpages';
149+
} else {
150+
context["$inlinecount"] = expression.selector.value;
151+
}
148152
},
149153
VisitEntitySetExpression: function (expression, context) {
150154
context.urlText += "/" + expression.instance.tableName;

Types/StorageProviders/oData/oDataProvider.js

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
var datajsPatch;
3-
datajsPatch = function () {
3+
datajsPatch = function (OData) {
44
// just datajs-1.1.0
55
if (OData && OData.jsonHandler && 'useJsonLight' in OData.jsonHandler && typeof datajs === 'object' && !datajs.version) {
66
$data.Trace.log('!!!!!!! - patch datajs 1.1.0');
@@ -25,11 +25,6 @@ datajsPatch = function () {
2525
$C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null,
2626
{
2727
constructor: function (cfg, ctx) {
28-
if (typeof OData === 'undefined') {
29-
Guard.raise(new Exception('datajs is required', 'Not Found!'));
30-
}
31-
datajsPatch();
32-
3328
this.SqlCommands = [];
3429
this.context = ctx;
3530
this.providerConfiguration = $data.typeSystem.extend({
@@ -48,6 +43,21 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
4843
UpdateMethod: 'PATCH'
4944
}, cfg);
5045

46+
if (this.providerConfiguration.maxDataServiceVersion === "4.0") {
47+
if (typeof odatajs === 'undefined' || typeof odatajs.oData === 'undefined') {
48+
Guard.raise(new Exception('odatajs is required', 'Not Found!'));
49+
} else {
50+
this.oData = odatajs.oData
51+
}
52+
} else {
53+
if (typeof OData === 'undefined') {
54+
Guard.raise(new Exception('datajs is required', 'Not Found!'));
55+
} else {
56+
this.oData = OData;
57+
datajsPatch(this.oData);
58+
}
59+
}
60+
5161
this.fixkDataServiceVersions(cfg);
5262

5363
if (this.context && this.context._buildDbType_generateConvertToFunction && this.buildDbType_generateConvertToFunction) {
@@ -94,7 +104,7 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
94104
//}
95105

96106
this.context.prepareRequest.call(this, requestData);
97-
OData.request.apply(this, requestData);
107+
this.oData.request.apply(this, requestData);
98108
} else {
99109
callBack.success(that.context);
100110
}
@@ -174,30 +184,31 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
174184
var schema = this.context;
175185

176186
var that = this;
187+
var countProperty = "__count";
188+
if (this.providerConfiguration.maxDataServiceVersion === "4.0") {
189+
countProperty = "@odata.count";
190+
}
191+
177192
var requestData = [
178193
{
179194
requestUri: this.providerConfiguration.oDataServiceHost + sql.queryText,
180195
method: sql.method,
181196
data: sql.postData,
182197
headers: {
183-
MaxDataServiceVersion: this.providerConfiguration.maxDataServiceVersion
184198
}
185199
},
186200
function (data, textStatus, jqXHR) {
201+
187202
if (!data && textStatus.body && !sql.isBatchExecuteQuery) data = JSON.parse(textStatus.body);
188203
if (callBack.success) {
189204
var processSuccess = function (query, data, sql) {
190205
query.rawDataList = typeof data === 'string' ? [{ cnt: Container.convertTo(data, $data.Integer) }] : data;
191-
if (sql.withInlineCount && typeof data === 'object' && (typeof data.__count !== 'undefined' || ('d' in data && typeof data.d.__count !== 'undefined'))) {
192-
query.__count = new Number(typeof data.__count !== 'undefined' ? data.__count : data.d.__count).valueOf();
206+
if (sql.withInlineCount && typeof data === 'object' && (typeof data[countProperty] !== 'undefined' || ('d' in data && typeof data.d[countProperty] !== 'undefined'))) {
207+
query.__count = new Number(typeof data[countProperty] !== 'undefined' ? data[countProperty] : data.d[countProperty]).valueOf();
193208
}
194209
}
195210

196211
if (sql.isBatchExecuteQuery) {
197-
if (data.__batchResponses.length !== sql.subQueries.length) {
198-
return callBack.error(new Exception("Batch response count failed", "Http request failed!", textStatus));
199-
}
200-
201212
query.rawDataList = sql.subQueries;
202213
for (var i = 0; i < data.__batchResponses.length; i++) {
203214
var resp = data.__batchResponses[i];
@@ -223,10 +234,14 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
223234
function (error) {
224235
callBack.error(that.parseError(error, arguments));
225236
},
226-
sql.isBatchExecuteQuery ? OData.batchHandler : undefined
237+
sql.isBatchExecuteQuery ? this.oData.batchHandler : undefined
227238
];
228239

229-
if (this.providerConfiguration.dataServiceVersion) {
240+
if (this.providerConfiguration.maxDataServiceVersion && this.providerConfiguration.maxDataServiceVersion !== "4.0") {
241+
requestData[0].headers.MaxDataServiceVersion = this.providerConfiguration.maxDataServiceVersion;
242+
}
243+
244+
if (this.providerConfiguration.dataServiceVersion && this.providerConfiguration.maxDataServiceVersion !== "4.0") {
230245
requestData[0].headers.DataServiceVersion = this.providerConfiguration.dataServiceVersion;
231246
}
232247

@@ -246,7 +261,7 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
246261
this.context.prepareRequest.call(this, requestData);
247262
//$data.ajax(requestData);
248263
//OData.request(requestData, requestData.success, requestData.error);
249-
OData.request.apply(this, requestData);
264+
this.oData.request.apply(this, requestData);
250265
},
251266
_compile: function (queryable, params) {
252267
var compiler = new $data.storageProviders.oData.oDataCompiler();
@@ -285,10 +300,12 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
285300
request = {
286301
requestUri: this.providerConfiguration.oDataServiceHost + '/',
287302
headers: {
288-
MaxDataServiceVersion: this.providerConfiguration.maxDataServiceVersion
289303
}
290304
};
291-
if (this.providerConfiguration.dataServiceVersion) {
305+
if (this.providerConfiguration.maxDataServiceVersion && this.providerConfiguration.maxDataServiceVersion !== "4.0") {
306+
request.headers.MaxDataServiceVersion = this.providerConfiguration.maxDataServiceVersion;
307+
}
308+
if (this.providerConfiguration.dataServiceVersion && this.providerConfiguration.maxDataServiceVersion !== "4.0") {
292309
request.headers.DataServiceVersion = this.providerConfiguration.dataServiceVersion;
293310
}
294311
if (typeof this.providerConfiguration.useJsonLight !== 'undefined') {
@@ -355,7 +372,7 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
355372
//}
356373

357374
this.context.prepareRequest.call(this, requestData);
358-
OData.request.apply(this, requestData);
375+
this.oData.request.apply(this, requestData);
359376
},
360377
_saveBatch: function (independentBlocks, index2, callBack) {
361378
var batchRequests = [];
@@ -365,9 +382,11 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
365382
convertedItem.push(independentBlocks[index][i].data);
366383
var request = {};
367384
request.headers = {
368-
"Content-Id": convertedItem.length,
369-
MaxDataServiceVersion: this.providerConfiguration.maxDataServiceVersion
385+
"Content-Id": convertedItem.length
370386
};
387+
if (this.providerConfiguration.maxDataServiceVersion != "4.0") {
388+
request.headers.MaxDataServiceVersion = this.providerConfiguration.maxDataServiceVersion;
389+
}
371390
switch (independentBlocks[index][i].data.entityState) {
372391
case $data.EntityState.Unchanged: continue; break;
373392
case $data.EntityState.Added:
@@ -391,7 +410,10 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
391410
default: Guard.raise(new Exception("Not supported Entity state"));
392411
}
393412

394-
if (this.providerConfiguration.dataServiceVersion) {
413+
if (this.providerConfiguration.maxDataServiceVersion && this.providerConfiguration.maxDataServiceVersion !== "4.0") {
414+
request.headers.MaxDataServiceVersion = this.providerConfiguration.maxDataServiceVersion;
415+
}
416+
if (this.providerConfiguration.dataServiceVersion && this.providerConfiguration.maxDataServiceVersion !== "4.0") {
395417
request.headers.DataServiceVersion = this.providerConfiguration.dataServiceVersion;
396418
}
397419
batchRequests.push(request);
@@ -406,7 +428,6 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
406428
__batchRequests: [{ __changeRequests: batchRequests }]
407429
},
408430
headers: {
409-
MaxDataServiceVersion: this.providerConfiguration.maxDataServiceVersion
410431
}
411432
}, function (data, response) {
412433
if (response.statusCode == 202) {
@@ -446,9 +467,12 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
446467

447468
}, function (e) {
448469
callBack.error(that.parseError(e));
449-
}, OData.batchHandler];
470+
}, this.oData.batchHandler];
450471

451-
if (this.providerConfiguration.dataServiceVersion) {
472+
if (this.providerConfiguration.maxDataServiceVersion && this.providerConfiguration.maxDataServiceVersion != "4.0") {
473+
requestData[0].headers.MaxDataServiceVersion = this.providerConfiguration.maxDataServiceVersion;
474+
}
475+
if (this.providerConfiguration.dataServiceVersion && this.providerConfiguration.maxDataServiceVersion != "4.0") {
452476
requestData[0].headers.DataServiceVersion = this.providerConfiguration.dataServiceVersion;
453477
}
454478
if (typeof this.providerConfiguration.useJsonLight !== 'undefined') {
@@ -462,7 +486,7 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
462486
//}
463487

464488
this.context.prepareRequest.call(this, requestData);
465-
OData.request.apply(this, requestData);
489+
this.oData.request.apply(this, requestData);
466490
},
467491
reload_fromResponse: function (item, data, response) {
468492
var that = this;
@@ -510,16 +534,43 @@ $C('$data.storageProviders.oData.oDataProvider', $data.StorageProviderBase, null
510534
}, this);
511535
},
512536

513-
save_getInitData: function (item, convertedItems) {
537+
//save_getInitData: function (item, convertedItems) {
538+
// var self = this;
539+
// item.physicalData = this.context._storageModel.getStorageModel(item.data.getType()).PhysicalType.convertTo(item.data, convertedItems);
540+
// var serializableObject = {}
541+
// item.physicalData.getType().memberDefinitions.asArray().forEach(function (memdef) {
542+
// if (memdef.kind == $data.MemberTypes.navProperty || memdef.kind == $data.MemberTypes.complexProperty || (memdef.kind == $data.MemberTypes.property && !memdef.notMapped)) {
543+
// if (typeof memdef.concurrencyMode === 'undefined' && (memdef.key === true || item.data.entityState === $data.EntityState.Added || item.data.changedProperties.some(function (def) { return def.name === memdef.name; }))) {
544+
// var typeName = Container.resolveName(memdef.type);
545+
// var converter = self.fieldConverter.toDb[typeName];
546+
// serializableObject[memdef.name] = converter ? converter(item.physicalData[memdef.name]) : item.physicalData[memdef.name];
547+
// }
548+
// }
549+
// }, this);
550+
// return serializableObject;
551+
//},
552+
save_getInitData: function (item, convertedItems, isComplex, isDeep) {
514553
var self = this;
515-
item.physicalData = this.context._storageModel.getStorageModel(item.data.getType()).PhysicalType.convertTo(item.data, convertedItems);
554+
if (!isComplex) {
555+
item.physicalData = this.context._storageModel.getStorageModel(item.data.getType()).PhysicalType.convertTo(item.data, convertedItems);
556+
} else {
557+
item.physicalData = item.data;
558+
}
516559
var serializableObject = {}
517560
item.physicalData.getType().memberDefinitions.asArray().forEach(function (memdef) {
518-
if (memdef.kind == $data.MemberTypes.navProperty || memdef.kind == $data.MemberTypes.complexProperty || (memdef.kind == $data.MemberTypes.property && !memdef.notMapped)) {
519-
if (typeof memdef.concurrencyMode === 'undefined' && (memdef.key === true || item.data.entityState === $data.EntityState.Added || item.data.changedProperties.some(function (def) { return def.name === memdef.name; }))) {
520-
var typeName = Container.resolveName(memdef.type);
521-
var converter = self.fieldConverter.toDb[typeName];
522-
serializableObject[memdef.name] = converter ? converter(item.physicalData[memdef.name]) : item.physicalData[memdef.name];
561+
if (memdef.kind == $data.MemberTypes.complexProperty && item.physicalData[memdef.name]) {
562+
serializableObject[memdef.name] = self.save_getInitData({ data: item.physicalData[memdef.name] }, convertedItems, true, true);
563+
}
564+
else if (memdef.kind == $data.MemberTypes.navProperty || (memdef.kind == $data.MemberTypes.property && !memdef.notMapped)) {
565+
if (isDeep || typeof memdef.concurrencyMode === 'undefined' && (memdef.key === true || item.data.entityState === $data.EntityState.Added || (item.data.changedProperties && item.data.changedProperties.some(function (def) { return def.name === memdef.name; })))) {
566+
567+
if (memdef.kind == $data.MemberTypes.navProperty && item.physicalData[memdef.name] && this.providerConfiguration.maxDataServiceVersion === "4.0") {
568+
serializableObject[memdef.name + "@odata.bind"] = item.physicalData[memdef.name].__metadata.uri;
569+
} else {
570+
var typeName = Container.resolveName(memdef.type);
571+
var converter = self.fieldConverter.toDb[typeName];
572+
serializableObject[memdef.name] = converter ? converter(item.physicalData[memdef.name]) : item.physicalData[memdef.name];
573+
}
523574
}
524575
}
525576
}, this);

0 commit comments

Comments
 (0)