Class: Oinky::Internal::Variant

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/oinky.rb,
lib/oinky.rb

Defined Under Namespace

Classes: ValueT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#refsObject

We must do this again. We can’t rely on the one in the DB_string we create because that can get discarded as soon as we are finished using it to initialize.



244
245
246
# File 'lib/oinky.rb', line 244

def refs
  @refs
end

Class Method Details

.make(val) ⇒ Object



246
247
248
249
250
# File 'lib/oinky.rb', line 246

def self.make(val)
  nv = Variant.new
  nv.refs = make_in_place(nv, val)
  nv
end

.make_from_set(set) ⇒ Object



287
288
289
290
291
292
293
294
# File 'lib/oinky.rb', line 287

def self.make_from_set(set)
  ptr = VariantSet.new(set.size)
  ptr.refs = []
  set.each_with_index{|v, i|
    ptr.refs << make_in_place(ptr[i], v)
  }
  ptr
end

.make_in_place(nv, val) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/oinky.rb', line 251

def self.make_in_place(nv, val)
  if val.is_a?(String) or val.is_a?(NativeBuffer)
    nv[:type] = NkyTypeCodes[:string]
    return DB_string.make_in_place(nv[:value][:string_value], val)
  elsif val.is_a?(DateTime) or val.is_a?(Time) or val.is_a?(Date) or val.is_a?(OinkyDateTime)
    dt = val.to_datetime
    nv[:value][:dt_value] = OinkyDateTime.from_dt(dt).intval
    nv[:type] = NkyTypeCodes[:datetime]
    return nil
  elsif val.is_a?(Fixnum) or val.is_a?(Bignum)
    if val < 0
      nv[:value][:int_value] = val
      nv[:type] = NkyTypeCodes[:int64]
    else
      nv[:value][:uint_value] = val
      nv[:type] = NkyTypeCodes[:uint64]
    end
    return nil
  elsif val.is_a?(Float)
    nv[:value][:f64_value] = val
    nv[:type] = NkyTypeCodes[:float64]
    return nil
  elsif val.is_a?(TrueClass)
    nv[:value][:int_value] = 1
    nv[:type] = NkyTypeCodes[:bit]
    return nil
  elsif val.is_a?(FalseClass)
    nv[:value][:int_value] = 0
    nv[:type] = NkyTypeCodes[:bit]
    return nil
  else
    raise OinkyException.new("Don't know how to make variant from given value of type: #{val.class}")
  end
  return nil
end

Instance Method Details

#rb_valObject

Convert to a ruby value

Raises:



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/oinky.rb', line 297

def rb_val
  v = self[:value]
  case self[:type]
  when :string
    return v[:string_value].rb_str
  when :datetime
    return OinkyDateTime.from_intval(v[:dt_value]).to_datetime
  when :bit
    return v[:int_value] == 1

  when :int8
    return v[:int_value]
  when :int16
    return v[:int_value]
  when :int32
    return v[:int_value]
  when :int64
    return v[:int_value]

  when :uint8
    return v[:uint_value]
  when :uint16
    return v[:uint_value]
  when :uint32
    return v[:uint_value]
  when :uint64
    return v[:uint_value]

  when :float32
    return v[:f32_value]
  when :float64
    return v[:f64_value]
  end
  raise OinkyException.new("Invalid type code in Variant.")
end