Class: SQLite::Recordset

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlite.rb

Overview

class Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRecordset

Returns a new instance of Recordset.



119
120
121
122
123
124
125
126
127
# File 'lib/sqlite.rb', line 119

def initialize()
  @columns        = []
  @types          = []
  @header_names   = {}
  @header_indexes = {}
  @data           = []
  @rows           = 0
  @headers        = {}
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



115
116
117
# File 'lib/sqlite.rb', line 115

def columns
  @columns
end

#dataObject

Returns the value of attribute data.



117
118
119
# File 'lib/sqlite.rb', line 117

def data
  @data
end

#header_indexesObject

Returns the value of attribute header_indexes.



116
117
118
# File 'lib/sqlite.rb', line 116

def header_indexes
  @header_indexes
end

#header_namesObject

Returns the value of attribute header_names.



116
117
118
# File 'lib/sqlite.rb', line 116

def header_names
  @header_names
end

#headersObject

Returns the value of attribute headers.



115
116
117
# File 'lib/sqlite.rb', line 115

def headers
  @headers
end

#rowsObject

Returns the value of attribute rows.



117
118
119
# File 'lib/sqlite.rb', line 117

def rows
  @rows
end

#typesObject

Returns the value of attribute types.



117
118
119
# File 'lib/sqlite.rb', line 117

def types
  @types
end

Instance Method Details

#[](row) ⇒ Object

Return row at given index



130
131
132
133
134
# File 'lib/sqlite.rb', line 130

def [](row)
  return nil if row > @rows - 1

  return Row.new(self, row)
end

#dumpObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/sqlite.rb', line 176

def dump()
  cols = {}
  0.upto(@columns.size-1) do |i|
    cols[i] = @columns[i].size || 0
  end

  self.each do |row|
    0.upto(@columns.size-1) do |i|
      size = row[i].size
      if size > cols[i]
        cols[i] = size
      end
    end
  end

  0.upto(@columns.size-1) do |i|
    printf "%-#{cols[i]+1}s", @columns[i]
    print " | " if i >= 0 and i < (@columns.size - 1)
  end
  puts

  self.each do |row|
    0.upto(@columns.size-1) do |i|
      printf "%-#{cols[i]+1}s", row[i]
      print " | " if i >= 0 and i < (@columns.size - 1)
    end
    puts
  end
end

#eachObject



136
137
138
139
140
# File 'lib/sqlite.rb', line 136

def each()
  0.upto(@rows-1) do |i|
    yield Row.new(self, i)
  end
end

#fromMsgPack(binary) ⇒ Object



152
153
154
# File 'lib/sqlite.rb', line 152

def fromMsgPack(binary)
  load MessagePack.unpack(binary)
end

#load(data) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/sqlite.rb', line 156

def load(data)
  @columns = data[0]
  @types   = data[1]
  @data    = data[2]    
  @headers = data[3]
  @rows    = @data.size

  i = 0
  @columns.each do |c|
    @header_names[i]   = c
    @header_indexes[c] = i
    i += 1
  end
end

#to_jsonObject



171
172
173
174
# File 'lib/sqlite.rb', line 171

def to_json()
  content = [@columns, @types, @data]
  return JSON::generate(content, {:pretty_print => true})
end

#toJson(data = []) ⇒ Object



147
148
149
150
# File 'lib/sqlite.rb', line 147

def toJson(data=[])
  data << @columns << @types << @data << @headers
  return data.to_json()
end

#toMsgPack(data = []) ⇒ Object



142
143
144
145
# File 'lib/sqlite.rb', line 142

def toMsgPack(data=[])
  data << @columns << @types << @data << @headers
  return data.to_msgpack
end