Comparing version 2.0.4 to 2.0.5
@@ -0,1 +1,4 @@ | ||
# 2.0.5 | ||
- Fix issue with arguments with column and table name | ||
# 2.0.4 | ||
@@ -2,0 +5,0 @@ - Fix BC break on exposed parsing functions |
@@ -7,2 +7,3 @@ function json2sql() { | ||
const args = []; | ||
let literalValue; | ||
if (nodeFun.arguments) { | ||
@@ -15,6 +16,12 @@ for (let i = 0, length = nodeFun.arguments.length; i < length; i++) { | ||
if (node.name) { | ||
args.push(`${node.name ? ` '${node.name}'=` : ''}"${node.value}"`); | ||
literalValue = `'${node.name}'="${node.value}"`; | ||
} else { | ||
args.push(`${node.name ? ` '${node.name}'=` : ''}${node.value}`); | ||
literalValue = `${node.value}`; | ||
} | ||
if (nodeFun.arguments[i - 1] && nodeFun.arguments[i - 1].type === 'dot') { | ||
args.push(`${args.pop()}${literalValue}`); | ||
} else { | ||
args.push(literalValue); | ||
} | ||
break; | ||
@@ -24,2 +31,6 @@ case 'string': | ||
break; | ||
case 'dot': | ||
literalValue = args.pop(); | ||
args.push(`${literalValue}.`); | ||
break; | ||
case 'wildcard': | ||
@@ -26,0 +37,0 @@ args.push(`*`); |
@@ -57,2 +57,7 @@ const lexer = require('rw-sql-parser').lexer; | ||
case 'DOT': | ||
stack.push({ | ||
type: 'dot' | ||
}); | ||
break; | ||
case 'LITERAL': | ||
@@ -795,2 +800,9 @@ case 'DBLSTRING': | ||
case 'LITERAL': | ||
stack.push({ | ||
type: 'literal', | ||
value: token[1], | ||
alias: null, | ||
direction: null | ||
}); | ||
break; | ||
case 'FUNCTION': | ||
@@ -797,0 +809,0 @@ stack.push(this.parseFunction(token[1])); |
{ | ||
"name": "sql2json", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"description": "PostgreSQL to/from JSON conversion lib", | ||
@@ -18,9 +18,17 @@ "main": "index.js", | ||
"author": { | ||
"name": "Raul Requero" | ||
"name": "Vizzuality", | ||
"email": "hello@vizzuality.com", | ||
"url": "http://vizzuality.com/" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Raul Requero" | ||
}, | ||
{ | ||
"name": "Sergio Gordillo" | ||
}, | ||
{ | ||
"name": "Enrique Cornejo" | ||
}, | ||
{ | ||
"name": "Tiago Garcia", | ||
@@ -27,0 +35,0 @@ "url": "https://github.com/tiagojsag" |
@@ -252,7 +252,37 @@ const assert = require('assert'); | ||
const response = 'SELECT * FROM tablename GROUP BY date_histogram( \'field\'="createdAt", \'interval\'=\'1d\')'; | ||
const response = 'SELECT * FROM tablename GROUP BY date_histogram(\'field\'="createdAt", \'interval\'=\'1d\')'; | ||
Json2sql.toSQL(data).should.deepEqual(response); | ||
}); | ||
it('Group with function with table and column name as argument', () => { | ||
const data = { | ||
select: [{ | ||
value: '*', | ||
alias: null, | ||
type: 'wildcard' | ||
}], | ||
from: 'tablename', | ||
group: [{ | ||
type: 'function', | ||
value: 'date_histogram', | ||
alias: null, | ||
arguments: [ | ||
{ | ||
type: 'literal', | ||
value: 'table' | ||
}, { | ||
type: 'dot', | ||
}, { | ||
type: 'literal', | ||
value: 'column' | ||
} | ||
] | ||
}] | ||
}; | ||
const response = 'SELECT * FROM tablename GROUP BY date_histogram(table.column)'; | ||
Json2sql.toSQL(data).should.deepEqual(response); | ||
}); | ||
}); | ||
@@ -369,2 +369,35 @@ const Json2sql = require('../../index').json2sql; | ||
}); | ||
it('With ORDER BY with function call with column name with table name as argument', () => { | ||
const data = { | ||
select: [{ | ||
value: '*', | ||
alias: null, | ||
type: 'wildcard' | ||
}], | ||
from: 'tablename', | ||
orderBy: [ | ||
{ | ||
value: 'avg', | ||
type: 'function', | ||
alias: null, | ||
direction: 'asc', | ||
arguments: [ | ||
{ | ||
value: 'tablename', | ||
type: 'literal' | ||
}, { | ||
type: 'dot' | ||
}, { | ||
value: 'name', | ||
type: 'literal', | ||
alias: null | ||
} | ||
] | ||
}] | ||
}; | ||
const response = 'SELECT * FROM tablename ORDER BY avg(tablename.name) asc'; | ||
Json2sql.toSQL(data).should.deepEqual(response); | ||
}); | ||
}); |
@@ -459,2 +459,33 @@ const assert = require('assert'); | ||
it('Group with function with table and column name as argument', () => { | ||
const response = { | ||
select: [{ | ||
value: '*', | ||
alias: null, | ||
type: 'wildcard' | ||
}], | ||
from: 'tablename', | ||
group: [{ | ||
type: 'function', | ||
value: 'date_histogram', | ||
alias: null, | ||
arguments: [ | ||
{ | ||
type: 'literal', | ||
value: 'table' | ||
}, { | ||
type: 'dot', | ||
}, { | ||
type: 'literal', | ||
value: 'column' | ||
} | ||
] | ||
}] | ||
}; | ||
const obj = new Sql2json('SELECT * FROM tablename GROUP BY date_histogram(table.column)'); | ||
const json = obj.toJSON(); | ||
json.should.deepEqual(response); | ||
}); | ||
}); |
@@ -311,3 +311,36 @@ const Sql2json = require('../../').sql2json; | ||
}); | ||
it('With ORDER BY with function call with column name with table name as argument', () => { | ||
const response = { | ||
select: [{ | ||
value: '*', | ||
alias: null, | ||
type: 'wildcard' | ||
}], | ||
from: 'tablename', | ||
orderBy: [ | ||
{ | ||
value: 'avg', | ||
type: 'function', | ||
alias: null, | ||
direction: 'asc', | ||
arguments: [ | ||
{ | ||
value: 'tablename', | ||
type: 'literal' | ||
}, { | ||
type: 'dot' | ||
}, { | ||
value: 'name', | ||
type: 'literal' | ||
} | ||
] | ||
}] | ||
}; | ||
const obj = new Sql2json('SELECT * FROM tablename ORDER BY avg(tablename.name) asc'); | ||
const json = obj.toJSON(); | ||
json.should.deepEqual(response); | ||
}); | ||
}); | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
239389
5707