Giter Club home page Giter Club logo

node-pg-query-native's Introduction

pg-query-native Build Status

The real PostgreSQL parser for nodejs.

This is based on the output of libpg_query. This wraps the static library output and links it into a node module for use in js.

All credit for the hard problems goes to Lukas Fittl.

Installation

npm install pg-query-native

Documentation

query.parse(query)

Parses the query and returns the parse tree.

Parameters

parameter type description
query String SQL query

Returns an object in the format:

{ query: <query|Object>,
  error: { message: <message|String>,
           fileName: <fileName|String>,
           lineNumber: <line|Number>,
           cursorPosition: <cursor|Number> }

Example

var parse = require('pg-query-native').parse;

console.log(parse('select 1').query);

Related

node-pg-query-native's People

Contributors

jahndis avatar zhm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-pg-query-native's Issues

Apple M1 - Symbol not found: _pg_query_init

Steps to repoduce:

  1. Use a mac with an M1 chip
  2. run npm install
  3. run npm test
  4. Get an error:
➜  node-pg-query-native git:(master) ✗ npm t

> [email protected] test /Users/eliya/temp/node-pg-query-native
> mocha

  pg-query
dyld: lazy symbol binding failed: Symbol not found: _pg_query_init
  Referenced from: /Users/eliya/temp/node-pg-query-native/build/Release/pg-query.node
  Expected in: flat namespace

dyld: Symbol not found: _pg_query_init
  Referenced from: /Users/eliya/temp/node-pg-query-native/build/Release/pg-query.node
  Expected in: flat namespace

Doesn't work in Rosetta as well

FYI: fork with async versions, PLPgSQL parsing, newer parser

Thanks for your work on this library!

I needed some features that were missing — namely, access to the libpg_query parsing for Pl/PgSQL functions, and the ability to do parsing asynchronously from Node — so I made my own fork and published it to npm as pg-plpgsql-query-native. To handle the async stuff, I also updated the C++ code to use the Node Addons API bindings to N-API.

Just thought I'd let you know in case you want to expose async parsing as part of your pg-query-parser package.

Parser throw error when met keyword "row"

This will throw error:

select  row_number() over(
    partition by id
    order by
      create_time
  )  row
from  sometable

as

{
  "error": {
    "fileName": "scan.l",
    "lineNumber": 1116,
    "cursorPosition": 89,
    "functionName": "scanner_yyerror",
    "context": null
  },
  "query": []
}

But this will be well:

select  row_number() over(
    partition by id
    order by
      create_time
  )  as row
from  sometable

!!!!!

I was just digging in to figuring out how to do this for node and @lfittl pointed me to this. So excited that this exists. Thanks for doing this work 💃

how to upgrade pg_query?

the version of pg_query is old and cannot parse quite a few statements now that are valid.

I'm happy to help and make a PR, are can somebody provide some insight on where to start?

npm ERR! Failed at the [email protected] install script.

Hello,
Facing issue in Windows, here is the error, can you please help me on this issue.

Error:

gyp ERR! stack Error: C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:191:23)
gyp ERR! stack at ChildProcess.emit (events.js:198:13)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
gyp ERR! System Windows_NT 10.0.17763
gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" "rebuild"
gyp ERR! cwd C:\WORKSPACE\Service\node_modules\pg-query-native
gyp ERR! node -v v10.18.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok
npm WARN [email protected] requires a peer of ajv@^5.0.0 but none is installed. You must install peer dependencies yourself.

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: node-gyp rebuild
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Install Errors

Trying to install this on the windows machine. getting build error attached are error details and npm log
npmierror

Thanks

Logical connections are not very clear in the FROM section.

I'm planning a postgresql query maker. The goal is to create an interface where I specify the data to be queryed, the filter conditions, and it uses the known query to query the appropriate data from the postgresql server.
I got there right now that the current data structure "fromClaus" is quite interesting.
As my understanding larg / rarg bind the individual JOIN clusters. However, in the case of a complicated query, the result will be "interesting". Practically, the above-mentioned goal is not or very difficult to use.
My idea was the following: I knew the tables I needed with the knowledge I needed and I rebuild it without unnecessary tables and run the querry.
In fact, and in the present data structure, solving the problem is quite cumbersome and only possible under certain conditions, which I think is not very good for my goals
I've looked at this querry better:
const parser = require('pg-query-native'); test=parser.parse('SELECT cv.void::oid , cv.dbid::oid, cv.cvoid::oid, cv.vonum::character(20), cv.ovnum::text, cv.pvnum::text, cv.genvoid::oid, i.itemid::oid, i.itp::character(4), i.pitemid::oid, i.citemid::oid, i.spitemid::oid, i.citem_type::character(4), i.ocsid::oid, i.dhid_ocs::oid, i.dstdbid::oid, i.pid::oid, i.prqty::numeric(16,6), i.qty::numeric(16,6), i.fwdqty::numeric(16,6), il.objid::oid, il.status::character(1), il.dstdbid::oid, il.dhid_obj::oid, il.dhid_maker::oid, il.prqty::numeric(16,6), il.value::numeric(20,6), il.qty::numeric(16,6), il.fwdqty::numeric(16,6), il.descr::text, il.xflds::text[], ss.svid::oid, ss.parsvid::oid, ss.stid::oid, ss.dstid::oid, ss.nstid::oid, ss.svord::oid, ss.status::character(1) as ss_status, ss.dstdbid::oid, ss.srcdbid::oid, ss.stflag::character(1) , ss.ststat::character(1), ss.stqid::character(4), ss.plctid::oid, ss.sttid::oid, ss.closed::boolean, ss.prqty::numeric(16,6), ss.qty::numeric(16,6), ss.resqty::numeric(16,6), ss.vfpath::text, ss.from_path::text, ss.to_path::text, ss.verify::character(1), ss.xflds::text[], ssi.qty::numeric as ssi_qty FROM voucher cv JOIN item i ON i.dbid=cv.dbid AND i.cvoid=cv.void AND i.deleted=0 JOIN cikktorzs ct ON ct.ocsid=i.ocsid JOIN stockv_item si ON si.itemid=i.itemid AND i.dbid=si.dbid AND si.deleted=0 JOIN stockv stc ON stc.void=si.void AND stc.dbid=si.dbid AND stc.deleted=0 JOIN item_lot il ON i.dbid=il.dbid AND i.itemid=il.itemid and il.deleted=0 JOIN stockv_stock_item ssi ON ssi.itemid=i.itemid AND ssi.dbid=i.dbid AND ssi.deleted=0 JOIN stockv_stock ss ON ss.dbid=cv.dbid AND ssi.svid=ss.svid AND il.objid=ss.objid AND ss.deleted=0 JOIN stockv_stock sso ON sso.dbid=ss.dbid AND sso.svid=(string_to_array(substr(ss.vfpath, 2), \'/\'))[1]::oid AND sso.deleted=0 JOIN lot l ON il.dbid=l.dbid AND il.objid=l.objid JOIN item li ON li.itemid=l.itemid_inl AND l.dbid=li.dbid JOIN voucher lv ON li.dbid=lv.dbid AND li.cvoid=lv.void LEFT JOIN invoice_item ii ON ii.itemid=i.itemid AND i.dbid=ii.dbid AND ii.deleted=0 LEFT JOIN invoice inv ON inv.void=ii.void AND inv.dbid=cv.dbid AND inv.deleted=0 LEFT JOIN szolgtorzs szt ON szt.ocsid=i.ocsid WHERE cv.deleted=0 AND cv.status in (\'c\',\'l\',\'z\') AND cv.invaddrid= ANY (string_to_array(\'6635\',\'|\')) AND coalesce(ct.kcikkcs,szt.kcikkcs)= ANY (string_to_array(\'65\',\'|\')::int4[]) AND ct.pszichotrop=\'t\' AND \'{"002"}\' <@ coalesce(ct.eltart,\'{}\') AND ct.cikkszam in (SELECT cikkszam FROM szallitok WHERE cegkod = ANY (string_to_array(\'20469\',\'|\')) AND deleted=0 AND !vevo) AND l.maker_id=\'95420\' AND cv.votp = ANY (string_to_array(\'VSLT|AKSB\',\'|\')) AND cv.shippartnerid= ANY (string_to_array(\'K0013\',\'|\')) AND stc.stockdate <= \'2018-10-10\' AND cv.shipaddrid= ANY (string_to_array(\'6635\',\'|\')) AND l.gyarszam like\'%5RGT%\' AND coalesce(stc.dir, CASE inv.kind WHEN \'B\' THEN iif(ii.discharge, \'I\', \'O\') WHEN \'S\' THEN iif(ii.discharge, \'O\', \'I\') WHEN \'I\' THEN \'N\' END) in (\'I\', \'O\', \'\', \'\') AND cv.dbid = 1 AND coalesce(ct.tcskod,szt.tcskod)= ANY (string_to_array(\'001\',\'|\')) AND cv.invpartnerid= ANY (string_to_array(\'K0253\',\'|\')) AND lv.shippartnerid=\'41877\'AND coalesce(ct.cikkod,szt.cikkod)=ANY (string_to_array(\'032\',\'|\')) AND lv.shipaddrid=\'14198\' AND stc.stockdate >= \'2018-10-10\' AND i.ocsid = ANY (string_to_array(\'3557\',\'|\')::int4[]::oid[]) AND stc.status=\'s\' AND i.qty>0 GROUP BY 1,2,3,4,5,6,7,8 ORDER BY 1,2,3');

