Created
December 5, 2009 20:25
-
-
Save irjudson/249849 to your computer and use it in GitHub Desktop.
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
require 'yaml' | |
require 'pp' | |
module Yogo | |
class SQLParser | |
attr_accessor :script | |
def initialize | |
@script = "perl -e 'use SQL::Parser; use SQL::Statement; use YAML; use YAML::Dumper; $parser = SQL::Parser->new(); $parser->{RaiseError}=1; $dumper = YAML::Dumper->new; $dumper->indent_width(4); $stmt = SQL::Statement->new(\"%s\", $parser); delete $stmt->{\"opts\"}; print $dumper->dump($stmt);'" | |
end | |
def parse(sql_string) | |
raise NotImplementedError | |
end | |
def convert(sql_string, to=:json_query) | |
raise NotImplementedError if to != :json_query | |
json_query = nil | |
job = @script % sql_string | |
yaml_query = `#{job}` | |
puts "YAML: #{yaml_query}" | |
yaml_obj = YAML.parse(yaml_query) | |
pp yaml_obj | |
json_query | |
end | |
end | |
end | |
$tsql = "SELECT id,name,age FROM users" | |
$test = Yogo::SQLParser.new | |
$test.convert($tsql) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment