Skip to content

Ruby extension to parse SQL queries using the PostgreSQL query parser

License

Notifications You must be signed in to change notification settings

beanieboi/pg_query

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pg_query

This Ruby extension uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parsetree.

When you build this extension, it fetches a copy of the PostgreSQL server source and builds it, and then statically links it into this extension.

This is slightly crazy, but is the only reliable way of parsing all valid PostgreSQL queries.

Note: This gem uses a patched version of the latest PostgreSQL stable, with improvements to outfuncs.c.

Installation

gem install pg_query

Due to compiling PostgreSQL, installation will take a while. Expect up to 10 minutes on a fast machine.

Usage

PgQuery.parse("SELECT 1")
# => #<PgQuery:0x007fc82a3ff080 @query="SELECT 1", @parsetree=[{"SELECT"=>{"distinctClause"=>nil, "intoClause"=>nil, "targetList"=>[{"RESTARGET"=>{"name"=>nil, "indirection"=>nil, "val"=>{"A_CONST"=>{"val"=>1, "location"=>7}}, "location"=>7}}], "fromClause"=>nil, "whereClause"=>nil, "groupClause"=>nil, "havingClause"=>nil, "windowClause"=>nil, "valuesLists"=>nil, "sortClause"=>nil, "limitOffset"=>nil, "limitCount"=>nil, "lockingClause"=>nil, "withClause"=>nil, "op"=>0, "all"=>"false", "larg"=>nil, "rarg"=>nil}}], @warnings=[]> 

Authors

License

Copyright (c) 2014 pganalyze

pg_query is licensed under the PostgreSQL license, see LICENSE file for details.

About

Ruby extension to parse SQL queries using the PostgreSQL query parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published