OracleSqlParser
SQL Parser for Oracle
Installation
Add this line to your application's Gemfile:
gem 'oracle-sql-parser'
And then execute:
$ bundle
Or install it yourself as:
$ gem install oracle-sql-parser
Usage
query = "select 1 from dual"
parser = OracleSqlParser::Grammar::GrammarParser.new
syntax_tree = parser.parse query
if syntax_tree.nil?
= "\n#{query}\n" + " " * (parser.failure_column.to_i-1) + "*\n"
raise parser.failure_reason +
end
ast = syntax_tree.ast
=> #<OracleSqlParser::Ast::SelectStatement {:subquery=>#<OracleSqlParser::Ast::Subquery
:has_parenthesis => nil,
:query_block => #<OracleSqlParser::Ast::QueryBlock
:hint => nil,
:modifier => nil,
:select_list => #<OracleSqlParser::Ast::Array [
#<OracleSqlParser::Ast::SelectColumn
:expr => #<OracleSqlParser::Ast::NumberLiteral {:value=>"1"}>,
:as => nil,
:c_alias => nil}>
]>
,
:select_sources => #<OracleSqlParser::Ast::Array [
#<OracleSqlParser::Ast::TableReference {:schema_name=>nil, :table_name=>#<OracleSqlParser::Ast::Identifier {:name=>"dual"}>, :dblink=>nil, :subquery=>nil, :table_alias=>nil}>
]>
,
:where_clause => nil,
:group_by_clause => nil,
:model_clause => nil}>
,
:subqueries => nil,
:subquery => nil,
:order_by_clause => nil}>
, :for_update_clause=>nil}>=> nil
ast.to_sql
=> "select 1 from dual"
p = ast.to_parameterized
p.to_sql
p.params.inspect
=> "select :a0 from dual"
=> {"a0"=>#<OracleSqlParser::Ast::NumberLiteral {:value=>"1"}>}
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/jksy/sql_parser.