Class: CassandraCQL::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cassandra-cql/result.rb

Direct Known Subclasses

V08::Result, V10::Result, V11::Result, V12::Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Result

Returns a new instance of Result.



44
45
46
47
48
# File 'lib/cassandra-cql/result.rb', line 44

def initialize(result)
  @result = result
  @schema = ResultSchema.new(result.schema) if rows?
  @cursor = 0
end

Instance Attribute Details

#cursorObject

Returns the value of attribute cursor.



42
43
44
# File 'lib/cassandra-cql/result.rb', line 42

def cursor
  @cursor
end

#resultObject (readonly)

Returns the value of attribute result.



42
43
44
# File 'lib/cassandra-cql/result.rb', line 42

def result
  @result
end

#schemaObject (readonly)

Returns the value of attribute schema.



42
43
44
# File 'lib/cassandra-cql/result.rb', line 42

def schema
  @schema
end

Instance Method Details

#eachObject



76
77
78
# File 'lib/cassandra-cql/result.rb', line 76

def each
  fetch { |row| yield row }
end

#fetchObject



97
98
99
100
101
102
103
104
105
# File 'lib/cassandra-cql/result.rb', line 97

def fetch
  if block_given?
    while row = fetch_row
      yield row
    end
  else
    fetch_row
  end
end

#fetch_arrayObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cassandra-cql/result.rb', line 127

def fetch_array
  if block_given?
    while row = fetch_row
      if row.kind_of?(Fixnum)
        yield [row]
      else
        yield row.to_a
      end
    end
  else
    if (row = fetch_row).kind_of?(Fixnum)
      [row]
    else
      row.to_a
    end
  end
end

#fetch_hashObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cassandra-cql/result.rb', line 107

def fetch_hash
  if block_given?
    while row = fetch_row
      if row.kind_of?(Fixnum)
        yield({row => row})
      else
        yield row.to_hash
      end
    end
  else
    if row = fetch_row
      if row.kind_of?(Fixnum)
        {row => row}
      else
        row.to_hash
      end
    end
  end
end

#fetch_rowObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cassandra-cql/result.rb', line 80

def fetch_row
  case @result.type
  when CassandraCQL::Thrift::CqlResultType::ROWS
    return nil if @cursor >= rows

    row = Row.new(@result.rows[@cursor], @schema)
    @cursor += 1
    return row
  when CassandraCQL::Thrift::CqlResultType::VOID
    return nil
  when CassandraCQL::Thrift::CqlResultType::INT
    return @result.num
  else
    raise Error::InvalidResultType, "Expects one of 0, 1, 2; was #{@result.type} "
  end
end

#int?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/cassandra-cql/result.rb', line 54

def int?
  @result.type == CassandraCQL::Thrift::CqlResultType::INT
end

#rowsObject Also known as: size, count, length



62
63
64
# File 'lib/cassandra-cql/result.rb', line 62

def rows
  @result.rows.size
end

#rows?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/cassandra-cql/result.rb', line 58

def rows?
  @result.type == CassandraCQL::Thrift::CqlResultType::ROWS
end

#void?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cassandra-cql/result.rb', line 50

def void?
  @result.type == CassandraCQL::Thrift::CqlResultType::VOID
end