Sha256: 5e9fee92b42207dfb092194f7905d825851b919a779af7dc9d178dcb5aacaee3

Contents?: true

Size: 934 Bytes

Versions: 1

Compression:

Stored size: 934 Bytes

Contents

module Dexter
  class Query
    attr_reader :statement, :fingerprint, :plans
    attr_accessor :missing_tables, :new_cost, :total_time, :calls, :indexes, :suggest_index, :pass1_indexes, :pass2_indexes

    def initialize(statement, fingerprint = nil)
      @statement = statement
      unless fingerprint
        fingerprint = PgQuery.fingerprint(statement) rescue "unknown"
      end
      @fingerprint = fingerprint
      @plans = []
    end

    def tables
      @tables ||= parse ? parse.tables : []
    end

    def tree
      parse.tree
    end

    def explainable?
      plans.any?
    end

    def costs
      plans.map { |plan| plan["Total Cost"] }
    end

    def initial_cost
      costs[0]
    end

    def high_cost?
      initial_cost && initial_cost >= 100
    end

    private

    def parse
      unless defined?(@parse)
        @parse = PgQuery.parse(statement) rescue nil
      end
      @parse
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pgdexter-0.2.1 lib/dexter/query.rb