Class: Kasket::QueryParser
- Inherits:
-
Object
- Object
- Kasket::QueryParser
- Defined in:
- lib/kasket/query_parser.rb
Constant Summary collapse
- AND =
Examples: SELECT * FROM ‘users` WHERE (`users`.`id` = 2) SELECT * FROM `users` WHERE (`users`.`id` = 2) LIMIT 1 ’SELECT * FROM 'posts' WHERE ('posts'.'id' = 574019247) ‘
/\s+AND\s+/i
- VALUE =
Matches: 123, ?, ‘123’, ‘12”3’
/'?(\d+|\?|(?:(?:[^']|''|\\')*))'?/
Instance Method Summary collapse
-
#initialize(model_class) ⇒ QueryParser
constructor
A new instance of QueryParser.
- #parse(sql) ⇒ Object
Constructor Details
#initialize(model_class) ⇒ QueryParser
Returns a new instance of QueryParser.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/kasket/query_parser.rb', line 12 def initialize(model_class) @model_class = model_class @supported_query_pattern = if AR30 /^select\s+(?:`#{@model_class.table_name}`.)?\* from (?:`|")#{@model_class.table_name}(?:`|") where (.*?)\s*$/i else /^select \* from (?:`|")#{@model_class.table_name}(?:`|") where \((.*)\)(|\s+limit 1)\s*$/i end @table_and_column_pattern = /(?:(?:`|")?#{@model_class.table_name}(?:`|")?\.)?(?:`|")?([a-zA-Z]\w*)(?:`|")?/ # Matches: `users`.id, `users`.`id`, users.id, id @key_eq_value_pattern = /^[\(\s]*#{@table_and_column_pattern}\s+(=|IN)\s+#{VALUE}[\)\s]*$/ # Matches: KEY = VALUE, (KEY = VALUE), ()(KEY = VALUE)) end |
Instance Method Details
#parse(sql) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kasket/query_parser.rb', line 23 def parse(sql) if match = @supported_query_pattern.match(sql) where, limit = match[1], match[2] if AR30 return if where =~ / (order by|group by|join|having) /i if where =~ /limit \d+\s*$/i # limit is harder to find in rails 3.0 since where does not use surrounding braces return unless where =~ /(.*?)(\s+limit 1)\s*$/i where, limit = $1, $2 end end query = Hash.new query[:attributes] = sorted_attribute_value_pairs(where) return nil if query[:attributes].nil? if query[:attributes].size > 1 && query[:attributes].map(&:last).any? {|a| a.is_a?(Array)} # this is a query with IN conditions AND other conditions return nil end query[:index] = query[:attributes].map(&:first) query[:limit] = limit.blank? ? nil : 1 query[:key] = @model_class.kasket_key_for(query[:attributes]) query[:key] << '/first' if query[:limit] == 1 && !query[:index].include?(:id) query end end |