Permalink
Browse files

668: Fix table serialization

  • Loading branch information...
1 parent 7f80f74 commit d22238db96c94f922d9c3c147a7950db694562f4 Andre Rodrigues committed Mar 21, 2013
@@ -21,52 +21,19 @@ exports = module.exports;
exports.serialize = function (databaseName, collation, edition, maxSizeInGB) {
var databaseDescription = { };
- if (collation) {
- databaseDescription['d:CollationName'] = collation;
- } else {
- databaseDescription['d:CollationName'] = { '$': { 'm:null': 'true' } };
- }
-
- databaseDescription['d:CreationDate'] = { '$': { 'm:type': 'Edm.DateTime' }, '_': '0001-01-01T00:00:00' };
-
- if (edition) {
- databaseDescription['d:Edition'] = edition;
- } else {
- databaseDescription['d:Edition'] = { '$': { 'm:null': 'true' } };
- }
-
- databaseDescription['d:Id'] = { '$': { 'm:type': 'Edm.Int32' }, '_': '0' };
- databaseDescription['d:IsFederationRoot'] = { '$': { 'm:type': 'Edm.Boolean', 'm:null': 'true' } };
- databaseDescription['d:IsReadonly'] = { '$': { 'm:type': 'Edm.Boolean' }, '_': 'false' };
- databaseDescription['d:IsRecursiveTriggersOn'] = { '$': { 'm:type': 'Edm.Boolean', 'm:null': 'true' } };
- databaseDescription['d:IsSystemObject'] = { '$': { 'm:type': 'Edm.Boolean' }, '_': 'false' };
-
- if (maxSizeInGB) {
- databaseDescription['d:MaxSizeGB'] = { '$': { 'm:type': 'Edm.Int32' }, '_': maxSizeInGB };
- } else {
- databaseDescription['d:MaxSizeGB'] = { '$': { 'm:type': 'Edm.Int32', 'm:null': 'true' } };
- }
-
- if (databaseName) {
- databaseDescription['d:Name'] = databaseName;
- }
-
- databaseDescription['d:SizeMB'] = { '$': { 'm:type': 'Edm.Decimal' }, '_': '0' };
- databaseDescription['d:Status'] = { '$': { 'm:type': 'Edm.Int32' }, '_': '0' };
-
- var atom = {
- 'title': '',
- 'updated': new Date().toISOString(),
- 'author': {
- name: ''
- },
- 'id': '',
- 'content': {
- '$': { type: 'application/xml' },
- 'm:properties': databaseDescription
- }
- };
+ databaseDescription['CollationName'] = { '$': { 'type': 'Edm.String' }, '_': collation };
+ databaseDescription['CreationDate'] = { '$': { 'm:type': 'Edm.DateTime' }, '_': '0001-01-01T00:00:00' };
+ databaseDescription['Edition'] = { '$': { 'type': 'Edm.String' }, '_': edition };
+ databaseDescription['Id'] = { '$': { 'type': 'Edm.Int32' }, '_': 0 };
+ databaseDescription['IsFederationRoot'] = { '$': { 'type': 'Edm.Boolean' }, '_': null };
+ databaseDescription['IsReadonly'] = { '$': { 'type': 'Edm.Boolean' }, '_': false };
+ databaseDescription['IsRecursiveTriggersOn'] = { '$': { 'type': 'Edm.Boolean' }, '_': null };
+ databaseDescription['IsSystemObject'] = { '$': { 'type': 'Edm.Boolean' }, '_': false };
+ databaseDescription['MaxSizeGB'] = { '$': { 'type': 'Edm.Int32' }, '_': maxSizeInGB };
+ databaseDescription['Name'] = { '$': { 'type': 'Edm.String' }, '_': databaseName };
+ databaseDescription['SizeMB'] = { '$': { 'type': 'Edm.Decimal' }, '_': 0 };
+ databaseDescription['Status'] = { '$': { 'type': 'Edm.Int32' }, '_': 0 };
var odataHandler = new OdataHandler();
- return odataHandler.serialize(atom);
-};
+ return odataHandler.serialize(databaseDescription);
+};
@@ -15,47 +15,12 @@
// Module dependencies.
var OdataHandler = require('../../../util/odatahandler');
-var Constants = require('../../../util/constants');
exports = module.exports;
exports.serialize = function (entity) {
- var properties = {};
-
- for (var name in entity) {
- if (name !== '_') {
-
- if (entity[name] &&
- entity[name][Constants.XML_METADATA_MARKER] &&
- entity[name][Constants.XML_METADATA_MARKER].type) {
-
- properties[OdataHandler.NSDATA + ':' + name] = entity[name];
- properties[OdataHandler.NSDATA + ':' + name][Constants.XML_METADATA_MARKER][OdataHandler.NSMETA + ':type'] = properties[OdataHandler.NSDATA + ':' + name][Constants.XML_METADATA_MARKER].type;
-
- delete properties[OdataHandler.NSDATA + ':' + name][Constants.XML_METADATA_MARKER].type;
- }
- else {
- properties[OdataHandler.NSDATA + ':' + name] = entity[name];
- }
- }
- }
-
- var atomEntity = {
- 'title': '',
- 'updated': new Date().toISOString(),
- 'author': {
- name: ''
- },
- 'id': entity.id ? entity.id : '',
- 'content': {
- '$': { type: 'application/xml' }
- }
- };
-
- atomEntity.content[OdataHandler.NSMETA + ':properties'] = properties;
-
var odataHandler = new OdataHandler();
- return odataHandler.serialize(atomEntity);
+ return odataHandler.serialize(entity);
};
exports.parse = function (entityXml) {
@@ -19,23 +19,8 @@ var OdataHandler = require('../../../util/odatahandler');
exports = module.exports;
exports.serialize = function (tableName) {
- var atomTable = {
- 'title': '',
- 'updated': new Date().toISOString(),
- 'author': {
- name: ''
- },
- 'id': '',
- 'content': {
- '$': { type: 'application/xml' },
- 'm:properties': {
- 'd:TableName': tableName
- }
- }
- };
-
var odataHandler = new OdataHandler();
- return odataHandler.serialize(atomTable);
+ return odataHandler.serialize({ TableName: tableName }, true);
};
exports.parse = function (tableXml) {
View
@@ -16,8 +16,6 @@
var _ = require('underscore');
var azureutil = require('./util');
-var dateFormat = require('dateformat');
-
/**
* Get the Edm type of an object.
*
@@ -26,7 +24,7 @@ var dateFormat = require('dateformat');
*/
exports.propertyType = function (value) {
if (_.isNumber(value)) {
- if (azureutil.stringIsInt(value)) {
+ if (azureutil.objectIsInt(value)) {
if (Math.abs(value) < Math.pow(2, 31)) {
return 'Edm.Int32';
} else {
@@ -65,6 +63,12 @@ exports.serializeValue = function (type, value) {
}
return '';
+ case 'Edm.DateTime':
+ if (!_.isDate(value)) {
+ value = new Date(value);
+ }
+
+ return value.toISOString();
default:
return value.toString();
}
@@ -121,9 +125,7 @@ exports.serializeQueryValue = function (value) {
if (_.isNumber(value)) {
return value.toString();
} else if (_.isDate(value)) {
- var dateValue = dateFormat(value, 'yyyy-mm-dd') + 'T' + dateFormat(value, 'HH:MM:sso');
- dateValue = dateValue.substr(0, dateValue.length - 2) + ':' + dateValue.substr(dateValue.length - 2);
- return 'datetime\'' + dateValue + '\'';
+ return 'datetime\'' + value.toISOString() + '\'';
} else if (_.isBoolean(value)) {
return value ? 'true' : 'false';
} else {
View
@@ -39,22 +39,37 @@ function _writeElementValue(parentElement, name, value) {
!_.isDate(value)) {
if (Array.isArray(value) && value.length > 0) {
+ // Example:
+ // JSON: element: [ { property1: 'hi there' }, { property2: 'hello there' } ]
+ // XML: <element><property1>hi there</property1></element><element><property2>hello there</property2></element>
+
Object.keys(value).forEach(function (i) {
parentElement = _writeElementValue(parentElement, name, value[i]);
});
// For an array no element was actually added at this level, so skip uping level.
ignored = true;
- } else if (_.isObject(value)) {
+ } else if (value[Constants.XML_METADATA_MARKER] !== undefined &&
+ value[Constants.XML_VALUE_MARKER] !== undefined) {
+ // Example:
+ // JSON: element: { '$': { 'm:type' = 'Edm.String' }, '_': 'hi there' }
+ // XML: <element m:type="Edm.String">hi there</element>
+
+ parentElement = parentElement.ele(propertyTagName);
+ if (!azureutil.stringIsEmpty(value[Constants.XML_VALUE_MARKER])) {
+ parentElement = parentElement.txt(value[Constants.XML_VALUE_MARKER]);
+ }
+ } else {
+ // Example:
+ // JSON: element: { '$': { 'metadata' = 'value' }, 'property1': 'hi there', 'property2': 'hello there' }
+ // XML: <element metadata="value"><property1>hi there</property1><property2>hello there</property2></element>
+
parentElement = parentElement.ele(propertyTagName);
for (var propertyName in value) {
if (propertyName !== Constants.XML_METADATA_MARKER && value.hasOwnProperty(propertyName)) {
parentElement = _writeElementValue(parentElement, propertyName, value[propertyName]);
}
}
- } else {
- // Ignoring complex elements
- ignored = true;
}
} else {
parentElement = parentElement.ele(propertyTagName);
Oops, something went wrong.

0 comments on commit d22238d

Please sign in to comment.