gpt4 book ai didi

javascript - node-sql-parser , parser.astify(sqlquery) 不返回准确的输出

转载 作者:行者123 更新时间:2023-12-02 21:44:25 58 4
gpt4 key购买 nike

我正在尝试使用node-sql-parser在nodejs中解析sql查询。

const {Parser} = require('node-sql-parser');
const parser = new Parser();
const ast = parser.astify(sqlQuery)

使用的sqlery如下

SELECT * FROM SUBSCRIPTION WHERE COLUMN_1 = 'NORM' AND COLUMN_2 IN ('11','12','13','14','15','16','17','18','42','43','44','45','101','102','103','104','128','129','130','131','159') AND (COLUMN_3 IS NOT NULL OR COLUMN_4 = 1) AND COLUMN_5 <> 1 AND (COLUMN_6 <> '8088' AND COLUMN_7 <>  '9088') 

在解析时得到输出

{ with: null,
type: 'select',
options: null,
distinct: null,
columns: '*',
from: [ { db: null, table: 'SUBSCRIPTION', as: null } ],
where:
{ type: 'binary_expr',
operator: 'AND',
left:
{ type: 'binary_expr',
operator: 'AND',
left: [Object],
right: [Object] },
right:
{ type: 'binary_expr',
operator: 'AND',
left: [Object],
right: [Object],
parentheses: true } },
groupby: null,
having: null,
orderby: null,
limit: null }

我无法迭代left:[Object]。有什么方法可以解析这个并只获取运算符列表吗?

最佳答案

您将其记录为对象,这就是您在其中看到对象的原因。您需要在控制台之前对对象进行 JSON.stringify(ast) 才能查看所有内容。

关于查找所有运算符,您可以使用此代码


const sqlQuery = "SELECT * FROM SUBSCRIPTION WHERE COLUMN_1 = 'NORM' AND COLUMN_2 IN ('11','12','13','14','15','16','17','18','42','43','44','45','101','102','103','104','128','129','130','131','159') AND (COLUMN_3 IS NOT NULL OR COLUMN_4 = 1) AND COLUMN_5 <> 1 AND (COLUMN_6 <> '8088' AND COLUMN_7 <> '9088')";
const {Parser} = require('node-sql-parser');
const parser = new Parser();
const ast = parser.astify(sqlQuery)

function getOperator(obj, result = []) {
let value;
Object.keys(obj).forEach(key => {
value = obj[key];
if (value instanceof Object) {
getOperator(value, result);
} else if (key === "operator") {
result.push(value);
}
});
return result;
}

console.log(getOperator(ast));

// output will be - ["AND", "AND","AND","AND","=","IN","OR","IS NOT","=","<>","AND","<>","<>"]


关于javascript - node-sql-parser , parser.astify(sqlquery) 不返回准确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60293795/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com