Class: TrinoSqlParser
- Inherits:
-
Object
- Object
- TrinoSqlParser
- Extended by:
- ClassMethods
- Defined in:
- lib/trino_sql_parser.rb,
lib/trino_sql_parser/version.rb,
lib/trino_sql_parser/support_process.rb
Defined Under Namespace
Modules: ClassMethods Classes: ParseError, SupportProcess
Constant Summary collapse
- VERSION =
"1.1.0"
Instance Attribute Summary
Attributes included from ClassMethods
#jar_path, #java_args, #java_cmd, #java_env
Instance Method Summary collapse
-
#initialize(with_tokens: false) ⇒ TrinoSqlParser
constructor
A new instance of TrinoSqlParser.
- #parse(sql) ⇒ Object
Constructor Details
#initialize(with_tokens: false) ⇒ TrinoSqlParser
Returns a new instance of TrinoSqlParser.
38 39 40 |
# File 'lib/trino_sql_parser.rb', line 38 def initialize(with_tokens: false) @support_process = SupportProcess.new(with_tokens: with_tokens) end |
Instance Method Details
#parse(sql) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/trino_sql_parser.rb', line 42 def parse(sql) unless sql.is_a?(String) raise ArgumentError, "SQL must be a String but got #{sql.class}" end request_line = JSON.dump({"sql" => sql}) success = false begin @support_process.send_line(request_line) response_line = @support_process.receive_line raise "Process crashed" unless response_line response = JSON.parse(response_line) statements = response['statements'] errors = response['errors'] success = true rescue => e raise "Support process failed with an error: #{e}" ensure @support_process.kill! unless success end if errors raise ParseError.new(errors) end statements.map do |h| h.reject! {|k, v| v == nil } end statements end |