The problem is in the evaluation of the FROM part. The current dependencies are:
1 FROM voucher cv
2 1 JOIN item i ON i.dbid=cv.dbid AND i.cvoid=cv.void AND i.deleted=0
3 2 JOIN cikktorzs ct ON ct.ocsid=i.ocsid
4 2 JOIN stockv_item si ON si.itemid=i.itemid AND i.dbid=si.dbid AND si.deleted=0
5 4 JOIN stockv stc ON stc.void=si.void AND stc.dbid=si.dbid AND stc.deleted=0
6 2 JOIN item_lot il ON i.dbid=il.dbid AND i.itemid=il.itemid and il.deleted=0
7 2 JOIN stockv_stock_item ssi ON ssi.itemid=i.itemid AND ssi.dbid=i.dbid AND ssi.deleted=0
8 7,6 JOIN stockv_stock ss ON ss.dbid=cv.dbid AND ssi.svid=ss.svid AND il.objid=ss.objid AND ss.deleted=0
9 8 JOIN stockv_stock sso ON sso.dbid=ss.dbid AND sso.svid=(string_to_array(substr(ss.vfpath, 2), '/'))[1]::oid AND sso.deleted=0
A 6 JOIN lot l ON il.dbid=l.dbid AND il.objid=l.objid
B A JOIN item li ON li.itemid=l.itemid_inl AND l.dbid=li.dbid
C B JOIN voucher lv ON li.dbid=lv.dbid AND li.cvoid=lv.void
D 2 LEFT JOIN invoice_item ii ON ii.itemid=i.itemid AND i.dbid=ii.dbid AND ii.deleted=0
E D LEFT JOIN invoice inv ON inv.void=ii.void AND inv.dbid=cv.dbid AND inv.deleted=0
F 2 LEFT JOIN szolgtorzs szt ON szt.ocsid=i.ocsid

While in your mapping:
((((((((((((((1/2)/3)/4)/5)/6)/7)/8)/9)/A)/B)/C)/D)/E)/F)

So if I skip the unnecessary tables, psqlQuery will not be created...
It is all in fact and true, but it is difficult to change...
Maybe some idea would help me to create my project?

does not handle the recurring functions

console.log(JSON.stringify(parser.parse('SELECT (x.cv).void,(x.cv).vonum,c.cegkod FROM x_function(12,56,hstore(\'alma\',5)) x (cv::voucher) JOIN ceg c ON c.cegkod=(x.cv).invpartnerid').query));
return:
[]
but if a function has a known return type then returns the expected value
console.log(JSON.stringify(parser.parse('SELECT (x.cv).void,(x.cv).vonum,c.cegkod FROM x_function(12,56,hstore(\'alma\',5)) x JOIN ceg c ON c.cegkod=(x.cv).invpartnerid').query));
return:
[{"SelectStmt":....}]
is correct

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.