Class: MonetDBData

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

Constant Summary collapse

@@DEBUG =
false

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ MonetDBData

Returns a new instance of MonetDBData.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/MonetDBData.rb', line 31

def initialize(connection)
  @connection = connection
  @lang = @connection.lang

  # Structure containing the header+results set for a fired Q_TABLE query     
  @header = []
  @query  = {}
  
  @record_set = []
  @index = 0 # Position of the last returned record
  
  
  @row_count = 0
  @row_offset = 10
  @row_index = Integer(REPLY_SIZE)
end

Instance Method Details

#execute(q) ⇒ Object

Fire a query and return the server response



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/MonetDBData.rb', line 49

def execute(q)
 # fire a query and get ready to receive the data      
  @connection.send(format_query(q))
  data = @connection.receive
  
  return if data == nil
  
  record_set = "" # temporarly store retrieved rows
  record_set = receive_record_set(data)

  if (@lang == LANG_SQL) or (@lang == LANG_XQUERY and XQUERY_OUTPUT_SEQ)
    rows = receive_record_set(data)
    # the fired query is a SELECT; store and return the whole record set
    if @action == Q_TABLE
      @header = parse_header_table(@header)
      @header.freeze
    
      if @row_index.to_i < @row_count.to_i
        block_rows = ""
        while next_block
          data = @connection.receive
          block_rows += receive_record_set(data)
        end
        record_set += block_rows
      end
    end

    # ruby string management seems to not properly understand the MSG_PROMPT escape character.
    # In order to avoid data loss the @record_set array is built once that all tuples have been retrieved
    @record_set = record_set.split("\t]\n")
    
    if @record_set.length != @query['rows'].to_i
      raise MonetDBQueryError, "Warning: Query #{@query['id']} declared to result in #{@query['rows']} but #{@record_set.length} returned instead"
    end
  elsif (@lang == XQUERY and ! XQUERY_OUTPUT_SEQ)
    return data # return an xml file
  end
  @record_set.freeze  
end

#fetchObject



145
146
147
148
149
150
151
152
153
# File 'lib/MonetDBData.rb', line 145

def fetch()
  @index
  if @index > @query['rows'].to_i 
    false
  else
    parse_tuple(@record_set[@index])
    @index += 1
  end
end

#fetch_allObject

Cursor method that retrieves all the records present in a table and stores them in a cache.



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

def fetch_all()
  if @query['type'] == Q_TABLE 
     rows = Array.new
     @record_set.each do |row| 
        rows << parse_tuple(row)
     end
     @index = Integer(rows.length)
   else
     raise MonetDBDataError, "There is no record set currently available"
   end

   return rows
end

#fetch_all_hashObject

Returns the record set entries hashed by column name orderd by column position



107
108
109
110
111
112
113
114
# File 'lib/MonetDBData.rb', line 107

def fetch_all_hash()
  columns = {}
  @header["columns_name"].each do |col_name|
    columns[col_name] = fetch_column_name(col_name)
  end

  return columns
end

#fetch_column_name(field = "") ⇒ Object

Returns the values for the column ‘field’



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/MonetDBData.rb', line 132

def fetch_column_name(field="")
  position = @header["columns_order"].fetch(field)

  col = Array.new
  # Scan the record set by row
  @record_set.each do |row|
    col << parse_tuple(row[position])
  end

  return col
end

#fetch_hashObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/MonetDBData.rb', line 116

def fetch_hash()
  if @index >= @query['rows'].to_i 
    return false
  else
    columns = {}
    @header["columns_name"].each do |col_name|
      position = @header["columns_order"].fetch(col_name)
      row = parse_tuple(@record_set[@index])
      columns[col_name] = row[position]
    end
    @index += 1
    return columns
  end
end

#freeObject

Free memory used to store the record set



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/MonetDBData.rb', line 90

def free()
  @connection = nil    
  
  @header = []
  @query = {}

  @record_set = []
  @index = 0 # Position of the last returned record


  @row_index = Integer(REPLY_SIZE)
  @row_count = 0
  @row_offset = 10
  
end

#name_fieldsObject

Returns the (ordered) name of the columns in the record set



181
182
183
# File 'lib/MonetDBData.rb', line 181

def name_fields()
  return @header['columns_name']
end

#num_fieldsObject

Returns the number of fields in the record set



176
177
178
# File 'lib/MonetDBData.rb', line 176

def num_fields()
  return @query['columns'].to_i
end

#num_rowsObject

Returns the number of rows in the record set



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

def num_rows()
   return @query['rows'].to_i
end

#type_fieldsObject

Returns the (ordered) name of the columns in the record set



186
187
188
# File 'lib/MonetDBData.rb', line 186

def type_fields
  return @header['columns_type']
end