Class: SQLAnywhere::BindParam

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

Instance Method Summary collapse

Instance Method Details

#get_directionObject



15
16
17
# File 'lib/bind_param.rb', line 15

def get_direction
  self[:direction]
end

#get_nameObject



11
12
13
# File 'lib/bind_param.rb', line 11

def get_name
  self[:name]
end

#inspectObject



23
24
25
# File 'lib/bind_param.rb', line 23

def inspect
  "<#{self.class} direction: #{self[:direction]}, name: #{self[:name]}, value: #{self[:value].inspect}>"
end

#set_direction(d) ⇒ Object



19
20
21
# File 'lib/bind_param.rb', line 19

def set_direction d
  self[:direction] = d
end

#set_value(value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
88
89
90
# File 'lib/bind_param.rb', line 27

def set_value(value)

  self[:value][:is_null] = FFI::MemoryPointer.new(SQLAnywhere::Bool, 1, true)

  if self[:direction] == :input

    case value
    when String
      self[:value][:length] = FFI::MemoryPointer.new(SQLAnywhere::SIZE_T, 1, false)
      length = value.bytesize
      SQLAnywhere.write_size_t(self[:value][:length], length)
      self[:value][:buffer] = FFI::MemoryPointer.new(:char, length + 1, false)

      self[:value][:buffer].put_string(0, value)
      self[:value][:type] = :string
    when Fixnum
      self[:value][:buffer] = FFI::MemoryPointer.new(:long, 1, false)
      self[:value][:type] = :val64
      byte_array = [value].pack('q')
      byte_array.each_byte.each_with_index do |b, offset|
        self[:value][:buffer].put_char(offset, b)
      end
    when Bignum
      self[:value][:buffer] = FFI::MemoryPointer.new(:long_long, 1, false)
      self[:value][:type] = :val64
      byte_array = [value].pack('Q')
      byte_array.each_byte.each_with_index do |b, offset|
        self[:value][:buffer].put_char(offset, b)
      end
    when Float
      self[:value][:buffer] = FFI::MemoryPointer.new(:double, 1, false)
      self[:value][:buffer].write_double(value)
      self[:value][:type] = :double
    when nil
      self[:value][:is_null].write_int(1)
      self[:value][:buffer] = FFI::MemoryPointer.new(:int, 1, false)
      self[:value][:type] = :val32
    else
      raise "Cannot convert type (#{value.class}). Must be STRING, FIXNUM, BIGNUM, FLOAT, or NIL"
    end

  else
    self[:value][:buffer] = FFI::MemoryPointer.new(:char,
      case self[:value][:type]
      when :string
        self[:value][:buffer_size]
      when :double
        FFI::Type::FLOAT.size # Is this right? it's what the old code does
      when :val64, :uval64
        FFI::Type::LONG_LONG.size
      when :val32, :uval32
        FFI::Type::INT.size
      when :val16, :uval16
        FFI::Type::SHORT.size
      when :val8, :uval8
        FFI::Type::CHAR.size
      else
        raise "Type unknown (#{self[:value][:type]})"
      end
      )
      
  end
  nil
end