diff --git a/lib/compile_text_parser.js b/lib/compile_text_parser.js index 851efb0336..bcfa313915 100644 --- a/lib/compile_text_parser.js +++ b/lib/compile_text_parser.js @@ -8,20 +8,50 @@ for (var t in Types) { typeNames[Types[t]] = t; } -function compile(fields) { +function compile(fields, options, config) { var result = []; var i=0; + var lvalue = ''; result.push('(function() { return function TextRow(packet) {'); + if (options.rowsAsArray) + result.push('var result = new Array(' + fields.length + ')') + + var resultTables = {}; + var resultTablesArray = []; + + if (typeof options.nestTables == 'boolean') { + for (i = 0; i < fields.length; i++) { + resultTables[fields[i].table] = 1; + } + resultTablesArray = Object.keys(resultTables); + for (i = 0; i < resultTablesArray.length; i++) { + result.push(' this[' + srcEscape(resultTablesArray[i]) + '] = {};'); + } + } + + var fieldName = ''; + var tableName = ''; for (i = 0; i < fields.length; i++) { - //debug: uncomment to see each column metadata as comment - //result.push(' // ' + typeNames[fields[i].columnType] + ': ' + JSON.stringify(fields[i])); - result.push(' this['+ srcEscape(fields[i].name) + '] = ' + readCodeFor(fields[i].columnType, fields[i].characterSet)); + fieldName = srcEscape(fields[i].name); + //debug: uncomment to see each column type in a parser source comment + result.push(' // ' + fieldName + ': '+ typeNames[fields[i].columnType]); + if (typeof options.nestTables == 'string') { + tableName = srcEscape(fields[i].table); + lvalue = [' this[', srcEscape(fields[i].table + options.nestTables + fields[i].name), ']'].join(''); + } else if (typeof options.nestTables == 'boolean') { + tableName = srcEscape(fields[i].table); + lvalue = [' this[', tableName, '][', fieldName, ']'].join(''); + } else if (options.rowsAsArray) { + lvalue = 'result[' + i.toString(10) + ']'; + } else + lvalue = ' this[' + srcEscape(fields[i].name) + ']'; + result.push(lvalue + ' = ' + readCodeFor(fields[i].columnType, fields[i].characterSet)); } result.push('};})()'); var src = result.join('\n'); - //console.log(src); + console.log(src); return vm.runInThisContext(src); } diff --git a/test/integration/connection/test-nested-tables-query.js b/test/integration/connection/test-nested-tables-query.js new file mode 100644 index 0000000000..be6f3e5bf5 --- /dev/null +++ b/test/integration/connection/test-nested-tables-query.js @@ -0,0 +1,48 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'nested_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`title` varchar(255),', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +connection.query('INSERT INTO ' + table + ' SET ?', {title: 'test'}); + +var options1 = { + nestTables: true, + sql: 'SELECT * FROM ' + table +}; +var options2 = { + nestTables: '_', + sql: 'SELECT * FROM ' + table +}; +var rows1, rows2; + +connection.query(options1, function(err, _rows) { + if (err) throw err; + + rows1 = _rows; +}); +connection.query(options2, function(err, _rows) { + if (err) throw err; + + rows2 = _rows; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(rows1.length, 1); + assert.equal(rows1[0].nested_test.id, 1); + assert.equal(rows1[0].nested_test.title, 'test'); + assert.equal(rows2.length, 1); + assert.equal(rows2[0].nested_test_id, 1); + assert.equal(rows2[0].nested_test_title, 'test'); +});