From f9caaafe37352388820f05debc865390d962c1bd Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 3 Dec 2014 14:09:52 -0800 Subject: [PATCH] Better handle discovery of nullable columns --- lib/discovery.js | 33 ++++++++++++++++++++++----------- test/mysql.discover.test.js | 2 ++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/discovery.js b/lib/discovery.js index 087cbab..2ff4696 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -141,18 +141,28 @@ function mixinDiscovery(MySQL) { function queryColumns(owner, table) { var sql = null; if (owner) { - sql = paginateSQL('SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName", data_type AS "dataType",' - + ' character_octet_length AS "dataLength", numeric_precision AS "dataPrecision", numeric_scale AS "dataScale", is_nullable AS "nullable"' - + ' FROM information_schema.columns' - + ' WHERE table_schema=\'' + owner + '\'' - + (table ? ' AND table_name=\'' + table + '\'' : ''), - 'table_name, ordinal_position', {}); + sql = paginateSQL('SELECT table_schema AS "owner",' + + ' table_name AS "tableName", column_name AS "columnName",' + + ' data_type AS "dataType",' + + ' character_octet_length AS "dataLength",' + + ' numeric_precision AS "dataPrecision",' + + ' numeric_scale AS "dataScale",' + + ' is_nullable = \'YES\' AS "nullable"' + + ' FROM information_schema.columns' + + ' WHERE table_schema=\'' + owner + '\'' + + (table ? ' AND table_name=\'' + table + '\'' : ''), + 'table_name, ordinal_position', {}); } else { - sql = paginateSQL('SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName", data_type AS "dataType",' - + ' character_octet_length AS "dataLength", numeric_precision AS "dataPrecision", numeric_scale AS "dataScale", is_nullable AS "nullable"' - + ' FROM information_schema.columns' - + (table ? ' WHERE table_name=\'' + table + '\'' : ''), - 'table_name, ordinal_position', {}); + sql = paginateSQL('SELECT table_schema AS "owner",' + + ' table_name AS "tableName", column_name AS "columnName",' + + ' data_type AS "dataType",' + + ' character_octet_length AS "dataLength",' + + ' numeric_precision AS "dataPrecision",' + + ' numeric_scale AS "dataScale",' + + ' is_nullable = \'YES\' AS "nullable"' + + ' FROM information_schema.columns' + + (table ? ' WHERE table_name=\'' + table + '\'' : ''), + 'table_name, ordinal_position', {}); } return sql; } @@ -178,6 +188,7 @@ function mixinDiscovery(MySQL) { } else { results.map(function (r) { r.type = mysqlDataTypeToJSONType(r.dataType, r.dataLength); + r.nullable = r.nullable ? 'Y' : 'N'; }); cb(err, results); } diff --git a/test/mysql.discover.test.js b/test/mysql.discover.test.js index ec9c7ad..f0d749e 100644 --- a/test/mysql.discover.test.js +++ b/test/mysql.discover.test.js @@ -200,12 +200,14 @@ describe('Discover LDL schema from a table', function () { assert(schema.options.mysql.schema === 'STRONGLOOP'); assert(schema.options.mysql.table === 'INVENTORY'); assert(schema.properties.productId); + assert(schema.properties.productId.required); assert(schema.properties.productId.type === 'String'); assert(schema.properties.productId.mysql.columnName === 'PRODUCT_ID'); assert(schema.properties.locationId); assert(schema.properties.locationId.type === 'String'); assert(schema.properties.locationId.mysql.columnName === 'LOCATION_ID'); assert(schema.properties.available); + assert(schema.properties.available.required === false); assert(schema.properties.available.type === 'Number'); assert(schema.properties.total); assert(schema.properties.total.type === 'Number');