Fix handling of functions in a select clause

This commit is contained in:
Timothy Warren 2014-10-23 11:59:42 -04:00
parent 490ec86bf7
commit 873bfbc9eb
5 changed files with 40 additions and 6 deletions

View File

@ -6,10 +6,10 @@ A node query builder for various SQL databases, based on CodeIgniter's query bui
var nodeQuery = require('node-query'); var nodeQuery = require('node-query');
var mysql = ... // Database module connection var connection = ... // Database module connection
// Three arguments: database type, database connection, database connection library // Three arguments: database type, database connection, database connection library
var query = nodeQuery('mysql', mysql, 'mysql2'); var query = nodeQuery('mysql', connection, 'mysql2');
query.select('foo') query.select('foo')
.from('bar') .from('bar')

View File

@ -177,6 +177,7 @@ var d = {
*/ */
quoteIdentifiers: function(str) { quoteIdentifiers: function(str) {
var hiers, raw; var hiers, raw;
var pattern = new RegExp(d.identifierChar + '(' + '([a-zA-Z0-9_]+)' + '(\((.*?)\))' + ')' + d.identifierChar, 'ig');
// Recurse for arrays of identifiiers // Recurse for arrays of identifiiers
if (Array.isArray(str)) if (Array.isArray(str))
@ -191,7 +192,19 @@ var d = {
hiers = str.split('.').map(d._quote); hiers = str.split('.').map(d._quote);
raw = hiers.join('.'); raw = hiers.join('.');
// TODO: fix functions // Fix functions
if (raw.contains('(') && raw.contains(')'))
{
var funcs = pattern.exec(raw);
console.log(funcs);
// Unquote the function
raw = raw.replace(funcs[0], funcs[1]);
// Quote the identifiers inside of the parens
var inParens = funcs[3].substring(1, funcs[3].length -1);
raw = raw.replace(inParens, d.quoteIdentifiers(inParens));
}
return raw; return raw;
} }

View File

@ -91,10 +91,10 @@
<article><h1>Node-query</h1><p>A node query builder for various SQL databases, based on CodeIgniter's query builder.</p> <article><h1>Node-query</h1><p>A node query builder for various SQL databases, based on CodeIgniter's query builder.</p>
<h3>Basic use</h3><pre class="prettyprint source"><code>var nodeQuery = require('node-query'); <h3>Basic use</h3><pre class="prettyprint source"><code>var nodeQuery = require('node-query');
var mysql = ... // Database module connection var connection = ... // Database module connection
// Three arguments: database type, database connection, database connection library // Three arguments: database type, database connection, database connection library
var query = nodeQuery('mysql', mysql, 'mysql2'); var query = nodeQuery('mysql', connection, 'mysql2');
query.select('foo') query.select('foo')
.from('bar') .from('bar')

View File

@ -106,6 +106,7 @@ var d = {
*/ */
quoteIdentifiers: function(str) { quoteIdentifiers: function(str) {
var hiers, raw; var hiers, raw;
var pattern = new RegExp(d.identifierChar + '(' + '([a-zA-Z0-9_]+)' + '(\((.*?)\))' + ')' + d.identifierChar, 'ig');
// Recurse for arrays of identifiiers // Recurse for arrays of identifiiers
if (Array.isArray(str)) if (Array.isArray(str))
@ -120,7 +121,19 @@ var d = {
hiers = str.split('.').map(d._quote); hiers = str.split('.').map(d._quote);
raw = hiers.join('.'); raw = hiers.join('.');
// TODO: fix functions // Fix functions
if (raw.contains('(') && raw.contains(')'))
{
var funcs = pattern.exec(raw);
console.log(funcs);
// Unquote the function
raw = raw.replace(funcs[0], funcs[1]);
// Quote the identifiers inside of the parens
var inParens = funcs[3].substring(1, funcs[3].length -1);
raw = raw.replace(inParens, d.quoteIdentifiers(inParens));
}
return raw; return raw;
} }

View File

@ -20,6 +20,14 @@ module.exports = (function() {
*/ */
base.tests = { base.tests = {
'Get tests' : { 'Get tests' : {
'Get with function': function(test) {
base.qb.select('id, COUNT(id) as count')
.from('create_test')
.groupBy('id')
.get(base.testCallback.bind(test, test));
test.done();
},
'Basic select all get': function(test) { 'Basic select all get': function(test) {
base.qb.get('create_test', base.testCallback.bind(test, test)); base.qb.get('create_test', base.testCallback.bind(test, test));
test.done(); test.done();