Sha256: 8f378b47294bbf72b600964476496761b98ecd3b5c180c1063c32b5f3484f473

Contents?: true

Size: 1.35 KB

Versions: 6

Compression:

Stored size: 1.35 KB

Contents

module SQLite3
  class ResultSet
    def initialize(statement, handle)
      @statement = statement
      @handle = handle
    end

    def each(&block)
      until @statement.done?
        yield current_row

        @statement.step
      end
    end

    private

    def columns
      @result_columns ||= begin
        columns = {}

        count = sqlite3_column_count(@handle.value)
        0.upto(count-1) do |i|
          name = sqlite3_column_name(@handle.value, i).to_sym
          type = sqlite3_column_type(@handle.value, i)

          columns[name] = ColumnMetadata.new(i, type)
        end

        columns
      end
    end

    def current_row
      row = {}

      columns.each do |name, metadata|
        case metadata.type
        when SQLITE_NULL
          row[name] = nil
        when SQLITE_TEXT
          row[name] = sqlite3_column_text(@handle.value, metadata.index)
        when SQLITE_BLOB
          row[name] = NSData.dataWithBytes(sqlite3_column_blob(@handle.value, metadata.index), length: sqlite3_column_bytes(@handle.value, metadata.index))
        when SQLITE_INTEGER
          row[name] = sqlite3_column_int(@handle.value, metadata.index)
        when SQLITE_FLOAT
          row[name] = sqlite3_column_double(@handle.value, metadata.index)
        end
      end

      row
    end
  end

  class ColumnMetadata < Struct.new(:index, :type); end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
motion-sqlite3-0.5.0 lib/motion-sqlite3/result_set.rb
motion-sqlite3-0.4.2 lib/motion-sqlite3/result_set.rb
motion-sqlite3-0.4.1 lib/motion-sqlite3/result_set.rb
motion-sqlite3-0.4.0 lib/motion-sqlite3/result_set.rb
motion-sqlite3-0.3.2 lib/motion-sqlite3/result_set.rb
motion-sqlite3-0.3.1 lib/motion-sqlite3/result_set.rb