LoginSignup
20
18

More than 5 years have passed since last update.

rubyでSQL文をパースする

Posted at

SQLをパースすることに迫られてPythonで自作していたが、rubyで書いたほうがより都合が良かったので書きなおしかーとなったところで見つけたいいもの。
ただし、READMEにどおりに使ってみても使えなかったのでメモ。

インストール

このモジュールを使えばパースできる。
https://github.com/cryodex/sql-parser

gemでのインストールは以下のようにする。

$ sudo gem install sql-parser

実行(失敗)

GitHubページのREADMEを見ながら実行してみる。
しかし、どれもメソッドが無いと怒られる。

irb(main):001:0> require 'sql-parser'
irb(main):002:0> parser = SQLParser::Parser.new
irb(main):003:0> ast = parser.scan_str('SELECT * FROM users WHERE id = 1')
irb(main):004:0> ast.select_list.to_sql
NoMethodError: undefined method `select_list' for #<SQLParser::Statement::DirectSelect:0x007f90cba20158>
    from (irb):4
    from /usr/bin/irb:12:in `<main>'
irb(main):005:0> ast.table_expression.to_sql
NoMethodError: undefined method `table_expression' for #<SQLParser::Statement::DirectSelect:0x007f90cba20158>
    from (irb):5
    from /usr/bin/irb:12:in `<main>'
irb(main):006:0> ast.table_expression.where_clause.to_sql
NoMethodError: undefined method `table_expression' for #<SQLParser::Statement::DirectSelect:0x007f90cba20158>
    from (irb):6
    from /usr/bin/irb:12:in `<main>'
irb(main):007:0> ast.table_expression.where_clause.search_condition.to_sql
NoMethodError: undefined method `table_expression' for #<SQLParser::Statement::DirectSelect:0x007f90cba20158>
    from (irb):7
    from /usr/bin/irb:12:in `<main>'
irb(main):008:0> ast.table_expression.where_clause.search_condition.left.to_sql
NoMethodError: undefined method `table_expression' for #<SQLParser::Statement::DirectSelect:0x007f90cba20158>
    from (irb):8
    from /usr/bin/irb:12:in `<main>'
irb(main):009:0> ast.table_expression.where_clause.search_condition.right.to_sql
NoMethodError: undefined method `table_expression' for #<SQLParser::Statement::DirectSelect:0x007f90cba20158>
    from (irb):9
    from /usr/bin/irb:12:in `<main>'

この時点で心折れそうになった。
色々試した苦労話はあるのだけれどもろもろ省略。
astの中身をよく見ると、query_expressionの下に色々あるみたい。
メソッド名もREADMEと違ったりしていたので、以下に自分が成功した例を記述。

実行(成功)

irb(main):001:0> require 'sql-parser'
irb(main):002:0> parser = SQLParser::Parser.new
irb(main):003:0> ast = parser.scan_str('SELECT * FROM users WHERE id = 1')
#識別子取得
irb(main):027:0> ast.query_expression.list.to_sql
=> "*"
#FROM以下の取得
irb(main):011:0> ast.query_expression.table_expression.to_sql
=> "FROM `users` WHERE `id` = 1"
#WHERE以下のドリルダウン
irb(main):012:0> ast.query_expression.table_expression.where_clause.to_sql
=> "WHERE `id` = 1"
irb(main):013:0> ast.query_expression.table_expression.where_clause.search_condition.to_sql
=> "`id` = 1"
irb(main):014:0> ast.query_expression.table_expression.where_clause.search_condition.left.to_sql
=> "`id`"
irb(main):015:0> ast.query_expression.table_expression.where_clause.search_condition.right.to_sql
=> "1"
20
18
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
18