Sha256: 081886f6c1e64a7430cc93b8b0fc22c4f52c4acad01e53c4d12a15e87f74a13c

Contents?: true

Size: 1.34 KB

Versions: 36

Compression:

Stored size: 1.34 KB

Contents

# encoding: utf-8
# author: Christoph Hartmann
# author: Dominik Richter

module DatabaseHelper
  class SQLColumn
    def initialize(row, name)
      @row = row
      @name = name
    end

    def value
      @row.nil? ? '' : @row[@name.downcase]
    end

    def to_s
      'SQL Column'
    end
  end

  class SQLRow
    def initialize(query, row)
      @query = query
      @row = row
    end

    def column(column)
      SQLColumn.new(@row, column)
    end

    def to_s
      'SQL Row'
    end
  end

  class SQLQueryResult
    attr_reader :error
    def initialize(cmd, results)
      @cmd = cmd
      @results = results
    end

    def empty?
      @results.empty?
    end

    def successful?
      @cmd.exit_status == 0 && @error.nil?
    end

    def rows
      @results
    end

    def row(id)
      SQLRow.new(self, @results[id])
    end

    def column(column)
      result = []
      @results.each do |row|
        result << row[column]
      end
      result
    end

    def size
      @results.size
    end

    def stdout
      warn '[DEPRECATION] The `stdout` method is deprecated. Use `row` instead.'
      @cmd.stdout
    end

    def stderr
      warn '[DEPRECATION] The `stderr` method is deprecated. Use `successful?` instead.'
      @cmd.stderr
    end

    def inspect
      to_s
    end

    def to_s
      'SQL ResultSet'
    end
  end
end

Version data entries

36 entries across 36 versions & 2 rubygems

Version Path
inspec-core-2.3.28 lib/utils/database_helpers.rb
inspec-2.3.28 lib/utils/database_helpers.rb
inspec-core-2.3.24 lib/utils/database_helpers.rb
inspec-2.3.24 lib/utils/database_helpers.rb
inspec-core-2.3.23 lib/utils/database_helpers.rb
inspec-2.3.23 lib/utils/database_helpers.rb
inspec-core-2.3.10 lib/utils/database_helpers.rb
inspec-2.3.10 lib/utils/database_helpers.rb
inspec-core-2.3.5 lib/utils/database_helpers.rb
inspec-2.3.5 lib/utils/database_helpers.rb
inspec-core-2.3.4 lib/utils/database_helpers.rb
inspec-2.3.4 lib/utils/database_helpers.rb
inspec-core-2.2.112 lib/utils/database_helpers.rb
inspec-2.2.112 lib/utils/database_helpers.rb
inspec-core-2.2.102 lib/utils/database_helpers.rb
inspec-2.2.102 lib/utils/database_helpers.rb
inspec-core-2.2.101 lib/utils/database_helpers.rb
inspec-2.2.101 lib/utils/database_helpers.rb
inspec-core-2.2.78 lib/utils/database_helpers.rb
inspec-2.2.78 lib/utils/database_helpers.rb