Skip to content

Commit

Permalink
nestTables and rowsAsArray options support in text protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
sidorares committed Apr 27, 2014
1 parent 120ebc3 commit 0638237
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
40 changes: 35 additions & 5 deletions lib/compile_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
48 changes: 48 additions & 0 deletions test/integration/connection/test-nested-tables-query.js
Original file line number Diff line number Diff line change
@@ -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');
});

0 comments on commit 0638237

Please sign in to comment.