Skip to content

allow sqlify to optionally return an array of SQL commands#1780

Open
klobetime wants to merge 4 commits intotaozhi8833998:masterfrom
klobetime:returnArrays
Open

allow sqlify to optionally return an array of SQL commands#1780
klobetime wants to merge 4 commits intotaozhi8833998:masterfrom
klobetime:returnArrays

Conversation

@klobetime
Copy link
Copy Markdown

I have SQL that has the occasional semicolon embedded. Converting this to AST is fine, but bringing it back to SQL causes issues when there are multiple SQL statements. Currently, sqlify() uses a join(' ; ') to combine all the statements into a single string, but when I need to get at those a split(';') finds those embedded semis and the results aren't what we really need.

I added a new opt, asArray, that will omit the join and give back an array of SQL commands rather than a string.

const opt = {
  asArray: true
}
const { Parser } = require('node-sql-parser')
const parser = new Parser()
// opt only affect sqlify() but it doesn't hurt to pass it to astify() as well
const ast = parser.astify("SELECT * FROM t; SELECT x AS has_semis FROM y WHERE notes LIKE '%;%'", opt)
const sql = parser.sqlify(ast, opt)
console.log(JSON.stringify(sql)); // ["SELECT * FROM `t`","SELECT `x` AS `has_semis` FROM `y` WHERE `notes` LIKE '%;%'"]

@taozhi8833998 taozhi8833998 added this to the 5.1.0 milestone Mar 25, 2024
@taozhi8833998 taozhi8833998 added the enhancement New feature or request label Mar 25, 2024
@taozhi8833998 taozhi8833998 modified the milestones: 5.1.0, 5.2.0 Apr 30, 2024
@taozhi8833998 taozhi8833998 modified the milestones: 5.2.0, 5.3.0 Jun 3, 2024
@taozhi8833998 taozhi8833998 modified the milestones: 5.3.0, 5.4.0 Jul 31, 2024
@taozhi8833998 taozhi8833998 removed this from the 5.3.1 milestone Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants