Class: ActionCost::SqlParser
- Inherits:
-
Object
- Object
- ActionCost::SqlParser
- Defined in:
- lib/action_cost/sql_parser.rb
Constant Summary collapse
- VALID_OPERATIONS =
%w{ select insert update delete }
Instance Attribute Summary collapse
-
#invalid ⇒ Object
readonly
Returns the value of attribute invalid.
-
#join_tables ⇒ Object
readonly
Returns the value of attribute join_tables.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Instance Method Summary collapse
-
#initialize(sql) ⇒ SqlParser
constructor
A new instance of SqlParser.
- #log ⇒ Object
- #parse ⇒ Object
Constructor Details
#initialize(sql) ⇒ SqlParser
Returns a new instance of SqlParser.
7 8 9 10 11 |
# File 'lib/action_cost/sql_parser.rb', line 7 def initialize(sql) @invalid = false @sql = sql @join_tables = [] end |
Instance Attribute Details
#invalid ⇒ Object (readonly)
Returns the value of attribute invalid.
3 4 5 |
# File 'lib/action_cost/sql_parser.rb', line 3 def invalid @invalid end |
#join_tables ⇒ Object (readonly)
Returns the value of attribute join_tables.
3 4 5 |
# File 'lib/action_cost/sql_parser.rb', line 3 def join_tables @join_tables end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
3 4 5 |
# File 'lib/action_cost/sql_parser.rb', line 3 def operation @operation end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
3 4 5 |
# File 'lib/action_cost/sql_parser.rb', line 3 def table_name @table_name end |
Instance Method Details
#log ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/action_cost/sql_parser.rb', line 38 def log if @invalid Rails.logger.debug "action_cost: non parsable query" else Rails.logger.debug "action_cost: operation=#{@operation} table_name=#{@table_name} " + "join_tables=#{@join_tables.inspect}" end end |
#parse ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/action_cost/sql_parser.rb', line 13 def parse if @sql =~ /^\s*(\w+)/ op = $1.downcase unless VALID_OPERATIONS.include?(op) Rails.logger.error "action_cost: unknown operation [#{op}]" @invalid = true return false end @operation = op else Rails.logger.error "action_cost: could not parse [#{@sql}]" @invalid = true return false end case @operation when 'select' then parse_select when 'insert' then parse_insert when 'update' then parse_update when 'delete' then parse_delete end return !@invalid end |