Better handle discovery of nullable columns

This commit is contained in:
Raymond Feng 2014-12-03 14:09:52 -08:00
parent f130b8c5fa
commit f9caaafe37
2 changed files with 24 additions and 11 deletions

View File

@ -141,17 +141,27 @@ function mixinDiscovery(MySQL) {
function queryColumns(owner, table) { function queryColumns(owner, table) {
var sql = null; var sql = null;
if (owner) { if (owner) {
sql = paginateSQL('SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName", data_type AS "dataType",' sql = paginateSQL('SELECT table_schema AS "owner",' +
+ ' character_octet_length AS "dataLength", numeric_precision AS "dataPrecision", numeric_scale AS "dataScale", is_nullable AS "nullable"' ' table_name AS "tableName", column_name AS "columnName",' +
+ ' FROM information_schema.columns' ' data_type AS "dataType",' +
+ ' WHERE table_schema=\'' + owner + '\'' ' character_octet_length AS "dataLength",' +
+ (table ? ' AND table_name=\'' + table + '\'' : ''), ' 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', {}); 'table_name, ordinal_position', {});
} else { } else {
sql = paginateSQL('SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName", data_type AS "dataType",' sql = paginateSQL('SELECT table_schema AS "owner",' +
+ ' character_octet_length AS "dataLength", numeric_precision AS "dataPrecision", numeric_scale AS "dataScale", is_nullable AS "nullable"' ' table_name AS "tableName", column_name AS "columnName",' +
+ ' FROM information_schema.columns' ' data_type AS "dataType",' +
+ (table ? ' WHERE table_name=\'' + table + '\'' : ''), ' 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', {}); 'table_name, ordinal_position', {});
} }
return sql; return sql;
@ -178,6 +188,7 @@ function mixinDiscovery(MySQL) {
} else { } else {
results.map(function (r) { results.map(function (r) {
r.type = mysqlDataTypeToJSONType(r.dataType, r.dataLength); r.type = mysqlDataTypeToJSONType(r.dataType, r.dataLength);
r.nullable = r.nullable ? 'Y' : 'N';
}); });
cb(err, results); cb(err, results);
} }

View File

@ -200,12 +200,14 @@ describe('Discover LDL schema from a table', function () {
assert(schema.options.mysql.schema === 'STRONGLOOP'); assert(schema.options.mysql.schema === 'STRONGLOOP');
assert(schema.options.mysql.table === 'INVENTORY'); assert(schema.options.mysql.table === 'INVENTORY');
assert(schema.properties.productId); assert(schema.properties.productId);
assert(schema.properties.productId.required);
assert(schema.properties.productId.type === 'String'); assert(schema.properties.productId.type === 'String');
assert(schema.properties.productId.mysql.columnName === 'PRODUCT_ID'); assert(schema.properties.productId.mysql.columnName === 'PRODUCT_ID');
assert(schema.properties.locationId); assert(schema.properties.locationId);
assert(schema.properties.locationId.type === 'String'); assert(schema.properties.locationId.type === 'String');
assert(schema.properties.locationId.mysql.columnName === 'LOCATION_ID'); assert(schema.properties.locationId.mysql.columnName === 'LOCATION_ID');
assert(schema.properties.available); assert(schema.properties.available);
assert(schema.properties.available.required === false);
assert(schema.properties.available.type === 'Number'); assert(schema.properties.available.type === 'Number');
assert(schema.properties.total); assert(schema.properties.total);
assert(schema.properties.total.type === 'Number'); assert(schema.properties.total.type === 'Number');