Sha256: 825b27c96425776341bae937570f3b3398484ecb62585c593e8fe420f664a7c6

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

if defined?(ActiveSupport) && defined?(ActiveRecord)
  module ServiceTemplate
    module ActiveRecordStats
      SQL_INSERT_DELETE_PARSER_REGEXP = /^(?<action>\w+)\s(\w+)\s\W*(?<table>\w+)/
      SQL_SELECT_REGEXP = /select .*? FROM \W*(?<table>\w+)/i
      SQL_UPDATE_REGEXP = /update \W*(?<table>\w+)/i

      # Returns the table and query type
      def self.extract_from_sql_inserts_deletes(query)
        m = query.match(SQL_INSERT_DELETE_PARSER_REGEXP)
        [m[:table], m[:action]]
      end

      def self.extract_sql_selects(query)
        m = query.match(SQL_SELECT_REGEXP)
        [m[:table], 'SELECT']
      end

      def self.extract_sql_updates(query)
        m = query.match(SQL_UPDATE_REGEXP)
        [m[:table], 'UPDATE']
      end

      def self.extract_sql_content(query)
        table = action = nil
        if query.match(SQL_UPDATE_REGEXP)
          table, action = extract_sql_updates(query)
        elsif query.match(SQL_SELECT_REGEXP)
          table, action =  extract_sql_selects(query)
        elsif query.match(SQL_INSERT_DELETE_PARSER_REGEXP)
          table, action =  extract_from_sql_inserts_deletes(query)
        end
        [table, action]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
service_template-0.5.2 lib/service_template/active_record_extensions/stats.rb
service_template-0.5.1 lib/service_template/active_record_extensions/stats.rb
service_template-0.5.0 lib/service_template/active_record_extensions/stats.rb