-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add .parse_plpgsql method to parse PL/pgSQL function definitions
This uses Postgres' PL/pgSQL parser (as extracted in libpg_query) to parse a PL/pgSQL CREATE FUNCTION statement into the AST.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,6 @@ | |
require 'pg_query/deparse' | ||
require 'pg_query/truncate' | ||
|
||
require 'pg_query/parse_plpgsql' | ||
|
||
require 'pg_query/scan' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'json' | ||
module PgQuery | ||
class PlpgsqlParseError < ArgumentError | ||
attr_reader :location | ||
def initialize(message, source_file, source_line, location) | ||
super("#{message} (#{source_file}:#{source_line})") | ||
@location = location | ||
end | ||
end | ||
|
||
def self.parse_plpgsql(input) | ||
PlpgsqlParserResult.new(input, JSON.parse(_raw_parse_plpgsql(input))) | ||
end | ||
|
||
class PlpgsqlParserResult | ||
attr_reader :input | ||
attr_reader :tree | ||
|
||
def initialize(input, tree) | ||
@input = input | ||
@tree = tree | ||
end | ||
|
||
def walk! | ||
nodes = [tree.dup] | ||
loop do | ||
parent_node = nodes.shift | ||
if parent_node.is_a?(Array) | ||
parent_node.each do |node| | ||
yield(node) | ||
nodes << node | ||
end | ||
elsif parent_node.is_a?(Hash) | ||
parent_node.each do |k, node| | ||
yield(node) | ||
nodes << node | ||
end | ||
end | ||
break if nodes.empty? | ||
end | ||
end | ||
end | ||
end |