This library implements the query language for the Google Visualization API wire protocol.
It can generate an AST of the query from a string. The AST nodes implement the Visitor Pattern so you can easily work with it.
gem install rgviz
There is a separate project on top of this library called rgviz-rails, check it out!
First you must require the library:
require 'rubygems'
require 'rgviz'
To parse a query:
query = Rgviz::Parser.parse 'select name where age > 20'
Read the source code for the AST nodes, it's not very big and the nodes just map to the query structure.
Rgviz supports the following extra functions:
- concat: converts each of its arguments to a string and then concatenates them. For example: concat(1, '-', '2') returns '1-2'. Can also receive just a single argument to convert it to a string.
- floor
- round
These new functions are not part of Google's query language, but they are very handy so we added them. These functions are also supported by rgviz-rails.
class MyVisitor < Rgviz::Visitor
def visit_select(node)
# do something with the node
puts 'before select'
puts node.columns.length
# returning true means visiting this node children
true
end
def end_visit_select(node)
# This will be invoked after visiting the node
puts "after select"
end
def visit_id_column(node)
puts node.name
end
end
query = Rgviz::Parser.parse 'select name, age'
query.accept MyVisitor.new
# outputs:
# before select
# 2
# name
# age
# after select
There is a visit_XXX and end_visit_XXX for every node in the language.
Their source code is here. You can use them to generate the javascript code to implement the wire protocol.
You can use Rgviz::HtmlRenderer.render(table) and Rgviz::CsvRenderer.render(table) to get a string to render in html or csv output format.