diff --git a/lib/connection/result/column.js b/lib/connection/result/column.js index e0d1717ef..b8eddbbec 100644 --- a/lib/connection/result/column.js +++ b/lib/connection/result/column.js @@ -14,6 +14,7 @@ const dateTimeFormatConverter = require('./datetime_format_converter'); const bigInt = require('big-integer'); const moment = require('moment'); const momentTimezone = require('moment-timezone'); +const util = require('../../util'); /** * Creates a new Column. @@ -296,7 +297,8 @@ function convertRawBoolean(rawColumnValue) { /** * Converts a raw column value of structured type object to javascript Object * - * @param {String} rawColumnValue + * @param {Object} json + * @param {Object} context * * @returns {Object} */ @@ -317,6 +319,14 @@ function convertJsonObject(json, context) { } } +/** + * Converts a raw column value of structured type array to javascript Object + * + * @param {Object} json + * @param {Object} context + * + * @returns {Object} + */ function convertJsonArray(json, context) { let result = []; if (context.fieldsMetadata) { @@ -329,7 +339,14 @@ function convertJsonArray(json, context) { return result; } - +/** + * Converts a raw column value of structured type map to javascript Object + * + * @param {Object} json + * @param {Object} context + * + * @returns {Object} + */ function convertJsonMap(json, context) { const result = new Map; if (Array.isArray(context.fieldsMetadata) && context.fieldsMetadata.length === 2) { @@ -346,37 +363,69 @@ function convertJsonMap(json, context) { } /** - * Converts a raw column value of structured type object to javascript Object + * Converts a raw column value of structured type OBJECT to javascript Object * - * @param {String} parsedJsonValue + * @param {String} rawColumnValue + * @param {Object} column + * @param {Object} context * * @returns {Object} */ function convertRawObject(rawColumnValue, column, context) { - const json = JSON.parse(rawColumnValue); - return convertJsonObject(json, context); + if (Util.string.isNotNullOrEmpty(rawColumnValue)) { + try { + const json = JSON.parse(rawColumnValue); + return convertJsonObject(json, context); + } catch (jsonParseError) { + Logger.getInstance().debug('Column %s raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message); + throw new Error(util.format('Column [%s] raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message)); + } + } else { + throw new Error(util.format('Column %s raw value is null or empty ', column.name)); + } } /** - * Converts a raw column value of structured type array to javascript Array + * Converts a raw column value of structured type ARRAY to javascript Array * - * @param {String} parsedJsonValue + * @param {String} rawColumnValue + * @param {Object} column + * @param {Object} context * * @returns {Object} */ function convertRawArray(rawColumnValue, column, context) { - const json = JSON.parse(rawColumnValue); - return convertJsonArray(json, context); + if (Util.string.isNotNullOrEmpty(rawColumnValue)) { + try { + const json = JSON.parse(rawColumnValue); + return convertJsonArray(json, context); + } catch (jsonParseError) { + Logger.getInstance().debug('Column %s raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message); + throw new Error(util.format('Column [%s] raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message)); + } + } else { + throw new Error(util.format('Column %s raw value is null or empty ', column.name)); + } } /** * Converts a raw column value of structured type MAP to javascript Map - * - * @param {String} parsedJsonValue + * @param {String} rawColumnValue + * @param {Object} column + * @param {Object} context * * @returns {Object} */ function convertRawMap(rawColumnValue, column, context) { - const json = JSON.parse(rawColumnValue); - return convertJsonMap(json, context); + if (Util.string.isNotNullOrEmpty(rawColumnValue)) { + try { + const json = JSON.parse(rawColumnValue); + return convertJsonMap(json, context); + } catch (jsonParseError) { + Logger.getInstance().debug('Column %s raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message); + throw new Error(util.format('Column [%s] raw value cannot be parsed as JSON: %s ', column.name, jsonParseError.message)); + } + } else { + throw new Error(util.format('Column %s raw value is null or empty ', column.name)); + } } function mapStructuredTypeValue(columnValue, context, metadataField) { diff --git a/lib/connection/result/datetime_format_converter.js b/lib/connection/result/datetime_format_converter.js index 971ebc888..880714c31 100644 --- a/lib/connection/result/datetime_format_converter.js +++ b/lib/connection/result/datetime_format_converter.js @@ -60,15 +60,7 @@ function convertSnowflakeFormatToMomentFormat(formatSql, scale) { } else { // we found one of our special tags if (out === '') { - if (tag === 'TZH') { - // format the moment to get the timezone string and extract the - // hours; for example, '-0700' will be converted to '-07' - out = moment.format('ZZ').substr(0, 3); - } else if (tag === 'TZM') { - // format the moment to get the timezone string and extract the - // minutes; for example, '-0700' will be converted to '00 - out = moment.format('ZZ').substr(3); - } else if (tag === 'FF') { + if (tag === 'FF') { // if 'FF' is followed by a digit, use the digit as the scale let digit = null; if (pos + tag.length < length) { diff --git a/test/integration/testStructuredType.js b/test/integration/testStructuredType.js index 5e9a420b2..e26b8d28c 100644 --- a/test/integration/testStructuredType.js +++ b/test/integration/testStructuredType.js @@ -26,7 +26,7 @@ function normalize(source) { } -describe.only('Test Structured types', function () { +describe('Test Structured types', function () { let connection; before(function (done) {